-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandalloc.c
44 lines (37 loc) · 865 Bytes
/
randalloc.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
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#define RANDALLOC_SIZE (1*1024*1024)
_Static_assert(RANDALLOC_SIZE < RAND_MAX);
static void* rand_start;
void _init_alloc(void) {
srand(time(NULL));
rand_start = sbrk(0);
sbrk(RANDALLOC_SIZE);
}
void* __wrap_malloc(size_t size) {
int offset = rand() % RANDALLOC_SIZE;
return (uint8_t*)rand_start + offset;
}
void __wrap_free(void* ptr) {
// BLAZING FAST
}
void* __wrap_calloc(size_t nmemb, size_t size) {
if (size && nmemb > SIZE_MAX / size) {
errno = ENOMEM;
return NULL;
}
size_t total = nmemb * size;
void* r = malloc(total);
if (total) {
memset(r, '\0', total);
}
return r;
}
void* __wrap_realloc(void* ptr, size_t size) {
return ptr;
}