soroban-cli icon indicating copy to clipboard operation
soroban-cli copied to clipboard

Remove absolute registry paths from panic messages in wasm

Open brson opened this issue 1 year ago • 3 comments
trafficstars

What

This uses the --remap-path-prefix option to rustc to eliminate absolute paths embedded in the wasm.

Fixes https://github.com/stellar/stellar-cli/issues/1445

Why

This is generally required to make builds reproducible across different machines.

These absolute paths can appear for both debuginfo and panic messages, though in our case since we are compiling in release mode and stripping, in practice this only affects panic messages.

The comments in the patch are fairly thorough as to why and how the patch works, but there are some things to note.

Known limitations

This relies on setting CARGO_ENCODED_RUSTFLAGS, which is similar to RUSTFLAGS, and which cargo interprets with higher priority. As of now this is the only way to pass the required flag to all dependencies, and crates.io dependencies are where this problem appears specifically (we can't just pass -- --remap-path-prefix to cargo rustc).

By setting one of the RUSTFLAGS vars, cargo ignores any build.rustflags and similar configuration options on the local filesystem. Detecting this situation and adapting to it is challenging and probably involves using cargo's own API as a dependency.

I do not quite understand the conditions under which the problem this addresses actually occurs, and don't know how to succinctly construct a test case. Panic messages in the local workspace do not exhibit the problem, nor do those in the pre-compiled standard library (which is already compiled in a similar way). Triggering the problem requires using dependencies from crates.io; and possibly crates.io crates that call into the core library where a panic is located - I'm not sure.

I know it occurs for the eth_abi example, which depends on some third-party crates.

The data section of the eth_abi wat before and after this patch:

  (data (;0;) (i32.const 1048576) "/home/brian/.cargo/registry/src/index.crates.io-6f17d22bba15001f/alloy-sol-t
ypes-0.6.3/src/types/data_type.rs/home/brian/.cargo/registry/src/index.crates.io-6f17d22bba15001f/soroban-sdk-2
1.6.0/src/bytes.rs\00\00\00m\00\10\00`\00\00\00>\02\00\00\0d\00\00\00\00\00\10\00m\00\00\00\ed\03\00\00\01\00\0
0\00/home/brian/.cargo/registry/src/index.crates.io-6f17d22bba15001f/alloy-sol-types-0.6.3/src/abi/decoder.rs\0
0\00\00\f0\00\10\00i\00\00\00\ad\00\00\00\1b\00\00\00\f0\00\10\00i\00\00\00\ba\00\00\00R\00\00\00\00\00\10\00m\
00\00\00\a9\00\00\00\0d\00\00\00\00\00\10\00m\00\00\00\a9\00\00\00\13\00\00\00\00\00\00\00\00\00\00\00\01\00\00
\00\06\00\00\00called `Result::unwrap()` on an `Err` valueTryFromSliceErrorsrc/lib.rs\00\00\e8\01\10\00\0a\00\0
0\00#\00\00\00-\00\00\00capacity overflow\00\00\00\04\02\10\00\11\00\00\00library/alloc/src/raw_vec.rs \02\10\0
0\1c\00\00\00\19\00\00\00\05\00\00\00()attempt to add with overflow\00\00N\02\10\00\1c\00\00\00attempt to multi
ply with overflow\00\00\00t\02\10\00!\00\00\00)\00\00\00\01\00\00\00\00\00\00\00explicit panic\00\00\ac\02\10\0
0\0e\00\00\00: \00\00\01\00\00\00\00\00\00\00\c4\02\10\00\02\00\00\00\00\00\00\00\0c\00\00\00\04\00\00\00\07\00\00\00\08\00\00\00\09\00\00\00    ,\0a((\0a00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 out of range for slice of length range end index \00\e3\03\10\00\10\00\00\00\c1\03\10\00\22\00\00\00source slice length () does not match destination slice length (\04\04\10\00\15\00\00\00\19\04\10\00+\00\00\00\a0\02\10\00\01\00\00\00/home/brian/.cargo/registry/src/index.crates.io-6f17d22bba15001f/soroban-sdk-21.6.0/src/alloc.rs\5c\04\10\00`\00\00\00\1b\00\00\00\0a\00\00\00\5c\04\10\00`\00\00\00$\00\00\00\1b\00\00\00\5c\04\10\00`\00\00\00?\00\00\00\0d\00\00\00"))
  (data (;0;) (i32.const 1048576) "index.crates.io-6f17d22bba15001f/alloy-sol-types-0.6.3/src/types/data_type.rsindex.crates.io-6f17d22bba15001f/soroban-sdk-21.6.0/src/bytes.rs\00\00\00M\00\10\00@\00\00\00>\02\00\00\0d\00\00\00\00\00\10\00M\00\00\00\ed\03\00\00\01\00\00\00index.crates.io-6f17d22bba15001f/alloy-sol-types-0.6.3/src/abi/decoder.rs\00\00\00\b0\00\10\00I\00\00\00\ad\00\00\00\1b\00\00\00\b0\00\10\00I\00\00\00\ba\00\00\00R\00\00\00\00\00\10\00M\00\00\00\a9\00\00\00\0d\00\00\00\00\00\10\00M\00\00\00\a9\00\00\00\13\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\06\00\00\00called `Result::unwrap()` on an `Err` valueTryFromSliceErrorsrc/lib.rs\00\00\88\01\10\00\0a\00\00\00#\00\00\00-\00\00\00capacity overflow\00\00\00\a4\01\10\00\11\00\00\00library/alloc/src/raw_vec.rs\c0\01\10\00\1c\00\00\00\19\00\00\00\05\00\00\00()attempt to add with overflow\00\00\ee\01\10\00\1c\00\00\00attempt to multiply with overflow\00\00\00\14\02\10\00!\00\00\00)\00\00\00\01\00\00\00\00\00\00\00explicit panic\00\00L\02\10\00\0e\00\00\00: \00\00\01\00\00\00\00\00\00\00d\02\10\00\02\00\00\00\00\00\00\00\0c\00\00\00\04\00\00\00\07\00\00\00\08\00\00\00\09\00\00\00    ,\0a((\0a00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 out of range for slice of length range end index \00\83\03\10\00\10\00\00\00a\03\10\00\22\00\00\00source slice length () does not match destination slice length (\a4\03\10\00\15\00\00\00\b9\03\10\00+\00\00\00@\02\10\00\01\00\00\00index.crates.io-6f17d22bba15001f/soroban-sdk-21.6.0/src/alloc.rs\fc\03\10\00@\00\00\00\1b\00\00\00\0a\00\00\00\fc\03\10\00@\00\00\00$\00\00\00\1b\00\00\00\fc\03\10\00@\00\00\00?\00\00\00\0d\00\00\00"))

I have tested this on windows in addition to linux and mac and it appears to work.

brson avatar Sep 10 '24 22:09 brson