rustyscreeps / screeps-game-api

Typed bindings to the Screeps in-game API for WASM Rust AIs
MIT License
138 stars 45 forks source link

Official servers will not load wasm binaries compiled using Rust >=1.70 #391

Open shanemadden opened 1 year ago

shanemadden commented 1 year ago

Current nightly builds of rust use new wasm opcodes for sign-extension which will not load under the LLVM running on the official servers. Private servers tested appear to have no problem with the new opcodes.

Errors like Compiling wasm function #1389:<screeps::constants::small_enums::Direction as ... failed: Invalid opcode (enable with --experimental-wasm-se) @+1009670 will occur on attempt to load the module.

Ideally we'll be able to have a setting to control disabling these newer opcodes (possibly integrated into cargo-screeps) but a working workaround has not yet been identified - see https://github.com/rust-lang/rust/issues/109807

shanemadden commented 1 year ago

The workaround that we've identified to this issue using the resources in the linked issue requires nightly rust. To work around:

  1. Add build flags with set the target cpu to mvp, and rebuild std, which is usually used from the distribution pre-compiled. Example from my screeps.toml:

     [ptr.build]
      extra_options = ["--features=mmo", "--config", "build.rustflags=['-Ctarget-cpu=mvp']", "-Z", "build-std=std,panic_abort"]
  2. Additionally (and technically a separate issue), add the --disable-sign-ext to the options passed to wasm-opt, as current versions will add the new opcodes even when rustc does not. Example from my Cargo.toml:

    [package.metadata.wasm-pack.profile.release]
    wasm-opt = ["-O4", "--disable-sign-ext"]
shanemadden commented 9 months ago

An alternative workaround identified in the linked rust issue, that doesn't require build-std (but does require wasm-opt), is to add --signext-lowering to your wasm-opt options.