Project

General

Profile

Formalizing Safety Rules » History » Version 4

Alexey Khoroshilov, 07/22/2013 08:26 AM

1 2 Alexey Khoroshilov
{{toc}}
2
3 1 Alexey Khoroshilov
h1. Formalizing Safety Rules
4
5
h2. Main Tasks
6
7
# Elicitation of all elements of kernel core API relevant to the rule
8
# Development of rule model that consists of
9
#* model state (one or more C variables);
10
#* transitions between states linked to appropriate events in device driver (usually calls of kernel core API)
11
#* a set of error states (that represents misuse of kernel core API)
12
# Extending code of the rule with model comments
13
# Test development
14
# Linux kernel verification and result analysis
15
# Markup of results of reference runs
16
# Rule description
17
# Report
18
# Publication
19
# Reporting bugs detected in Linux kernel
20
21
Usually process of work on the tasks is iterative and the sequence of the tasks can be different.
22
23
h2. Typical Workflow
24
25
# Reserve a rule identifier by creating a feature request here:
26
http://forge.ispras.ru/projects/ldv-rules/issues.
27
# Collect a list of functions from kernel API relevant to the rule (at first iterations the list can be incomplete).
28
# Write a small pseudo driver that uses a couple of key function from the list above.
29
# Create a template of your model in a separate folder (by copying kernel-rules). ldv-manager is able to use this copy of RuleDB using environment variable LDV_KERNEL_RULES=/path/to/folder. Use of the copy will help you to avoid problems with commiting to git submodule later. Create model comments (actual description can be just a fake at the beginning, but they still will help to visualize error trace.
30
# Write a positive (SAFE) and negative (UNSAFE) test for each assertion your have in the rule. It is recommended to develop tests and the model in parallel and check them using ldv-manager and its embedded vizualization (LDV_VIEW=y):
31
<pre>
32
  prefix=/xxx make -C ldv-tests/rule-models install
33
  cp /xxx/ldv-tools/ldv-tests/rule-models/__install/ldv-tests/rule-models/MODEL_testname.tar.bz2  /ldv-manager/
34
  cd ldv-manager
35
  LDV_VIEW=y LDV_KERNEL_RULES=/home/user/work/ldv/ldv-tools/144 \
36
      ldv-manager envs=linux-3.10.tar.bz2 drivers=test_name.tar.bz2 rule_models=144_1a 2>&1
37
</pre>
38
# Run verification on all drivers from the kernel. Analyze the results. If there are issues with your rule model, fix them and rerun the verification.
39
40
41
It is recommended after each modification of the rule to rerun all tests for the rule, then run verification of small (but interesting for the modifications) subfolder from Linux drivers, and only after that to run verification of all the drivers.
42 2 Alexey Khoroshilov
43
h2. Elicitation of kernel core API relevant to the rule
44
45
Usually several relevant interfaces (i.e. function and macroses) are obvious from a description of the rule and from examples of the corresponding bug fixes.
46
47
Reading documentation to already identified interfaces and of the corresponding subsystem helps to extend the list. To find documentation look to header files
48
where the interfaces are declared and to grep Documentation folder of linux kernel. http://lxr.free-electrons.com/ is a useful resource for navigation across kernel code.
49
50
The rest of the interfaces are detected during of analysis of results of verification runs.
51 3 Alexey Khoroshilov
52
h2. Development of rule model
53
54
All rule models are described in kernel-rules/model-db.xml. 
55
There is a <model> XML element for each rule model.
56
Identifier of the model is declared by ''id'' attribute.
57
58
Child elements of the <model> element include:
59
* rule – identifier of a rule (there can be several models for the same rule);
60
* status – status of readiness (''public'' means it is ready for public use, other values (e.g., ''default'' or ''private'') mean it is not recommended for some reasons);
61
* kind – kind of the model (''aspect'' means the model is defined with CIF aspects, ''plain'' means it is defined with C macro definitions) if unsure use ''aspect'' one;
62
* error – error label (for now, it must be LDV_ERROR);
63
* description – informal description of the model (optional, usually used if there are several models for the same rule);
64
* files - list of files defining the model, contains the following child elements:
65
** aspect – a relative path to file containing CIF "advices":http://en.wikipedia.org/wiki/Advice_%28programming%29 that are applied to all source files of a driver under analysis (this file contains an actual definition of the model);
66
** config – a relative path to a config file (should be models/config-tracers.h for new rules);
67
* hints – hints to verification engine such as extra options to be used for this particular model (if unsure leave empty).
68
69
An example of the model element:
70
<pre>
71
<model id="100_1a">
72
    <rule>0100</rule>
73
    <status>public</status>
74
    <kind>aspect</kind>
75
    <error>LDV_ERROR</error>
76
    <engine>blast</engine>
77
    <description></description>
78
    <files>
79
        <aspect>./models/model0100.aspect</aspect>
80
        <config>./models/config-tracers.h</config>
81
    </files>
82
    <hints></hints>
83
</model>
84
</pre>
85 4 Alexey Khoroshilov
86
h2. Model Comments
87
88
Model comments is used by error trace visualizer to highlight important elements of error traces. It makes analysis of error traces much easier, so it is highly recommended to write model comments from the very beginning. The comments should be short but it should clearly express corresponding situation. For example, comments to ldv_assert should explain what is a problem detected.
89
90
Model comments are usual C comments starting with '' LDV_COMMENT*''. For example:
91
92
<pre>
93
/* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name=''ldv_check_alloc_flags'') Check that a memory allocating function was called with a correct value of flags in spin locking. */
94
void ldv_check_alloc_flags(gfp_t flags)
95
{
96
  /* LDV_COMMENT_ASSERT If spinlock is locked then a memory allocating function should be called with GFP_ATOMIC flag set. */
97
  ldv_assert(ldv_spin == LDV_SPIN_UNLOCKED || flags & GFP_ATOMIC);
98
}
99
100
/* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name=''ldv_spin_lock'') Lock spinlock. */
101
void ldv_spin_lock(void)
102
{
103
  /* LDV_COMMENT_CHANGE_STATE Lock spinlock. */
104
  ldv_spin = LDV_SPIN_LOCKED;
105
}
106
</pre>
107
108
h3. Kinds of Model Comments
109
110
* LDV_COMMENT_MODEL_FUNCTION_DEFINITION(name=''function name to match with'') describes a model function. The name argument is mandatory.
111
It is used to match corresponding functions in error trace. In simple cases it equals to name of function in declaration.
112
* LDV_COMMENT_ASSERT describes what is a problem detected if the assertion failed.
113
* LDV_COMMENT_CHANGE_STATE decribes changes in model state.
114
* LDV_COMMENT_RETURN describes returned value of a model function.
115
* LDV_COMMENT_OTHER is used to describe any other interesting elements of a model.