File: empty_struct.c

package info (click to toggle)
frama-c 20220511-manganese-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,492 kB
  • sloc: ml: 278,834; ansic: 47,093; sh: 4,823; makefile: 3,613; javascript: 2,436; python: 1,919; perl: 897; lisp: 293; xml: 62
file content (101 lines) | stat: -rw-r--r-- 2,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* run.config*
   STDOPT: +" -machdep gcc_x86_32 -cpp-extra-args=-DP1 -then -lib-entry"
   STDOPT: +" -machdep gcc_x86_32 -cpp-extra-args=-DP2 -lib-entry"
   STDOPT: +" -machdep gcc_x86_32 -cpp-extra-args=-DP3 -lib-entry"
   STDOPT: +" -machdep gcc_x86_32 -cpp-extra-args=-DP1 -lib-entry"
   STDOPT: +" -machdep gcc_x86_32 -cpp-extra-args=-DP1 -absolute-valid-range 0-1 -main main2"
   STDOPT: +"  -cpp-extra-args=\"-DP1 -DP5\" -machdep gcc_x86_32 -absolute-valid-range 0-1 -main main3"
   STDOPT: +" -machdep gcc_x86_32 -cpp-extra-args=-DP1 -main main4"
*/

// BTS 1416 and 1874

struct s {}; // empty structs only allowed in GCC/MSVC mode
struct s2 { int i1; struct s s; int i2; };

#define S struct s s;
#define S2 struct s2 s2;
#define T struct s t[10];

// Reorder the variables so that we get an error for each one
#ifdef P1
S // direct empty struct
S2
T
#endif

#ifdef P2
S2 // empty struct inside a struct
S
T
#endif

#ifdef P3
T // array of empty struct
S2
S
#endif

void main() {
  void *p = &s;
  s2.s = s;
  t[0] = t[1];
}

#ifdef P4 // Original example of bts 1874. Not explicitly tested, as the core functionality is checked by the tests above
struct lock_class_key {}; /* pas de struct-declaration-list */

struct dentry {
 struct super_block *d_sb;
};

void task_pgrp_nr_ns(struct dentry x);

extern struct dentry a;

struct super_block {
 struct lock_class_key s_writers_key[4];
} task_pgrp_nr(void) {
 task_pgrp_nr_ns(a);
 /* pas de return */
}

#endif

// tests that dereferencing a (invalid) pointer to an empty struct does not
// crash when -valid-absolute-range is set
struct empty {};
void main2(int n) {
  struct empty * ptr_ret = (struct empty *)0x2;
  if (n) *ptr_ret; // invalid access, but should not crash
}

#ifdef P5
#include <stdlib.h>
struct empty empties[100];
volatile int nondet;
void main3(int n) {
  struct empty *q = malloc(0);
  struct empty *r = realloc(q, 0);
  struct empty *p = empties;
  for (int i = 0; i < 100; i++) {
    empties[i] = *r;
  }
  *p = empties[99];
  *p = *r;
  free(r);
}
#endif

struct st {
   int a;
};
struct s gs, *pgs = &gs;
//@ assigns \result \from pgs;
struct s *f(int);
void g(struct s *array);
void main4() {
  struct s *r;
  r = f(42);
  g(r);
}