|
| 1 | +/* |
| 2 | + * Copyright 2025 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.code_intelligence.jazzer.mutation.support; |
| 18 | + |
| 19 | +import static com.code_intelligence.jazzer.mutation.support.Preconditions.require; |
| 20 | + |
| 21 | +import com.code_intelligence.jazzer.mutation.annotation.DictionaryProvider; |
| 22 | +import com.code_intelligence.jazzer.mutation.runtime.MutationRuntime; |
| 23 | +import java.lang.reflect.AnnotatedType; |
| 24 | +import java.lang.reflect.InvocationTargetException; |
| 25 | +import java.lang.reflect.Method; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +public class DictionaryProviderSupport { |
| 34 | + |
| 35 | + /** |
| 36 | + * Extract inverse probability of the very first {@code DictionaryProvider} annotation on the |
| 37 | + * given type. The {@code @DictionaryProvider} annotation directly on the type is preferred; if |
| 38 | + * there is none, the first one appended because of {@code PropertyConstraint.RECURSIVE} is used. |
| 39 | + * Any further {@code @DictionaryProvider} annotations appended later to this type because of |
| 40 | + * {@code PropertyConstraint.RECURSIVE}, are ignored. Callers should ensure that at least one |
| 41 | + * {@code @DictionaryProvider} annotation is present on the type. |
| 42 | + */ |
| 43 | + public static int extractFirstInvProbability(AnnotatedType type) { |
| 44 | + // If there are several @DictionaryProvider annotations on the type, this will take the most |
| 45 | + // immediate one, because @DictionaryProvider is not repeatable. |
| 46 | + DictionaryProvider[] dictObj = type.getAnnotationsByType(DictionaryProvider.class); |
| 47 | + if (dictObj.length == 0) { |
| 48 | + // If we are here, it's a bug in the caller. |
| 49 | + throw new IllegalStateException("Expected to find @DictionaryProvider, but found none."); |
| 50 | + } |
| 51 | + int pInv = dictObj[0].pInv(); |
| 52 | + require(pInv >= 2, "@DictionaryProvider.pInv must be at least 2"); |
| 53 | + return pInv; |
| 54 | + } |
| 55 | + |
| 56 | + /** Extract the provider streams using MutatorRuntime */ |
| 57 | + public static Optional<Stream<?>> extractRawValues(AnnotatedType type) { |
| 58 | + DictionaryProvider[] providers = |
| 59 | + Arrays.stream(type.getAnnotations()) |
| 60 | + .filter(a -> a instanceof DictionaryProvider) |
| 61 | + .map(a -> (DictionaryProvider) a) |
| 62 | + .toArray(DictionaryProvider[]::new); |
| 63 | + if (providers.length == 0) { |
| 64 | + return Optional.empty(); |
| 65 | + } |
| 66 | + HashSet<String> providerMethodNames = |
| 67 | + Arrays.stream(providers) |
| 68 | + .map(DictionaryProvider::value) |
| 69 | + .flatMap(Arrays::stream) |
| 70 | + .filter(name -> !name.isEmpty()) |
| 71 | + .collect(Collectors.toCollection(HashSet::new)); |
| 72 | + if (providerMethodNames.isEmpty()) { |
| 73 | + return Optional.empty(); |
| 74 | + } |
| 75 | + Map<String, Method> fuzzTestMethods = |
| 76 | + Arrays.stream(MutationRuntime.fuzzTestMethod.getDeclaringClass().getDeclaredMethods()) |
| 77 | + .filter(m -> m.getParameterCount() == 0) |
| 78 | + .filter(m -> m.getReturnType().equals(Stream.class)) |
| 79 | + .filter( |
| 80 | + m -> |
| 81 | + (m.getModifiers() & java.lang.reflect.Modifier.STATIC) |
| 82 | + == java.lang.reflect.Modifier.STATIC) |
| 83 | + .collect(Collectors.toMap(Method::getName, m -> m)); |
| 84 | + return Optional.ofNullable( |
| 85 | + providerMethodNames.stream() |
| 86 | + .flatMap( |
| 87 | + name -> { |
| 88 | + Method m = fuzzTestMethods.get(name); |
| 89 | + if (m == null) { |
| 90 | + throw new IllegalStateException( |
| 91 | + "Found no static supplier method 'Stream<?> " |
| 92 | + + name |
| 93 | + + "()' in class " |
| 94 | + + MutationRuntime.fuzzTestMethod.getDeclaringClass().getName()); |
| 95 | + } |
| 96 | + try { |
| 97 | + m.setAccessible(true); |
| 98 | + return (Stream<?>) m.invoke(null); |
| 99 | + } catch (IllegalAccessException e) { |
| 100 | + throw new IllegalStateException("Cannot access method " + name, e); |
| 101 | + } catch (InvocationTargetException e) { |
| 102 | + throw new RuntimeException( |
| 103 | + "Supplier method " + name + " threw an exception", e.getCause()); |
| 104 | + } |
| 105 | + }) |
| 106 | + .distinct()); |
| 107 | + } |
| 108 | +} |
0 commit comments