2022-10-30 14:52:26 -07:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
Copyright (c) 2018-2020, Microsoft Research, Daan Leijen
|
|
|
|
This is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the MIT license. A copy of the license can be found in the file
|
|
|
|
"LICENSE" at the root of this distribution.
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* test file for valgrind support.
|
|
|
|
Compile in an "out/debug" folder:
|
|
|
|
|
|
|
|
> cd out/debug
|
|
|
|
> cmake ../.. -DMI_VALGRIND=1
|
|
|
|
> make -j8
|
|
|
|
|
2022-12-03 00:23:43 +01:00
|
|
|
and then compile this file as:
|
2022-10-30 14:52:26 -07:00
|
|
|
|
|
|
|
> gcc -g -o test-wrong -I../../include ../../test/test-wrong.c libmimalloc-valgrind-debug.a -lpthread
|
|
|
|
|
|
|
|
and test as:
|
|
|
|
|
|
|
|
> valgrind ./test-wrong
|
|
|
|
*/
|
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) {
|
2022-11-28 09:59:48 -08:00
|
|
|
int* p = (int*)mi(malloc)(3*sizeof(int));
|
2022-12-03 00:23:43 +01:00
|
|
|
|
2022-11-28 09:59:48 -08:00
|
|
|
int* r = (int*)mi_malloc_aligned(8,16);
|
2022-10-29 11:43:09 -07:00
|
|
|
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-11-28 09:59:48 -08:00
|
|
|
int* q = (int*)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-12-03 00:23:43 +01:00
|
|
|
|
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-12-03 00:23:43 +01:00
|
|
|
|
2022-10-28 20:07:31 -07:00
|
|
|
mi(free)(q);
|
|
|
|
|
|
|
|
// 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)
|
2022-12-03 00:23:43 +01:00
|
|
|
return 0;
|
2022-10-28 20:07:31 -07:00
|
|
|
}
|