wasi-libc | WASI libc implementation for WebAssembly
kandi X-RAY | wasi-libc Summary
kandi X-RAY | wasi-libc Summary
WASI Libc is a libc for WebAssembly programs built on top of WASI system calls. It provides a wide array of POSIX-compatible C APIs, including support for standard I/O, file I/O, filesystem manipulation, memory management, time, string, environment variables, program startup, and many other APIs. WASI Libc is sufficiently stable and usable for many purposes, as most of the POSIX-compatible APIs are stable, though it is continuing to evolve to better align with wasm and WASI.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of wasi-libc
wasi-libc Key Features
wasi-libc Examples and Code Snippets
Community Discussions
Trending Discussions on wasi-libc
QUESTION
I have a re-entrant C++ function whose wasm
output is not "thread-safe" when using imported shared memory, because the function makes use of an aliased stack that lives on the shared linear memory on a hardcoded position.
I'm aware that multithreading is not fully supported yet, and if I want to use multiple instances of the same module concurrently, avoiding crashing and data races it's my responsibility, but I accept the challenge.
My X problem is: My code is not thread-safe, and I need it to be by having non-overlapping stacks.
My Y problem is: I'm trying to modify the __stack_pointer
so I can implement the stack separation, but it doesn't compile. I have tried with extern unsigned char __stack_pointer;
but it throws me the following error:
ANSWER
Answered 2021-Nov-02 at 17:46Firstly, if you are doing multi-threading with emscripten then each thread will already have its own stack and its own value for __stack_pointer
. Thats is part of what defines a thread.
If you still want to manipulate the stack yourself (perhaps to have many stacks within a single thread) then you can use the emscripten helper functions stackSave
(to get the SP of the current thread) and stackRestore
(to set the SP of the current thread).
If you are not using emscripten at all, then you are in uncharted territory (what runtime are using using? how are you starting new threads?), but the simplest way to do stack pointer manipulation would be with assembly code. See how emscripten implements these functions:
https://github.com/emscripten-core/emscripten/blob/main/system/lib/compiler-rt/stack_ops.S
So you could do something like this:
QUESTION
I used clang --target=wasm32-unknown-wasi ...
to compile a Hello World C program into WebAssembly. Everything goes fine, and the .wasm
file can run correctly in runtime like wasmer.
But when I check the .wasm
file(in .wat
format), I found some i64 instructions like:
ANSWER
Answered 2021-Oct-25 at 15:45I see nothing wrong with that. Targeting 32bits means that the memory addressing is 32bits. For this reason before the memory store instruction, all you have are 32bit instructions (i.e. the offset and the value are i32) you have this at some point:
QUESTION
I'm trying to run some small C demos on the web with WebAssembly and pure JS, and I'm compiling my code using WASI-SDK/WASI-libc:
clang --target=wasm32-unknown-wasi --sysroot= -nostartfiles -O3 -flto -Wl,--no-entry -Wl,--export=malloc -Wl,--export-all -Wl,--lto-O3 src.c -o src.wasm
I'm then using this small JS library to implement the WASI functions. This works fine for printing with stdout, and I've even tested passing strings and other types into different functions. But I can't figure out how to pass an array of strings into main
as an argument.
I don't want to use Node or Emscripten, which is why I went with a minimal JS implementation.
Edit:
To add command line arguments, I removed both -nostartfiles
and -Wl,--no-entry
from my compiler call and implemented args_get
and args_sizes_get
from the WASI standard. From there, it was as simple as calling _start
from the wasm
's exported functions.
ANSWER
Answered 2021-May-12 at 07:11If you want to use WASI then the way to pass args to main is to implement the wasi_snapshot_preview1.args_get
and wasi_snapshot_preview1.args_sizes_get
syscalls which are used by WASI programs to access the argv values. In that case you would want to call _start
rather than main
.
If you want to bypass that and call main
directly you would need to somehow allocate and array of char *
pointers in the linear memory which you could then pass as your argv value. The problem that you face if you try to take this approach is that allocating memory (e.g. via malloc) before calling main is hard. My advise would be to go with the first method which is to call _start
and implement that wasi
syscall needed to access argv
. (You can call also then remove the -Wl,--no-entry
from your link command).
QUESTION
Yes, there is a duplicate problem, but it was asked 5 years ago and haven't been updated for a long time.
In 2020, with the development of WebAssembly, is there a way to compile and run a simple C program locally in the browser?
There is a platform called WasmFiddle which can compile C to wasm in browser, but it lacks the support of standard libraries, such as stdio.h. I think we can implement standard librarys in js and maybe export it to wasm? But this requires lots of work.
My original goal is to build a web-based IDE for students to learn C programming without costing a lot on servers for remote running. So, only libraries like stdio.h, math.h, string.h are required.
UPDATE: this seems like a great implementation of libc to wasm.
High performance is not required, so wasm-based solutions and maybe a VM running c implemented in JS are both greate solutions.
...ANSWER
Answered 2020-May-21 at 01:03Emscripten and WASM are the two popular solutions here.
Don't expect great performance, but you should then be able to link it up with a little bit of JavaScript, CSS and HTML for your code editing and console views.
If you're okay with running a server, then you can use this Jupyter Notebook kernel: https://github.com/jupyter-xeus/xeus-cling
Here's an example in WASM, with no server: https://github.com/tbfleming/cib
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install wasi-libc
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page