WASI build: compile error on dev3 (getuid/geteuid) and undefined symbol clock
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 76/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- c, wasm
- 领域
- build-system
调研方向
从 src/prim/os.c 中的 mi_os_detect_elevated 和 src/prim/wasi/prim.c 中的 mi_prim_clock_now_lowres 开始,同时阅读 include/mimalloc/arch.h 中 MI_HAS_UNISTDH 的检测逻辑。使用 wasi-sdk 25 编译所示的 static.c 示例以复现问题,然后检查它在带有和不带有 -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks 时都能链接成功。两个 WASI 错误都消失且不破坏非 WASI 构建,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Building for WASI with wasi-sdk 25 fails on current dev3 (15d1247d), and on v3.7.0 it compiles but does not link without extra flags. Two separate problems (#1134 was an earlier WASI compile error, which is fixed now):
1. Compile error on dev3 (regression since 5d3f1ef4)
mi_os_detect_elevated in src/prim/os.c calls getuid/geteuid/getgid/getegid whenever MI_HAS_UNISTDH is set. wasi-libc has unistd.h but none of these functions. Since 5d3f1ef4 ("Fix unistd detection independently of profiling"), include/mimalloc/arch.h sets MI_HAS_UNISTDH from __has_include(<unistd.h>) for builds that compile src/static.c directly without CMake (like contrib/rust), so WASI now takes that branch. The parent commit and v3.7.0 compile fine.
#include <mimalloc.h>
#include <stdio.h>
int main(void) {
void* p = mi_malloc(100);
printf("%p\n", p);
mi_free(p);
return 0;
}
$ clang --sysroot=$WASI_SDK/share/wasi-sysroot -O2 -Imimalloc/include mimalloc/src/static.c main.c -o main.wasm
In file included from mimalloc/src/static.c:37:
mimalloc/src/prim/os.c:123:11: error: call to undeclared function 'getuid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
123 | return (getuid() != geteuid() || getgid() != getegid());
| ^
mimalloc/src/prim/os.c:123:23: error: call to undeclared function 'geteuid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
mimalloc/src/prim/os.c:123:36: error: call to undeclared function 'getgid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
mimalloc/src/prim/os.c:123:48: error: call to undeclared function 'getegid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
1 warning and 4 errors generated.
2. Link error: undefined symbol clock (also in v3.7.0)
mi_prim_clock_now_lowres in src/prim/wasi/prim.c calls clock(). wasi-libc only provides clock with -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks; otherwise it is deprecated at compile time and missing at link time. The call is only a fallback for when clock_gettime fails, but the reference alone breaks the link:
$ clang --sysroot=$WASI_SDK/share/wasi-sysroot -O2 -Imimalloc/include mimalloc/src/static.c main.c -o main.wasm # v3.7.0
mimalloc/src/prim/wasi/prim.c:198:34: warning: 'clock' is deprecated: WASI lacks process-associated clocks; to enable emulation of the `clock` function using the wall clock, which isn't sensitive to whether the program is running or suspended, compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks [-Wdeprecated-declarations]
wasm-ld: error: /tmp/static-2f14ae.o: undefined symbol: clock (11 times)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Possible fix
Return early on WASI in mi_os_detect_elevated (there are no users or setuid there), and only use clock when the emulation is enabled:
--- a/src/prim/os.c
+++ b/src/prim/os.c
@@ -113,6 +113,8 @@
static bool mi_os_detect_elevated(void) {
#if defined(_WIN32)
return false; // no direct setuid/setgid equivalent; a UAC elevation does not inherit the parent's environment
+ #elif defined(__wasi__)
+ return false; // no users or setuid/setgid in WASI
#elif MI_HAS_UNISTDH && (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__))
return (issetugid() != 0);
#elif MI_HAS_UNISTDH
--- a/src/prim/wasi/prim.c
+++ b/src/prim/wasi/prim.c
@@ -195,6 +195,9 @@
// low resolution timer
static mi_msecs_t mi_prim_clock_now_lowres(void) {
+ #if !defined(_WASI_EMULATED_PROCESS_CLOCKS)
+ return 0; // `clock` is only available with -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks
+ #else
const int64_t ticks = (int64_t)clock();
#if !defined(CLOCKS_PER_SEC)
return ticks;
@@ -209,6 +212,7 @@
return ticks / ((mi_msecs_t)CLOCKS_PER_SEC / 1000);
}
#endif
+ #endif
}
With this, the example compiles and links cleanly with wasi-sdk 25, with and without -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks. CI builds Emscripten but not WASI, which is why neither problem shows up there. I can send a PR, separately for each part if you prefer.
- 主要语言
- C
- 星标
- 13.6k
- 派生
- 1.2k
- 平均合并
- 12 小时 35 分钟
- 30 天内合并 PR
- 18
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/mimalloc 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
microsoft/mimalloc#1513 · 6 条评论 ·
维护者通常 1 天内回复
-
Building mimalloc with `-Wundef` compiler flag logs many warnings on undefined macros可能已有人在做 @vstinner 于 3 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 64/100
microsoft/mimalloc#1506 · 5 条评论 ·
维护者通常 1 天内回复
-
stale
难度 2/5 1-3 小时 新手友好度 74/100
microsoft/mimalloc#1113 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
microsoft/mimalloc#886 · 4 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
microsoft/mimalloc#368 · 1 条评论 ·
维护者通常 1 天内回复
查看 microsoft/mimalloc 的全部 Issue
相似的 Issue
-
Builder: a structure opened without calculation data shows the Diffraction (fibre) view by default未关闭bug
难度 2/5 1-3 小时 新手友好度 65/100
FriendsofECCE/ECCE#1245 · 1 条评论 ·
-
Arch: ARM/AArch64 Doc
难度 1/5 1 小时以内 新手友好度 90/100
RT-Thread/rt-thread#12082 · 1 条评论 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
apache/nuttx-apps#4108 · 4 条评论 ·
维护者通常 1 天内回复
-
bug
难度 1/5 1 小时以内 新手友好度 84/100
VirtualBox/virtualbox#996 ·
维护者通常 6 天内回复
-
难度 1/5 1 小时以内 新手友好度 82/100
trezor/trezor-firmware#8357 ·
维护者通常 2 天内回复