  Cyber-physical software continually interacts with its physical environment
for adaptation in order to deliver smart services. However, the interactions
can be subject to various errors when the software's assumption on its
environment no longer holds, thus leading to unexpected misbehavior or even
failure. To address this problem, one promising way is to conduct runtime
monitoring of invariants, so as to prevent cyber-physical software from
entering such errors (a.k.a. abnormal states). To effectively detect abnormal
states, we in this article present an approach, named Context-based
Multi-Invariant Detection (CoMID), which consists of two techniques:
context-based trace grouping and multi-invariant detection. The former infers
contexts to distinguish different effective scopes for CoMID's derived
invariants, and the latter conducts ensemble evaluation of multiple invariants
to detect abnormal states. We experimentally evaluate CoMID on real-world
cyber-physical software. The results show that CoMID achieves a 5.7-28.2%
higher true-positive rate and a 6.8-37.6% lower false-positive rate in
detecting abnormal states, as compared with state-of-the-art approaches (i.e.,
Daikon and ZoomIn). When deployed in field tests, CoMID's runtime monitoring
improves the success rate of cyber-physical software in its task executions by
15.3-31.7%.
