-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support discovery of SELFDESTRUCT (#88)
* Support discovery of SELFDESTRUCT * fix integration_test:test_one check * 为selfdestruct单独建立测试合约 * fix oracle result type * fix test type check --------- Co-authored-by: xForce <cc@sexsec.com>
- Loading branch information
1 parent
e5826df
commit fc7cb50
Showing
12 changed files
with
226 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ pub enum MiddlewareType { | |
OnChain, | ||
Concolic, | ||
Flashloan, | ||
Selfdestruct, | ||
InstructionCoverage | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod abi_decompiler; | |
pub mod endpoints; | ||
pub mod flashloan; | ||
pub mod onchain; | ||
pub mod selfdestruct; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
use crate::evm::input::{EVMInput, EVMInputT, EVMInputTy}; | ||
use crate::evm::middlewares::middleware::CallMiddlewareReturn::ReturnSuccess; | ||
use crate::evm::middlewares::middleware::{Middleware, MiddlewareOp, MiddlewareType, add_corpus}; | ||
use crate::evm::host::FuzzHost; | ||
use crate::generic_vm::vm_state::VMStateT; | ||
use libafl::inputs::Input; | ||
use libafl::prelude::{HasCorpus, State, HasMetadata}; | ||
use crate::state::{HasCaller, HasItyState}; | ||
use crate::evm::types::{convert_u256_to_h160, EVMAddress, EVMU256}; | ||
use std::fmt::Debug; | ||
use crate::input::VMInputT; | ||
use revm_interpreter::Interpreter; | ||
use revm_primitives::Bytecode; | ||
use crate::evm::abi::get_abi_type_boxed; | ||
use crate::evm::mutator::AccessPattern; | ||
use crate::state_input::StagedVMState; | ||
|
||
use std::rc::Rc; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
use std::cell::RefCell; | ||
|
||
pub struct Selfdestruct<VS, I, S> | ||
where | ||
S: State + HasCaller<EVMAddress> + Debug + Clone + 'static, | ||
I: VMInputT<VS, EVMAddress, EVMAddress> + EVMInputT, | ||
VS: VMStateT, | ||
{ | ||
_phantom: std::marker::PhantomData<(VS, I, S)>, | ||
} | ||
|
||
impl<VS, I, S> Selfdestruct<VS, I, S> | ||
where | ||
S: State + HasCaller<EVMAddress> + HasCorpus<I> + Debug + Clone + 'static, | ||
I: VMInputT<VS, EVMAddress, EVMAddress> + EVMInputT, | ||
VS: VMStateT, | ||
{ | ||
pub fn new() -> Self { | ||
Self { | ||
_phantom: std::marker::PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<VS, I, S> Debug for Selfdestruct<VS, I, S> | ||
where | ||
S: State + HasCaller<EVMAddress> + Debug + Clone + 'static, | ||
I: VMInputT<VS, EVMAddress, EVMAddress> + EVMInputT, | ||
VS: VMStateT, | ||
{ | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("Selfdestruct") | ||
.finish() | ||
} | ||
} | ||
|
||
|
||
impl<VS, I, S> Middleware<VS, I, S> for Selfdestruct<VS, I, S> | ||
where | ||
S: State | ||
+ Debug | ||
+ HasCaller<EVMAddress> | ||
+ HasCorpus<I> | ||
+ HasItyState<EVMAddress, EVMAddress, VS> | ||
+ HasMetadata | ||
+ Clone | ||
+ 'static, | ||
I: Input + VMInputT<VS, EVMAddress, EVMAddress> + EVMInputT + 'static, | ||
VS: VMStateT, | ||
{ | ||
unsafe fn on_step(&mut self, interp: &mut Interpreter, host: &mut FuzzHost<VS, I, S>, state: &mut S) | ||
where | ||
S: HasCaller<EVMAddress>, | ||
{ | ||
|
||
|
||
let offset_of_arg_offset = match *interp.instruction_pointer { | ||
// detect whether it mutates token balance | ||
0xff => { | ||
host.selfdestruct_hit = true; | ||
|
||
} | ||
_ => { | ||
return; | ||
} | ||
}; | ||
} | ||
|
||
unsafe fn on_insert(&mut self, bytecode: &mut Bytecode, address: EVMAddress, host: &mut FuzzHost<VS, I, S>, state: &mut S) { | ||
|
||
} | ||
|
||
fn get_type(&self) -> MiddlewareType { | ||
return MiddlewareType::Selfdestruct; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::evm::input::EVMInput; | ||
use crate::evm::oracle::dummy_precondition; | ||
use crate::evm::oracles::erc20::ORACLE_OUTPUT; | ||
use crate::evm::producers::pair::PairProducer; | ||
use crate::evm::types::{EVMAddress, EVMFuzzState, EVMOracleCtx, EVMU256}; | ||
use crate::evm::vm::EVMState; | ||
use crate::oracle::{Oracle, OracleCtx, Producer}; | ||
use crate::state::HasExecutionResult; | ||
use bytes::Bytes; | ||
use revm_primitives::Bytecode; | ||
use std::borrow::Borrow; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use std::ops::Deref; | ||
use std::rc::Rc; | ||
use crate::evm::oracles::SELFDESTRUCT_BUG_IDX; | ||
|
||
pub struct SelfdestructOracle; | ||
|
||
impl SelfdestructOracle { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
} | ||
|
||
impl Oracle<EVMState, EVMAddress, Bytecode, Bytes, EVMAddress, EVMU256, Vec<u8>, EVMInput, EVMFuzzState> | ||
for SelfdestructOracle | ||
{ | ||
fn transition(&self, _ctx: &mut EVMOracleCtx<'_>, _stage: u64) -> u64 { | ||
0 | ||
} | ||
|
||
fn oracle( | ||
&self, | ||
ctx: &mut OracleCtx< | ||
EVMState, | ||
EVMAddress, | ||
Bytecode, | ||
Bytes, | ||
EVMAddress, | ||
EVMU256, | ||
Vec<u8>, | ||
EVMInput, | ||
EVMFuzzState, | ||
>, | ||
stage: u64, | ||
) -> Vec<u64> { | ||
let is_hit = ctx.post_state.selfdestruct_hit; | ||
if is_hit { | ||
unsafe { | ||
ORACLE_OUTPUT = format!( | ||
"[selfdestruct] selfdestruct() hit at contract {:?}", | ||
ctx.input.contract | ||
) | ||
} | ||
vec![SELFDESTRUCT_BUG_IDX] | ||
} | ||
else { | ||
vec![] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.15; | ||
|
||
import "../../solidity_utils/lib.sol"; | ||
|
||
contract main { | ||
address private owner; | ||
|
||
function destruct() external { | ||
selfdestruct(payable(msg.sender)); | ||
} | ||
|
||
constructor() { | ||
owner = msg.sender; | ||
} | ||
|
||
modifier onlyOwner() { | ||
require(msg.sender == owner, "not owner"); | ||
_; | ||
} | ||
|
||
function admin_destruct() onlyOwner external { | ||
selfdestruct(payable(msg.sender)); | ||
} | ||
|
||
function getOwner() public view returns (address) { | ||
return owner; | ||
} | ||
} |