0xPolygonMiden / miden-vm

STARK-based virtual machine
MIT License
611 stars 148 forks source link

bug: substract with overflow when compiling on debug mode #1371

Open mFragaBA opened 1 week ago

mFragaBA commented 1 week ago

Context

When trying to run tests from here I get the following error when it tries to compile this account code:

git clone https://github.com/RizeLabs/aze-cli.git
cd aze-cli
git checkout feat/remove-note-asset
cd cli
cargo test --test e2e -- --nocapture
attempt to subtract with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:72:14
   2: core::panicking::panic
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:145:5
   3: miden_assembly::assembler::instruction::mem_ops::local_to_absolute_addr
             at /Users/lambda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/miden-assembly-0.9.2/src/assembler/instruction/mem_ops.rs:114:15
   4: miden_assembly::assembler::instruction::mem_ops::mem_write_imm
             at /Users/lambda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/miden-assembly-0.9.2/src/assembler/instruction/mem_ops.rs:81:9
   5: miden_assembly::assembler::instruction::<impl miden_assembly::assembler::Assembler>::compile_instruction
             at /Users/lambda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/miden-assembly-0.9.2/src/assembler/instruction/mod.rs:272:41
   6: miden_assembly::assembler::Assembler::compile_body
             at /Users/lambda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/miden-assembly-0.9.2/src/assembler/mod.rs:325:42
   7: miden_assembly::assembler::Assembler::compile_procedure
             at /Users/lambda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/miden-assembly-0.9.2/src/assembler/mod.rs:297:13
   8: miden_assembly::assembler::Assembler::compile_module
             at /Users/lambda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/miden-assembly-0.9.2/src/assembler/mod.rs:246:13
   9: miden_objects::accounts::code::AccountCode::new
             at /Users/lambda/lambda/miden-base/objects/src/accounts/code.rs:56:26
  10: aze_lib::accounts::create_basic_aze_player_account

However if I run it with --release we don't get an error.

Relevant deps

miden-lib = { version = "0.3.0", features= ["concurrent","testing"] }
miden-tx = { version = "0.3.0", default-features = false }
miden-client = { version = "0.3.0", features= ["concurrent","testing"] }
miden-objects = { version = "0.3.0", default-features = false }
Overcastan commented 5 days ago

Hi @mFragaBA, thank you for the bug report!

As far as I can say this error occurs because procedures encrypt_shuffle and remask_shuffle in the player.masm don't specify the number of locals, both of them should have at least 2. Without it program tries to calculate the indexes for the local values, which causes the subtraction with underflow. We should add a proper check in this place to print the real error message.

Although it is still unclear why the —release version works fine, probably it is connected with some optimizations of the compiler and assembler. Are you sure that you use encrypt_shuffle and remask_shuffle procedures during the test?

bobbinth commented 5 days ago

Although it is still unclear why the —release version works fine,

In release mode, the compiler don't emit overflow/underflow checks as far as I now.

We should add a proper check in this place to print the real error message.

Yes - let's do that.

mFragaBA commented 4 days ago

Are you sure that you use encrypt_shuffle and remask_shuffle procedures during the test?

Yeap! Notes shuffle.masm and remask.mask (they call encrypt_shuffle and remask_shuffle) get consumed during the test.

BTW, thanks for debugging the issue! I tried for a bit but I'm not too familiar with the compiler internals yet