-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][analyzer] Add checker 'alpha.core.FixedAddressDereference' #127191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f2ad6d
[clang][analyzer] Add checker 'alpha.core.FixedAddressDereference'
balazske d60d9c7
fix link in documentation
balazske 69f42e9
addressing review comments
balazske 080431c
checker option moved to configuration option
balazske 00ae0c6
small NFC fixes
balazske 3529c65
updated documentation, renamed option
balazske File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,163 @@ | ||
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core -verify %s | ||
// expected-no-diagnostics | ||
|
||
void foo(void) { | ||
extern void __assert_fail (__const char *__assertion, __const char *__file, | ||
unsigned int __line, __const char *__function) | ||
__attribute__ ((__noreturn__)); | ||
|
||
#define assert(expr) \ | ||
((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) | ||
|
||
typedef unsigned long uintptr_t; | ||
|
||
void f0(void) { | ||
int *p = (int*) 0x10000; // Should not crash here. | ||
*p = 3; | ||
*p = 3; // expected-warning{{Dereference of a fixed address}} | ||
} | ||
|
||
void f1(int *p) { | ||
if (p != (int *)-1) | ||
*p = 1; | ||
else | ||
*p = 0; // expected-warning{{Dereference of a fixed address}} | ||
} | ||
|
||
struct f2_struct { | ||
int x; | ||
}; | ||
|
||
int f2(struct f2_struct* p) { | ||
|
||
if (p != (struct f2_struct *)1) | ||
p->x = 1; | ||
|
||
return p->x++; // expected-warning{{Access to field 'x' results in a dereference of a fixed address (loaded from variable 'p')}} | ||
} | ||
|
||
int f3_1(char* x) { | ||
int i = 2; | ||
|
||
if (x != (char *)1) | ||
return x[i - 1]; | ||
|
||
return x[i+1]; // expected-warning{{Array access (from variable 'x') results in a dereference of a fixed address}} | ||
} | ||
|
||
int f3_2(char* x) { | ||
int i = 2; | ||
|
||
if (x != (char *)1) | ||
return x[i - 1]; | ||
|
||
return x[i+1]++; // expected-warning{{Array access (from variable 'x') results in a dereference of a fixed address}} | ||
} | ||
|
||
int f4_1(int *p) { | ||
uintptr_t x = (uintptr_t) p; | ||
|
||
if (x != (uintptr_t)1) | ||
return 1; | ||
|
||
int *q = (int*) x; | ||
return *q; // expected-warning{{Dereference of a fixed address (loaded from variable 'q')}} | ||
} | ||
|
||
int f4_2(void) { | ||
short array[2]; | ||
uintptr_t x = (uintptr_t)array; | ||
short *p = (short *)x; | ||
|
||
// The following branch should be infeasible. | ||
if (!(p == &array[0])) { | ||
p = (short *)1; | ||
*p = 1; // no-warning | ||
} | ||
|
||
if (p != (short *)1) { | ||
*p = 5; // no-warning | ||
p = (short *)1; // expected-warning {{Using a fixed address is not portable}} | ||
} | ||
else return 1; | ||
|
||
*p += 10; // expected-warning{{Dereference of a fixed}} | ||
return 0; | ||
} | ||
|
||
int f5(void) { | ||
char *s = "hello world"; | ||
return s[0]; // no-warning | ||
} | ||
|
||
void f6(int *p, int *q) { | ||
if (p != (int *)1) | ||
if (p == (int *)1) | ||
*p = 1; // no-warning | ||
|
||
if (q == (int *)1) | ||
if (q != (int *)1) | ||
*q = 1; // no-warning | ||
} | ||
|
||
int* qux(int); | ||
|
||
int f7_1(unsigned len) { | ||
assert (len != 0); | ||
int *p = (int *)1; | ||
unsigned i; | ||
|
||
for (i = 0; i < len; ++i) | ||
p = qux(i); | ||
|
||
return *p++; // no-warning | ||
} | ||
|
||
int f7_2(unsigned len) { | ||
assert (len > 0); // note use of '>' | ||
int *p = (int *)1; | ||
unsigned i; | ||
|
||
for (i = 0; i < len; ++i) | ||
p = qux(i); | ||
|
||
return *p++; // no-warning | ||
} | ||
|
||
struct f8_s { | ||
int x; | ||
int y[2]; | ||
}; | ||
|
||
void f8(struct f8_s *s, int coin) { | ||
if (s != (struct f8_s *)7) | ||
return; | ||
|
||
if (coin) | ||
s->x = 5; // expected-warning{{Access to field 'x' results in a dereference of a fixed address (loaded from variable 's')}} | ||
else | ||
s->y[1] = 6; // expected-warning{{Array access (via field 'y') results in a dereference of a fixed address}} | ||
} | ||
|
||
void f9() { | ||
int (*p_function) (char, char) = (int (*)(char, char))0x04040; // FIXME: warn at this initialization | ||
p_function = (int (*)(char, char))0x04080; // expected-warning {{Using a fixed address is not portable}} | ||
// FIXME: there should be a warning from calling the function pointer with fixed address | ||
int x = (*p_function) ('x', 'y'); | ||
} | ||
|
||
#define AS_ATTRIBUTE volatile __attribute__((address_space(256))) | ||
steakhal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#define _get_base() ((void * AS_ATTRIBUTE *)0x10) | ||
|
||
void* test_address_space_array(unsigned long slot) { | ||
return _get_base()[slot]; // no-warning | ||
} | ||
void test_address_space_condition(int AS_ATTRIBUTE *cpu_data) { | ||
if (cpu_data == (int *)0x10) { | ||
*cpu_data = 3; // no-warning | ||
} | ||
} | ||
struct X { int member; }; | ||
int test_address_space_member(void) { | ||
struct X AS_ATTRIBUTE *data = (struct X AS_ATTRIBUTE *)0x10UL; | ||
int ret; | ||
ret = data->member; // no-warning | ||
return ret; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.