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

Aperta Adatta ai principianti
#1,515 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, wasm
Ambito
build-system

Direzione di ricerca

Inizia da mi_os_detect_elevated in src/prim/os.c e da mi_prim_clock_now_lowres in src/prim/wasi/prim.c; leggi anche il rilevamento di MI_HAS_UNISTDH in include/mimalloc/arch.h. Riproduci il problema compilando l'esempio static.c mostrato con wasi-sdk 25, poi verifica che il linking funzioni sia con sia senza -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks. È completato quando entrambi gli errori WASI sono scomparsi senza compromettere le build non WASI.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C
Stelle
13.6k
Fork
1.2k
Merge medio
12h 35m
PR unite (30g)
18

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/mimalloc

Tutte le issue di microsoft/mimalloc

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.