1// RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair
2
3// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2:halt_on_error=0 %run %t 2>&1 | FileCheck %s
4
5#include <assert.h>
6#include <stdlib.h>
7
8int foo(char *p, char *q) {
9 return p > q;
10}
11
12char global1[100] = {}, global2[100] = {};
13char __attribute__((used)) smallest_global[5] = {};
14char small_global[7] = {};
15char __attribute__((used)) little_global[10] = {};
16char __attribute__((used)) medium_global[4000] = {};
17char large_global[5000] = {};
18char __attribute__((used)) largest_global[6000] = {};
19
20int main() {
21 // Heap allocated memory.
22 char *heap1 = (char *)malloc(size: 42);
23 char *heap2 = (char *)malloc(size: 42);
24
25 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
26 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
27 foo(p: heap1, q: heap2);
28 free(ptr: heap1);
29 free(ptr: heap2);
30
31 heap1 = (char *)malloc(size: 1024);
32 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
33 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
34 foo(p: heap1, q: heap1 + 1025);
35 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
36 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
37 foo(p: heap1 + 1024, q: heap1 + 1025);
38 free(ptr: heap1);
39
40 heap1 = (char *)malloc(size: 4096);
41 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
42 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
43 foo(p: heap1, q: heap1 + 4097);
44 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
45 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
46 foo(p: heap1, q: 0);
47
48 // Global variables.
49 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
50 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
51 foo(p: &global1[0], q: &global2[10]);
52
53 char *p = &small_global[0];
54 foo(p, q: p); // OK
55 foo(p, q: p + 7); // OK
56 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
57 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
58 foo(p, q: p + 8);
59 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
60 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
61 foo(p: p - 1, q: p);
62 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
63 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
64 foo(p, q: p - 1);
65 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
66 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
67 foo(p: p - 1, q: p + 8);
68
69 p = &large_global[0];
70 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
71 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
72 foo(p: p - 1, q: p);
73 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
74 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
75 foo(p, q: p - 1);
76 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
77 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
78 foo(p, q: &global1[0]);
79 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
80 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
81 foo(p, q: &small_global[0]);
82 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
83 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
84 foo(p, q: 0);
85
86 // Stack variables.
87 char stack1, stack2;
88 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
89 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
90 foo(p: &stack1, q: &stack2);
91
92 // Mixtures.
93 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
94 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
95 foo(p: heap1, q: &stack1);
96 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
97 foo(p: heap1, q: &global1[0]);
98 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
99 foo(p: &stack1, q: &global1[0]);
100 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
101 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-compare-errors.cpp:[[@LINE+1]]
102 foo(p: &stack1, q: 0);
103
104 free(ptr: heap1);
105
106 return 0;
107}
108

source code of compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-errors.cpp