@@ -756,18 +756,46 @@ public static void arraysCompareRange(
756756 // key closest to the current lookup key in the mapGet hook.
757757 private static final int MAX_NUM_KEYS_TO_ENUMERATE = 100 ;
758758
759- @ SuppressWarnings ({"rawtypes" , "unchecked" })
760- @ MethodHook (type = HookType .AFTER , targetClassName = "java.util.Map" , targetMethod = "get" )
759+ @ MethodHook (
760+ type = HookType .AFTER ,
761+ targetClassName = "java.util.Map" ,
762+ targetMethod = "containsKey" ,
763+ targetMethodDescriptor = "(Ljava/lang/Object;)Z" )
764+ public static void containsKey (
765+ MethodHandle method , Object thisObject , Object [] arguments , int hookId , Boolean isContained ) {
766+ if (!isContained ) {
767+ mapHookInternal ((Map ) thisObject , arguments [0 ], hookId );
768+ }
769+ }
770+
771+ @ MethodHook (
772+ type = HookType .AFTER ,
773+ targetClassName = "java.util.Map" ,
774+ targetMethod = "get" ,
775+ targetMethodDescriptor = "(Ljava/lang/Object;)Ljava/lang/Object;" )
761776 public static void mapGet (
762- MethodHandle method , Object thisObject , Object [] arguments , int hookId , Object returnValue ) {
763- if (returnValue != null ) return ;
764- if (arguments .length != 1 ) {
765- return ;
777+ MethodHandle method , Object thisObject , Object [] arguments , int hookId , Object value ) {
778+ if (value == null ) {
779+ mapHookInternal ((Map ) thisObject , arguments [0 ], hookId );
766780 }
767- if (thisObject == null ) return ;
768- final Map map = (Map ) thisObject ;
769- if (map .size () == 0 ) return ;
770- final Object currentKey = arguments [0 ];
781+ }
782+
783+ @ MethodHook (
784+ type = HookType .AFTER ,
785+ targetClassName = "java.util.Map" ,
786+ targetMethod = "getOrDefault" ,
787+ targetMethodDescriptor = "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;" )
788+ public static void mapGetOrDefault (
789+ MethodHandle method , Object thisObject , Object [] arguments , int hookId , Object value ) {
790+ Object defaultValue = arguments [1 ];
791+ if (value == defaultValue ) {
792+ mapHookInternal ((Map ) thisObject , arguments [0 ], hookId );
793+ }
794+ }
795+
796+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
797+ private static <K , V > void mapHookInternal (Map <K , V > map , K currentKey , int hookId ) {
798+ if (map == null || map .isEmpty ()) return ;
771799 if (currentKey == null ) return ;
772800 // Find two valid map keys that bracket currentKey.
773801 // This is a generalization of libFuzzer's __sanitizer_cov_trace_switch:
@@ -776,7 +804,7 @@ public static void mapGet(
776804 Object upperBoundKey = null ;
777805 try {
778806 if (map instanceof TreeMap ) {
779- final TreeMap treeMap = (TreeMap ) map ;
807+ final TreeMap < K , V > treeMap = (TreeMap < K , V > ) map ;
780808 try {
781809 lowerBoundKey = treeMap .floorKey (currentKey );
782810 upperBoundKey = treeMap .ceilingKey (currentKey );
0 commit comments