Repository navigation
Expand file tree
/
Copy pathwiki.cpp
More file actions
66 lines (58 loc) · 1.83 KB
/
Copy pathwiki.cpp
File metadata and controls
66 lines (58 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdlib.h>
#include "wiki.h"
/* https://github.com/google/sanitizers/wiki/AddressSanitizerExampleUseAfterFree */
int wiki_UseAfterFree(){
int *array = new int[100];
delete [] array;
return array[1]; // BOOM
}
/* https://github.com/google/sanitizers/wiki/AddressSanitizerExampleHeapOutOfBounds */
int wiki_HeapOutOfBounds(){
int *array = new int[100];
array[0] = 0;
int res = array[1 + 100]; // BOOM
delete [] array;
return res;
}
/* https://github.com/google/sanitizers/wiki/AddressSanitizerExampleStackOutOfBounds */
int wiki_StackOutOfBounds(){
int stack_array[100];
stack_array[1] = 0;
return stack_array[1 + 100]; // BOOM
}
/* https://github.com/google/sanitizers/wiki/AddressSanitizerExampleGlobalOutOfBounds */
volatile int global_array[100] = {-1};
int wiki_GlobalOutOfBounds(){
return global_array[1 + 100]; // BOOM
}
/* https://github.com/google/sanitizers/wiki/AddressSanitizerExampleUseAfterReturn
> By default, AddressSanitizer does not try to detect stack-use-after-return bugs.
> It may still find such bugs occasionally and report them as a hard-to-explain stack-buffer-overflow.
> You need to run the test with ASAN_OPTIONS=detect_stack_use_after_return=1
*/
volatile int *ptr;
__attribute__((noinline)) void FunctionThatEscapesLocalObject() {
int local[100];
ptr = &local[0];
}
int wiki_UseAfterReturn() {
FunctionThatEscapesLocalObject();
return ptr[1];
}
/* https://github.com/google/sanitizers/wiki/AddressSanitizerExampleUseAfterScope */
volatile int *p = 0;
int wiki_UseAfterScope() {
{
int x = 0;
p = &x;
}
*p = 5;
return 0;
}
/* https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer */
void *pp;
int wiki_LeakSanitizer() {
pp = malloc(7);
pp = 0; // The memory is leaked here.
return 0;
}