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

How to use wasi-vfs with Python? #5

Closed
rjzak opened this issue Aug 3, 2022 · 16 comments
Closed

How to use wasi-vfs with Python? #5

rjzak opened this issue Aug 3, 2022 · 16 comments

Comments

@rjzak
Copy link

rjzak commented Aug 3, 2022

I'd like to use wasi-vfs with Python-Wasi, as I've done with Ruby, but it seems something has to be done with the .wasm file before wasi-vfs can add the files. How can I get started? Thank you.

CC: @nickvidal @bstrie

@kateinoigakukun
Copy link
Owner

I haven't tried it yet but it works well in theory. As shown in the section (https://github.com/kateinoigakukun/wasi-vfs#build-and-run-wasi-application), it would work by just linking libwasi_vfs.a, which is distributed in the GitHub Release page (https://github.com/kateinoigakukun/wasi-vfs/releases/tag/v0.1.1)

@pvetere
Copy link

pvetere commented Aug 25, 2022

I was able to successfully build WASI Python with the VFS. I cloned tiran's cpython fork (https://github.com/tiran/cpython.git) and added a couple of hacks, as shown in this diff (obviously, update the paths for your own environment):

pvetere/cpython@e291f4e

Then, from the repo's root, I did the following:

  1. Build WASI-enabled Python:
Tools/wasm/wasm_build.py wasi compile
  1. Create the runtime structure in ./pyroot:
cd builddir/wasi && make install && cd ../..
  1. Pack the runtime libs into the Wasm module. Run the following command from the root of the repo. It may take a minute or two:
wasi-vfs pack builddir/wasi/python.wasm --mapdir lib::$(pwd)/pyroot -o builddir/wasi/python-with-vfs.wasm
  1. Now you should be able to run python in Wasm with the VFS, like in the following example. The --dir . option is necessary to give the right capabilities to wasmtime, but the libs are all read from the VFS.
% wasmtime run --dir . -- builddir/wasi/python-with-vfs.wasm
Python 3.12.0a0 (heads/main-dirty:32ac98e899, Aug 25 2022, 10:59:15) [Clang 13.0.0 (https://github.com/llvm/llvm-project fd1d8c2f04dde23bee0fb3a7d069 on wasi
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import json
>>>

Hope this helps!

@rjzak
Copy link
Author

rjzak commented Aug 25, 2022

Would that still work without --dir . ?

@rjzak
Copy link
Author

rjzak commented Aug 25, 2022

I was able to compile with libwasi_vfs.a, which is progress! But at the packing step, I got:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: failed to compile the Wasm module

Caused by:
    0: WebAssembly translation error
    1: Invalid input WebAssembly code at offset 3430015: bulk memory support is not enabled', crates/wasi-vfs-cli/src/bin/wasi-vfs.rs:5:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@pvetere
Copy link

pvetere commented Aug 25, 2022

It doesn't work without --dir . for me. I'm not positive, but I suspect that this might be caused by https://github.com/WebAssembly/WASI/issues/303, and python is trying calling a getcwd() when it starts.

@rjzak
Copy link
Author

rjzak commented Aug 25, 2022

Would it be catastrophic to have os.getcwd() (or whichever getcwd()) just return "/" on Wasi?

@pvetere
Copy link

pvetere commented Aug 25, 2022

I forgot a step above. I also had to hack a few lines into wasi-vfs as well. Then, I used this custom binary on the compile and pack steps. See this diff:

pvetere@0a36099

Maybe this will get you further.

@rjzak
Copy link
Author

rjzak commented Aug 25, 2022

Still not working, same issue with your Wasi-VFS fork.

@rjzak
Copy link
Author

rjzak commented Aug 25, 2022

Do you get an error like this when omitting --dir .?

Exception ignored error evaluating path:
Traceback (most recent call last):
  File "<frozen getpath>", line 353, in <module>
OSError: [Errno 76] Capabilities insufficient
Fatal Python error: error evaluating path
Python runtime state: core initialized

Current thread 0x00000000 (most recent call first):
  <no Python frame>
Error: failed to execute default function

@pvetere
Copy link

pvetere commented Aug 25, 2022

Yes, that's the error I get as well.

@kateinoigakukun
Copy link
Owner

Thank you for your detailed report!

The problems here were:

  1. We shouldn't change PREFIX and should use DESTDIR to change the python installation directory
  2. VFS doesn't support symlink yet so path_readlink should always return ERRNO_INVAL

I've also fixed some minor issues that trigger some warnings here: #6
Could you try again by following the instruction described in the PR?

@rjzak
Copy link
Author

rjzak commented Aug 26, 2022

@kateinoigakukun I still get the error #5 (comment), even with the latest wasi-vfs code, and recompiling both libwasi_vfs.a and wasi-vfs-cli. Maybe could you share the VFS-packed binary so I can see if it works with Enarx?

@kateinoigakukun
Copy link
Owner

kateinoigakukun commented Aug 26, 2022

@rjzak Hmm, it looks like you didn't enable wasmparser or wasmtime's bulk memory feature in your application. Please review configuration of wasmtime in your loader code.

The compiled binary is too large to upload to GitHub issue ... 😅 (because I packed the whole installation directory)

Screen Shot 2022-08-27 at 5 20 26

@rjzak
Copy link
Author

rjzak commented Aug 26, 2022

@kateinoigakukun The application is wasi-vfs (not wasmtime or any code I wrote) and it doesn't seem to have any features to enable.

@kateinoigakukun
Copy link
Owner

@rjzak Ah sorry, I got it. I think you are using newer version of wasi-sdk, which requires bulk-memory feature and I'm using wasi-sdk 14.0. The root issue is wizer, which is a pre-initialization tool for wasm and this project depends on it, doesn't support the feature for now, so it disables it here: https://github.com/bytecodealliance/wizer/blob/76602d16828648e82720762b83f2ff6f9489ca84/src/lib.rs#L511

We have to add support for the feature to wizer, but could you try with older wasi-sdk for now?

@rjzak
Copy link
Author

rjzak commented Oct 12, 2022

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

3 participants