Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Supported `polkadot-sdk` rev: `unstable2507`
- Instruct the LLVM backend and linker to `--relax` (may lead to smaller contract code size).
- Standard JSON mode: Don't forward EVM bytecode related output selections to solc.
- The supported `polkadot-sdk` release is `unstable2507`.
- The `INVALID` opcode and OOB memory accesses now consume all remaining gas.

### Fixed:
- The missing `STOP` instruction at the end of `code` blocks.
Expand Down
10 changes: 5 additions & 5 deletions crates/integration/codesize.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"Baseline": 911,
"Computation": 2293,
"DivisionArithmetics": 14353,
"ERC20": 16936,
"Computation": 2337,
"DivisionArithmetics": 14488,
"ERC20": 17041,
"Events": 1672,
"FibonacciIterative": 1454,
"Flipper": 2083,
"SHA1": 7727
"Flipper": 2106,
"SHA1": 7814
}
45 changes: 45 additions & 0 deletions crates/integration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,48 @@ fn sbrk_bounds_checks() {
"not seeing a trap means the contract did not catch the OOB"
);
}

#[test]
fn invalid_opcode_works() {
let code = &build_yul(&[(
"invalid.yul",
r#"object "Test" {
code {
invalid()
}
object "Test_deployed" {
code {
invalid()
}
}
}"#,
)])
.unwrap()["invalid.yul:Test"];

let results = Specs {
actions: vec![
Instantiate {
origin: TestAddress::Alice,
value: 0,
gas_limit: Some(GAS_LIMIT),
storage_deposit_limit: None,
code: Code::Bytes(code.to_vec()),
data: Default::default(),
salt: OptionalHex::default(),
},
VerifyCall(VerifyCallExpectation {
success: false,
..Default::default()
}),
],
differential: false,
..Default::default()
}
.run();

let CallResult::Instantiate { result, .. } = results.last().unwrap() else {
unreachable!()
};

assert_eq!(result.weight_consumed, GAS_LIMIT);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ impl RuntimeFunction for WordToPointer {
)?;

let block_continue = context.append_basic_block("offset_pointer_ok");
let block_trap = context.append_basic_block("offset_pointer_overflow");
context.build_conditional_branch(is_overflow, block_trap, block_continue)?;
let block_invalid = context.append_basic_block("offset_pointer_overflow");
context.build_conditional_branch(is_overflow, block_invalid, block_continue)?;

context.set_basic_block(block_trap);
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
context.set_basic_block(block_invalid);
context.build_runtime_call(revive_runtime_api::polkavm_imports::INVALID, &[]);
context.build_unreachable();

context.set_basic_block(block_continue);
Expand Down
14 changes: 8 additions & 6 deletions crates/llvm-context/src/polkavm/evm/return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ pub fn stop(context: &mut Context) -> anyhow::Result<()> {
/// Translates the `invalid` instruction.
/// Burns all gas using an out-of-bounds memory store, causing a panic.
pub fn invalid(context: &mut Context) -> anyhow::Result<()> {
crate::polkavm::evm::memory::store(
context,
context.word_type().const_all_ones(),
context.word_const(0),
)?;
context.build_call(context.intrinsics().trap, &[], "invalid_trap");
let invalid_block = context.append_basic_block("explicit_invalid");
context.build_unconditional_branch(invalid_block);
context.set_basic_block(invalid_block);
context.build_runtime_call(revive_runtime_api::polkavm_imports::INVALID, &[]);
context.build_unreachable();

context.set_basic_block(context.append_basic_block("dead_code"));

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions crates/runtime-api/src/polkavm_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ POLKAVM_IMPORT(uint64_t, code_size, uint32_t)

POLKAVM_IMPORT(void, code_hash, uint32_t, uint32_t)

POLKAVM_IMPORT(void, consume_all_gas)

POLKAVM_IMPORT(uint32_t, delegate_call, uint64_t, uint64_t, uint64_t, uint32_t, uint64_t, uint64_t)

POLKAVM_IMPORT(void, deposit_event, uint32_t, uint32_t, uint32_t, uint32_t)
Expand Down
5 changes: 4 additions & 1 deletion crates/runtime-api/src/polkavm_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub static HASH_KECCAK_256: &str = "hash_keccak_256";

pub static INSTANTIATE: &str = "instantiate";

pub static INVALID: &str = "consume_all_gas";

pub static NOW: &str = "now";

pub static ORIGIN: &str = "origin";
Expand All @@ -70,7 +72,7 @@ pub static VALUE_TRANSFERRED: &str = "value_transferred";

/// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 33] = [
pub static IMPORTS: [&str; 34] = [
ADDRESS,
BALANCE,
BALANCE_OF,
Expand All @@ -94,6 +96,7 @@ pub static IMPORTS: [&str; 33] = [
GET_STORAGE,
HASH_KECCAK_256,
INSTANTIATE,
INVALID,
NOW,
ORIGIN,
REF_TIME_LEFT,
Expand Down
Loading