-
Notifications
You must be signed in to change notification settings - Fork 163
feat: dictionary provider for Strings and Integrals #975
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
Draft
oetr
wants to merge
11
commits into
main
Choose a base branch
from
CIF-1785-dictionary-provider
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
61e3cef
feat: allow creation of parameterized and generic array types
oetr 43c11fb
chore: add array of lists to stress test
oetr f6e34eb
feat: add meta-annotation for multi-level annotation propagation
oetr 97509be
feat internal: weighted random sampler util
oetr 9bf54cc
chore: add global MutationRuntime
oetr 5aa778c
feat: @ValuePool - propagate values to type mutators
oetr 4c06817
feat: use @ValuePool when mutating Strings
oetr 22e397b
feat: use @ValuePool when mutating Integrals
oetr cdfcf52
feat: use @ValuePool when mutating primitive arrays
oetr 5a118b8
chore: update tests to use the sampler
oetr a8f5226
chore: update stress test expectations
oetr 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
94 changes: 94 additions & 0 deletions
94
src/main/java/com/code_intelligence/jazzer/mutation/annotation/ValuePool.java
This file contains hidden or 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,94 @@ | ||
| /* | ||
| * Copyright 2024 Code Intelligence GmbH | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.code_intelligence.jazzer.mutation.annotation; | ||
|
|
||
| import static com.code_intelligence.jazzer.mutation.utils.PropertyConstraint.RECURSIVE; | ||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import com.code_intelligence.jazzer.mutation.utils.IgnoreRecursiveConflicts; | ||
| import com.code_intelligence.jazzer.mutation.utils.PropertyConstraint; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Provides values to user-selected mutator types to start fuzzing from. Currently supported | ||
| * mutators are: | ||
| * | ||
| * <ul> | ||
| * <li>String mutator | ||
| * <li>Integral mutators (byte, short, int, long) | ||
| * </ul> | ||
| * | ||
| * <p>This annotation can be applied to fuzz test methods and any parameter type or subtype. By | ||
| * default, this annotation is propagated to all nested subtypes unless specified otherwise via the | ||
| * {@link #constraint()} attribute. | ||
| * | ||
| * <p>Example usage: | ||
| * | ||
| * <pre>{@code | ||
| * public class MyFuzzTargets { | ||
| * | ||
| * static Stream<?> valluesVisibleByAllArgumentMutators() { | ||
| * return Stream.of("example1", "example2", "example3", 1232187321, -182371); | ||
| * } | ||
| * | ||
| * static Stream<?> valuesVisibleOnlyByAnotherInput() { | ||
| * return Stream.of("code-intelligence.com", "secret.url.1082h3u21ibsdsazuvbsa.com"); | ||
| * } | ||
| * | ||
| * @ValuePool("valuesVisibleByAllArgumentMutators") | ||
| * @FuzzTest | ||
| * public void fuzzerTestOneInput(String input, @ValuePool("valuesVisibleOnlyByAnotherInput") String anotherInput) { | ||
| * // Fuzzing logic here | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * In this example, the mutator for the String parameter {@code input} of the fuzz test method | ||
| * {@code fuzzerTestOneInput} will be using the values returned by {@code provide} method during | ||
| * mutation, while the mutator for String {@code anotherInput} will use values from both methods: | ||
| * from the method-level {@code ValuePool} annotation that uses {@code provide} and the | ||
| * parameter-level {@code ValuePool} annotation that uses {@code provideSomethingElse}. | ||
| */ | ||
| @Target({ElementType.METHOD, TYPE_USE}) | ||
| @Retention(RUNTIME) | ||
| @IgnoreRecursiveConflicts | ||
| @PropertyConstraint | ||
| public @interface ValuePool { | ||
| /** | ||
| * Specifies supplier methods that generate values for fuzzing the annotated method or type. The | ||
| * specified supplier methods must be static and return a {@code Stream <?>} of values. The values | ||
| * don't need to match the type of the annotated method or parameter exactly. The mutation | ||
| * framework will extract only the values that are compatible with the target type. | ||
| */ | ||
| String[] value() default {""}; | ||
|
|
||
| /** | ||
| * This {@code ValuePool} will be used with probability {@code 1/p} by the mutator responsible for | ||
| * fitting types. Not all mutators respect this probability. | ||
| */ | ||
| int pInv() default 10; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this make sense as a float setting? |
||
|
|
||
| /** | ||
| * Defines the scope of the annotation. Possible values are defined in {@link | ||
| * com.code_intelligence.jazzer.mutation.utils.PropertyConstraint}. By default it's {@code | ||
| * RECURSIVE}. | ||
| */ | ||
| String constraint() default RECURSIVE; | ||
| } | ||
127 changes: 127 additions & 0 deletions
127
src/main/java/com/code_intelligence/jazzer/mutation/combinator/SamplingUtils.java
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * Copyright 2025 Code Intelligence GmbH | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.code_intelligence.jazzer.mutation.combinator; | ||
|
|
||
| import static com.code_intelligence.jazzer.mutation.support.Preconditions.require; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import com.code_intelligence.jazzer.mutation.api.PseudoRandom; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public final class SamplingUtils { | ||
|
|
||
| public static <T> Function<PseudoRandom, T> weightedSampler(T[] values, double[] weights) { | ||
| // Use Vose's alias method for O(1) sampling after O(n) preprocessing. | ||
| requireNonNull(values, "Values must not be null"); | ||
| requireNonNull(weights, "Weights must not be null"); | ||
| require(values.length > 0, "Values must not be empty"); | ||
| require(values.length == weights.length, "Values and weights must have the same length"); | ||
|
|
||
| double sum = Arrays.stream(weights).sum(); | ||
| require(sum > 0, "At least one weight must be positive"); | ||
|
|
||
| int n = values.length; | ||
| int[] alias = new int[n]; | ||
| double[] probability = new double[n]; | ||
| double[] scaledWeights = Arrays.stream(weights).map(w -> w * n / sum).toArray(); | ||
| int[] small = new int[n]; | ||
| int[] large = new int[n]; | ||
| int smallCount = 0; | ||
| int largeCount = 0; | ||
| for (int i = 0; i < n; i++) { | ||
| if (scaledWeights[i] < 1.0) { | ||
| small[smallCount++] = i; | ||
| } else { | ||
| large[largeCount++] = i; | ||
| } | ||
| } | ||
|
|
||
| while (smallCount > 0 && largeCount > 0) { | ||
| int less = small[--smallCount]; | ||
| int more = large[--largeCount]; | ||
|
|
||
| probability[less] = scaledWeights[less]; | ||
| alias[less] = more; | ||
| scaledWeights[more] = (scaledWeights[more] + scaledWeights[less]) - 1.0; | ||
|
|
||
| if (scaledWeights[more] < 1.0) { | ||
| small[smallCount++] = more; | ||
| } else { | ||
| large[largeCount++] = more; | ||
| } | ||
| } | ||
| while (largeCount > 0) { | ||
| probability[large[--largeCount]] = 1.0; | ||
| } | ||
|
|
||
| while (smallCount > 0) { | ||
| probability[small[--smallCount]] = 1.0; | ||
| } | ||
| return (PseudoRandom random) -> { | ||
| int column = random.indexIn(n); | ||
| return values[random.closedRange(0.0, 1.0) < probability[column] ? column : alias[column]]; | ||
| }; | ||
| } | ||
|
|
||
| public static <T> Function<PseudoRandom, T> weightedSampler( | ||
| List<WeightedValue<T>> weightedFunctions) { | ||
| requireNonNull(weightedFunctions, "Weighted functions must not be null"); | ||
| require(!weightedFunctions.isEmpty(), "Weighted functions must not be empty"); | ||
|
|
||
| double[] weights = weightedFunctions.stream().mapToDouble(m -> m.weight).toArray(); | ||
|
|
||
| T[] fns = (T[]) weightedFunctions.stream().map(m -> m.value).toArray(Object[]::new); | ||
|
|
||
| return weightedSampler(fns, weights); | ||
| } | ||
|
|
||
| @SafeVarargs | ||
| public static <T> Function<PseudoRandom, T> weightedSampler( | ||
| Optional<WeightedValue<T>>... values) { | ||
| return weightedSampler( | ||
| Arrays.stream(values) | ||
| .filter(Optional::isPresent) | ||
| .map(Optional::get) | ||
| .collect(Collectors.toList())); | ||
| } | ||
|
|
||
| /** | ||
| * A simple struct to hold a value and its weight. It is here just for stylistic reasons, to make | ||
| * the definitions of weights and values more readable. | ||
| */ | ||
| public static class WeightedValue<T> { | ||
| public final double weight; | ||
| public final T value; | ||
|
|
||
| public WeightedValue(double weight, T value) { | ||
| this.value = value; | ||
| this.weight = weight; | ||
| } | ||
|
|
||
| public static <T> WeightedValue<T> of(double weight, T fn) { | ||
| return new WeightedValue<>(weight, fn); | ||
| } | ||
|
|
||
| public static <T> Optional<WeightedValue<T>> ofOptional(double weight, T fn) { | ||
| return Optional.of(new WeightedValue<>(weight, fn)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import static com.code_intelligence.jazzer.mutation.mutator.collection.ChunkMutations.MutationAction.pickRandomMutationAction; | ||
| import static com.code_intelligence.jazzer.mutation.support.Preconditions.require; | ||
| import static com.code_intelligence.jazzer.mutation.support.PropertyConstraintSupport.propagatePropertyConstraints; | ||
| import static com.code_intelligence.jazzer.mutation.support.TypeSupport.extractRawClass; | ||
| import static java.lang.Math.min; | ||
| import static java.lang.String.format; | ||
|
|
||
|
|
@@ -35,6 +36,7 @@ | |
| import java.lang.reflect.AnnotatedArrayType; | ||
| import java.lang.reflect.AnnotatedType; | ||
| import java.lang.reflect.Array; | ||
| import java.lang.reflect.Type; | ||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import java.util.function.Predicate; | ||
|
|
@@ -53,12 +55,16 @@ public Optional<SerializingMutator<?>> tryCreate( | |
|
|
||
| AnnotatedType elementType = ((AnnotatedArrayType) type).getAnnotatedGenericComponentType(); | ||
| AnnotatedType propagatedElementType = propagatePropertyConstraints(type, elementType); | ||
| Class<?> propagatedElementClazz = (Class<?>) propagatedElementType.getType(); | ||
| return Optional.of(propagatedElementType) | ||
| .flatMap(factory::tryCreate) | ||
| .map( | ||
| elementMutator -> | ||
| new ArrayMutator<>(elementMutator, propagatedElementClazz, minLength, maxLength)); | ||
| Type rawType = propagatedElementType.getType(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change relevant to the |
||
| return extractRawClass(rawType) | ||
| .flatMap( | ||
| propagatedElementClass -> | ||
| Optional.of(propagatedElementType) | ||
| .flatMap(factory::tryCreate) | ||
| .map( | ||
| elementMutator -> | ||
| new ArrayMutator<>( | ||
| elementMutator, propagatedElementClass, minLength, maxLength))); | ||
| } | ||
|
|
||
| enum CrossOverAction { | ||
|
|
||
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.
The error message could be better if we remove the default value which forces users to specify
value.