Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-encode calldata in mutator and refactor #380

Merged
merged 4 commits into from
Jul 2, 2024
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
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
}