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] = {};
13
14int main() {
15 // Heap allocated memory.
16 char *heap1 = (char *)malloc(size: 42);
17 char *heap2 = (char *)malloc(size: 42);
18
19 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
20 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-subtract-errors.cpp:[[@LINE+1]]
21 foo(p: heap1, q: heap2);
22
23 // Global variables.
24 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
25 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-subtract-errors.cpp:[[@LINE+1]]
26 foo(p: &global1[0], q: &global2[10]);
27
28 // Stack variables.
29 char stack1, stack2;
30 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
31 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-subtract-errors.cpp:[[@LINE+1]]
32 foo(p: &stack1, q: &stack2);
33
34 // Mixtures.
35 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
36 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-subtract-errors.cpp:[[@LINE+1]]
37 foo(p: heap1, q: &stack1);
38 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
39 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-subtract-errors.cpp:[[@LINE+1]]
40 foo(p: heap1, q: &global1[0]);
41 // CHECK: ERROR: AddressSanitizer: invalid-pointer-pair
42 // CHECK: #{{[0-9]+ .*}} in main {{.*}}invalid-pointer-pairs-subtract-errors.cpp:[[@LINE+1]]
43 foo(p: &stack1, q: &global1[0]);
44
45 free(ptr: heap1);
46 free(ptr: heap2);
47 return 0;
48}
49

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