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

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