Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion era-compiler-solidity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
26 changes: 26 additions & 0 deletions era-compiler-solidity/src/yul/parser/statement/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}

Expand Down Expand Up @@ -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(())
Expand Down
24 changes: 24 additions & 0 deletions era-compiler-solidity/tests/cli/eravm/enable_eravm_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
4 changes: 4 additions & 0 deletions era-compiler-solidity/tests/common/const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
18 changes: 18 additions & 0 deletions era-compiler-solidity/tests/data/contracts/yul/StaleConstant.yul
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading