rust-lang / rust-lang/rust

wasm32-wasip1 depends on libc memset with no_std

Open
#130,621 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion O-wasi O-wasm T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I've looked high and low between the WASM/WASI specifications and can't find what is the "correct" behavior here but the current rust behavior seems wrong to me.

I don't believe we should be trying to import memset from the "env" module:

(func $import0 (import "env" "memset") (param i32 i32 i32) (result i32))

wasm3 engine can't run this code either due to this:

$ wasm3 target/wasm32-wasip1/release/wasm_br_test.wasm 
Error: missing imported function ('env.memset')
Code
#![no_std]
#![no_main]

#[no_mangle]
pub fn _start() {
    let _asdf = [0; 40];
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}
Cargo.toml
[lib]
crate-type = ["cdylib"]

[profile.release]
lto = true
#opt-level = 's'
opt-level = 0
codegen-units = 1
panic = "abort"
strip = true
.cargo/config.toml
[build]
target = "wasm32-wasip1"


[target.wasm32-wasip1]
rustflags = ["-C", "link-arg=-zstack-size=65520",]
Meta

rustc --version --verbose:

rustc 1.83.0-nightly (f79a912d9 2024-09-18)
binary: rustc
commit-hash: f79a912d9edc3ad4db910c0e93672ed5c65133fa
commit-date: 2024-09-18
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
wasm generated binary
(module
  (type $type0 (func (param i32 i32 i32) (result i32)))
  (type $type1 (func))
  (func $import0 (import "env" "memset") (param i32 i32 i32) (result i32))
  (table $table0 1 1 funcref)
  (memory $memory0 1)
  (global $global0 (mut i32) (i32.const 65520))
  (export "memory" (memory $memory0))
  (export "_start" (func $func1))
  (func $func1
    (local $var0 i32) (local $var1 i32) (local $var2 i32) (local $var3 i32) (local $var4 i32) (local $var5 i32) (local $var6 i32)
    global.get $global0
    local.set $var0
    i32.const 160
    local.set $var1
    local.get $var0
    local.get $var1
    i32.sub
    local.set $var2
    local.get $var2
    global.set $global0
    i32.const 160
    local.set $var3
    i32.const 0
    local.set $var4
    local.get $var2
    local.get $var4
    local.get $var3
    call $import0
    drop
    i32.const 160
    local.set $var5
    local.get $var2
    local.get $var5
    i32.add
    local.set $var6
    local.get $var6
    global.set $global0
    return
  )
)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided no_std example, Cargo.toml, and .cargo/config.toml targeting wasm32-wasip1. Inspect the generated wasm binary for the env.memset import and compare its behavior with wasm3. Done means determining the correct target behavior and preventing an unusable memset dependency if the report is confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.