Skip to content

Introduce object creation using builders for fuzzers#2583

Open
Markoutte wants to merge 11 commits intomainfrom
pelevin/builders-providers
Open

Introduce object creation using builders for fuzzers#2583
Markoutte wants to merge 11 commits intomainfrom
pelevin/builders-providers

Conversation

@Markoutte
Copy link
Copy Markdown
Collaborator

Description

We introduce new BuilderObjectValueProvider for Java that finds any method, that can create a value of given type. Together with AbstractObjectValueProvider it works only when ObjectValueProvider and others fails to build a value.

The provider searches any method from any class that can return object of this type or any type that can be inferenced from the given one. It doesn't matter if this method static or not, but if this method is not static, that we suppose that we have a builder class for it. As a builder class we try to find other methods that returns an instance of this builder. This logic added because of default way to create such builder, where any set or add method returns the instance of builder to support chain calling. This builder should work to find methods for factories and singletones as well.

To support this provider, we introduce new routine in fuzzer's API called Modify (the name maybe is not final). It is added for Seed.Recurisve and works as follows: it accepts a sequence of routines that changes or creates new object with given this instance and fuzzed values. For example, we firstly create builder's object and update it with builder method, then finally we call the method, that creates an object using this builder. This sequence is always called when creating an object and never changed its ordering.

In addition, you can use this method, apply some method to the instance and return the instance object itself, for example, to always close a Closable object.

As a result, ContestEstimator covers 53% of guava project code, before it covers only 43%.

How to test

Automated tests

All fuzzer tests should pass.

Manual tests

You can try to generate tests for this little project:

public interface Muzzi {
    int getA();
}
public class RealMuzzi implements Muzzi {

    private final int a;

    private RealMuzzi(int a) {
        this.a = a;
    }

    @Override
    public int getA() {
        return a;
    }

    public static class Builder {

        private int a;

        public Builder setA(int a) {
            this.a = a;
            return this;
        }

        public Muzzi build() {
            return new RealMuzzi(a);
        }
    }
}
public class MuzziTest {
    public int size(Muzzi muzzi) {
        if (muzzi.getA() < 0) {
            return -1;
        } else if (muzzi.getA() > 0) {
            return 1;
        }
        return 0;
    }
}

Self-check list

Check off the item if the statement is true. Hint: [x] is a marked item.

Please do not delete the list or its items.

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-fuzzing Issue is related to the fuzzing ctg-enhancement New feature, improvement or change request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants