xref: /llvm-project/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-success.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
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 %run %t
4 
5 #include <assert.h>
6 #include <stdlib.h>
7 
foo(char * p)8 int foo(char *p) {
9   char *p2 = p + 20;
10   return p > p2;
11 }
12 
bar(char * p,char * q)13 int bar(char *p, char *q) {
14   return p <= q;
15 }
16 
baz(char * p,char * q)17 int baz(char *p, char *q) {
18   return p != 0 && p < q;
19 }
20 
21 char global[8192] = {};
22 char small_global[7] = {};
23 
main()24 int main() {
25   // Heap allocated memory.
26   char *p = (char *)malloc(42);
27   int r = foo(p);
28   free(p);
29 
30   p = (char *)malloc(1024);
31   bar(p, p + 1024);
32   bar(p + 1024, p + 1023);
33   bar(p + 1, p + 1023);
34   free(p);
35 
36   p = (char *)malloc(4096);
37   bar(p, p + 4096);
38   bar(p + 10, p + 100);
39   bar(p + 1024, p + 4096);
40   bar(p + 4095, p + 4096);
41   bar(p + 4095, p + 4094);
42   bar(p + 100, p + 4096);
43   bar(p + 100, p + 4094);
44   free(p);
45 
46   // Global variable.
47   bar(&global[0], &global[1]);
48   bar(&global[1], &global[2]);
49   bar(&global[2], &global[1]);
50   bar(&global[0], &global[100]);
51   bar(&global[1000], &global[7000]);
52   bar(&global[500], &global[10]);
53   p = &global[0];
54   bar(p, p + 8192);
55   p = &global[8000];
56   bar(p, p + 192);
57 
58   p = &small_global[0];
59   bar(p, p + 1);
60   bar(p, p + 7);
61   bar(p + 7, p + 1);
62   bar(p + 6, p + 7);
63   bar(p + 7, p + 7);
64 
65   // Stack variable.
66   char stack[10000];
67   bar(&stack[0], &stack[100]);
68   bar(&stack[1000], &stack[9000]);
69   bar(&stack[500], &stack[10]);
70 
71   baz(0, &stack[10]);
72 
73   return 0;
74 }
75