summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java
blob: 9fe2e40576ac5ec75238943e7804db92fbbd1c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.seaofnodes.simple.fuzzer;

import com.seaofnodes.simple.IterPeeps;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.node.StopNode;
import com.seaofnodes.simple.type.TypeInteger;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.util.BitSet;

/**
 * Some utilities for the fuzzer.
 */
class FuzzerUtils {

    /**
     * Get the val of a private field
     */
    @SuppressWarnings("unchecked")
    public static <T> T getFieldValue(Object obj, String name) throws NoSuchFieldException, IllegalAccessException {
        return (T) (obj instanceof Class<?> clz ? getField(clz, name).get(null) : getField(obj.getClass(), name).get(obj));
    }

    /**
     * Get access to a private field
     */
    public static Field getField(Class<?> clz, String name) throws NoSuchFieldException {
        var field = clz.getDeclaredField(name);
        try {
            field.setAccessible(true);
        } catch (Throwable e) {
            // Intentionally left empty
        }
        return field;
    }

    /**
     * Try to rethrow an exception or create a runtime exception and throw it.
     */
    public static RuntimeException rethrow(Throwable e) {
        if (e instanceof RuntimeException re) return re;
        if (e instanceof Error er) throw er;
        return new RuntimeException(e);
    }

    /**
     * Some problems might trigger debug output. Suppress this output with a null print stream during fuzzing.
     */
    public static final PrintStream NULL_PRINT_STREAM = new PrintStream(OutputStream.nullOutputStream());

    /**
     * Check that the messages are similar.
     */
    private static boolean similarMessages(String a, String b) {
        if (a == null || b == null) return true;
        a = a.replaceAll("'\\w+'", "'normalized'").replaceAll("variable \\w+", "variable normalized");
        b = b.replaceAll("'\\w+'", "'normalized'").replaceAll("variable \\w+", "variable normalized");
        return a.equals(b);
    }

    /**
     * Check if both stack traces have the same first entry. Empty stack traces can happen with NPE optimization
     * and allow the other to be any stack trace.
     */
    private static boolean sameStackTraceOrigin(StackTraceElement[] a, StackTraceElement[] b) {
        if (a == null || a.length == 0 || b == null || b.length == 0) return true; // One or both stack traces are missing.
        return a[0].equals(b[0]);
    }

    /**
     * Checks if two exceptions are caused by the same problem.
     */
    public static boolean isExceptionFromSameCause(Throwable a, Throwable b) {
        if (a == null || b == null) return a == b; // Handle the null case
        if (a.getClass() != b.getClass()) return false; // Different classes
        if (!similarMessages(a.getMessage(), b.getMessage())) return false; // Different messages. Null is allowed as wildcard
        var s1 = a.getStackTrace();
        var s2 = b.getStackTrace();
        if (!sameStackTraceOrigin(s1, s2)) return false; // Different stack trace origins
        if (!isExceptionFromSameCause(a.getCause(), b.getCause())) return false; // Different causes
        var o1 = a.getSuppressed();
        var o2 = b.getSuppressed();
        if (o1 == null || o2 == null || o1.length != o2.length) return o1 == o2; // Different suppression
        for (int i=0; i<o1.length; i++) {
            if (!isExceptionFromSameCause(o1[i], o2[i])) return false; // Different suppression
        }
        return true;
    }

    /**
     * Parse and optimize using the given worklist seed
     */
    public static CodeGen parse(String script, long workListSeed, boolean resetTypes ) {
        var err = System.err;
        var out = System.out;
        try {
            System.setErr(NULL_PRINT_STREAM);
            System.setOut(NULL_PRINT_STREAM);
            var code = new CodeGen(script, workListSeed, resetTypes);
            return code.driver(CodeGen.Phase.LocalSched);
        } finally {
            System.setErr(err);
            System.setOut(out);
        }
    }

}