Skip to content

Commit 82ad5ef

Browse files
authored
fix: ensure no invalid opcodes in wasm light client binary (#92)
also added a check to ensure that sign-ext opcodes don't find their way back into the binary. this also provides a significant improvement to the size of the binary, from 2.8M to 1.1M (758K to 322K gzipped). closes #91
2 parents f32af1e + c09430f commit 82ad5ef

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

dictionary.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,7 @@ Boneh
493493
Shacham
494494
zkps
495495
Verkle
496-
Polkadot
496+
Polkadot
497+
passthru
498+
wasi
499+
rustlib

flake.nix

+15-7
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,24 @@
5757
];
5858
perSystem = { config, self', inputs', pkgs, system, lib, ... }:
5959
let
60-
rust-nightly = pkgs.rust-bin.fromRustupToolchain {
60+
nightlyConfig = {
6161
channel = "nightly-2023-05-16";
6262
components = [ "rust-src" "rust-analyzer" ];
6363
profile = "default";
6464
};
6565

66-
withBuildTarget = target: crane.lib.${system}.overrideToolchain (pkgs.rust-bin.fromRustupToolchain {
67-
channel = "nightly-2023-05-16";
68-
components = [ "cargo" "rustc" "rust-src" ];
69-
targets = [ target ];
70-
});
66+
rust-nightly = pkgs.rust-bin.fromRustupToolchain nightlyConfig;
67+
68+
withBuildTarget = target:
69+
let
70+
toolchain = pkgs.rust-bin.fromRustupToolchain {
71+
inherit (nightlyConfig) channel profile;
72+
components = nightlyConfig.components ++ [ "cargo" "rustc" "rust-src" ];
73+
# hopefully if we ever use wasi this issue will be resolved: pkgs.rust.toRustTarget pkgs.hostPlatform
74+
targets = [ target (pkgs.rust.toRustTarget pkgs.hostPlatform) ];
75+
};
76+
in
77+
crane.lib.${system}.overrideToolchain (toolchain) // { inherit toolchain; };
7178
craneLib = crane.lib.${system}.overrideToolchain rust-nightly;
7279

7380
mkChecks = pkgName: checks: pkgs.lib.mapAttrs' (name: value: { name = "${pkgName}-${name}"; value = value; }) checks;
@@ -128,7 +135,8 @@
128135

129136
crane = {
130137
lib = craneLib;
131-
inherit withBuildTarget cargoArtifacts commonAttrs mkChecks;
138+
hostTarget = pkgs.rust.toRustTarget pkgs.hostPlatform;
139+
inherit withBuildTarget cargoArtifacts commonAttrs mkChecks rustSrc;
132140
};
133141

134142
proto = {
+32-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
{ ... }: {
22
perSystem = { self', pkgs, system, config, inputs', crane, ... }:
33
let
4-
attrs = crane.commonAttrs
5-
// (crane.lib.crateNameFromCargoToml { cargoToml = ./ethereum-light-client/Cargo.toml; })
4+
rustToolchain = crane.withBuildTarget CARGO_BUILD_TARGET;
5+
6+
attrs = (crane.lib.crateNameFromCargoToml { cargoToml = ./ethereum-light-client/Cargo.toml; })
67
// {
78
cargoExtraArgs = "-p union-ethereum-lc --features eth-minimal";
9+
src = crane.rustSrc;
10+
cargoVendorDir = crane.lib.vendorMultipleCargoDeps {
11+
inherit (crane.lib.findCargoFiles crane.rustSrc) cargoConfigs;
12+
cargoLockList = [
13+
../Cargo.lock
14+
"${rustToolchain.toolchain.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/Cargo.lock"
15+
];
16+
};
817
};
918

10-
# cargoArtifacts = crane.lib.buildDepsOnly attrs;
11-
1219
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
1320
in
1421
{
1522
packages = {
16-
wasm-ethereum-lc = (crane.withBuildTarget CARGO_BUILD_TARGET).buildPackage (attrs // {
23+
wasm-ethereum-lc = rustToolchain.buildPackage (attrs // {
1724
inherit CARGO_BUILD_TARGET;
1825

19-
# RUSTFLAGS are used to optimize the binary size
26+
cargoBuildCommand = "RUSTFLAGS='-C target-feature=-sign-ext -C link-arg=-s -C target-cpu=mvp' cargo -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort build --release --lib --target ${CARGO_BUILD_TARGET}";
27+
28+
checkPhase = ''
29+
cargo test ${attrs.cargoExtraArgs} --target ${crane.hostTarget}
30+
31+
# grep exits 0 if a match is found
32+
if ${pkgs.binaryen}/bin/wasm-dis target/wasm32-unknown-unknown/release/union_ethereum_lc.wasm | grep -P '\.extend\d{1,2}_s'
33+
then
34+
echo "wasm binary contains invalid opcodes!"
35+
exit 1
36+
else
37+
echo "wasm binary doesn't contain any sign-extension opcodes!"
38+
fi
39+
'';
40+
2041
installPhase = ''
2142
mkdir -p $out/lib
2243
# Optimize the binary size a little bit more
@@ -28,9 +49,10 @@
2849
});
2950
};
3051

31-
checks = crane.mkChecks "wasm-ethereum-lc" {
32-
clippy = crane.lib.cargoClippy (attrs // { inherit (crane) cargoArtifacts; });
33-
test = crane.lib.cargoTest (attrs // { inherit (crane) cargoArtifacts; });
34-
};
52+
checks = crane.mkChecks "wasm-ethereum-lc"
53+
{
54+
clippy = crane.lib.cargoClippy (attrs // { inherit (crane) cargoArtifacts; });
55+
test = crane.lib.cargoTest (attrs // { inherit (crane) cargoArtifacts; });
56+
};
3557
};
3658
}

light-clients/ethereum-light-client/.cargo/config.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ unit-test = "test --lib"
55
integration-test = "test --test integration"
66
schema = "run --bin schema"
77

8-
rustflags = ["-C link-arg=-s"]
8+
rustflags = ["-C target-feature=-sign-ext", "-C link-arg=-s", "-C target-cpu=mvp"]
9+

0 commit comments

Comments
 (0)