|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.client.rulesengine; |
| 7 | + |
| 8 | +import com.code_intelligence.jazzer.junit.FuzzTest; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +/** |
| 13 | + * Fuzz tests for {@link BytecodeEvaluator} — evaluating bytecode with random instruction streams. |
| 14 | + * |
| 15 | + * <p>Constructs minimal valid Bytecode objects with fuzz-derived instruction bytes, |
| 16 | + * then runs the evaluator to check for crash safety. |
| 17 | + */ |
| 18 | +class BytecodeEvaluatorFuzzTest { |
| 19 | + |
| 20 | + private static final int MAX_FUZZ_INPUT = 512; |
| 21 | + |
| 22 | + @FuzzTest |
| 23 | + void fuzzEvaluateCondition(byte[] data) { |
| 24 | + if (data.length < 1 || data.length > MAX_FUZZ_INPUT) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Build a Bytecode with the fuzz data as the instruction stream and a single condition at offset 0. |
| 29 | + Bytecode bytecode = new Bytecode( |
| 30 | + data, |
| 31 | + new int[] {0}, // one condition at offset 0 |
| 32 | + new int[0], |
| 33 | + new RegisterDefinition[] { |
| 34 | + new RegisterDefinition("p0", false, "default", null, false), |
| 35 | + new RegisterDefinition("t0", false, null, null, true) |
| 36 | + }, |
| 37 | + new Object[] {"a", "b", 1, true, false, null, List.of(), Map.of()}, |
| 38 | + new RulesFunction[0], |
| 39 | + new int[] {0, 1, -1}, // BDD: condition 0 -> TRUE/FALSE |
| 40 | + 2 // root = node 0 |
| 41 | + ); |
| 42 | + |
| 43 | + BytecodeEvaluator evaluator = new BytecodeEvaluator(bytecode, |
| 44 | + new RulesExtension[0], |
| 45 | + RegisterFiller.of(bytecode, Map.of())); |
| 46 | + evaluator.reset(null, Map.of()); |
| 47 | + |
| 48 | + try { |
| 49 | + evaluator.test(0); |
| 50 | + } catch (RulesEvaluationError | IllegalArgumentException ignored) {} |
| 51 | + } |
| 52 | + |
| 53 | + @FuzzTest |
| 54 | + void fuzzEvaluateResult(byte[] data) { |
| 55 | + if (data.length < 1 || data.length > MAX_FUZZ_INPUT) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + Bytecode bytecode = new Bytecode( |
| 60 | + data, |
| 61 | + new int[0], |
| 62 | + new int[] {0}, // one result at offset 0 |
| 63 | + new RegisterDefinition[] { |
| 64 | + new RegisterDefinition("p0", false, "https://example.com", null, false), |
| 65 | + new RegisterDefinition("t0", false, null, null, true) |
| 66 | + }, |
| 67 | + new Object[] {"https://example.com", "key", "value", 42, true, false}, |
| 68 | + new RulesFunction[0], |
| 69 | + new int[] {-1, 100_000_000, -1}, // Terminal -> result 0 |
| 70 | + 100_000_000); |
| 71 | + |
| 72 | + BytecodeEvaluator evaluator = new BytecodeEvaluator(bytecode, |
| 73 | + new RulesExtension[0], |
| 74 | + RegisterFiller.of(bytecode, Map.of())); |
| 75 | + evaluator.reset(null, Map.of()); |
| 76 | + |
| 77 | + try { |
| 78 | + evaluator.resolveResult(0); |
| 79 | + } catch (RulesEvaluationError | IllegalArgumentException ignored) {} |
| 80 | + } |
| 81 | +} |
0 commit comments