-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
pkg/fuzzer: use a MAB to decide on exec fuzz vs exec gen #4632
Open
a-nogikh
wants to merge
1
commit into
google:master
Choose a base branch
from
a-nogikh:features/exec-gen-mab
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 syzkaller project authors. All rights reserved. | ||
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. | ||
|
||
package learning | ||
|
||
import ( | ||
"math/rand" | ||
"sync" | ||
) | ||
|
||
type Action[T comparable] struct { | ||
Arm T | ||
index int | ||
} | ||
|
||
func (a Action[T]) Empty() bool { | ||
return a == Action[T]{} | ||
} | ||
|
||
type countedValue struct { | ||
value float64 | ||
count int64 | ||
} | ||
|
||
func (cv *countedValue) update(value, minStep float64) { | ||
// Using larger steps at the beginning allows us to | ||
// converge faster to the actual value. | ||
// The minStep limit ensures that we can still track | ||
// non-stationary problems. | ||
cv.count++ | ||
step := 1.0 / float64(cv.count) | ||
if step < minStep { | ||
step = minStep | ||
} | ||
cv.value += (value - cv.value) * step | ||
} | ||
|
||
// PlainMAB is a very simple epsylon-greedy MAB implementation. | ||
type PlainMAB[T comparable] struct { | ||
MinLearningRate float64 | ||
ExplorationRate float64 | ||
|
||
mu sync.RWMutex | ||
arms []T | ||
weights []countedValue | ||
} | ||
|
||
func (p *PlainMAB[T]) AddArms(arms ...T) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
for _, arm := range arms { | ||
p.arms = append(p.arms, arm) | ||
p.weights = append(p.weights, countedValue{0, 0}) | ||
} | ||
} | ||
|
||
func (p *PlainMAB[T]) Action(r *rand.Rand) Action[T] { | ||
p.mu.RLock() | ||
defer p.mu.RUnlock() | ||
var pos int | ||
if r.Float64() < p.ExplorationRate { | ||
pos = r.Intn(len(p.arms)) | ||
} else { | ||
for i := 1; i < len(p.arms); i++ { | ||
if p.weights[i].value > p.weights[pos].value { | ||
pos = i | ||
} | ||
} | ||
} | ||
return Action[T]{Arm: p.arms[pos], index: pos} | ||
} | ||
|
||
func (p *PlainMAB[T]) SaveReward(action Action[T], reward float64) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.weights[action.index].update(reward, p.MinLearningRate) | ||
} |
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 @@ | ||
// Copyright 2024 syzkaller project authors. All rights reserved. | ||
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. | ||
|
||
package learning | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/google/syzkaller/pkg/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMABSmallDiff(t *testing.T) { | ||
r := rand.New(testutil.RandSource(t)) | ||
bandit := &PlainMAB[int]{ | ||
MinLearningRate: 0.0001, | ||
ExplorationRate: 0.1, | ||
} | ||
arms := []float64{0.65, 0.7} | ||
for i := range arms { | ||
bandit.AddArms(i) | ||
} | ||
const steps = 40000 | ||
counts := runMAB(r, bandit, arms, steps) | ||
t.Logf("counts: %v", counts) | ||
assert.Greater(t, counts[1], steps/4*3) | ||
} | ||
|
||
func TestNonStationaryMAB(t *testing.T) { | ||
r := rand.New(testutil.RandSource(t)) | ||
bandit := &PlainMAB[int]{ | ||
MinLearningRate: 0.02, | ||
ExplorationRate: 0.04, | ||
} | ||
|
||
arms := []float64{0.2, 0.7, 0.5, 0.1} | ||
for i := range arms { | ||
bandit.AddArms(i) | ||
} | ||
|
||
const steps = 25000 | ||
counts := runMAB(r, bandit, arms, steps) | ||
t.Logf("initially: %v", counts) | ||
|
||
// Ensure that we've found the best arm. | ||
assert.Greater(t, counts[1], steps/2) | ||
|
||
// Now change the best arm's avg reward. | ||
arms[3] = 0.9 | ||
counts = runMAB(r, bandit, arms, steps) | ||
t.Logf("after reward change: %v", counts) | ||
assert.Greater(t, counts[3], steps/2) | ||
} | ||
|
||
func runMAB(r *rand.Rand, bandit *PlainMAB[int], arms []float64, steps int) []int { | ||
counts := make([]int, len(arms)) | ||
for i := 0; i < steps; i++ { | ||
action := bandit.Action(r) | ||
// TODO: use normal distribution? | ||
reward := r.Float64() * arms[action.Arm] | ||
counts[action.Arm]++ | ||
bandit.SaveReward(action, reward) | ||
} | ||
return counts | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would store this as time.Duration. Floats are pretty specific thing, if something wants floats (learning), it should convert the standard time to floats.
Also if we add per-syscall execution time, storing it as seconds will be somewhat strange since most syscalls execute within microseconds.