Skip to content

Commit 5642046

Browse files
fix: weight corpus by timestamp to favor 'hardest-to-discover' inputs (#383)
* fix: weight corpus by timestamp to favor 'hardest-to-discover' inputs * use first match * fix error handling --------- Co-authored-by: Anish Naik <[email protected]>
1 parent 11d0ac5 commit 5642046

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

fuzzing/corpus/corpus.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ package corpus
33
import (
44
"bytes"
55
"fmt"
6-
"math/big"
7-
"os"
8-
"path/filepath"
9-
"sync"
10-
"time"
11-
12-
"github.com/crytic/medusa/utils"
13-
146
"github.com/crytic/medusa/chain"
157
"github.com/crytic/medusa/fuzzing/calls"
168
"github.com/crytic/medusa/fuzzing/coverage"
179
"github.com/crytic/medusa/logging"
1810
"github.com/crytic/medusa/logging/colors"
11+
"github.com/crytic/medusa/utils"
1912
"github.com/crytic/medusa/utils/randomutils"
2013
"github.com/ethereum/go-ethereum/common"
2114
"github.com/google/uuid"
15+
"math/big"
16+
"os"
17+
"path/filepath"
18+
"regexp"
19+
"strconv"
20+
"sync"
21+
"time"
2222

2323
"github.com/crytic/medusa/fuzzing/contracts"
2424
)
@@ -282,7 +282,22 @@ func (c *Corpus) initializeSequences(sequenceFiles *corpusDirectory[calls.CallSe
282282
// If the sequence was replayed successfully, we add it. If it was not, we exclude it with a warning.
283283
if sequenceInvalidError == nil {
284284
if useInMutations && c.mutationTargetSequenceChooser != nil {
285-
c.mutationTargetSequenceChooser.AddChoices(randomutils.NewWeightedRandomChoice[calls.CallSequence](sequence, big.NewInt(1)))
285+
// If the filename is a timestamp as expected, use it as a weight for the mutation chooser.
286+
re := regexp.MustCompile("[0-9]+")
287+
var weight *big.Int
288+
if filename := re.FindAllString(sequenceFileData.fileName, 1); len(filename) > 0 {
289+
// The timestamp will be the only element in the filename array
290+
// If we can parse the timestamp with no errors, set the weight
291+
if timestamp, err := strconv.ParseUint(filename[0], 10, 64); err == nil {
292+
weight = new(big.Int).SetUint64(timestamp)
293+
}
294+
}
295+
296+
// Fallback to 1 if we couldn't parse the timestamp.
297+
if weight == nil {
298+
weight = big.NewInt(1)
299+
}
300+
c.mutationTargetSequenceChooser.AddChoices(randomutils.NewWeightedRandomChoice[calls.CallSequence](sequence, weight))
286301
}
287302
c.unexecutedCallSequences = append(c.unexecutedCallSequences, sequence)
288303
} else {

0 commit comments

Comments
 (0)