From 1190e0c053b92d752db7d9d49f51bbba5c93f475 Mon Sep 17 00:00:00 2001 From: Tim Blechmann Date: Wed, 26 Aug 2020 11:47:24 +0800 Subject: [PATCH 1/5] iOS compile fix `crt_externs.h` is available only available with iOS-13 sdk. we therefore add a `__has_include` check to see if it is actually available --- src/options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index f29b387c..68aeb1df 100644 --- a/src/options.c +++ b/src/options.c @@ -422,7 +422,7 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) { #elif !defined(MI_USE_ENVIRON) || (MI_USE_ENVIRON!=0) // On Posix systemsr use `environ` to acces environment variables // even before the C runtime is initialized. -#if defined(__APPLE__) +#if defined(__APPLE__) && defined(__has_include) && __has_include() #include static char** mi_get_environ(void) { return (*_NSGetEnviron()); From f107acb3c822718aae685f35aa8f0a3cffddab63 Mon Sep 17 00:00:00 2001 From: daan Date: Fri, 4 Sep 2020 10:40:05 -0700 Subject: [PATCH 2/5] fix __cplusplus test (pr #287) --- include/mimalloc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mimalloc.h b/include/mimalloc.h index 80cee2be..c9127e1f 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -24,7 +24,7 @@ terms of the MIT license. A copy of the license can be found in the file #define mi_attr_noexcept #endif -#if (__cplusplus >= 201703) +#if defined(__cplusplus) && (__cplusplus >= 201703) #define mi_decl_nodiscard [[nodiscard]] #elif (__GNUC__ >= 4) || defined(__clang__) // includes clang, icc, and clang-cl #define mi_decl_nodiscard __attribute__((warn_unused_result)) From d73d6beb71df5478f081be7fd1108a619ebba461 Mon Sep 17 00:00:00 2001 From: daan Date: Fri, 4 Sep 2020 10:41:10 -0700 Subject: [PATCH 3/5] add aslr test (issue #289) --- test/main-override-static.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/main-override-static.c b/test/main-override-static.c index f738c517..82feb37c 100644 --- a/test/main-override-static.c +++ b/test/main-override-static.c @@ -12,6 +12,8 @@ static void double_free2(); static void corrupt_free(); static void block_overflow1(); static void invalid_free(); +static void test_aslr(void); + int main() { mi_version(); @@ -21,6 +23,7 @@ int main() { // double_free2(); // corrupt_free(); // block_overflow1(); + // test_aslr(); invalid_free(); void* p1 = malloc(78); @@ -121,3 +124,10 @@ static void corrupt_free() { malloc(SZ); } } + +static void test_aslr(void) { + void* p[256]; + p[0] = malloc(378200); + p[1] = malloc(1134626); + printf("p1: %p, p2: %p\n", p[0], p[1]); +} From 032eb2a75a868534e8130ea8eb32914f8040d211 Mon Sep 17 00:00:00 2001 From: daan Date: Fri, 4 Sep 2020 13:06:18 -0700 Subject: [PATCH 4/5] use pragma warning only on msvc (issue #291) --- src/alloc-posix.c | 10 ++++++---- src/options.c | 8 +++++--- src/os.c | 5 ++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/alloc-posix.c b/src/alloc-posix.c index 4395893b..1ba1509b 100644 --- a/src/alloc-posix.c +++ b/src/alloc-posix.c @@ -20,6 +20,10 @@ terms of the MIT license. A copy of the license can be found in the file #include // memcpy #include // getenv +#ifdef _MSC_VER +#pragma warning(disable:4996) // getenv _wgetenv +#endif + #ifndef EINVAL #define EINVAL 22 #endif @@ -111,8 +115,7 @@ mi_decl_restrict unsigned char* mi_mbsdup(const unsigned char* s) mi_attr_noexc int mi_dupenv_s(char** buf, size_t* size, const char* name) mi_attr_noexcept { if (buf==NULL || name==NULL) return EINVAL; if (size != NULL) *size = 0; - #pragma warning(suppress:4996) - char* p = getenv(name); + char* p = getenv(name); // mscver warning 4996 if (p==NULL) { *buf = NULL; } @@ -132,8 +135,7 @@ int mi_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name) *buf = NULL; return EINVAL; #else - #pragma warning(suppress:4996) - unsigned short* p = (unsigned short*)_wgetenv((const wchar_t*)name); + unsigned short* p = (unsigned short*)_wgetenv((const wchar_t*)name); // msvc warning 4996 if (p==NULL) { *buf = NULL; } diff --git a/src/options.c b/src/options.c index a2432aa3..baaef460 100644 --- a/src/options.c +++ b/src/options.c @@ -14,6 +14,11 @@ terms of the MIT license. A copy of the license can be found in the file #include // toupper #include +#ifdef _MSC_VER +#pragma warning(disable:4996) // strncpy, strncat +#endif + + static uintptr_t mi_max_error_count = 16; // stop outputting errors after this static void mi_add_stderr_output(); @@ -215,7 +220,6 @@ static void mi_out_buf_stderr(const char* msg, void* arg) { // Should be atomic but gives errors on many platforms as generally we cannot cast a function pointer to a uintptr_t. // For now, don't register output from multiple threads. -#pragma warning(suppress:4180) static mi_output_fun* volatile mi_out_default; // = NULL static _Atomic(void*) mi_out_arg; // = NULL @@ -389,13 +393,11 @@ void _mi_error_message(int err, const char* fmt, ...) { static void mi_strlcpy(char* dest, const char* src, size_t dest_size) { dest[0] = 0; - #pragma warning(suppress:4996) strncpy(dest, src, dest_size - 1); dest[dest_size - 1] = 0; } static void mi_strlcat(char* dest, const char* src, size_t dest_size) { - #pragma warning(suppress:4996) strncat(dest, src, dest_size - 1); dest[dest_size - 1] = 0; } diff --git a/src/os.c b/src/os.c index e35fc82a..3de708d1 100644 --- a/src/os.c +++ b/src/os.c @@ -22,6 +22,10 @@ terms of the MIT license. A copy of the license can be found in the file #include // strerror +#ifdef _MSC_VER +#pragma warning(disable:4996) // strerror +#endif + #if defined(_WIN32) #include @@ -233,7 +237,6 @@ static bool mi_os_mem_free(void* addr, size_t size, bool was_committed, mi_stats if (was_committed) _mi_stat_decrease(&stats->committed, size); _mi_stat_decrease(&stats->reserved, size); if (err) { - #pragma warning(suppress:4996) _mi_warning_message("munmap failed: %s, addr 0x%8li, size %lu\n", strerror(errno), (size_t)addr, size); return false; } From ec2c83a6339430f9a9a598d9c18fcdd0dd3def68 Mon Sep 17 00:00:00 2001 From: daan Date: Fri, 4 Sep 2020 14:20:13 -0700 Subject: [PATCH 5/5] fix whitespace --- src/region.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/region.c b/src/region.c index e916e452..db2871d6 100644 --- a/src/region.c +++ b/src/region.c @@ -88,12 +88,12 @@ typedef union mi_region_info_u { typedef struct mem_region_s { _Atomic(uintptr_t) info; // mi_region_info_t.value _Atomic(void*) start; // start of the memory area - mi_bitmap_field_t in_use; // bit per in-use block - mi_bitmap_field_t dirty; // track if non-zero per block - mi_bitmap_field_t commit; // track if committed per block - mi_bitmap_field_t reset; // track if reset per block + mi_bitmap_field_t in_use; // bit per in-use block + mi_bitmap_field_t dirty; // track if non-zero per block + mi_bitmap_field_t commit; // track if committed per block + mi_bitmap_field_t reset; // track if reset per block _Atomic(uintptr_t) arena_memid; // if allocated from a (huge page) arena - uintptr_t padding; // round to 8 fields + uintptr_t padding; // round to 8 fields } mem_region_t; // The region map