Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File read Error #72

Open
LeeJU0912 opened this issue Dec 4, 2024 · 2 comments
Open

File read Error #72

LeeJU0912 opened this issue Dec 4, 2024 · 2 comments

Comments

@LeeJU0912
Copy link

LeeJU0912 commented Dec 4, 2024

If I try to compile with GCC, I could see this error.

root@79bff74f036a:~/elfconv/bin# TARGET=Wasi ./elfconv.sh ../examples/mnist-neural-network-plain-c/mnist
[INFO] ELF -> LLVM bitcode...
elflift: /__w/cxx-common/cxx-common/vcpkg/buildtrees/llvm-16/src/org-16.0.5-90a3d25b70.clean/llvm/lib/IR/Instructions.cpp:631: void llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, const llvm::Twine&): Assertion `(Args.size() == FTy->getNumParams() || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Calling a function with bad signature!"' failed.
./elfconv.sh: line 22:   399 Aborted                 (core dumped) ./elflift --arch aarch64 --bc_out lift.bc --target_elf "$ELFPATH" --dbg_fun_cfg "$2" --target_arch "$wasi32_target_arch"
[INFO] LLVM bitcode (lift.bc) was generated.
[INFO] Compiling to Wasm (for WASI)... 
clang++: error: no such file or directory: 'lift.bc'
[INFO] exe.wasm was generated.
rm: cannot remove '/root/elfconv/bin/lift.bc': No such file or directory

So, I tried to cross-compile to aarch64 with clang-16 on x86 environment, elfconv container. It converted properly, but It cannot read files. This happens not only in this file, I tried with below test code but it couldn't find or create test_file.txt.

#include <unistd.h>
#include <fcntl.h>

int main() {
    // Open or create a test file
    int fd = open("/root/elfconv/bin/test_file.txt", O_WRONLY | O_CREAT, 0644);
    if (fd < 0) {
        return 1; // Error opening file
    }

    // Write some data to the file (minimal write syscall)
    write(fd, "Hello, fsync!", 13);

    // Call fsync to flush changes to disk
    fsync(fd);

    // Close the file
    close(fd);

    return 0;
}

This is mnist-neural-network-plain-c test error log. I also tried move exe.wasm to mnist-neural-network-plain-c folder but nothing changed.

root@79bff74f036a:~/elfconv/examples/mnist-neural-network-plain-c# clang-16 -static --target=aarch64-linux-gnu --gcc-toolchain=/usr --sysroot=/usr/aarch64-linux-gnu mnist.c mnist_file.c neural_network.c -lm -o mnist -fuse-ld=lld;
root@79bff74f036a:~/elfconv/examples/mnist-neural-network-plain-c# ls
Makefile  README.md  data  include  mnist  mnist.c  mnist_file.c  neural_network.c
root@79bff74f036a:~/elfconv/examples/mnist-neural-network-plain-c# cd ../../bin
root@79bff74f036a:~/elfconv/bin# ls
a.out  core.399  elfconv.sh  elflift  exe.html  exe.wasm  test.c
root@79bff74f036a:~/elfconv/bin# TARGET=Wasi ./elfconv.sh ../examples/mnist-neural-network-plain-c/mnist
[INFO] ELF -> LLVM bitcode...
[INFO] Opt Pass 1: [874/874]
[INFO] Opt Pass 2: [874/874]
[INFO] LLVM bitcode (lift.bc) was generated.
[INFO] Compiling to Wasm (for WASI)... 
[INFO] exe.wasm was generated.
root@79bff74f036a:~/elfconv/bin# wasmtime exe.wasm
openat error!: No such file or directory
Could not open file: data/train-images-idx3-ubyte
openat error!: No such file or directory
Could not open file: data/t10k-images-idx3-ubyte
Error: failed to run main module `exe.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x1cf7d7 - exe.wasm!__remill_read_memory_8
           1: 0x1407af - exe.wasm!neural_network_training_step_____1172_23a174
           2: 0x13fe58 - exe.wasm!main_____1183_239418
           3: 0x13fa9a - exe.wasm!__wrap_main
           4: 0x1cfb99 - exe.wasm!__remill_function_call
           5: 0xabb4c - exe.wasm!__libc_start_call_main_____1151_23a7b0
           6: 0xa8b41 - exe.wasm!__libc_start_main_impl_____1149_23a840
           7: 0x1b2f32 - exe.wasm!_start_____1191_239180
           8: 0x1ce064 - exe.wasm!main
           9: 0x1fe07e - exe.wasm!__main_void
          10: 0x18fe - exe.wasm!_start
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: memory fault at wasm address 0x7476e202 in linear memory of size 0x40290000
    3: wasm trap: out of bounds memory access
@yomaytk
Copy link
Owner

yomaytk commented Dec 4, 2024

Thank you for the report.

I think that we cannot execute examples/mnist-neural-network-plain-c on the latest commit by recently adding an optimization pass for LLVM IR (e.g. #53), but you will execute at least on the 8a236da as demo shows.
It is not hard work to fix for executing mnist-NN, so I will fix it soon.

However, we should be able to execute the simple file I/O program like the one you showed.
Generally, we need to explicitly indicate what path of the host filesystem is mapped inside the target Wasm when executing by WASI runtime because WASI security is based on capability-based security. Can you try the command wasmtime --dir=. exe.wasm? (it may be good to refer this).

If it still doesn't work with that command, please attach the binary file to this issue.

@LeeJU0912
Copy link
Author

Thank you for your kind explanation.

Oh, I understand. It is running on sandbox so I have to connect file manually. I tried wasmtime --dir=. exe.wasm but it cannot read files so I tried with absolute path wasmtime --dir=/root/elfconv/bin exe.wasm and it worked! Thank you 👍

I'm excited about fix mnist soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants