-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnolibc_test.c
148 lines (125 loc) · 2.37 KB
/
nolibc_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
148
#include "nolibc.h"
// // Use these to test the real libc.
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include <sys/mman.h>
static void test_mmap(void)
{
const size_t len = 1024;
void *p;
p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
if (!p) {
puts("mmap() error");
return;
}
munmap(p, len);
}
static void test_malloc(void)
{
void *ptr, *tmp;
const size_t len1 = 4096;
const size_t len2 = 8192;
ptr = malloc(len1);
if (!ptr) {
puts("malloc() error");
return;
}
memset(ptr, 'a', len1);
tmp = realloc(ptr, len2);
if (!tmp) {
free(ptr);
puts("realloc() error");
return;
}
ptr = tmp;
memset(ptr, 'b', len2);
free(ptr);
}
static void test_calloc(void)
{
void *ptr, *tmp;
const size_t len1 = 4096;
const size_t len2 = 8192;
ptr = calloc(sizeof(char), len1);
if (!ptr) {
puts("malloc() error");
return;
}
memset(ptr, 'a', len1);
tmp = realloc(ptr, len2);
if (!tmp) {
free(ptr);
puts("realloc() error");
return;
}
ptr = tmp;
memset(ptr, 'b', len2);
free(ptr);
}
static void test_realloc(void)
{
char *ptr;
struct nolibc_heap *heap;
size_t user_p_len;
int i;
const size_t len1 = 4096;
const size_t len2 = 8192;
/* imitate malloc to add out-of-bounds padding */
heap = mmap(NULL, len2, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
if (!heap) {
puts("mmap() error");
return;
}
memset(heap, 'b', len2);
heap->len = len1;
user_p_len = heap->len - sizeof(*heap);
ptr = heap->user_p;
memset(ptr, 'a', user_p_len);
/* this should only copy 'a' values */
ptr = realloc(ptr, len2);
if (!ptr) {
munmap(heap, len2);
puts("realloc() error");
return;
}
/* unmap the memory realloc didn't */
munmap(heap + len1, len1);
/* if we copied any 'b' values, we went too far */
user_p_len = len2 - sizeof(*heap);
for (i = 0; i < user_p_len; i++) {
if (ptr[i] == 'b') {
free(ptr);
puts("realloc() error");
return;
}
}
free(ptr);
}
static void test_string_heap(void)
{
static const char str[] = "Hello World!";
char *x;
x = strdup(str);
if (memcmp(x, str, sizeof(str))) {
printf("Wrong strdup()!\n");
return;
}
free(x);
x = strndup(str, 5);
if (memcmp(x, str, 4) || x[5] != '\0') {
printf("Wrong strndup()!\n");
return;
}
free(x);
}
int main(void)
{
test_mmap();
test_malloc();
test_calloc();
test_string_heap();
test_realloc();
}