WASI build: compile error on dev3 (getuid/geteuid) and undefined symbol clock

Open Beginner friendly
#1,515 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, wasm
Domain
build-system

Research direction

Start with mi_os_detect_elevated in src/prim/os.c and mi_prim_clock_now_lowres in src/prim/wasi/prim.c; also read the MI_HAS_UNISTDH detection in include/mimalloc/arch.h. Reproduce by compiling the shown static.c example with wasi-sdk 25, then check that it links both with and without -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks. Done means both WASI errors are gone without breaking non-WASI builds.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
13.6k
Forks
1.2k
Avg merge
12h 35m
Merged PRs (30d)
18

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/mimalloc

All issues in microsoft/mimalloc

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.