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

Support Wasm sign extension and bulk memory extension #4367

Open
SaiProServ opened this issue Oct 20, 2023 · 3 comments
Open

Support Wasm sign extension and bulk memory extension #4367

SaiProServ opened this issue Oct 20, 2023 · 3 comments
Assignees

Comments

@SaiProServ
Copy link

Take a look into casper-wasmi/casper-wasm and asses possibility to support sign extension and bulk memory extension without dealing with wasmi upgrade. If it’s possible by enabling flags, we should do it, otherwise we’d have to wait and upgrade wasmi first (ticket above). Modern compilers i.e. rust, and golang are emitting these extensions by default, so if we want to support these compilers we should also support these extensions.

@koxu1996
Copy link

koxu1996 commented Nov 7, 2023

Developing smart contracts on Casper is currently far from being developer-friendly. I am delighted we are facing this issue, as it presents a great opportunity to make some improvements.

Manual WASM Postprocessing

I created minimal-casper-code to demonstrate that minimal WASM is ~916 KB 😨, which is really close to the deployment limit of 1 MB.

In practice, all Casper developers are forced to manually strip the binary.

This is somewhat mitigated by the provided contract template, which includes wasm-strip command in the Makefile:

Additionally, the documentation describes stripping code as a tip, even though it is an essential step that should not be overlooked.


Along with identifying issues, I am eager to offer my assistance and solutions!

I also encountered a sign-extension problem that resulted in:

Wasm preprocessing error: Deserialization error: Unknown opcode 192

Opcode 192 (0xC0 in hex) is the i32.extend8_s instruction in WebAssembly. Since it is not supported in wasmi, we can find some workaround.

Replacing Opcode 192

An easy mitigation is described in near/nearcore#8358 (comment):

use wasm-opt (from binaryen) on the WebAssembly binary file with the --signext-lowering option

I applied this to my binary, and the change was minor:

<     i32.extend8_s
---
>     i32.const 24
>     i32.shl
>     i32.const 24
>     i32.shr_s

Fortunately, that was sufficient to allow my code - compiled with rustc 1.75.0 - to execute successfully on the Casper blockchain 😀.

Additionally, I got smaller binary size than with wasm-strip.

My Recommendation

I recommend three action items:

  1. Since users are already forced to manually strip the binary, we should modify the contract template by replacing wasm-strip (from wabt) with wasm-opt --strip-debug --signext-lowering.
  2. Update the documentation accordingly.
  3. Finally, bump toolchain to the latest version nightly-2023-10-03.

As an additional point, I propose considering the integration of WASM postprocessing directly within the node, although I recognize this may be a complex task.

@devendran-m
Copy link
Contributor

Hi @mpapierski , can we close this?

@mpapierski
Copy link
Collaborator

Hi @mpapierski , can we close this?

Sign extensions are addressed by #4894, we'd still need another ticket for supporting bulk memory operations if we decide it's worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants