Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Support fuzzing unexported fields #63

Merged
merged 3 commits into from May 24, 2021

Conversation

yanzhoupan
Copy link
Contributor

@yanzhoupan yanzhoupan commented May 22, 2021

The current gofuzz only support fuzzing the exported fields (i.e. the fields that start with an uppercase letter), other fields are simply filtered out according to this line.

However, a lot of struct also contains un-exported fields (i.e. fields start with lowercase letters) and sometimes people want to do fuzzing on those structs. In this pr I fixed this problem by adding allowUnexportedFields to the fuzzer, which allows user to decide whether they want to include unexported fields while fuzzing. It is set to false by default. But it can be turned on with AllowUnexportedFields function.

Tests are also added.

@google-cla
Copy link

google-cla bot commented May 22, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label May 22, 2021
@yanzhoupan
Copy link
Contributor Author

@googlebot I signed it!

fuzz.go Outdated
@@ -263,7 +273,11 @@ func (fc *fuzzerContext) doFuzz(v reflect.Value, flags uint64) {
defer func() { fc.curDepth-- }()

if !v.CanSet() {
return
if fc.fuzzer.allowUnexportedFields{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure the field is addressable -- I think it always should be but verifying seems good.

Suggested change
if fc.fuzzer.allowUnexportedFields{
if fc.fuzzer.allowUnexportedFields && v.CanAddr() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Daniel,
Thanks for the suggestion! I added that

fuzz.go Outdated
return
if fc.fuzzer.allowUnexportedFields{
v = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
}else{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run gofmt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just did gofmt, please checkout the latest files

fuzz.go Outdated
@@ -263,7 +273,11 @@ func (fc *fuzzerContext) doFuzz(v reflect.Value, flags uint64) {
defer func() { fc.curDepth-- }()

if !v.CanSet() {
return
if fc.fuzzer.allowUnexportedFields && v.CanAddr() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I was too slow making this comment -- can you flip the logic around on this?

if !fc.fuzzer.allowUnexportedFields || !v.CanAddr() {
  return
}
v = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()

This reduces the indentation. Minor style nit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! I just did that.
And all tests passed

@lavalamp
Copy link
Contributor

LGTM, I'll double check the tests in a bit because the CI integration seems to be off or not working.

@yanzhoupan
Copy link
Contributor Author

LGTM, I'll double check the tests in a bit because the CI integration seems to be off or not working.

Cool! Looking forward to my first contribution to google lol

@lavalamp lavalamp merged commit 9eed411 into google:master May 24, 2021
@lavalamp
Copy link
Contributor

Thanks for the change!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants