main thread random context doesn't reinitialize correctly with non-weak source

未关闭
#1,133 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
c

调研方向

从 src/init.c 中的 _mi_auto_process_init 开始,跟踪对 mi_heap_main_init 和 _mi_random_reinit_if_weak 的调用,然后检查 src/random.c 中 mi_random_init_ex 和 chacha_init 附近的代码。检查 Windows 的非共享库初始化路径,并验证主线程的随机上下文能够正确重新初始化,而不会在线程初始化期间触发有问题的 BCrypt 操作。

由索引模型根据 Issue 内容生成。

描述

TLDR is that _mi_auto_process_init attempts to reseed the main thread's random ctx (which would cause BCrypt to get loaded there) but there's a bug preventing that from working.

Starting point:

https://github.com/microsoft/mimalloc/blob/09a27098aa6e9286518bd9c74e6ffa7199c3f04e/src/init.c#L580

_mi_auto_process_init calls mi_heap_main_init which on _WIN32 && !MI_SHARED_LIB builds calls _mi_random_init_weak for bcrypt-related reasons.

Then, we call _mi_random_reinit_if_weak at the end of _mi_auto_process_init

https://github.com/microsoft/mimalloc/blob/09a27098aa6e9286518bd9c74e6ffa7199c3f04e/src/init.c#L600-L601

BUT, that will never actually do anything.

Why? The mi_random_ctx_t weak bool will always be false after any call to mi_random_init_ex.

Why? At the end of mi_random_init_ex, for either the weak or normal path, we call chacha_init.

And what does that guy do? Memsets the entire mi_random_ctx_t to zero, wiping out the weak bool.

https://github.com/microsoft/mimalloc/blob/09a27098aa6e9286518bd9c74e6ffa7199c3f04e/src/random.c#L97-L102

We discovered this when we saw a thread was doing the mimalloc BCrypt-related initialization (e.g. LoadLibrary) during the initialization of a thread, which involved calling LoadLibrary a couple times and holding an internal BCrypt critical section while the thread initialization has the loader lock held (because the root calloc in that new thread is inside of __vcrt_getptd_noexit). So it seems, given the above mentioned code, that it's not intended that we'd be leaving the main thread's initialization without initializing the BCrypt stuff?

主要语言
C
星标
13.6k
派生
1.2k
平均合并
18 小时 12 分钟
30 天内合并 PR
20

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/mimalloc 的其他 Issue

查看 microsoft/mimalloc 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。