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

Abierto Apto para principiantes
#1,515 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c, wasm
Área
build-system

Línea de trabajo

Empieza con mi_os_detect_elevated en src/prim/os.c y mi_prim_clock_now_lowres en src/prim/wasi/prim.c; lee también la detección de MI_HAS_UNISTDH en include/mimalloc/arch.h. Reproduce el problema compilando el ejemplo static.c mostrado con wasi-sdk 25, y luego comprueba que enlaza tanto con como sin -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks. Está terminado cuando ambos errores de WASI desaparecen sin romper las compilaciones que no son de WASI.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C
Estrellas
13.6k
Forks
1.2k
Merge medio
12 h 35 min
PR fusionados (30 d)
18

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/mimalloc

Todos los issues de microsoft/mimalloc

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.