Skip to content

getcwd returns incorrect answer using OPFS with WASMFS #24141

Open
@rickg-hcl

Description

@rickg-hcl

Please include the following in your bug report:

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.6 (1ddaae4)
clang version 21.0.0git (https:/github.com/llvm/llvm-project 4775e6d9099467df9363e1a3cd5950cc3d2fde05)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin
Full link command and output with -v appended:
emcc -g -v -s WASMFS -pthread -s PROXY_TO_PTHREAD=1 -s EXPORTED_FUNCTIONS='malloc,free' wasmfs_opfs_chdir.c -o test.html
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot -D__EMSCRIPTEN_SHARED_MEMORY
=1 -DEMSCRIPTEN -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -g3 -v -pthread -c wasmfs_opfs_chdir.c -o /var/folders/d8/08bv2b6s2b50q04__ln3cmbh0000gp/T/emscripten_temp_6oqmw94u/wasmfs_opfs_chdir_0.o
clang version 21.0.0git (https:/github.com/llvm/llvm-project 4775e6d9099467df9363e1a3cd5950cc3d2fde05)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin
(in-process)
"/Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin/clang-21" -cc1 -triple wasm32-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name wasmfs_opfs_chdir.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-feature +atomics -target-feature +bulk-memory -target-feature +mutable-globals -target-feature +sign-ext -target-cpu generic -fvisibility=hidden -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/test -v -fcoverage-compilation-dir=/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/test -resource-dir /Users/richardgillaspy/GitHubOpen/emsdk/upstream/lib/clang/21 -D EMSCRIPTEN_SHARED_MEMORY=1 -D EMSCRIPTEN -isysroot /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot -internal-isystem /Users/richardgillaspy/GitHubOpen/emsdk/upstream/lib/clang/21/include -internal-isystem /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten -internal-isystem /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/include -ferror-limit 19 -pthread -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/d8/08bv2b6s2b50q04__ln3cmbh0000gp/T/emscripten_temp_6oqmw94u/wasmfs_opfs_chdir_0.o -x c wasmfs_opfs_chdir.c
clang -cc1 version 21.0.0git based upon LLVM 21.0.0git default target x86_64-apple-darwin23.6.0
ignoring nonexistent directory "/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/include/fakesdl
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/include/compat
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/lib/clang/21/include
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/include
End of search list.
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin/clang --version
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin/wasm-ld -o test.wasm /var/folders/d8/08bv2b6s2b50q04__ln3cmbh0000gp/T/emscripten_temp_6oqmw94u/wasmfs_opfs_chdir_0.o -L/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten -L/Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/src/lib /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crtbegin.o /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crt1_proxy_main.o -lGL-mt-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-mt-debug -ldlmalloc-mt-debug -lcompiler_rt-mt -lc++-mt-noexcept -lc++abi-debug-mt-noexcept -lsockets-mt -lwasmfs_no_fs -lwasmfs-mt-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /var/folders/d8/08bv2b6s2b50q04__ln3cmbh0000gp/T/tmp0xbwqnvmlibemscripten_js_symbols.so --import-memory --shared-memory --export=malloc --export=free --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=wasmfs_flush --export=_emscripten_stack_alloc --export=_emscripten_thread_free_data --export=_emscripten_thread_crashed --export=__wasm_call_ctors --export=_emscripten_tls_init --export=_emscripten_thread_init --export=_emscripten_stack_restore --export=emscripten_stack_set_limits --export=_emscripten_thread_exit --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-memory=16777216 --entry=_emscripten_proxy_main --stack-first --table-base=1
/Users/richardgillaspy/GitHubOpen/emsdk/upstream/bin/llvm-objcopy test.wasm test.wasm --remove-section=producers
emcc: warning: main is defined in the input files, but _main is not in EXPORTED_FUNCTIONS. Add it to this list if you want main to run. [-Wunused-main]
/Users/richardgillaspy/GitHubOpen/emsdk/node/20.18.0_64bit/bin/node /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/tools/compiler.mjs -
/Users/richardgillaspy/GitHubOpen/emsdk/node/20.18.0_64bit/bin/node /Users/richardgillaspy/GitHubOpen/emsdk/upstream/emscripten/tools/preprocessor.mjs - shell.html

Note: Where possible, please avoid attaching screen shots of code or console
logs. Instead, please include them as text so that they may be copied /
searched. To make code blocks more readable and syntax highlighted, please
escape them with three backticks before and after, like this:

#include <assert.h>
#include <dirent.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/wasmfs.h>


int main(int argc, char* argv[]) {
  int err, fd, nwritten;
  const char* msg = "Hello, OPFS!";
  const char* msg2 = "Hello, OPFS!Hello, OPFS!";

  emscripten_console_log("starting main");

  backend_t opfs = wasmfs_create_opfs_backend();
  emscripten_console_log("created OPFS backend");

  err = wasmfs_create_directory("/opfs", 0777, opfs);
  assert(err == 0);
  emscripten_console_log("mounted OPFS root directory");

  mkdir("/opfs/data", 0777);
  symlink("/opfs/data", "/data");

  chdir("/opfs");
  char CWD[100] = {};
  getcwd(CWD, sizeof(CWD));
  emscripten_console_logf("CWD: %s", CWD);
  assert(strcmp(CWD, "/opfs") == 0); // fail, result is '/' should be '/opfs'


   chdir("/data");
   getcwd(CWD, sizeof(CWD));
   emscripten_console_logf("CWD: %s", CWD);
   assert(strcmp(CWD, "/data") == 0); // fail, result is '//data' should be '/data'
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions