Release coccinelle-0.1.2
[bpt/coccinelle.git] / standard.iso
CommitLineData
34e49164
C
1// ****************************************************************************
2// Prelude
3// ****************************************************************************
4
5// Note: some isomorphisms are handled in the engine directly because they
6// require special support. They can not be easily described with a
7// XX <=> YY. But some of them have names, so they can be disabled, as for
8// any other isomorphism rule. That also means that those names can not
9// be used for regular isomorphism rule. Those reserved rule names are:
10// - optional_storage
11// - optional_qualifier
12// - value_format
13// See parse_cocci.ml, pattern.ml, transformation.ml.
14
15
16// Note: the order of the rules has some importance. As we don't do a fixpoint,
17// changing the order may impact the result. For instance, if we have
18//
19// iso1 = x+y <=> y+x
20// iso2 = i++ <=> i=i+1;
21//
22// and if
23// in SP we have i++;
24// in C we have i=1+i;
25//
26// Does the SP matches the C ?
27// - Yes if iso2 precedes iso1 in this file,
28// - No otherwise.
29
30
31// ****************************************************************************
32// Standard C isomorphisms
33// ****************************************************************************
34
35// ---------------------------------------------------------------------------
36// Spacing (include comments) isomorphisms
37// ---------------------------------------------------------------------------
38// They are handled at lex time.
39
40// ---------------------------------------------------------------------------
41// Dataflow isomorphisms (copy propagation, assignments)
42// ---------------------------------------------------------------------------
43// They are handled in engine (TODO).
44
45
46// ---------------------------------------------------------------------------
47// Iso-by-absence (optional qualifier, storage, sign, cast) isomorphisms
48// ---------------------------------------------------------------------------
49// Some of them are handled in cocci_vs_c. Some of them handled here.
50
51
52// We would like that
53// chip = (ak4117_t *)snd_magic_kcalloc(ak4117_t, 0, GFP_KERNEL);
54// also matches
55// X = snd_magic_kcalloc(T, 0, C)
56//
57// For the moment because the iso is (T) E => E and not <=>, it forces
58// us to rewrite the SP as X = (T) snd_magic_kcalloc(T, 0, C)
59
60Expression
61@ drop_cast @
62expression E;
63pure type T;
64@@
65
66// in the following, the space at the beginning of the line is very important!
67 (T)E => E
68
69Type
70@ add_signed @
71@@
72int => signed int
73
74Type
75@ add_int1 @
76@@
77unsigned => unsigned int
78
79Type
80@ add_int2 @
81@@
82signed => signed int
83
84
85// ---------------------------------------------------------------------------
86// Field isomorphisms
87// ---------------------------------------------------------------------------
88// Dereferences
89
90
91// Those iso were introduced for the 'generic program matching' paper,
92// with sgrep. The idea is that when we want to detect bugs,
93// we want to detect something like free(X) ... *X
94// meaning that you try to access something that have been freed.
95// But *X is not the only way to deference X, there is also
96// X->fld, hence those iso.
97
98// The following don't see like a good idea, because eg fld could be
99// instantiated in different ways in different places, meaning that the
100// two occurrences of "*E" would not refer to the same thing at all.
101// This could be addressed by making E pure, but then I think it would
102// have no purpose.
103
104// Expression
105// @@
106// expression E;
107// identifier fld;
108// @@
109//
110// *E => E->fld
111//
112// Expression
113// @@
114// expression E;
115// identifier fld;
116// @@
117//
118// *E => (E)->fld
119//
120// Expression
121// @@
122// expression E,E1;
123// @@
124//
125// *E => E[E1]
126
127// ---------------------------------------------------------------------------
128// Typedef isomorphisms
129// ---------------------------------------------------------------------------
130// They are handled in engine.
131
132
133// ---------------------------------------------------------------------------
134// Boolean isomorphisms
135// ---------------------------------------------------------------------------
136
137// the space at the beginning of the line is very important!
138Expression
139@ not_int1 @
140int X;
141@@
142 !X => 0 == X
143
144// the space at the beginning of the line is very important!
145Expression
146@ not_int2 @
147int X;
148@@
149 !X => X == 0
150
151Expression
152@ is_zero @
153expression X;
154@@
155X == 0 <=> 0 == X => !X
156
157Expression
158@ isnt_zero @
159expression X;
160@@
161X != 0 <=> 0 != X => X
162
163Expression
164@ bitor_comm @
165expression X,Y;
166@@
167X | Y => Y | X
168
169Expression
170@ bitand_comm @
171expression X,Y;
172@@
173X & Y => Y & X
174
175// only if side effect free in theory ...
176Expression
177@ and_comm @
178expression X,Y;
179@@
180X && Y => Y && X
181
182Expression
183@ or_comm @
184expression X,Y;
185@@
186X || Y => Y || X
187
188
189// ---------------------------------------------------------------------------
190// Arithmetic isomorphisms
191// ---------------------------------------------------------------------------
192//todo: require check side-effect free expression
193
194Expression
195@ plus_comm @
196expression X, Y;
197@@
198X + Y => Y + X
199
200
201// needed in kcalloc CE, where have a -kzalloc(c * sizeof(T), E)
202Expression
203@ mult_comm @
204expression X, Y;
205@@
206X * Y => Y * X
207
208Expression
209@ plus_assoc @
210expression X, Y, Z;
211@@
212 // note space before (
213 (X + Y) + Z <=> X + Y + Z
214
215Expression
216@ minus_assoc @
217expression X, Y, Z;
218@@
219
220 (X - Y) - Z <=> X - Y - Z
221
222Expression
223@ plus_minus_assoc1 @
224expression X, Y, Z;
225@@
226
227 (X + Y) - Z <=> X + Y - Z
228
229Expression
230@ plus_minus_assoc2 @
231expression X, Y, Z;
232@@
233
234 (X - Y) + Z <=> X - Y + Z
235
236Expression
237@ times_assoc @
238expression X, Y, Z;
239@@
240
241 (X * Y) * Z <=> X * Y * Z
242
243Expression
244@ div_assoc @
245expression X, Y, Z;
246@@
247
248 (X / Y) / Z <=> X / Y / Z
249
250Expression
251@ times_div_assoc1 @
252expression X, Y, Z;
253@@
254
255 (X * Y) / Z <=> X * Y / Z
256
257Expression
258@ times_div_assoc2 @
259expression X, Y, Z;
260@@
261
262 (X / Y) * Z <=> X / Y * Z
263
264// ---------------------------------------------------------------------------
265// Relational isomorphisms
266// ---------------------------------------------------------------------------
267
268Expression
269@ gtr_lss @
270expression X, Y;
271@@
272X < Y <=> Y > X
273
274// ---------------------------------------------------------------------------
275// Increment isomorphisms
276// ---------------------------------------------------------------------------
277
278// equivalences between i++, +=1, etc.
279// note: there is an addition in this SP.
280Statement
281@ inc @
282identifier i;
283@@
284i++; <=> ++i; <=> i+=1; <=> i=i+1;
285
286// I would like to avoid the following rule, but we cant transform a ++i
287// in i++ everywhere. We can do it only when the instruction is alone,
288// such as when there is not stuff around it (not as in x = i++) That's why in
289// the previous iso, we have explicitely force the i++ do be alone with
290// the ';'. But unfortunately in the last expression of the for there is
291// no ';' so the previous rule cannot be applied, hence this special
292// case.
293
294Statement
295@ for_inc @
296expression X, Y;
297statement S;
298identifier i;
299@@
300for(X;Y;i++) S <=> for(X;Y;++i) S
301
302
303// ---------------------------------------------------------------------------
304// Pointer isomorphisms
305// ---------------------------------------------------------------------------
306
307// the space at the beginning of the line is very important!
308Expression
309@ not_ptr1 @
310expression *X;
311@@
312 !X => NULL == X
313
314// the space at the beginning of the line is very important!
315Expression
316@ not_ptr2 @
317expression *X;
318@@
319 !X => X == NULL
320
321Expression
322@ is_null @
323expression X;
324@@
325X == NULL <=> NULL == X => !X
326
327Expression
328@ isnt_null1 @
329expression X;
330@@
331X != NULL <=> NULL != X
332
333TestExpression
334@ isnt_null1 @
335expression X;
336@@
337NULL != X => X
338
339// pointer arithmetic equivalences
340
341// ---------------------------------------------------------------------------
342// Statement isomorphisms
343// ---------------------------------------------------------------------------
344
345// ----------------
346// If
347// ----------------
348
349Statement
350@ int_if_test1 @
351int X;
352statement S1, S2;
353@@
354if (X) S1 else S2 => if (X != 0) S1 else S2 <=> if (0 != X) S1 else S2
355
356Statement
357@ int_if_test2 @
358int X;
359statement S;
360@@
361if (X) S => if (X != 0) S <=> if (0 != X) S
362
363Statement
364@ ptr_if_test1 @
365expression *X;
366statement S1, S2;
367@@
368if (X) S1 else S2 => if (X != NULL) S1 else S2 => if (NULL != X) S1 else S2
369
370Statement
371@ ptr_if_test2 @
372expression *X;
373statement S;
374@@
375if (X) S => if (X != NULL) S <=> if (NULL != X) S
376
377// ---------------------------------------------------------------------------
378// Value isomorphisms
379// ---------------------------------------------------------------------------
380
381// There is also equal_c_int in cocci_vs_c to dealing with other
382// integer decimal/hexadecimal isomorphisms.
383// an argexpression applies only at top level, in the argument of a
384// function call, or on the right-hand side of an assignment
385ArgExpression
386@ zero_multiple_format @
387@@
388 0 => '\0'
389
390// ****************************************************************************
391// gcc specific isomorphisms
392// ****************************************************************************
393
394// likely and unlikely are used to give hints to gcc to improve performance.
395
396Expression
397@ unlikely @
398expression E;
399@@
400
401unlikely(E) => E
402
403Expression
404@ likely @
405expression E;
406@@
407
408likely(E) => E
409
410// ****************************************************************************
411// if structure isomorphisms
412// ****************************************************************************
413
414// these after after the above so that the introduced negation will distribute
415// properly over the argument to likely/unlikely
416
417Statement
418@ neg_if @
419expression X;
420statement S1, S2;
421@@
422if (X) S1 else S2 => if (!X) S2 else S1
423
424Statement
425@ drop_else @
426expression E;
427statement S1;
428pure statement S2;
429@@
430
431if (E) S1 else S2 => if (E) S1
432
433Expression
434@ neg_if_exp @
435expression E1, E2, E3;
436@@
437
438E1 ? E2 : E3 => !E1 ? E3 : E2
439
440
441// if (X) Y else Z <=> X ? Y : Z sometimes.
442
443// ----------------
444// Loops
445// ----------------
446
447// ---------------------------------------------------------------------------
448// Optional initializers
449// ---------------------------------------------------------------------------
450// this is not safe when the declaration is replaced
451// attempt to indicate that by requiring that Z is context
452// no optional static/extern for isos
453Declaration
454@ decl_init @
455type T;
456context identifier Z;
457@@
458T Z; => T Z = ...;
459
460Declaration
461@ const_decl_init @
462type T;
463identifier Z;
464constant C;
465@@
466T Z; => T Z = C;
467
468Declaration
469@ extern_decl_init @
470type T;
471context identifier Z;
472@@
473extern T Z; => extern T Z = ...;
474
475Declaration
476@ const_extern_decl_init @
477type T;
478identifier Z;
479constant C;
480@@
481extern T Z; => extern T Z = C;
482
483Declaration
484@ static_decl_init @
485type T;
486context identifier Z;
487@@
488static T Z; => static T Z = ...;
489
490Declaration
491@ const_static_decl_init @
492type T;
493identifier Z;
494constant C;
495@@
496static T Z; => static T Z = C;
497
498// ---------------------------------------------------------------------------
499// Branch (or compound) isomorphisms
500// ---------------------------------------------------------------------------
501// maybe a cocci patch should require something that looks like what is on
502// the left above to occur in a if or while
503
504// could worry that this has to be a simple statement, but this should work
505// better as it allows + code on S
506Statement
507@ braces1 @
508statement S;
509@@
510{ ... S } => S
511
512Statement
513@ braces2 @
514statement S;
515@@
516{ ... S ... } => S
517
518Statement
519@ braces3 @
520statement S;
521@@
522{ S ... } => S
523
524Statement
525@ braces4 @
526statement S;
527@@
528{ S } => S
529
530Statement
531@ ret @
532@@
533return ...; => return;
534
535
536// ---------------------------------------------------------------------------
537// Declaration isomorphisms
538// ---------------------------------------------------------------------------
539// They are handled in engine (TODO)
540
541// int i,j,k; <=> int i; int j; int k;
542
543
544// ---------------------------------------------------------------------------
545// Affectation/initialisation isomorphism
546// ---------------------------------------------------------------------------
547// They are handled in engine.
548// 'X = Y' should also match 'type X = Y';
549
550// ---------------------------------------------------------------------------
551// Parenthesis isomorphisms
552// ---------------------------------------------------------------------------
553//Expression
554//@@ expression E; @@
555// E => (E)
556//// E => ((E))
557
558// todo: isomorphism avec les () around ? cf sizeof 3.
559// (E) => E with some conditions.
560
561
562// ---------------------------------------------------------------------------
563// Pointer/Array isomorphisms
564// ---------------------------------------------------------------------------
565
566// pointer arithmetic equivalences
567// a + x <=> a[x]
568
569// ---------------------------------------------------------------------------
570// Pointer/Field isomorphisms
571// ---------------------------------------------------------------------------
572
573Expression
574@ ptr_to_array @
575expression E1, E2; // was pure, not sure why that's needed, not good for rule27
576identifier fld;
577@@
578
579E1->fld => E1[E2].fld
580
581
582
583TopLevel
584@ mkinit @
585type T;
586pure context T E;
587identifier I;
588identifier fld;
589expression E1;
590@@
591
592E.fld = E1; => T I = { .fld = E1, };
593
594// ---------------------------------------------------------------------------
595// more pointer field iso
596// ---------------------------------------------------------------------------
597
598// pure means that either the whole field reference expression is dropped,
599// or E is context code and has no attached + code
600// not really... pure means matches a unitary unplussed metavariable
601// but this rule doesn't work anyway
602
603Expression
604@ fld_to_ptr @
605type T;
606pure T E;
607pure T *E1;
608identifier fld;
609@@
610
611E.fld => E1->fld
612
613
614// ---------------------------------------------------------------------------
615// sizeof isomorphisms
616// ---------------------------------------------------------------------------
617
618Expression
619@ sizeof_parens @
620expression E;
621@@
622
623sizeof(E) => sizeof E
624
625
626Expression
627@ sizeof_type_expr @
628pure type T; // pure because we drop a metavar
629T E;
630@@
631
632sizeof(T) => sizeof(E)
633
485bce71
C
634// Expression
635// @ fld_func_call @
636// expression list ES;
637// identifier fld;
638// expression E;
639// @@
640// E.fld(ES) <=> (*E.fld)(ES)
641
34e49164
C
642
643// ****************************************************************************
644// Linux specific isomorphisms
645// ****************************************************************************
646
647// Examples: many functions are equivalent/related, and one SP modifying
648// such a function should also modify the equivalent/related one.
649
650
651// ---------------------------------------------------------------------------
652// in rule18, needed ?
653// ---------------------------------------------------------------------------
654// (
655// - test_and_set_bit(ev, &bcs->event);
656// |
657// - set_bit(ev, &bcs->event);
658// |
659// - bcs->event |= 1 << ev; // the only case that is used
660
661
662// ****************************************************************************
663// Everything that is required to be in last position, for ugly reasons ...
664// ****************************************************************************