2022-10-28 20:07:31 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "mimalloc.h"
|
|
|
|
|
|
|
|
#ifdef USE_STD_MALLOC
|
|
|
|
# define mi(x) x
|
|
|
|
#else
|
|
|
|
# define mi(x) mi_##x
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int* p = mi(malloc)(3*sizeof(int));
|
2022-10-30 10:45:51 -07:00
|
|
|
|
2022-10-29 11:43:09 -07:00
|
|
|
int* r = mi_malloc_aligned(8,16);
|
|
|
|
mi_free(r);
|
|
|
|
|
2022-10-30 10:45:51 -07:00
|
|
|
// illegal byte wise read
|
|
|
|
char* c = (char*)mi(malloc)(3);
|
|
|
|
printf("invalid byte: over: %d, under: %d\n", c[4], c[-1]);
|
|
|
|
mi(free)(c);
|
|
|
|
|
2022-10-28 20:07:31 -07:00
|
|
|
// undefined access
|
2022-10-30 10:45:51 -07:00
|
|
|
int* q = mi(malloc)(sizeof(int));
|
2022-10-29 14:37:55 -07:00
|
|
|
printf("undefined: %d\n", *q);
|
2022-10-30 10:45:51 -07:00
|
|
|
|
|
|
|
// illegal int read
|
|
|
|
printf("invalid: over: %d, under: %d\n", q[1], q[-1]);
|
2022-10-28 20:07:31 -07:00
|
|
|
|
|
|
|
*q = 42;
|
|
|
|
|
|
|
|
// buffer overflow
|
2022-10-29 14:37:55 -07:00
|
|
|
q[1] = 43;
|
|
|
|
|
|
|
|
// buffer underflow
|
|
|
|
q[-1] = 44;
|
2022-10-28 20:07:31 -07:00
|
|
|
|
|
|
|
mi(free)(q);
|
|
|
|
|
2022-10-30 10:45:51 -07:00
|
|
|
|
2022-10-28 20:07:31 -07:00
|
|
|
// double free
|
2022-10-29 14:37:55 -07:00
|
|
|
mi(free)(q);
|
2022-10-28 20:07:31 -07:00
|
|
|
|
|
|
|
// use after free
|
|
|
|
printf("use-after-free: %d\n", *q);
|
|
|
|
|
|
|
|
// leak p
|
|
|
|
// mi_free(p)
|
|
|
|
return 0;
|
|
|
|
}
|