RFR: 8337221: CompileFramework: test library to conveniently compile java and jasm sources for fuzzing [v23]

Christian Hagedorn chagedorn at openjdk.org
Mon Oct 14 11:06:30 UTC 2024


On Mon, 14 Oct 2024 08:36:12 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

>> **Motivation**
>> 
>> I want to write small dedicated fuzzers:
>> - Generate `java` and `jasm` source code: just some `String`.
>> - Quickly compile it (with this framework).
>> - Execute the compiled code.
>> 
>> The primary users of the CompileFramework are Compiler-Engineers. Imagine you are working on some optimization. You already have a list of **hand-written tests**, but you are worried that this does not give you good coverage. You also do not trust that an existing Fuzzer will catch your bugs (at least not fast enough). Hence, you want to **script-generate** a large list of tests. But where do you put this script? It would be nice if it was also checked in on git, so that others can modify and maintain the test easily. But with such a script, you can only generate a **static test**. In some cases that is good enough, but sometimes the list of all possible tests your script would generate is very very large. Too large. So you need to randomly sample some of the tests. At this point, it would be nice to generate different tests with every run: a "mini-fuzzer" or a **fuzzer dedicated to a compiler feature**.
>> 
>> **The CompileFramework**
>> 
>> Java sources are compiled with `javac`, jasm sources with `asmtools` that are delivered with `jtreg`.
>> An important factor: Integration with the IR-Framwrork (`TestFramework`): we want to be able to generate IR-rules for our tests.
>> 
>> I implemented a first, simple version of the framework. I added some tests and examples.
>> 
>> **Example**
>> 
>> 
>> CompileFramework comp = new CompileFramework();
>> comp.add(SourceCode.newJavaSourceCode("XYZ", "<source-code-here>"));
>> comp.compile();
>> comp.invoke("XYZ", "test", new Object[] {5}); // XYZ.test(5);
>> 
>> 
>> https://github.com/openjdk/jdk/blob/e869cce8092ee995cf2f3ad1ab2bca69c5e256ab/test/hotspot/jtreg/testlibrary_tests/compile_framework/examples/SimpleJavaExample.java#L42-L74
>> 
>> **Below some use cases: tests that would have been better with the CompileFramework**
>> 
>> **Use case: test/hotspot/jtreg/compiler/loopopts/superword/TestAlignVectorFuzzer.java**
>> 
>> I needed to test loops with various `init / stride / limit / scale / unrolling-factor / ...`.
>> 
>> For this I used `MethodHandle constant = MethodHandles.constant(int.class, value);`,
>> 
>> to be able to chose different values before the C2 compilation, and then the C2 compilation would see them as constants and optimize assuming those constants. This works, but is difficult to extract reproducers once something i...
>
> Emanuel Peter has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 76 additional commits since the last revision:
> 
>  - Merge branch 'master' into fuzzer-test
>  - test refactoring
>  - Add example where I use the framework with VM flags
>  - Apply suggestions from code review
>    
>    Co-authored-by: Christian Hagedorn <christian.hagedorn at oracle.com>
>  - move some code for Christian
>  - more for Christian
>  - Apply suggestions from code review
>    
>    Co-authored-by: Christian Hagedorn <christian.hagedorn at oracle.com>
>  - another small suggestion from Christian
>  - more fixup for Christian
>  - Apply suggestions from code review
>    
>    Co-authored-by: Christian Hagedorn <christian.hagedorn at oracle.com>
>  - ... and 66 more: https://git.openjdk.org/jdk/compare/bcd1673b...5178e7c2

Nice that you added an additional test. Still looks good.

test/hotspot/jtreg/compiler/lib/compile_framework/README.md line 51:

> 49: Should one require the modified classpath that includes the compiled classes, this is available with `compileFramework.getEscapedClassPathOfCompiledClasses()`. This can be necessary if the test launches any other VMs that also access the compiled classes. This is for example necessary when using the IR Framework.
> 50: 
> 51: ### Running the compiled code in a new VM

Following the capital letter style from the other titles:
Suggestion:

### Running the Compiled Code in a New VM

test/hotspot/jtreg/testlibrary_tests/compile_framework/examples/RunWithFlagsExample.java line 66:

> 64:         CompileFramework comp = new CompileFramework();
> 65: 
> 66:         // Add a java source file.

Suggestion:

        // Add a Java source file.

test/hotspot/jtreg/testlibrary_tests/compile_framework/examples/RunWithFlagsExample.java line 78:

> 76:             comp.getEscapedClassPathOfCompiledClasses(),
> 77:             // Pass additional flags here.
> 78:             // "-Xbatch" is a harmless VM flag, so this example runs everywhere without issue.

Suggestion:

            // "-Xbatch" is a harmless VM flag, so this example runs everywhere without issues.

test/hotspot/jtreg/testlibrary_tests/compile_framework/examples/RunWithFlagsExample.java line 87:

> 85: 
> 86:         // Execute the command, and capture the output.
> 87:         // The JTREG VM options are automatically passed to the test VM.

Suggestion:

        // The JTREG Java and VM options are automatically passed to the test VM.

-------------

Marked as reviewed by chagedorn (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/20184#pullrequestreview-2366239197
PR Review Comment: https://git.openjdk.org/jdk/pull/20184#discussion_r1799268346
PR Review Comment: https://git.openjdk.org/jdk/pull/20184#discussion_r1799272237
PR Review Comment: https://git.openjdk.org/jdk/pull/20184#discussion_r1799272889
PR Review Comment: https://git.openjdk.org/jdk/pull/20184#discussion_r1799274775


More information about the hotspot-compiler-dev mailing list