Skip to content

Commit

Permalink
feat(codegen): grow jump targets for label offsets on pc greater than…
Browse files Browse the repository at this point in the history
… 0xff
  • Loading branch information
clearloop committed Nov 16, 2024
1 parent 6ac3241 commit bb5b435
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
13 changes: 12 additions & 1 deletion codegen/src/jump/relocate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ impl JumpTable {

let offset = relocate::offset(pc)?;
let mut target = self.target(&jump)?;

if jump.is_offset() {
target += pc;

// for the case the marked offset has growed to 2 bytes.
if buffer.len() + pc as usize > 0xff {
target += 1
}
}

// // for the case target from < 0xff to > 0xff
// if buffer.len() + target as usize > 0xff && target > 0xff {
// target -= 1;
// }

relocate::pc(buffer, pc, target, offset)?;
self.shift_label_pc(pc, offset)?;
}
Expand Down Expand Up @@ -53,7 +64,7 @@ pub fn offset(original_pc: u16) -> Result<u16> {
// | PC | PC BYTES | TARGET PC |
// |------|----------|-----------|
// | 0xfe | 1 | 0xff |
// | 0xff | 2 | 0x101 |
// | 0xff | 2 | 0x0101 |
offset += if pc > 0xfe { 2 } else { 1 }
}

Expand Down
30 changes: 15 additions & 15 deletions examples/approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ fn test_approval() -> anyhow::Result<()> {
// This could be caused by the `caller()` on stack is not consumed correctly
//
// // spend allowance
// let half_value = 21;
// let info = evm
// .calldata(&contract.encode(&[
// b"spend_allowance(address,uint256)".to_vec(),
// spender.to_bytes32().to_vec(),
// half_value.to_bytes32().to_vec(),
// ])?)
// .call(address)?;
// println!("{info:?}");
// assert_eq!(info.ret, true.to_bytes32());
// let allowance = evm.storage(
// address,
// Allowance::storage_key(Address(evm.caller), Address(spender)),
// )?;
// assert_eq!(half_value.to_bytes32(), allowance);
let half_value = 21;
let info = evm
.calldata(&contract.encode(&[
b"spend_allowance(address,uint256)".to_vec(),
spender.to_bytes32().to_vec(),
half_value.to_bytes32().to_vec(),
])?)
.call(address)?;
println!("{info:?}");
assert_eq!(info.ret, true.to_bytes32());
let allowance = evm.storage(
address,
Allowance::storage_key(Address(evm.caller), Address(spender)),
)?;
assert_eq!(half_value.to_bytes32(), allowance);
Ok(())
}
1 change: 1 addition & 0 deletions zink/src/storage/dkmapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub trait DoubleKeyMapping {
fn storage_key(key1: Self::Key1, key2: Self::Key2) -> [u8; 32];

/// Get value from storage key.
#[inline(always)]
fn get(key1: Self::Key1, key2: Self::Key2) -> Self::Value {
load_double_key(key1, key2, Self::STORAGE_SLOT);
Self::Value::sload()
Expand Down
2 changes: 1 addition & 1 deletion zint/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Contract {
let compiler = Compiler::new(config);
self.artifact = compiler.compile(&self.wasm)?;

tracing::debug!("abi: {:#}", self.json_abi()?);
// tracing::debug!("abi: {:#}", self.json_abi()?);
tracing::debug!("bytecode: {}", hex::encode(&self.artifact.runtime_bytecode));
Ok(self)
}
Expand Down

0 comments on commit bb5b435

Please sign in to comment.