Debian Codegraph @

clang/test/Analysis/out-of-bounds-notes.c

raw
1 // RUN: %clang_analyze_cc1 -Wno-array-bounds -analyzer-output=text \
2 // RUN: -analyzer-checker=core,alpha.security.ArrayBoundV2,unix.Malloc,alpha.security.taint -verify %s
3
4 int TenElements[10];
5
6 int irrelevantAssumptions(int arg) {
7 int a = TenElements[arg];
8 // Here the analyzer assumes that `arg` is in bounds, but doesn't report this
9 // because `arg` is not interesting for the bug.
10 int b = TenElements[13];
11 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
12 // expected-note@-2 {{Access of 'TenElements' at index 13, while it holds only 10 'int' elements}}
13 return a + b;
14 }
15
16
17 int assumingBoth(int arg) {
18 int a = TenElements[arg];
19 // expected-note@-1 {{Assuming index is non-negative and less than 10, the number of 'int' elements in 'TenElements'}}
20 int b = TenElements[arg]; // no additional note, we already assumed that 'arg' is in bounds
21 int c = TenElements[arg + 10];
22 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
23 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 10 'int' elements}}
24 return a + b + c;
25 }
26
27 int assumingBothPointerToMiddle(int arg) {
28 // If we're accessing an TenElements through a pointer pointing to its middle, the checker
29 // will speak about the "byte offset" measured from the beginning of the TenElements.
30 int *p = TenElements + 2;
31 int a = p[arg];
32 // FIXME: The following note does not appear:
33 // {{Assuming byte offset is non-negative and less than 40, the extent of 'TenElements'}}
34 // It seems that the analyzer "gives up" modeling this pointer arithmetics
35 // and says that `p[arg]` is just an UnknownVal (instead of calculating that
36 // it's equivalent to `TenElements[2+arg]`).
37
38 int b = TenElements[arg]; // This is normal access, and only the lower bound is new.
39 // expected-note@-1 {{Assuming index is non-negative}}
40 int c = TenElements[arg + 10];
41 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
42 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 10 'int' elements}}
43 return a + b + c;
44 }
45
46 int assumingLower(int arg) {
47 // expected-note@+2 {{Assuming 'arg' is < 10}}
48 // expected-note@+1 {{Taking false branch}}
49 if (arg >= 10)
50 return 0;
51 int a = TenElements[arg];
52 // expected-note@-1 {{Assuming index is non-negative}}
53 int b = TenElements[arg + 10];
54 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
55 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 10 'int' elements}}
56 return a + b;
57 }
58
59 int assumingUpper(int arg) {
60 // expected-note@+2 {{Assuming 'arg' is >= 0}}
61 // expected-note@+1 {{Taking false branch}}
62 if (arg < 0)
63 return 0;
64 int a = TenElements[arg];
65 // expected-note@-1 {{Assuming index is less than 10, the number of 'int' elements in 'TenElements'}}
66 int b = TenElements[arg - 10];
67 // expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
68 // expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
69 return a + b;
70 }
71
72 int assumingUpperIrrelevant(int arg) {
73 // FIXME: The assumption "assuming index is less than 10" is printed because
74 // it's assuming something about the interesting variable `arg`; however,
75 // it's irrelevant because in this testcase the out of bound access is
76 // deduced from the _lower_ bound on `arg`. Currently the analyzer cannot
77 // filter out assumptions that are logically irrelevant but "touch"
78 // interesting symbols; eventually it would be good to add support for this.
79
80 // expected-note@+2 {{Assuming 'arg' is >= 0}}
81 // expected-note@+1 {{Taking false branch}}
82 if (arg < 0)
83 return 0;
84 int a = TenElements[arg];
85 // expected-note@-1 {{Assuming index is less than 10, the number of 'int' elements in 'TenElements'}}
86 int b = TenElements[arg + 10];
87 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
88 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 10 'int' elements}}
89 return a + b;
90 }
91
92 int assumingUpperUnsigned(unsigned arg) {
93 int a = TenElements[arg];
94 // expected-note@-1 {{Assuming index is less than 10, the number of 'int' elements in 'TenElements'}}
95 int b = TenElements[(int)arg - 10];
96 // expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
97 // expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
98 return a + b;
99 }
100
101 int assumingNothing(unsigned arg) {
102 // expected-note@+2 {{Assuming 'arg' is < 10}}
103 // expected-note@+1 {{Taking false branch}}
104 if (arg >= 10)
105 return 0;
106 int a = TenElements[arg]; // no note here, we already know that 'arg' is in bounds
107 int b = TenElements[(int)arg - 10];
108 // expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
109 // expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
110 return a + b;
111 }
112
113 short assumingConvertedToCharP(int arg) {
114 // When indices are reported, the note will use the element type that's the
115 // result type of the subscript operator.
116 char *cp = (char*)TenElements;
117 char a = cp[arg];
118 // expected-note@-1 {{Assuming index is non-negative and less than 40, the number of 'char' elements in 'TenElements'}}
119 char b = cp[arg]; // no additional note, we already assumed that 'arg' is in bounds
120 char c = cp[arg + 40];
121 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
122 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 40 'char' elements}}
123 return a + b + c;
124 }
125
126 struct foo {
127 int num;
128 char a[8];
129 char b[5];
130 };
131
132 int assumingConvertedToIntP(struct foo f, int arg) {
133 // When indices are reported, the note will use the element type that's the
134 // result type of the subscript operator.
135 int a = ((int*)(f.a))[arg];
136 // expected-note@-1 {{Assuming index is non-negative and less than 2, the number of 'int' elements in 'f.a'}}
137 // However, if the extent of the memory region is not divisible by the
138 // element size, the checker measures the offset and extent in bytes.
139 int b = ((int*)(f.b))[arg];
140 // expected-note@-1 {{Assuming byte offset is less than 5, the extent of 'f.b'}}
141 int c = TenElements[arg-2];
142 // expected-warning@-1 {{Out of bound access to memory preceding 'TenElements'}}
143 // expected-note@-2 {{Access of 'TenElements' at negative byte offset}}
144 return a + b + c;
145 }
146
147 int assumingPlainOffset(struct foo f, int arg) {
148 // This TC is intended to check the corner case that the checker prints the
149 // shorter "offset" instead of "byte offset" when it's irrelevant that the
150 // offset is measured in bytes.
151
152 // expected-note@+2 {{Assuming 'arg' is < 2}}
153 // expected-note@+1 {{Taking false branch}}
154 if (arg >= 2)
155 return 0;
156
157 int b = ((int*)(f.b))[arg];
158 // expected-note@-1 {{Assuming byte offset is non-negative and less than 5, the extent of 'f.b'}}
159 // FIXME: this should be {{Assuming offset is non-negative}}
160 // but the current simplification algorithm doesn't realize that arg <= 1
161 // implies that the byte offset arg*4 will be less than 5.
162
163 int c = TenElements[arg+10];
164 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
165 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 10 'int' elements}}
166 return b + c;
167 }
168
169 typedef __typeof(sizeof(int)) size_t;
170 void *malloc(size_t size);
171 void free(void *ptr);
172
173 int assumingExtent(int arg) {
174 // Verify that the assumption note is printed when the extent is interesting
175 // (even if the index isn't interesting).
176 int *mem = (int*)malloc(arg);
177
178 mem[12] = 123;
179 // expected-note@-1 {{Assuming index '12' is less than the number of 'int' elements in the heap area}}
180
181 free(mem);
182
183 return TenElements[arg];
184 // expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
185 // expected-note@-2 {{Access of 'TenElements' at an overflowing index, while it holds only 10 'int' elements}}
186 }
187
188 int *extentInterestingness(int arg) {
189 // Verify that in an out-of-bounds access issue the extent is marked as
190 // interesting (so assumptions about its value are printed).
191 int *mem = (int*)malloc(arg);
192
193 TenElements[arg] = 123;
194 // expected-note@-1 {{Assuming index is non-negative and less than 10, the number of 'int' elements in 'TenElements'}}
195
196 return &mem[12];
197 // expected-warning@-1 {{Out of bound access to memory after the end of the heap area}}
198 // expected-note@-2 {{Access of 'int' element in the heap area at index 12}}
199 }