1 // Copyright 2022 Code Intelligence GmbH 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.code_intelligence.jazzer.runtime; 16 17 import com.github.fmeum.rules_jni.RulesJni; 18 import java.lang.invoke.MethodHandle; 19 import java.lang.invoke.MethodType; 20 import jdk.incubator.foreign.CLinker; 21 import jdk.incubator.foreign.FunctionDescriptor; 22 import jdk.incubator.foreign.MemoryAddress; 23 import jdk.incubator.foreign.MemoryLayout; 24 import jdk.incubator.foreign.MemorySegment; 25 import jdk.incubator.foreign.ResourceScope; 26 import jdk.incubator.foreign.SymbolLookup; 27 28 /** 29 * Pure-Java implementation of the fuzzer callbacks backed by Project Panama (requires JDK 16+). 30 * To include the implementation in the benchmark on a supported JDK, uncomment the relevant lines 31 * in BUILD.bazel. 32 */ 33 public class FuzzerCallbacksPanama { 34 static { 35 RulesJni.loadLibrary("fuzzer_callbacks", FuzzerCallbacks.class); 36 } 37 38 private static final MethodHandle traceCmp4 = CLinker.getInstance().downcallHandle( 39 SymbolLookup.loaderLookup().lookup("__sanitizer_cov_trace_cmp4").get(), 40 MethodType.methodType(void.class, int.class, int.class), 41 FunctionDescriptor.ofVoid(CLinker.C_INT, CLinker.C_INT)); 42 private static final MethodHandle traceSwitch = CLinker.getInstance().downcallHandle( 43 SymbolLookup.loaderLookup().lookup("__sanitizer_cov_trace_switch").get(), 44 MethodType.methodType(void.class, long.class, MemoryAddress.class), 45 FunctionDescriptor.ofVoid(CLinker.C_LONG, CLinker.C_POINTER)); 46 traceCmpInt(int arg1, int arg2, int pc)47 static void traceCmpInt(int arg1, int arg2, int pc) throws Throwable { 48 traceCmp4.invokeExact(arg1, arg2); 49 } 50 traceCmpSwitch(long val, long[] cases, int pc)51 static void traceCmpSwitch(long val, long[] cases, int pc) throws Throwable { 52 try (ResourceScope scope = ResourceScope.newConfinedScope()) { 53 MemorySegment nativeCopy = MemorySegment.allocateNative( 54 MemoryLayout.sequenceLayout(cases.length, CLinker.C_LONG), scope); 55 nativeCopy.copyFrom(MemorySegment.ofArray(cases)); 56 traceSwitch.invokeExact(val, nativeCopy.address()); 57 } 58 } 59 } 60