1 // Copyright 2021 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.code_intelligence.jazzer.api.HookType; 18 import com.code_intelligence.jazzer.api.MethodHook; 19 import java.lang.invoke.MethodHandle; 20 21 @SuppressWarnings("unused") 22 final public class TraceDivHooks { 23 @MethodHook(type = HookType.BEFORE, targetClassName = "java.lang.Integer", 24 targetMethod = "divideUnsigned", targetMethodDescriptor = "(II)I") 25 @MethodHook(type = HookType.BEFORE, targetClassName = "java.lang.Integer", 26 targetMethod = "remainderUnsigned", targetMethodDescriptor = "(II)I") 27 public static void intUnsignedDivide(MethodHandle method, Object thisObject, Object[] arguments, int hookId)28 intUnsignedDivide(MethodHandle method, Object thisObject, Object[] arguments, int hookId) { 29 // Since the arguments are to be treated as unsigned integers we need a long to fit the 30 // divisor. 31 TraceDataFlowNativeCallbacks.traceDivLong(Integer.toUnsignedLong((int) arguments[1]), hookId); 32 } 33 34 @MethodHook(type = HookType.BEFORE, targetClassName = "java.lang.Long", 35 targetMethod = "divideUnsigned", targetMethodDescriptor = "(JJ)J") 36 @MethodHook(type = HookType.BEFORE, targetClassName = "java.lang.Long", 37 targetMethod = "remainderUnsigned", targetMethodDescriptor = "(JJ)J") 38 public static void longUnsignedDivide(MethodHandle method, Object thisObject, Object[] arguments, int hookId)39 longUnsignedDivide(MethodHandle method, Object thisObject, Object[] arguments, int hookId) { 40 long divisor = (long) arguments[1]; 41 // Run the callback only if the divisor, which is regarded as an unsigned long, fits in a 42 // signed long, i.e., does not have the sign bit set. 43 if (divisor > 0) { 44 TraceDataFlowNativeCallbacks.traceDivLong(divisor, hookId); 45 } 46 } 47 } 48