-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
147 lines (130 loc) · 3.73 KB
/
test.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2021, Xcalar Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Author: Eric D. Cohen
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <assert.h>
void allocLeak(void) {
// Reflected in summary output and grdump.py leak tracking.
const int alloc_sz = 8192;
char *buf = malloc(alloc_sz);
memset(buf, 'A', alloc_sz);
}
void useAfterFree(void) {
int *ptr = malloc(sizeof(int));
*ptr = 7;
// fool optimizer
printf("ptr: %d\n", *ptr);
free(ptr);
printf("ptr: %d\n", *ptr);
*ptr = 8;
printf("ptr: %d\n", *ptr);
}
void doubleFree(void) {
int *ptr = malloc(sizeof(int));
*ptr = 7;
// fool optimizer
printf("ptr: %d\n", *ptr);
free(ptr);
free(ptr);
}
void bufOverrun(void) {
const int alloc_sz = 32;
char *ptr = malloc(alloc_sz);
ptr[0] = 2;
ptr[alloc_sz+1] = 7;
// fool optimizer
printf("ptr: %d\n", *ptr);
printf("ptr: %d\n", ptr[alloc_sz+1]);
}
void checkPoison(const char expected) {
const int alloc_sz = 32;
char *buf = malloc(alloc_sz);
for (size_t idx = 0; idx < alloc_sz; idx++) {
assert(buf[idx] == expected);
}
free(buf);
}
void usage(const char *myname) {
printf(
"Trivial tests of some guardrails features.\n"
"These will often run fine without guardrails, and cause a segfault\n"
"when run in guardrails.\n\n"
"NOTE: guardrails works by causing a segfault/crash/core on error.\n"
"Tests INTENDED TO CRASH under guardrails marked with (*).\n"
"\nNATIVE: %s <options>\n"
"GUARDRAILS: grrun.sh %s <options>\n"
" -d (*) Double free\n"
" -D Debugger pause\n"
" -l Leak memory\n"
" -o (*) Buffer overrun\n"
" -p <val> Verify poison (must match grargs.txt -p option)\n"
" -s Succeed and exit\n"
" -u (*) Use after free\n"
, myname, myname);
}
int main(int argc, char **argv) {
int c;
const char *progName = argv[0];
if (argc == 1) {
usage(progName);
return 1;
}
const char * const optstr = "Ddhlop:su";
while ((c = getopt (argc, argv, optstr)) != -1) {
if (c == 'D') {
printf("\nDebugger pause (PID: %d), press <enter> to continue...\n", getpid());
getchar();
}
}
optind = 1;
while ((c = getopt (argc, argv, optstr)) != -1) {
switch(c) {
case 'D':
break;
case 'l':
allocLeak();
return 0;
break;
case 'o':
bufOverrun();
break;
case 'd':
doubleFree();
break;
case 'p':
checkPoison(atoi(optarg));
return 0;
break;
case 's':
return 0;
break;
case 'u':
useAfterFree();
break;
case 'h':
default:
usage(progName);
return 0;
}
}
return 1;
}