Skip to content

Commit

Permalink
fix: re-encode calldata in mutator and refactor (#380)
Browse files Browse the repository at this point in the history
* fix: re-encode calldata in mutator and refactor

* panic on misuse
  • Loading branch information
0xalpharush authored and s4nsec committed Jul 8, 2024
1 parent 827dbed commit 8320d6e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
22 changes: 21 additions & 1 deletion fuzzing/calls/call_message.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package calls

import (
"math/big"

"github.com/crytic/medusa/chain"
"github.com/crytic/medusa/logging"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
coreTypes "github.com/ethereum/go-ethereum/core/types"
"golang.org/x/exp/slices"
"math/big"
)

// The following directives will be picked up by the `go generate` command to generate JSON marshaling code from
Expand Down Expand Up @@ -126,6 +127,25 @@ func NewCallMessageWithAbiValueData(from common.Address, to *common.Address, non
}
}

// WithDataAbiValues resets the call message's data and ABI values, ensuring the values are in sync and
// reusing the other existing fields.
func (m *CallMessage) WithDataAbiValues(abiData *CallMessageDataAbiValues) {
if abiData == nil {
logging.GlobalLogger.Panic("Method ABI and data should always be defined")
}

// Pack the ABI value data
var data []byte
var err error
data, err = abiData.Pack()
if err != nil {
logging.GlobalLogger.Panic("Failed to pack call message ABI values", err)
}
// Set our data and ABI values
m.DataAbiValues = abiData
m.Data = data
}

// FillFromTestChainProperties populates gas limit, price, nonce, and other fields automatically based on the worker's
// underlying test chain properties if they are not yet set.
func (m *CallMessage) FillFromTestChainProperties(chain *chain.TestChain) {
Expand Down
9 changes: 2 additions & 7 deletions fuzzing/fuzzer_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
fuzzerTypes "github.com/crytic/medusa/fuzzing/contracts"
"github.com/crytic/medusa/fuzzing/coverage"
"github.com/crytic/medusa/fuzzing/valuegeneration"
"github.com/crytic/medusa/logging"
"github.com/crytic/medusa/utils"
"github.com/crytic/medusa/utils/randomutils"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -482,12 +481,8 @@ func (fw *FuzzerWorker) shrinkCallSequence(callSequence calls.CallSequence, shri
abiValuesMsgData.InputValues[j] = mutatedInput
}

// Re-encode the ABI values as calldata.
abiData, err := abiValuesMsgData.Pack()
if err != nil {
logging.GlobalLogger.Panic("Failed to pack call message ABI values", err)
}
possibleShrunkSequence[i].Call.Data = abiData
// Re-encode the message's calldata
possibleShrunkSequence[i].Call.WithDataAbiValues(abiValuesMsgData)

// Test the shrunken sequence.
validShrunkSequence, err := fw.testShrunkenCallSequence(possibleShrunkSequence, shrinkRequest)
Expand Down
3 changes: 3 additions & 0 deletions fuzzing/fuzzer_worker_sequence_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,5 +460,8 @@ func prefetchModifyCallFuncMutate(sequenceGenerator *CallSequenceGenerator, elem
}
abiValuesMsgData.InputValues[i] = mutatedInput
}
// Re-encode the message's calldata
element.Call.WithDataAbiValues(abiValuesMsgData)

return nil
}

0 comments on commit 8320d6e

Please sign in to comment.