(Legal Code) Disclaimer
(Legal Code) Disclaimer
0 대한민국
Disclaimer
Thesis for the Degree of Master of Science
Chung Yoohee
December 2017
Chung Yoohee
December 2017
Chairman ________________________○
인
________________________○
인
________________________○
인
1 Introduction 1
2 Related Work 6
2.1 OSEK/VDX Operating System ................................................. 6
2.2 Model Checking ............................................................................. 8
2.3 CSP ........................................................................................... 10
2.4 Verification and Validation of Automotive Software ................. 11
2.5 Automatic Code Generation of API Functions ....................... 12
2.6 Sequentialization of Concurrent Programs .................................. 13
4 Properties of OS Model 29
4.1 Classification of Properties .......................................................... 29
4.2 Functional API Properties ....................................................... 31
4.3 Invariant Properties .................................................................. 33
4.4 OS Temporal Properties .......................................................... 35
4.5 Code Safety Properties ................................................................. 37
5 Validation of OS Model 40
5.1 Validation of OS Model through Model Checking ...................... 40
5.2 Validation of OS Model through Application Simulation……..42
6 Verification of Applications 44
6.1 Implementation ............................................................................. 44
6.2 Verification of Application .......................................................... 45
7 Conclusion 50
Bibliography 51
Abstract
List of Figures
Introduction
rigorous verification is necessary to ensure its correct and safe operation [1].
verifying embedded software: operating system (OS) and applications are coupled
tightly since they are compiled together and executed as a unified executable
program. The verification of embedded system must also consider the interaction
1
model-based approaches, where operating systems are formally modeled
reused for any control software that uses the same OS.
and the languages used for writing the application code. In most
such as Promela [8], NuSMV [9], and CSP [10], while applications
from the model using commercial tools such as SCADE [12] and
2
Simulink [13]. However, the verification of code safety properties
3
OSEK/VDX standard, two validation approaches were proposed;
properties are used to validate that the OS model satisfies the basic
OSEK/VDX OS.
CSP are defined first, and the properties to be satisfied are defined
CSP model and C model are reviewed to identify the source of the
4
problem. If the source of the error is an incorrect translation from
accordingly. This process continues until all issues in the model are
5
Chapter 2
Related Work
the automobile industry. This OS is intended for use in any type of control unit,
scalability, error checking, and the portability of the application software. The
major characteristics of the OSEK OS are that (1) it assumes that it is running
on a single processor, and (2) all system variables are required to be statically
Interrupt Service Routine (ISR), Resource, and Event (Figure 3). The OS kernel
manages the information on these objects. A task provides the framework for the
6
recurring events. An ISR is similar to a basic task, but it has higher priority and
occupied by tasks or ISRs. The priority of the task or ISR that occupies the
resource cannot be higher than the ceiling priority of the resource. An event is
an object that can be used only by extended tasks and is used apply binary
enforces a rescheduling.
enforces a rescheduling.
resource.
7
3. API functions for event mechanism, such as
a) WaitEvent makes the caller task wait until the event mask is set. This
whether the given system model satisfies a property or not [3]. It has been
satisfies the property or not. Formally, this problem can be stated using the
mechanism.
The model checker checks whether the system model satisfies the given
8
2.2.1 BMC
The model checker used in this study is CBMC. CBMC [16] is a tool for the
model checking through Binary Decision Diagrams (BDDs) [17] could alleviate
the state-space explosion problem more successfully than explicit model checking.
However, BDDs can grow exponentially. BMC uses a propositional SAT solver
rather than BDD. Since it restricts the bound of the target system model, BMC can
handle the growth of memory usage more easily. Moreover, the SAT formula is
9
canonical in itself while BDD manipulation may consume memory, according to
safety of the assertions given a search bound and is the most widely used model
In CBMC, the transition relations of C code and its specifications are jointly
solver [20].
division by zero.
2.3 CSP
Communicating Sequential Processes (CSP) is a formal modeling language that
10
describes the interaction of processes in concurrent systems [10] and is used to
specify and verify the concurrent aspects of a system. Each process is defined as an
communicate via message channel objects. The BNF describes the syntax of the
CSP from the code-level OS and performed formal verification of the OS’
properties. The reference model of this work is based on their findings. In this study,
some modifications were made to the model to make it smoother to translate into C.
The behavioral model of context switching was added explicitly, and there were
Choi [4] presents a method for converting the Trampoline OS into Promela
formal models and reports on the potential safety issues found in the Trampoline
OS.
11
Several approaches apply formal verification directly to the source code level in
SystemC model from the original C code and verified its temporal properties with
CBMC and BLAST [23]. Schlich et al. [24] proposed an approach to verify the
assembly codes [25]. Kim et al. [2] checked the correctness of the API-call
them with an OS model using Spin [27]. Kim et al. [5] presented an approach for
fully functional models to randomly generate test inputs. It is a useful tool for the
random testing of automotive software, but is difficult to apply for safety property
verification.
12
2.5 Automatic Code Generation of API Functions
functions from documents. In this work, the OS was manually modeled to maintain
consistency between the API model and other models for the objects managed by
the OS, since the OS model requires schedulers and behaviors embedded in the
Zhai et al.’s approach. Unless this work defines a template for arbitrary natural
fashion as our work. In addition, the tool they implemented was used to
sequentialize threads that express interrupts in our study to reduce verification costs.
13
Chapter 3
Modeling of OSEK/VDX
Components
This work took a two-step approach to maintaining the level of abstraction of the
and how they are used to define an OS model in the C language. Our definition of
the CSP model is influenced by the work of Huang et al. [21], but with some
14
at runtime as the OSEK/VDX specification requires that they
improve modularity.
program. The OS manages objects, and the application interacts with its underlying
OS by invoking the API functions provided by the OS (Figure 3). Therefore, the
15
control system is defined as a parallel composition of OS and Application.
Alarm, and ISR. Application is composed of multiple tasks that invoke API
functions to interact with the OS. APIHandler handles API functions that deal with
that supports system multitasking. The CSP model and detailed explanation about the
behavior of the scheduler will be introduced in the following sections. The scope of OS will
16
A task is an object that provides the framework for the execution of API
activate other tasks, occupy and release resources, wait, set, or clear the masks of
17
events, preempt another task, and finally terminate.
This is the CSP model of tasks at the application level. The symbol means
channel, respectively. SKIP is a special kind of process that does nothing but
function names such as AT, TT, CT, S, GR, RR, WE, SE, and CE for ActivateTask,
Each task runs in its turn (run tid = tid). Tasks send a message to APIChan,
Some of API calls may cause preemption of a task; ActivateTask is one of them.
A task can get two kinds of messages as a response from API function: Switch or
Keep. If a task gets Switch as a response, it ceases its execution until it is its turn
again (run tid = tid). If the returned message is Keep, the task keeps running. Since
pctid has been updated whenever an API function is called, it keeps the point
where to start performing instructions again. The action goto LABELpctid makes the
control flow go directly to the starting position of the task. If a task terminates, the
program counter is reinitialized to START. The Tasktid attached at the end of the
18
process specification makes it go back to its initial state after termination.
This CSP model is used as a reference to define the C code of the task model
shown in Code 1; when the task function is called, the control flow jumps to the
point to resume running and the program counter of the task is updated. When an
API function is called, the task gets the return value from the function. A task
keeps running or stops according to the return value. It starts to run from the point
19
3.2.2 APIHandler
from tasks such as resource/event identifier and handles the error code
modify the value of the data table. After that, it invokes Scheduler and returns a
The CSP model above shows the general behavior of APIHandler. The
API function, WaitEvent. If the condition between ⊲ and ⊳ is true, then the
sequence of the left side is executed. If the condition is false, the sequence of the
right side is executed. When WaitEvent is called, APIHandler first checks whether
the task is an extended task, and whether it is occupying resources or not. If the task
is not an extended task, then it generates an E_OS_ACCESS error. If the task is still
APIHandler changes the state of the task from running to waiting and invokes
Scheduler. Only the model of WaitEvent API because of the limited space in this
paper, but all API functions of OSEK/VDX have a similar structure; they get
20
function calls from applications, handle errors, modify tables, and return
whether rescheduling occurs or not. In this work, nine API functions that each have
similar manner.
These CSP models are used to define C functions for API handlers. The major
difference between the CSP model and the C model is the message passing
in CSP, function calls are used to express message passing in C. Thus, the behavior
21
3.2.3 Scheduler
Scheduler in our model checks whether the rescheduling condition is true or not, pushes
the preempted task into the ready queue, and gets the task that will be running from the
may happen without explicitly calling these API functions; if there is a task with higher
priority in the ready queue, it preempts the running task. If the scheduler gets a task from the
ready queue successfully, it returns Switch to the API function that invoked it. Otherwise,
22
Scheduler returns Keep and terminates. If rescheduling is required but the ready queue is
empty, Scheduler returns Keep to notify that task-context switching is failed. Code 3 shows
the Scheduler model in C defined from the CSP model, which is (almost) syntactic
sequential language by default and does not maintain information on the program
23
model context-switching in our C model. For this purpose, labels are located in
positions where task-context switching may occur, and the program counter of
each task is saved to know the progress of a task, which makes it possible to jump
Code 4 is an example code with two tasks. This example code assumes that
Task2 has a higher priority than Task1, and both tasks are following full-
preemptive scheduling policy. The original program has only API function calls.
24
initial value is 1). ActivateTask(2) causes a context switching since Task2 has a
higher priority. After the call of the function, run_tid is 2 and task2() is called in this
turn. After TerminateTask(), run_tid becomes 1 again. Using the value of pc[1]),
the task function task1() begins running again from the Label2, in line 9. After
ChainTask(1), pc[1] becomes 1 again and Task1 starts running from the first
statement again.
Task: A task object needs four kinds of information; an identifier of the task,
file (e.g., autostart, preemptive, etc.), and dynamic attributes that are changed by
cannot be occupied by multiple tasks. It also uses priority ceiling protocol [31] to
25
Event: An event has its own identifier and a state; an event can be set, cleared,
tasks simultaneously.
various kinds of primitive data types. Thus, this work also has to consider the data
type and available values that can be assigned to each field. For example, the
priority of a task has an integer value. Since the lowest priority of an object is 0,
26
An alarm is an object used to process recurring events. An alarm is based on a
timer. If its predefined counter value is reached, the alarm is expired and performs
provided by the application. If an alarm is a single alarm (which means the cycle of
the alarm is defined as 0), then the alarm stops. Otherwise, the alarm does its
In C, alarms are also expressed in a function called with tasks repetitively. When
an alarm function is called, it checks the current time and it is compared with the
27
3.4 Modeling of Interrupt Service Routines
Interrupt Service Routine (ISR ). Since interrupts may occur at any point in the
ISR. In this work, the pthread library is used to model interrupts. When interrupts
are enabled, the function pthread_create is called. When the system shuts down,
the function pthread_cancel is called to cancel the threads that are not terminated
yet.
28
Chapter 4
Properties of OS Model
code through model checking, to validate the design of task sequences through
using the model for those purposes, it is necessary to validate the correctness of the
model. Properties to validate the model were derived from the OSEK/VDX
29
4.1 Classification of Properties
The properties are first classified into two categories: those dependent
on specific API functions and those that are totally independent of API functions
and call sequence of functions. API-dependent properties are classified again into
System Services [14] and OS temporal properties. The former check whether states of
are also classified into two subcategories: invariant properties and code safety
properties. The former should be true from the start to the termination of the
system. The latter are not derived from the specification, but the general safety
30
property class.
There are two types of functional API properties: 1) Assertions that check a
normal behavior of the target API function according to the functional description,
and 2) assertions that check error code handling according to the Status of the API
error handling works correctly when TaskID is a wrong task identifier, and
property 2 checks whether task state changes correctly when there is no error.
but more exhaustive was used. The idea to check a unit function in a model-based
approach is not a new idea. Gunter and Peled [32] also proposed an approach
named unit checking, which is a symbolic approach for model checking a unit of
code. Based on their idea, the unit checking driver was developed to validate the
correctness of a single API function. The structure of the unit checking driver is
illustrated in Figure 5.
31
Table 1: Assertion of API Functions
32
The driver first declares parameters that will be passed to the target API function.
assumptions restrict the ranges of parameters and eliminate false alarms caused by
infeasible values. After them, the unit checking driver calls the target API function
and checks whether the API handles the error correctly or behaves in a specified
Invariant properties are a special kind of requirements that must be satisfied throughout
the entire system execution. They have the form “The model must/must not satisfy a
condition
33
Table 2: Invariant Properties of OSEK/VDX Operating System
A in any case”. From the specification, it was possible to define six invariant properties
(Table 2).
ensure that a given property holds during the entire lifetime of the system,
assertions should be put between every single statement. However, this approach
will severely undermine the efficiency of the verification. Instead, assertions for
invariant properties were located only after assignments of variables whose values
after assignment of the variable. Meanwhile, from the property 21 to 23, Start is added in
front of a Boolean expression. This means these properties need to be checked when a task
starts. In this case, assertions are located right after the statement that assigns running to a
34
state of a task.
Among invariant properties, there are also some properties which need to be
checked at the specific point of the system. For instance, the property “The number
of running task cannot exceed 1.” can be checked when a task is transferring from
ready state to running state and it is sufficient. Thus, it is also necessary to define
where to put assertions of these properties. From properties 21 to 23, the specific
position start is added in front of a Boolean expression. This means these properties
should be checked when a task starts; in other words, when the state of a task is
There are properties that can be expressed neither in a single assertion nor in an invariant
Table 3 are OS temporal properties since they include temporal operators other than
Globally (G). For example, property 24 means “Whenever a task goes to ready state, then
35
Table 3: Temporal Properties of the OSEK/VDX Operating System
modeling the properties in finite automata and inserting them in the C model as a
monitoring code, which is a general approach taken by SPIN [27] and CBMC [16].
The monitoring code samples the transition of state after each API function call.
Code 4.1 is an example of the monitoring code of property 24(Table 3). When a
task terminates, the final state of automata should be in the predefined safe state.
Since these OS temporal properties are not perfectly independent of API functions,
inappropriate use of API functions may cause false alarm when attempting to
property 24 of Table 2 is violated when tasks are not terminated with the API
36
incorrectness of the OS model. To eliminate these false alarms, inappropriate API
call sequences are aborted before the execution of applications while validating OS
temporal properties. Assertions are located at the end of the task or at the end
single task, and 26 and 27 are global properties that have a dependency on all
C language is notorious for many corner cases that can easily go unnoticed but
model does not include any typical safety issues from the language itself.
1. Use of arrays: As arrays are used to represent object tables and the ready
queue in the model, there might be a use of an index that is out of array
bounds.
2. Use of pointers: As the model uses pointers to change the value of given
3. Variables: The model uses a lot of integer type variables to express objects
37
4. Circular queue: The task ready queue is modeled with a circular
There were four code safety requirements from these potential issues. Since
code safety properties of the OS model should be satisfied with any arbitrary
38
Chapter 5
The OS model was validated with the properties defined in Chapter 4. All
unwinding bound 100. CBMC also reported that the loop bound is sufficient. Table
Non-deterministic API function call sequences with two tasks, resources, and
events were used to validate the properties. Each task calls five API functions to
39
cover scenarios with nested resource allocation.
40
given bound.
However, CBMC reported that there is an unwinding assertion,
meaning that the loop unwinding bound is not sufficiently big.
In the validation of code safety properties, giving options to validate them makes
length of the call sequence of each task is 5, it exceeds the power of SAT solver
and returns an error message, even within small bounds. To get around this issue,
the length of the API call sequence of each task was decreased, from 5 to 3, to
reduce the number of possible combinations of API call sequence. Table 6 shows
the performance of the validation of code safety properties. There was no violation
As the result, CBMC was able to validate the invariant properties, temporal
properties, and code safety properties under the condition, two tasks, two resources,
41
two events, 3–5 arbitrary API function calls in each task, and the given bound.
However, CBMC could not guarantee the validation in other conditions due to the
using CBMC, the model is integrated with a set of example application codes from
example codes were used to check whether the execution of the code on our C
model shows the same behavior as the execution of the same code on the actual
codes for testing the portability of the system in the x86 environment were used.
In the integration process, it is confirmed that our C model can easily substitute
the Erika OS implementation. The steps required for this process are (1) changing
the header file from the Erika specific “ee.h” to our OS header file, (2) redefining
data types defined in the “ee.h” file to match them with the data types in our C
model, and (3) annotating the explicit context switching behavior on the code, like
in Code 3.4.
42
Through the first simulation, it was found out that most of the example codes are
TerminateTask or ChainTask is strictly forbidden and causes undefined behavior.”. After adding
TerminateTask, the behavior of simulation using our OS model and the concrete
43
Chapter 6
Verification of Applications
6.1 Implementation
This section introduces implementing the tool Switchy, which generates files for
Before starting the system, static attributes of objects should be initialized at first.
OS C model is not compiled with the OIL file. Instead, by using Switchy, a header
file and source code file which initializes objects are generated automatically.
After generating these files, Switchy also annotates jump operation and program
Control Flow Graph (CFG) from the source code and finds the OSEK API
44
function call by traversing the CFG. If there is an API function call, a statement
that updates a program counter of the task and a label is added. When verifying
an application, this annotated code will be compiled with the generated header file,
In previous chapters, the validity of the model was checked with two
validation approaches. The model was used to verify functional and code-safety
properties of application codes. The application codes are example codes of Erika
Enterprise [33] and Trampoline 2.0 [34]. Application codes were verified with
CBMC [16], and the result compared with the simulation. The experiment
In some example codes, there are one interrupt or more. Interrupt service
routines were modeled with a thread. When verifying these example codes only
with CBMC and a back-end SMT solver [35], CBMC consumed too much time and
format. Thus, to reduce the verification cost, the sequentialization [30] technique
45
6.2.1 Functional properties
Erika Enterprise example codes, there are EE_assertion functions. These functions
are used to log the execution order of the control flow. If the program runs in the
order that the programmer intended, values saved in EE_assertions [EE ASSERT
DIM] are all EE ASSERT YES. Otherwise, it means that the program behaved in a
different way. Thus, while verifying Erika Enterprise example codes, we check
Table 7 shows the verification result of application code in a given loop bound
10. From the verification result, it was possible to determine that functional
violation. However, model checkers do not consider the concrete time; therefore, it
interrupts was increased from the original one, property violations were discovered
also in simulations. The other target application satisfied all of their functional
46
Table 7: Functional Verification of Applications
47
6.2.2 Code safety properties
The four code safety properties defined to validate OS model were used to verify
applications also. Code properties were verified under bound 5 from 20. Until the
loop bound reached 20, there was no violation of code safety properties.
Figure 6 and 7 shows the increase of time and memory consumption of the
verification. If the application has many tasks, API function calls, or interrupts, then
the verification cost is noticeably high. Otherwise, the verification takes only a
48
Figure 7. Memory Consumption of Code Safety Property Checking.
49
Chapter 7
Conclusion
and avoid potential problems caused thereby. To the best of my knowledge, this is
model was gained sufficiently by using a reference model in CSP. The model was
OSEK/VDX-based OS were verified with the OS model. The model could be used
to verify not only functional properties but also code safety properties of the
applications.
50
Backmatter
Bibliography
[2] D. Kim and Y. Choi, “Light-weight API-call safety checking for automotive
control software using constraint patterns,” in IT Convergence and Security
(ICITCS), 2016 6th International Conference on. IEEE, 2016, pp. 1–5.
[12] F.-X. Dormoy, “Scade 6: A model based solution for safety-critical software
52
development,” in Proceedings of the 4th European Congress on Embedded
Real-Time Software (ERTS’08), 2008, pp. 1–9.
[13] “Simulink,” www.mathworks.com/.
[19] H. Post and W. Küchlin, “Integrated static analysis for Linux device
driver verification,” in International Conference on Integrated Formal
Methods. Springer, 2007, pp. 518–537.
[21] Y. Huang, Y. Zhao, L. Zhu, Q. Li, H. Zhu, and J. Shi, “Modeling and
verifying the code-level OSEK/VDX operating system with CSP,” in
Theoretical Aspects of Software Engineering (TASE), 2011 Fifth International
Symposium on. IEEE, 2011, pp. 142–149.
54
[28] T. Arts, J. Hughes, U. Norell, and H. Svensson, “Testing AUTOSAR
software with QuickCheck,” in Software Testing, Verification and Validation
Workshops (ICSTW), 2015 IEEE Eighth International Conference on. IEEE,
2015, pp. 1–4.
[32] E. Gunter and D. Peled, “Unit checking: Symbolic model checking for a
unit of code,” in Verification: Theory and Practice. Springer, 2003, pp.
548–567.
[35] L. De Moura and N. Bjørner, “Z3: An efficient smt solver,” Tools and
Algorithms for the Construction and Analysis of Systems, pp. 337–340, 2008.
55
Modeling OSEK/VDX Operating System in C and Verifying
Applications Using the OS Model
Chung Yoohee
Abstract
formal CSP model is defined first and the C model is defined by using the CSP
model as a reference. The model was validated with two methods: property-based
validation via bounded model checking and simulation. With the validated model,
it was possible to verify application codes for two kinds of OSEK/VDX-based OS,
Erika Enterprise, and Trampoline OS. Not only functional properties, but also
code safety properties could be verified by using the C model.
56
C 언어를 이용한 OSEK/VDX 운영체제의 모델링 기법 및
이를 사용한 응용 프로그램의 검증
정유희
지도교수 최윤자
(초 록)
57