mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-07-06 19:38:41 +03:00
merge from dev-track
This commit is contained in:
commit
66525ccae3
15 changed files with 221 additions and 45 deletions
|
@ -47,3 +47,8 @@ target_link_libraries(static-override PUBLIC mimalloc-static)
|
|||
|
||||
add_executable(static-override-cxx main-override.cpp)
|
||||
target_link_libraries(static-override-cxx PUBLIC mimalloc-static)
|
||||
|
||||
|
||||
## test memory errors
|
||||
add_executable(test-wrong test-wrong.c)
|
||||
target_link_libraries(test-wrong PUBLIC mimalloc)
|
||||
|
|
|
@ -309,6 +309,10 @@ bool check_zero_init(uint8_t* p, size_t size) {
|
|||
|
||||
#if MI_DEBUG >= 2
|
||||
bool check_debug_fill_uninit(uint8_t* p, size_t size) {
|
||||
#if MI_VALGRIND
|
||||
(void)p; (void)size;
|
||||
return true; // when compiled with valgrind we don't init on purpose
|
||||
#else
|
||||
if(!p)
|
||||
return false;
|
||||
|
||||
|
@ -317,9 +321,14 @@ bool check_debug_fill_uninit(uint8_t* p, size_t size) {
|
|||
result &= p[i] == MI_DEBUG_UNINIT;
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool check_debug_fill_freed(uint8_t* p, size_t size) {
|
||||
#if MI_VALGRIND
|
||||
(void)p; (void)size;
|
||||
return true; // when compiled with valgrind we don't fill on purpose
|
||||
#else
|
||||
if(!p)
|
||||
return false;
|
||||
|
||||
|
@ -328,5 +337,6 @@ bool check_debug_fill_freed(uint8_t* p, size_t size) {
|
|||
result &= p[i] == MI_DEBUG_FREED;
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -72,7 +72,9 @@ int main(void) {
|
|||
result = (mi_calloc((size_t)&mi_calloc,SIZE_MAX/1000) == NULL);
|
||||
};
|
||||
CHECK_BODY("calloc0") {
|
||||
result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
|
||||
void* p = mi_calloc(0,1000);
|
||||
result = (mi_usable_size(p) <= 16);
|
||||
mi_free(p);
|
||||
};
|
||||
CHECK_BODY("malloc-large") { // see PR #544.
|
||||
void* p = mi_malloc(67108872);
|
||||
|
|
49
test/test-wrong.c
Normal file
49
test/test-wrong.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#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));
|
||||
|
||||
int* r = mi_malloc_aligned(8,16);
|
||||
mi_free(r);
|
||||
|
||||
// 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);
|
||||
|
||||
// undefined access
|
||||
int* q = mi(malloc)(sizeof(int));
|
||||
printf("undefined: %d\n", *q);
|
||||
|
||||
// illegal int read
|
||||
printf("invalid: over: %d, under: %d\n", q[1], q[-1]);
|
||||
|
||||
*q = 42;
|
||||
|
||||
// buffer overflow
|
||||
q[1] = 43;
|
||||
|
||||
// buffer underflow
|
||||
q[-1] = 44;
|
||||
|
||||
mi(free)(q);
|
||||
|
||||
|
||||
// double free
|
||||
mi(free)(q);
|
||||
|
||||
// use after free
|
||||
printf("use-after-free: %d\n", *q);
|
||||
|
||||
// leak p
|
||||
// mi_free(p)
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue