Skip to content

Commit 6762580

Browse files
committed
feat: decode bytes as CESU-8 when converting to char[]
1 parent 683af30 commit 6762580

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/main/java/com/code_intelligence/jazzer/mutation/mutator/lang/PrimitiveArrayMutatorFactory.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.lang.reflect.AnnotatedArrayType;
4343
import java.lang.reflect.AnnotatedType;
4444
import java.nio.ByteBuffer;
45+
import java.nio.charset.Charset;
4546
import java.util.Optional;
4647
import java.util.function.BiFunction;
4748
import java.util.function.Function;
@@ -253,16 +254,16 @@ private static AnnotatedType convertWithLength(AnnotatedType type, AnnotatedType
253254
}
254255
}
255256

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[].
257+
// The strings we pass to native callbacks to trace data flow are CESU-8 encoded.
258+
// As a result, libFuzzer's TORC contains CESU-8 encoded strings.
259+
// Therefore, in 50% of times we decode the byte array as a CESU-8 string.
259260
public char[] postMutateChars(byte[] bytes, PseudoRandom prng) {
260261
if (prng.choice()) {
261262
return (char[]) toPrimitive.apply(bytes);
262263
} else {
263-
char[] chars = new char[bytes.length];
264+
char[] chars = new String(bytes, Charset.forName("CESU-8")).toCharArray();
264265
for (int i = 0; i < chars.length; i++) {
265-
chars[i] = (char) bytes[i];
266+
chars[i] = (char) forceInRange(chars[i], minRange, maxRange);
266267
}
267268
return chars;
268269
}

tests/src/test/java/com/example/CharArrayFuzzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public static void fuzzerTestOneInput(char[] data) {
2222
return;
2323
}
2424
String expression = new String(data);
25-
if (expression.contains("jazzer")) {
26-
throw new RuntimeException("found jazzer");
25+
if (expression.equals("中 Bös3r \uD801\uDC00 C0d3 中")) {
26+
throw new RuntimeException("Found evil code");
2727
}
2828
}
2929
}

0 commit comments

Comments
 (0)