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.io.UnsupportedEncodingException; 19 20 /** 21 * Optimized implementations of the libFuzzer callbacks that do not rely on the deprecated 22 * CriticalJNINatives feature. Methods with `Java` in their name implement some parts in Java. 23 */ 24 public final class FuzzerCallbacksOptimizedNonCritical { 25 static { 26 RulesJni.loadLibrary("fuzzer_callbacks", FuzzerCallbacksOptimizedNonCritical.class); 27 } 28 traceSwitch(long val, long[] cases, int pc)29 static native void traceSwitch(long val, long[] cases, int pc); 30 traceMemcmp(byte[] b1, byte[] b2, int result, int pc)31 static native void traceMemcmp(byte[] b1, byte[] b2, int result, int pc); 32 traceStrstr(String s1, String s2, int pc)33 static native void traceStrstr(String s1, String s2, int pc); 34 traceStrstrJava(String haystack, String needle, int pc)35 static void traceStrstrJava(String haystack, String needle, int pc) 36 throws UnsupportedEncodingException { 37 // Note that we are not encoding as modified UTF-8 here: The FuzzedDataProvider transparently 38 // converts CESU8 into modified UTF-8 by coding null bytes on two bytes. Since the fuzzer is 39 // more likely to insert literal null bytes, having both the fuzzer input and the reported 40 // string comparisons be CESU8 should perform even better than the current implementation using 41 // modified UTF-8. 42 traceStrstrInternal(needle.substring(0, Math.min(needle.length(), 64)).getBytes("CESU8"), pc); 43 } 44 traceStrstrInternal(byte[] needle, int pc)45 private static native void traceStrstrInternal(byte[] needle, int pc); 46 } 47