From 45974efdb7377514863f69445053f2d2b4a36745 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 5 May 2020 19:37:50 -0700 Subject: [PATCH 1/9] use environ on posix systems to read environment variables before the C runtime is initialized (issue #241) --- src/options.c | 57 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/options.c b/src/options.c index 1a4633ee..9714d264 100644 --- a/src/options.c +++ b/src/options.c @@ -400,6 +400,14 @@ static void mi_strlcat(char* dest, const char* src, size_t dest_size) { dest[dest_size - 1] = 0; } +static inline int mi_strnicmp(const char* s, const char* t, size_t n) { + if (n==0) return 0; + for (; *s != 0 && *t != 0 && n > 0; s++, t++, n--) { + if (toupper(*s) != toupper(*t)) break; + } + return (n==0 ? 0 : *s - *t); +} + #if defined _WIN32 // On Windows use GetEnvironmentVariable instead of getenv to work // reliably even when this is invoked before the C runtime is initialized. @@ -411,11 +419,45 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) { size_t len = GetEnvironmentVariableA(name, result, (DWORD)result_size); return (len > 0 && len < result_size); } -#else +#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__) +#include +static char** mi_get_environ(void) { + return (*_NSGetEnviron()); +} +#else +extern char** environ; +static char** mi_get_environ(void) { + return environ; +} +#endif static bool mi_getenv(const char* name, char* result, size_t result_size) { + if (name==NULL) return false; + const size_t len = strlen(name); + if (len == 0) return false; + char** env = mi_get_environ(); + if (env == NULL) return false; + // compare all entries + for (; *env != NULL; env++) { + const char* s = *env; + if (mi_strnicmp(name, s, len) == 0 && s[len] == '=') { // case insensitive + // found it + mi_strlcpy(result, s + len + 1, result_size); + return true; + } + } + return false; +} +#else +// fallback: use standard C `getenv` but this cannot be used while initializing the C runtime +static bool mi_getenv(const char* name, char* result, size_t result_size) { + // cannot call getenv() when still initializing the C runtime. + if (_mi_preloading()) return false; const char* s = getenv(name); if (s == NULL) { - // in unix environments we check the upper case name too. + // we check the upper case name too. char buf[64+1]; size_t len = strlen(name); if (len >= sizeof(buf)) len = sizeof(buf) - 1; @@ -434,11 +476,8 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) { } } #endif -static void mi_option_init(mi_option_desc_t* desc) { - #ifndef _WIN32 - // cannot call getenv() when still initializing the C runtime. - if (_mi_preloading()) return; - #endif + +static void mi_option_init(mi_option_desc_t* desc) { // Read option value from the environment char buf[64+1]; mi_strlcpy(buf, "mimalloc_", sizeof(buf)); @@ -471,9 +510,9 @@ static void mi_option_init(mi_option_desc_t* desc) { desc->init = DEFAULTED; } } + mi_assert_internal(desc->init != UNINIT); } - else { + else if (!_mi_preloading()) { desc->init = DEFAULTED; } - mi_assert_internal(desc->init != UNINIT); } From 4f020e5da4552fb254ce35db1adad5c63d68d09e Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 5 May 2020 20:19:20 -0700 Subject: [PATCH 2/9] put a bound on the environment search --- src/options.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/options.c b/src/options.c index 9714d264..f29b387c 100644 --- a/src/options.c +++ b/src/options.c @@ -439,9 +439,9 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) { if (len == 0) return false; char** env = mi_get_environ(); if (env == NULL) return false; - // compare all entries - for (; *env != NULL; env++) { - const char* s = *env; + // compare up to 256 entries + for (int i = 0; i < 256 && env[i] != NULL; i++) { + const char* s = env[i]; if (mi_strnicmp(name, s, len) == 0 && s[len] == '=') { // case insensitive // found it mi_strlcpy(result, s + len + 1, result_size); From 967513d5363cc298fc888fcb883421455a6bd62f Mon Sep 17 00:00:00 2001 From: daan Date: Wed, 6 May 2020 11:35:35 -0700 Subject: [PATCH 3/9] add extra checks if unreset (commit) succeeds --- src/segment.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/segment.c b/src/segment.c index d36cf1c5..aa32c72c 100644 --- a/src/segment.c +++ b/src/segment.c @@ -240,7 +240,7 @@ static void mi_page_reset(mi_segment_t* segment, mi_page_t* page, size_t size, m if (reset_size > 0) _mi_mem_reset(start, reset_size, tld->os); } -static void mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, mi_segments_tld_t* tld) +static bool mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, mi_segments_tld_t* tld) { mi_assert_internal(page->is_reset); mi_assert_internal(page->is_committed); @@ -250,8 +250,12 @@ static void mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, uint8_t* start = mi_segment_raw_page_start(segment, page, &psize); size_t unreset_size = (size == 0 || size > psize ? psize : size); bool is_zero = false; - if (unreset_size > 0) _mi_mem_unreset(start, unreset_size, &is_zero, tld->os); + bool ok = true; + if (unreset_size > 0) { + ok = _mi_mem_unreset(start, unreset_size, &is_zero, tld->os); + } if (is_zero) page->is_zero_init = true; + return ok; } @@ -630,7 +634,7 @@ static mi_segment_t* mi_segment_init(mi_segment_t* segment, size_t required, mi_ mi_segments_track_size((long)segment_size, tld); } mi_assert_internal(segment != NULL && (uintptr_t)segment % MI_SEGMENT_SIZE == 0); - + mi_assert_internal(segment->mem_is_fixed ? segment->mem_is_committed : true); if (!pages_still_good) { // zero the segment info (but not the `mem` fields) ptrdiff_t ofs = offsetof(mi_segment_t, next); @@ -731,7 +735,13 @@ static bool mi_segment_page_claim(mi_segment_t* segment, mi_page_t* page, mi_seg segment->used++; // check reset if (page->is_reset) { - mi_page_unreset(segment, page, 0, tld); // todo: only unreset the part that was reset? + mi_assert_internal(!segment->mem_is_fixed); + bool ok = mi_page_unreset(segment, page, 0, tld); + if (!ok) { + page->segment_in_use = false; + segment->used--; + return false; + } } mi_assert_internal(page->segment_in_use); mi_assert_internal(segment->used <= segment->capacity); From 0ea4e3f2796685792dfec68b59e096acfbe496a2 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 7 May 2020 20:09:16 +0100 Subject: [PATCH 4/9] IOS build fix, large pages unsupported. --- src/os.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/os.c b/src/os.c index f33cfbc3..53bd39dc 100644 --- a/src/os.c +++ b/src/os.c @@ -26,9 +26,12 @@ terms of the MIT license. A copy of the license can be found in the file #include // linux mmap flags #endif #if defined(__APPLE__) +#include +#if !TARGET_IOS_IPHONE && !TARGET_IOS_SIMULATOR #include #endif #endif +#endif /* ----------------------------------------------------------- Initialization. From bf6b781e40ceed8e40a122ca310987e19fad288f Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 18 May 2020 10:07:45 -0700 Subject: [PATCH 5/9] fix semicolon (#247) --- src/segment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/segment.c b/src/segment.c index d36cf1c5..7960a176 100644 --- a/src/segment.c +++ b/src/segment.c @@ -427,7 +427,7 @@ static size_t mi_segment_size(size_t capacity, size_t required, size_t* pre_size guardsize = page_size; required = _mi_align_up(required, page_size); } -; + if (info_size != NULL) *info_size = isize; if (pre_size != NULL) *pre_size = isize + guardsize; return (required==0 ? MI_SEGMENT_SIZE : _mi_align_up( required + isize + 2*guardsize, MI_PAGE_HUGE_ALIGN) ); From c9ffe305130cdb90967719ee68419fd929474054 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 18 May 2020 10:17:58 -0700 Subject: [PATCH 6/9] weaken alignment requirement to not need to be a multiple of sizeof(void*); see #246 --- src/alloc-aligned.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index 7eeb9e92..15ebd59d 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -17,8 +17,7 @@ terms of the MIT license. A copy of the license can be found in the file static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept { // note: we don't require `size > offset`, we just guarantee that // the address at offset is aligned regardless of the allocated size. - mi_assert(alignment > 0 && alignment % sizeof(void*) == 0); - + mi_assert(alignment > 0); if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see ) if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see ) const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)` From 74986c1dd141f1b255b82ae9e7950fe0614f1f95 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 19 May 2020 09:56:37 -0700 Subject: [PATCH 7/9] weaken aligmenment assertion (issue #245) --- src/alloc-aligned.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index 15ebd59d..ca16d367 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -53,7 +53,7 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t // .. and align within the allocation uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask); - mi_assert_internal(adjust % sizeof(uintptr_t) == 0); + mi_assert_internal(adjust <= alignment); void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust)); if (aligned_p != p) mi_page_set_has_aligned(_mi_ptr_page(p), true); mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0); From a7d2bc8ad63699a8b661c2048be69f4362126a65 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 19 May 2020 10:16:28 -0700 Subject: [PATCH 8/9] edit warning messages to be more consistent --- include/mimalloc-internal.h | 2 +- src/os.c | 5 ++++- src/page.c | 5 +++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index d0c0b3f3..35413315 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -263,7 +263,7 @@ static inline bool mi_count_size_overflow(size_t count, size_t size, size_t* tot return false; } else if (mi_unlikely(mi_mul_overflow(count, size, total))) { - _mi_error_message(EOVERFLOW, "allocation request too large (%zu * %zu bytes)\n", count, size); + _mi_error_message(EOVERFLOW, "allocation request is too large (%zu * %zu bytes)\n", count, size); *total = SIZE_MAX; return true; } diff --git a/src/os.c b/src/os.c index f33cfbc3..72214a9f 100644 --- a/src/os.c +++ b/src/os.c @@ -262,7 +262,7 @@ static void* mi_win_virtual_alloc(void* addr, size_t size, size_t try_alignment, p = mi_win_virtual_allocx(addr, size, try_alignment, flags); } if (p == NULL) { - _mi_warning_message("unable to allocate memory: error code: %i, addr: %p, size: 0x%x, large only: %d, allow_large: %d\n", GetLastError(), addr, size, large_only, allow_large); + _mi_warning_message("unable to allocate OS memory (%zu bytes, error code: %i, address: %p, large only: %d, allow large: %d)\n", size, GetLastError(), addr, large_only, allow_large); } return p; } @@ -399,6 +399,9 @@ static void* mi_unix_mmap(void* addr, size_t size, size_t try_alignment, int pro } #endif } + if (p == NULL) { + _mi_warning_message("unable to allocate OS memory (%zu bytes, error code: %i, address: %p, large only: %d, allow large: %d)\n", size, errno, addr, large_only, allow_large); + } return p; } #endif diff --git a/src/page.c b/src/page.c index 08aa88c7..18f1812e 100644 --- a/src/page.c +++ b/src/page.c @@ -792,7 +792,7 @@ static mi_page_t* mi_find_page(mi_heap_t* heap, size_t size) mi_attr_noexcept { const size_t req_size = size - MI_PADDING_SIZE; // correct for padding_size in case of an overflow on `size` if (mi_unlikely(req_size > (MI_LARGE_OBJ_SIZE_MAX - MI_PADDING_SIZE) )) { if (mi_unlikely(req_size > PTRDIFF_MAX)) { // we don't allocate more than PTRDIFF_MAX (see ) - _mi_error_message(EOVERFLOW, "allocation request is too large (%zu b requested)\n", req_size); + _mi_error_message(EOVERFLOW, "allocation request is too large (%zu bytes)\n", req_size); return NULL; } else { @@ -833,7 +833,8 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size) mi_attr_noexcept } if (mi_unlikely(page == NULL)) { // out of memory - _mi_error_message(ENOMEM, "cannot allocate memory (%zu bytes requested)\n", size); + const size_t req_size = size - MI_PADDING_SIZE; // correct for padding_size in case of an overflow on `size` + _mi_error_message(ENOMEM, "unable to allocate memory (%zu bytes)\n", req_size); return NULL; } From a09a64e29b5535f6f4ea70fc58ac41c91c73a440 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 19 May 2020 10:40:46 -0700 Subject: [PATCH 9/9] add extra check in mi_page_unreset to not unreset for huge OS pages --- src/segment.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/segment.c b/src/segment.c index d36cf1c5..9190034a 100644 --- a/src/segment.c +++ b/src/segment.c @@ -245,6 +245,7 @@ static void mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, mi_assert_internal(page->is_reset); mi_assert_internal(page->is_committed); mi_assert_internal(!segment->mem_is_fixed); + if (segment->mem_is_fixed || !page->is_committed || !page->is_reset) return; page->is_reset = false; size_t psize; uint8_t* start = mi_segment_raw_page_start(segment, page, &psize);