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

Bug when accessing string compiled into binary #247

Open
cmichi opened this issue Jan 7, 2025 · 0 comments
Open

Bug when accessing string compiled into binary #247

cmichi opened this issue Jan 7, 2025 · 0 comments

Comments

@cmichi
Copy link

cmichi commented Jan 7, 2025

I believe I found a bug in one of the polkavm components. I've put the reproducer here.

The reproducer program tries to output a string that is already present in its own blob (in the .data section, I think). But instead of printing the intended string ("Success" in the reproducer) we get an artifact from another string present in the blob. The length of the printed string is correct, the output is just a different part of another string from the blob.

cd examples/program-for-bug/
cargo +nightly build --release --no-default-features --target ../../crates/polkavm-linker/riscv64emac-unknown-none-polkavm.json -Zbuild-std="core,alloc" 

cd ../trigger-bug/
INPUT=../program-for-bug/target/riscv64emac-unknown-none-polkavm/release/program-for-bug cargo run

The output will be along the lines of

Calling into the guest program:
print: "heap: the nul"
print: "stack: "
print: "the nul"

This is wrong, it should be

Calling into the guest program:
print: "heap: Success"
print: "stack: Success"

The string the nul is present in the blob and part of a longer string. This can be checked via strings $INPUT. The length (7) corresponds to the length of "Success".

The bug also occurs if I use a different Rust version or don't use an allocator. If I don't include an allocator, the fragment that is output is just different.

Compiling program-for-bug without --release seems to mitigate the bug at least for the heap variant:

Calling into the guest program:
print: "heap: Success"
thread 'main' panicked at examples/trigger-bug/src/main.rs:48:10:
called `Result::unwrap()` on an `Err` value: Trap
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

1 participant