Debian Codegraph @

clang/test/Analysis/cstring-addrspace.c

raw
1 // RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
2 // RUN: -analyzer-checker=core,alpha.unix.cstring,debug.ExprInspection \
3 // RUN: -analyzer-config crosscheck-with-z3=true -verify %s \
4 // RUN: -Wno-incompatible-library-redeclaration
5 // REQUIRES: z3
6
7 void clang_analyzer_warnIfReached();
8
9 // From https://llvm.org/docs/AMDGPUUsage.html#address-spaces,
10 // select address space 3 (local), since the pointer size is
11 // different than Generic.
12 #define DEVICE __attribute__((address_space(3)))
13 _Static_assert(sizeof(int *) == 8, "");
14 _Static_assert(sizeof(DEVICE int *) == 4, "");
15 _Static_assert(sizeof(void *) == 8, "");
16 _Static_assert(sizeof(DEVICE void *) == 4, "");
17
18 // Copy from host to device memory. Note this is specialized
19 // since one of the parameters is assigned an address space such
20 // that the sizeof the the pointer is different than the other.
21 //
22 // Some downstream implementations may have specialized memcpy
23 // routines that copy from one address space to another. In cases
24 // like that, the address spaces are assumed to not overlap, so the
25 // cstring overlap check is not needed. When a static analysis report
26 // is generated in as case like this, SMTConv may attempt to create
27 // a refutation to Z3 with different bitwidth pointers which lead to
28 // a crash. This is not common in directly used upstream compiler builds,
29 // but can be seen in specialized downstrean implementations. This case
30 // covers those specific instances found and debugged.
31 //
32 // Since memcpy is a builtin, a specialized builtin instance named like
33 // 'memcpy_special' will hit in cstring, triggering this behavior. The
34 // best we can do for an upstream test is use the same memcpy function name.
35 DEVICE void *memcpy(DEVICE void *dst, const void *src, unsigned long len);
36
37 void top1(DEVICE void *dst, void *src, int len) {
38 memcpy(dst, src, len);
39
40 // Create a bugreport for triggering Z3 refutation.
41 clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
42 }
43
44 void top2(DEVICE int *dst, void *src, int len) {
45 memcpy(dst, src, len);
46
47 // Create a bugreport for triggering Z3 refutation.
48 clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
49 }
50
51 void top3(DEVICE int *dst, int *src, int len) {
52 memcpy(dst, src, len);
53
54 // Create a bugreport for triggering Z3 refutation.
55 clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
56 }
57
58 void top4() {
59 memcpy((DEVICE void *)1, (const void *)1, 1);
60
61 // Create a bugreport for triggering Z3 refutation.
62 clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
63 }