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

Does not link env module when compiling libs with [no_std] #333

Open
Loara opened this issue Sep 5, 2024 · 1 comment
Open

Does not link env module when compiling libs with [no_std] #333

Loara opened this issue Sep 5, 2024 · 1 comment

Comments

@Loara
Copy link

Loara commented Sep 5, 2024

cargo component build fails to compile Rust libraries with [no_std] flags because it cannot resolve the env import interface:

error: failed to decode world from module

Caused by:
    0: module was not valid
    1: module requires an import interface named `env`

Minimum reproducible example:

src/lib.rs (it uses an allocator from the lol_alloc crate)

#![no_std]

extern crate alloc;
extern crate lol_alloc;

#[allow(warnings)]
mod bindings;

#[global_allocator]
static ALLOCATOR : lol_alloc::FailAllocator = lol_alloc::FailAllocator;

#[panic_handler]
fn panic(_i : &core::panic::PanicInfo) -> ! {
    core::arch::wasm32::unreachable()
}

use bindings::Guest;
use alloc::string::{String, ToString};

struct Component;

impl Guest for Component {
    /// Say hello!
    fn hello_world() -> String {
        "Hello, World!".to_string()
    }
}

bindings::export!(Component with_types_in bindings);

wit/world.wit

package component:nostd;

/// An example world for the component to target.
world example {
    export hello-world: func() -> string;
}
@primoly
Copy link
Contributor

primoly commented Sep 11, 2024

For what it’s worth, the compilation succeeds but the final output wasm imports the symbols "env" "memset" and "env" "memcpy".

To avoid this you can use RUSTFLAGS='-C target-feature=+bulk-memory' cargo component build to generate a wasip1 component which depends on the widely supported1 bulk memory operations.

Alternatively you can use cargo component build --target wasm32-unknown-unknown to generate a non Wasi component instead.

Footnotes

  1. https://webassembly.org/features/

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