forked from dogefuzz/dogefuzz
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from PAMunb/distance-coverage-orderer
Distance coverage orderer
- Loading branch information
Showing
8 changed files
with
214 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package fuzz | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
|
||
"github.com/dogefuzz/dogefuzz/pkg/dto" | ||
) | ||
|
||
type distanceCoverageBasedOrderer struct { | ||
contract *dto.ContractDTO | ||
} | ||
|
||
func newDistanceCoverageBasedOrderer(contract *dto.ContractDTO) *distanceCoverageBasedOrderer { | ||
return &distanceCoverageBasedOrderer{contract} | ||
} | ||
|
||
func (o *distanceCoverageBasedOrderer) OrderTransactions(transactions []*dto.TransactionDTO) { | ||
sort.SliceStable(transactions, func(i, j int) bool { | ||
return o.computeScore(transactions[i]) > o.computeScore(transactions[j]) | ||
}) | ||
} | ||
|
||
func (o *distanceCoverageBasedOrderer) computeScore(transaction *dto.TransactionDTO) float64 { | ||
return math.Max(o.computeCriticalInstructionsHits(transaction), math.Max(o.computeCoverage(transaction), o.computeDistance(transaction))) | ||
} | ||
|
||
func (o *distanceCoverageBasedOrderer) computeCoverage(transaction *dto.TransactionDTO) float64 { | ||
var totalInstructions = len(o.contract.CFG.Instructions) | ||
var executedInstructions = len(transaction.ExecutedInstructions) | ||
|
||
if totalInstructions != 0 { | ||
return float64(executedInstructions) / float64(totalInstructions) | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (o *distanceCoverageBasedOrderer) computeDistance(transaction *dto.TransactionDTO) float64 { | ||
var maxDistance map[string]uint32 | ||
var distanceSum int64 = 0 | ||
var distancePercentage float64 = 0 | ||
var minDistance uint64 = transaction.DeltaMinDistance | ||
|
||
for _, distance := range o.contract.DistanceMap { | ||
if maxDistance == nil { | ||
maxDistance = make(map[string]uint32) | ||
for pc := range distance { | ||
maxDistance[pc] = 0 | ||
} | ||
} | ||
|
||
for instr := range maxDistance { | ||
if val, ok := distance[instr]; ok { | ||
if val != math.MaxUint32 && val > maxDistance[instr] { | ||
maxDistance[instr] = val | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, distance := range maxDistance { | ||
distanceSum += int64(distance) | ||
} | ||
|
||
if minDistance >= uint64(math.MaxUint32) { | ||
minDistance -= math.MaxUint32 | ||
} | ||
|
||
if distanceSum != 0 { | ||
distancePercentage = float64(minDistance) / float64(distanceSum) | ||
} | ||
|
||
return distancePercentage | ||
} | ||
|
||
func (o *distanceCoverageBasedOrderer) computeCriticalInstructionsHits(transaction *dto.TransactionDTO) float64 { | ||
if o.contract.TargetInstructionsFreq != 0 { | ||
return float64(transaction.CriticalInstructionsHits) / float64(o.contract.TargetInstructionsFreq) | ||
} | ||
|
||
return 0 | ||
} |
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,66 @@ | ||
package fuzz | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/dogefuzz/dogefuzz/pkg/common" | ||
"github.com/dogefuzz/dogefuzz/pkg/interfaces" | ||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
) | ||
|
||
// This is temporally, it is like 'directed_greybox' but with another strategy to order seeds | ||
type otherDirectedGreyboxFuzzer struct { | ||
powerSchedule interfaces.PowerSchedule | ||
|
||
solidityService interfaces.SolidityService | ||
functionService interfaces.FunctionService | ||
contractService interfaces.ContractService | ||
} | ||
|
||
func NewOtherDirectedGreyboxFuzzer(e env) *otherDirectedGreyboxFuzzer { | ||
return &otherDirectedGreyboxFuzzer{ | ||
powerSchedule: e.PowerSchedule(), | ||
solidityService: e.SolidityService(), | ||
functionService: e.FunctionService(), | ||
contractService: e.ContractService(), | ||
} | ||
} | ||
|
||
func (f *otherDirectedGreyboxFuzzer) GenerateInput(functionId string) ([]interface{}, error) { | ||
function, err := f.functionService.Get(functionId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
contract, err := f.contractService.Get(function.ContractId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
abiDefinition, err := abi.JSON(strings.NewReader(contract.AbiDefinition)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
method := abiDefinition.Methods[function.Name] | ||
|
||
seedsList, err := f.powerSchedule.RequestSeeds(functionId, common.DISTANCE_COVERAGE_BASED_STRATEGY) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
chosenSeeds := common.RandomChoice(seedsList) | ||
|
||
inputs := make([]interface{}, len(method.Inputs)) | ||
for inputsIdx, inputDefinition := range method.Inputs { | ||
handler, err := f.solidityService.GetTypeHandlerWithContext(inputDefinition.Type) | ||
if err != nil { | ||
return nil, err | ||
} | ||
handler.SetValue(chosenSeeds[inputsIdx]) | ||
mutationFunction := common.RandomChoice(handler.GetMutators()) | ||
mutationFunction() | ||
inputs[inputsIdx] = handler.GetValue() | ||
} | ||
|
||
return inputs, nil | ||
} |
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