emscripten-core / emscripten-core/emscripten

/proc/self/fd only supports very limited actions

Open
#17,694 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

The current fs implementation does not allow to access `/proc/self/fd` and anything in it,
with anything else than `open()` and `readlink()` for the files inside the directory. So anything like using `readdir()` on the fd folder or `stat()` on either the dir or the files inside result in errors.

The problem seems to lie here: https://github.com/emscripten-core/emscripten/blob/77da62969eb0b5efe4ce4a0c9ce67c45b9e32638/src/library_fs.js#L1362-L1381

Tar with a demo project: [em_proc_fd_bug.zip](https://github.com/emscripten-core/emscripten/files/9389431/em_proc_fd_bug.zip)

Result of the demo project on linux
```
sucessfully opened /proc/self/fd: DIR* 0x561c7082e2a0
- entry: .
- entry: ..
- entry: 0
- entry: 1
- entry: 2
- entry: 3
successfully got position in dir: 66
sucessfully closed dir
stat(/proc/self/fd) was sucessfull; st_mode: 16384
stat(/proc/self/fd/0) was sucessfull; st_mode: 8192
fd0: /dev/pts/7
done
```

Result running it with emscripten
```
sucessfully opened /proc/self/fd: DIR* 0x5017c8
error in readdir(0x5017c8): Not a directory (54)
successfully got position in dir: 0
sucessfully closed dir
error in stat(/proc/self/fd): Operation not permitted (63)
error in stat(/proc/self/fd/0): Operation not permitted (63)
fd0: /dev/tty
done
```

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.17-git (fbc532773d84d2bd7da876275671970e792ad1cd)
clang version 15.0.0 (/startdir/llvm-project 661577b5f40bb05534869b9635315e9265c37417)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/emscripten-llvm/bin

**Full link command and output with `-v` appended:**

emcc -v -o main.html main.c

```
"/opt/emscripten-llvm/bin/clang" -target wasm32-unknown-emscripten -DEMSCRIPTEN -D__EMSCRIPTEN_major__=3 -D__EMSCRIPTEN_minor__=1 -D__EMSCRIPTEN_tiny__=17 -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/SDL --sysroot=/home/mai/.emscripten_cache/sysroot -Xclang -iwithsysroot/include/compat -v main.c -c -o /tmp/emscripten_temp_ua6oinb5/main_0.o
clang version 15.0.0 (/startdir/llvm-project 661577b5f40bb05534869b9635315e9265c37417)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/emscripten-llvm/bin
(in-process)
"/opt/emscripten-llvm/bin/clang-15" -cc1 -triple wasm32-unknown-emscripten -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/mai/projects/github/em_bug -resource-dir /opt/emscripten-llvm/lib/clang/15.0.0 -D EMSCRIPTEN -D __EMSCRIPTEN_major__=3 -D __EMSCRIPTEN_minor__=1 -D __EMSCRIPTEN_tiny__=17 -isysroot /home/mai/.emscripten_cache/sysroot -internal-isystem /opt/emscripten-llvm/lib/clang/15.0.0/include -internal-isystem /home/mai/.emscripten_cache/sysroot/include/wasm32-emscripten -internal-isystem /home/mai/.emscripten_cache/sysroot/include -Werror=implicit-function-declaration -fdebug-compilation-dir=/home/mai/projects/github/em_bug -ferror-limit 19 -fvisibility default -fgnuc-version=4.2.1 -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/SDL -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /tmp/emscripten_temp_ua6oinb5/main_0.o -x c main.c
clang -cc1 version 15.0.0 based upon LLVM 15.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/mai/.emscripten_cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
/home/mai/.emscripten_cache/sysroot/include/SDL
/home/mai/.emscripten_cache/sysroot/include/compat
/opt/emscripten-llvm/lib/clang/15.0.0/include
/home/mai/.emscripten_cache/sysroot/include
End of search list.
"/opt/emscripten-llvm/bin/wasm-ld" -o main.wasm /tmp/emscripten_temp_ua6oinb5/main_0.o -L/home/mai/.emscripten_cache/sysroot/lib/wasm32-emscripten -lGL -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024
"/usr/bin/wasm-emscripten-finalize" --dyncalls-i64 main.wasm -o main.wasm --detect-features
"/usr/bin/node" /usr/lib/emscripten/src/compiler.js /tmp/tmpd2demrh4.json
"/opt/emscripten-llvm/bin/llvm-objcopy" main.wasm main.wasm --remove-section=.debug* --remove-section=producers
"/usr/bin/node" /usr/lib/emscripten/tools/preprocessor.js /tmp/emscripten_temp_ua6oinb5/settings.js shell.html
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.