diff --git a/Cargo.lock b/Cargo.lock index 76c99ade..416b64aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -655,7 +655,7 @@ dependencies = [ [[package]] name = "era-compiler-llvm-context" version = "2.0.0" -source = "git+https://github.com/matter-labs/era-compiler-llvm-context?branch=main#68e4674db2c5030d589d758582e9c12ecf12b8a2" +source = "git+https://github.com/matter-labs/era-compiler-llvm-context?branch=fix%2Fyul-remove-constant#29a786852256865c8958b0843f2c5bd7386f9497" dependencies = [ "anyhow", "era-compiler-common", diff --git a/era-compiler-solidity/Cargo.toml b/era-compiler-solidity/Cargo.toml index 1e5b647f..04135e59 100644 --- a/era-compiler-solidity/Cargo.toml +++ b/era-compiler-solidity/Cargo.toml @@ -30,7 +30,10 @@ hex = "0.4" num = "0.4" zkevm_opcode_defs = { git = "https://github.com/matter-labs/zksync-protocol", branch = "main" } -era-compiler-llvm-context = { git = "https://github.com/matter-labs/era-compiler-llvm-context", branch = "main" } +# TEMPORARY: pinned to the branch of matter-labs/era-compiler-llvm-context#95, +# which adds `YulData::remove_constant`. Revert to `branch = "main"` once that +# PR has merged; this crate does not compile against main until then. +era-compiler-llvm-context = { git = "https://github.com/matter-labs/era-compiler-llvm-context", branch = "fix/yul-remove-constant" } era-compiler-common = { git = "https://github.com/matter-labs/era-compiler-common", branch = "main" } era-solc = { path = "../era-solc" } diff --git a/era-compiler-solidity/src/yul/parser/statement/assignment.rs b/era-compiler-solidity/src/yul/parser/statement/assignment.rs index 96b223e7..dbd9cd7a 100644 --- a/era-compiler-solidity/src/yul/parser/statement/assignment.rs +++ b/era-compiler-solidity/src/yul/parser/statement/assignment.rs @@ -36,7 +36,26 @@ impl era_compiler_llvm_context::EraVMWriteLLVM for Assignment { identifier.inner, ) })?; + // The constant annotation attached to a variable must equal that + // variable's value on every control-flow path. `variable_declaration` + // caches a literal initializer; an assignment that does not refresh + // the entry leaves a stale constant behind, and a later read + // re-attaches it to the new value. Under `--enable-eravm-extensions` + // that annotation selects the extension intrinsic for a call + // destination, so a stale entry turns an external call into an + // unrelated instruction. + let constant = value.constant.clone(); context.build_store(pointer, value.to_llvm())?; + let function = context.current_function(); + let mut function = function.borrow_mut(); + match constant { + Some(constant) => function + .yul_mut() + .insert_constant(identifier.inner, constant), + None => function + .yul_mut() + .remove_constant(identifier.inner.as_str()), + } return Ok(()); } @@ -73,6 +92,13 @@ impl era_compiler_llvm_context::EraVMWriteLLVM for Assignment { format!("assignment_binding_{index}_value").as_str(), )?; context.build_store(binding_pointer, value)?; + // A tuple element is loaded from memory and is never a known + // constant, so any cached entry for this binding is now stale. + context + .current_function() + .borrow_mut() + .yul_mut() + .remove_constant(binding.inner.as_str()); } Ok(()) diff --git a/era-compiler-solidity/tests/cli/eravm/enable_eravm_extensions.rs b/era-compiler-solidity/tests/cli/eravm/enable_eravm_extensions.rs index 7e655857..112069b5 100644 --- a/era-compiler-solidity/tests/cli/eravm/enable_eravm_extensions.rs +++ b/era-compiler-solidity/tests/cli/eravm/enable_eravm_extensions.rs @@ -95,3 +95,27 @@ fn deprecated_system_mode() -> anyhow::Result<()> { Ok(()) } + +#[test] +fn stale_constant_on_assignment() -> anyhow::Result<()> { + crate::common::setup()?; + + let args = &[ + "--yul", + "--enable-eravm-extensions", + "--asm", + crate::common::TEST_YUL_CONTRACT_STALE_CONSTANT_PATH, + ]; + + // The call destination is reassigned from the `meta` simulation selector to + // an ordinary address, so it must be compiled as a call to 0x8002 rather + // than as the `meta` extension. + let result = crate::cli::execute_zksolc(args)?; + result + .success() + .stdout(predicate::str::contains("@__staticcall")) + .stdout(predicate::str::contains("32770")) + .stdout(predicate::str::contains("meta").not()); + + Ok(()) +} diff --git a/era-compiler-solidity/tests/common/const.rs b/era-compiler-solidity/tests/common/const.rs index 9b609d66..88bf07b0 100644 --- a/era-compiler-solidity/tests/common/const.rs +++ b/era-compiler-solidity/tests/common/const.rs @@ -71,6 +71,10 @@ pub const SOLIDITY_ASM_OUTPUT_NAME_ERAVM: &str = "Test.zasm"; /// A test input file. pub const TEST_YUL_CONTRACT_PATH: &str = "tests/data/contracts/yul/Default.yul"; +/// A test input file. +pub const TEST_YUL_CONTRACT_STALE_CONSTANT_PATH: &str = + "tests/data/contracts/yul/StaleConstant.yul"; + /// A test input file. pub const TEST_LLVM_IR_CONTRACT_ERAVM_PATH: &str = "tests/data/contracts/llvm_ir/Test.eravm.ll"; diff --git a/era-compiler-solidity/tests/data/contracts/yul/StaleConstant.yul b/era-compiler-solidity/tests/data/contracts/yul/StaleConstant.yul new file mode 100644 index 00000000..986302fc --- /dev/null +++ b/era-compiler-solidity/tests/data/contracts/yul/StaleConstant.yul @@ -0,0 +1,18 @@ +object "Test" { + code { + return(0, 0) + } + object "Test_deployed" { + code { + // `dest` first holds the `meta` simulation selector, so the constant + // propagation caches 0xFFFC against it. The reassignment must drop + // that entry: the call below goes to 0x8002 and has to be compiled + // as an ordinary static call, not as the meta extension. + let dest := 0xFFFC + dest := 0x8002 + let result := staticcall(0, dest, 0, 0, 0, 0) + mstore(0, result) + return(0, 32) + } + } +}