mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-05 23:19:31 +03:00
potential fix for windows static linking with thread creation in dll's
This commit is contained in:
parent
312ce6f916
commit
3e1d800e9b
4 changed files with 38 additions and 9 deletions
|
@ -60,6 +60,8 @@ void _mi_error_message(int err, const char* fmt, ...);
|
||||||
|
|
||||||
// random.c
|
// random.c
|
||||||
void _mi_random_init(mi_random_ctx_t* ctx);
|
void _mi_random_init(mi_random_ctx_t* ctx);
|
||||||
|
void _mi_random_init_weak(mi_random_ctx_t* ctx);
|
||||||
|
void _mi_random_reinit_if_weak(mi_random_ctx_t * ctx);
|
||||||
void _mi_random_split(mi_random_ctx_t* ctx, mi_random_ctx_t* new_ctx);
|
void _mi_random_split(mi_random_ctx_t* ctx, mi_random_ctx_t* new_ctx);
|
||||||
uintptr_t _mi_random_next(mi_random_ctx_t* ctx);
|
uintptr_t _mi_random_next(mi_random_ctx_t* ctx);
|
||||||
uintptr_t _mi_heap_random_next(mi_heap_t* heap);
|
uintptr_t _mi_heap_random_next(mi_heap_t* heap);
|
||||||
|
|
|
@ -357,6 +357,7 @@ typedef struct mi_random_cxt_s {
|
||||||
uint32_t input[16];
|
uint32_t input[16];
|
||||||
uint32_t output[16];
|
uint32_t output[16];
|
||||||
int output_available;
|
int output_available;
|
||||||
|
bool weak;
|
||||||
} mi_random_ctx_t;
|
} mi_random_ctx_t;
|
||||||
|
|
||||||
|
|
||||||
|
|
18
src/init.c
18
src/init.c
|
@ -142,8 +142,13 @@ mi_stats_t _mi_stats_main = { MI_STATS_NULL };
|
||||||
static void mi_heap_main_init(void) {
|
static void mi_heap_main_init(void) {
|
||||||
if (_mi_heap_main.cookie == 0) {
|
if (_mi_heap_main.cookie == 0) {
|
||||||
_mi_heap_main.thread_id = _mi_thread_id();
|
_mi_heap_main.thread_id = _mi_thread_id();
|
||||||
_mi_heap_main.cookie = _mi_os_random_weak((uintptr_t)&mi_heap_main_init);
|
_mi_heap_main.cookie = 1;
|
||||||
_mi_random_init(&_mi_heap_main.random);
|
#if defined(_WIN32) && !defined(MI_SHARED_LIB)
|
||||||
|
_mi_random_init_weak(&_mi_heap_main.random); // prevent allocation failure during bcrypt dll initialization with static linking
|
||||||
|
#else
|
||||||
|
_mi_random_init(&_mi_heap_main.random);
|
||||||
|
#endif
|
||||||
|
_mi_heap_main.cookie = _mi_heap_random_next(&_mi_heap_main);
|
||||||
_mi_heap_main.keys[0] = _mi_heap_random_next(&_mi_heap_main);
|
_mi_heap_main.keys[0] = _mi_heap_random_next(&_mi_heap_main);
|
||||||
_mi_heap_main.keys[1] = _mi_heap_random_next(&_mi_heap_main);
|
_mi_heap_main.keys[1] = _mi_heap_random_next(&_mi_heap_main);
|
||||||
}
|
}
|
||||||
|
@ -502,12 +507,13 @@ static void mi_process_load(void) {
|
||||||
MI_UNUSED(dummy);
|
MI_UNUSED(dummy);
|
||||||
#endif
|
#endif
|
||||||
os_preloading = false;
|
os_preloading = false;
|
||||||
|
mi_assert_internal(_mi_is_main_thread());
|
||||||
#if !(defined(_WIN32) && defined(MI_SHARED_LIB)) // use Dll process detach (see below) instead of atexit (issue #521)
|
#if !(defined(_WIN32) && defined(MI_SHARED_LIB)) // use Dll process detach (see below) instead of atexit (issue #521)
|
||||||
atexit(&mi_process_done);
|
atexit(&mi_process_done);
|
||||||
#endif
|
#endif
|
||||||
_mi_options_init();
|
_mi_options_init();
|
||||||
mi_process_init();
|
mi_process_setup_auto_thread_done();
|
||||||
//mi_stats_reset();-
|
mi_process_init();
|
||||||
if (mi_redirected) _mi_verbose_message("malloc is redirected.\n");
|
if (mi_redirected) _mi_verbose_message("malloc is redirected.\n");
|
||||||
|
|
||||||
// show message from the redirector (if present)
|
// show message from the redirector (if present)
|
||||||
|
@ -516,6 +522,9 @@ static void mi_process_load(void) {
|
||||||
if (msg != NULL && (mi_option_is_enabled(mi_option_verbose) || mi_option_is_enabled(mi_option_show_errors))) {
|
if (msg != NULL && (mi_option_is_enabled(mi_option_verbose) || mi_option_is_enabled(mi_option_show_errors))) {
|
||||||
_mi_fputs(NULL,NULL,NULL,msg);
|
_mi_fputs(NULL,NULL,NULL,msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reseed random
|
||||||
|
_mi_random_reinit_if_weak(&_mi_heap_main.random);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) && (defined(_M_IX86) || defined(_M_X64))
|
#if defined(_WIN32) && (defined(_M_IX86) || defined(_M_X64))
|
||||||
|
@ -542,7 +551,6 @@ void mi_process_init(void) mi_attr_noexcept {
|
||||||
_mi_process_is_initialized = true;
|
_mi_process_is_initialized = true;
|
||||||
mi_process_setup_auto_thread_done();
|
mi_process_setup_auto_thread_done();
|
||||||
|
|
||||||
|
|
||||||
mi_detect_cpu_features();
|
mi_detect_cpu_features();
|
||||||
_mi_os_init();
|
_mi_os_init();
|
||||||
mi_heap_main_init();
|
mi_heap_main_init();
|
||||||
|
|
26
src/random.c
26
src/random.c
|
@ -168,7 +168,7 @@ If we cannot get good randomness, we fall back to weak randomness based on a tim
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
#if defined(MI_USE_RTLGENRANDOM) || defined(__cplusplus)
|
#if defined(MI_USE_RTLGENRANDOM) // || defined(__cplusplus)
|
||||||
// We prefer to use BCryptGenRandom instead of (the unofficial) RtlGenRandom but when using
|
// We prefer to use BCryptGenRandom instead of (the unofficial) RtlGenRandom but when using
|
||||||
// dynamic overriding, we observed it can raise an exception when compiled with C++, and
|
// dynamic overriding, we observed it can raise an exception when compiled with C++, and
|
||||||
// sometimes deadlocks when also running under the VS debugger.
|
// sometimes deadlocks when also running under the VS debugger.
|
||||||
|
@ -303,23 +303,41 @@ uintptr_t _mi_os_random_weak(uintptr_t extra_seed) {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _mi_random_init(mi_random_ctx_t* ctx) {
|
static void mi_random_init_ex(mi_random_ctx_t* ctx, bool use_weak) {
|
||||||
uint8_t key[32];
|
uint8_t key[32];
|
||||||
if (!os_random_buf(key, sizeof(key))) {
|
if (use_weak || !os_random_buf(key, sizeof(key))) {
|
||||||
// if we fail to get random data from the OS, we fall back to a
|
// if we fail to get random data from the OS, we fall back to a
|
||||||
// weak random source based on the current time
|
// weak random source based on the current time
|
||||||
#if !defined(__wasi__)
|
#if !defined(__wasi__)
|
||||||
_mi_warning_message("unable to use secure randomness\n");
|
if (!use_weak) { _mi_warning_message("unable to use secure randomness\n"); }
|
||||||
#endif
|
#endif
|
||||||
uintptr_t x = _mi_os_random_weak(0);
|
uintptr_t x = _mi_os_random_weak(0);
|
||||||
for (size_t i = 0; i < 8; i++) { // key is eight 32-bit words.
|
for (size_t i = 0; i < 8; i++) { // key is eight 32-bit words.
|
||||||
x = _mi_random_shuffle(x);
|
x = _mi_random_shuffle(x);
|
||||||
((uint32_t*)key)[i] = (uint32_t)x;
|
((uint32_t*)key)[i] = (uint32_t)x;
|
||||||
}
|
}
|
||||||
|
ctx->weak = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ctx->weak = false;
|
||||||
}
|
}
|
||||||
chacha_init(ctx, key, (uintptr_t)ctx /*nonce*/ );
|
chacha_init(ctx, key, (uintptr_t)ctx /*nonce*/ );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _mi_random_init(mi_random_ctx_t* ctx) {
|
||||||
|
mi_random_init_ex(ctx, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _mi_random_init_weak(mi_random_ctx_t * ctx) {
|
||||||
|
mi_random_init_ex(ctx, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _mi_random_reinit_if_weak(mi_random_ctx_t * ctx) {
|
||||||
|
if (ctx->weak) {
|
||||||
|
_mi_random_init(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------
|
/* --------------------------------------------------------
|
||||||
test vectors from <https://tools.ietf.org/html/rfc8439>
|
test vectors from <https://tools.ietf.org/html/rfc8439>
|
||||||
----------------------------------------------------------- */
|
----------------------------------------------------------- */
|
||||||
|
|
Loading…
Add table
Reference in a new issue