|
42 | 42 | import java.lang.reflect.AnnotatedArrayType; |
43 | 43 | import java.lang.reflect.AnnotatedType; |
44 | 44 | import java.nio.ByteBuffer; |
| 45 | +import java.nio.charset.Charset; |
45 | 46 | import java.util.Optional; |
46 | 47 | import java.util.function.BiFunction; |
47 | 48 | import java.util.function.Function; |
@@ -71,6 +72,7 @@ public Optional<SerializingMutator<?>> tryCreate( |
71 | 72 | public static final class PrimitiveArrayMutator<T> extends SerializingMutator<T> { |
72 | 73 | private static final int DEFAULT_MIN_LENGTH = 0; |
73 | 74 | private static final int DEFAULT_MAX_LENGTH = 1000; |
| 75 | + private static final Charset FUZZED_DATA_CHARSET = Charset.forName("CESU-8"); |
74 | 76 | private long minRange; |
75 | 77 | private long maxRange; |
76 | 78 | private boolean allowNaN; |
@@ -253,16 +255,16 @@ private static AnnotatedType convertWithLength(AnnotatedType type, AnnotatedType |
253 | 255 | } |
254 | 256 | } |
255 | 257 |
|
256 | | - // Randomly maps the byte array from libFuzzer directly onto char[] or converts each byte into a |
257 | | - // 2 byte char. This helps in cases where a String is constructed out of char[] and libFuzzer |
258 | | - // inserts CESU8 encoded bytes into the byte[]. |
| 258 | + // The strings we pass to native callbacks to trace data flow are CESU-8 encoded. |
| 259 | + // As a result, libFuzzer's TORC contains CESU-8 encoded strings. |
| 260 | + // Therefore, in 50% of times we decode the byte array as a CESU-8 string. |
259 | 261 | public char[] postMutateChars(byte[] bytes, PseudoRandom prng) { |
260 | 262 | if (prng.choice()) { |
261 | 263 | return (char[]) toPrimitive.apply(bytes); |
262 | 264 | } else { |
263 | | - char[] chars = new char[bytes.length]; |
| 265 | + char[] chars = new String(bytes, FUZZED_DATA_CHARSET).toCharArray(); |
264 | 266 | for (int i = 0; i < chars.length; i++) { |
265 | | - chars[i] = (char) bytes[i]; |
| 267 | + chars[i] = (char) forceInRange(chars[i], minRange, maxRange); |
266 | 268 | } |
267 | 269 | return chars; |
268 | 270 | } |
|
0 commit comments