Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.AnnotatedType;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

Expand Down Expand Up @@ -81,6 +83,7 @@ public static final class PrimitiveArrayMutator<T> extends SerializingMutator<T>
private final SerializingMutator<byte[]> innerMutator;
private final Function<byte[], T> toPrimitive;
private final Function<T, byte[]> toBytes;
private final BiFunction<byte[], PseudoRandom, T> toPrimitiveAfterMutate;

@SuppressWarnings("unchecked")
public PrimitiveArrayMutator(AnnotatedType type) {
Expand All @@ -92,6 +95,8 @@ public PrimitiveArrayMutator(AnnotatedType type) {
innerMutator =
(SerializingMutator<byte[]>) LibFuzzerMutatorFactory.tryCreate(innerByteArray).get();
toPrimitive = (Function<byte[], T>) makeBytesToPrimitiveArrayConverter(elementType);
toPrimitiveAfterMutate =
(BiFunction<byte[], PseudoRandom, T>) makeBytesToPrimitiveArrayAfterMutate(elementType);
toBytes = (Function<T, byte[]>) makePrimitiveArrayToBytesConverter(elementType);
}

Expand Down Expand Up @@ -128,14 +133,13 @@ public T init(PseudoRandom prng) {

@Override
public T mutate(T value, PseudoRandom prng) {
return (T) toPrimitive.apply(innerMutator.mutate(toBytes.apply(value), prng));
return toPrimitiveAfterMutate.apply(innerMutator.mutate(toBytes.apply(value), prng), prng);
}

@Override
public T crossOver(T value, T otherValue, PseudoRandom prng) {
return (T)
toPrimitive.apply(
innerMutator.crossOver(toBytes.apply(value), toBytes.apply(otherValue), prng));
return toPrimitive.apply(
innerMutator.crossOver(toBytes.apply(value), toBytes.apply(otherValue), prng));
}

private void extractRange(AnnotatedType type) {
Expand Down Expand Up @@ -250,6 +254,29 @@ private static AnnotatedType convertWithLength(AnnotatedType type, AnnotatedType
}
}

// The strings we pass to native callbacks to trace data flow are CESU-8 encoded.
// As a result, libFuzzer's TORC contains CESU-8 encoded strings.
// Therefore, in 50% of times we decode the byte array as a CESU-8 string.
public char[] postMutateChars(byte[] bytes, PseudoRandom prng) {
if (prng.choice()) {
return (char[]) toPrimitive.apply(bytes);
} else {
char[] chars = new String(bytes, Charset.forName("CESU-8")).toCharArray();
Copy link
Contributor

@oetr oetr Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to respect tha length provided by @WithLength annotation.

Oh, it does, never mind!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: Doesn't the current code here (specifically convertWithLength) assume that 2 byte = 1 char?
Wouldn't this assumption be violated here because:

  • 1 CESU-8 byte can be converted to 1 char (for ASCII chars) → array becomes too large
  • 3 CESU-8 byte can be converted to 1 char (for chars >= U+0800) → array becomes too short

Maybe I am overlooking something though; sorry for the trouble in that case.

(Also side note: Would it make sense to store the Charset.forName("CESU-8") in a static final field?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also couldn't see how the length gets respected here, and did some fuzzing with various @WithLength(min=..., max=...) values, and couldn't get the array length out of bounds. So it seems to be respected, for some reason!

Would it make sense to store the Charset.forName("CESU-8") in a static final field?

It would make sense, I think.

for (int i = 0; i < chars.length; i++) {
chars[i] = (char) forceInRange(chars[i], minRange, maxRange);
}
return chars;
}
}

public BiFunction<byte[], PseudoRandom, ?> makeBytesToPrimitiveArrayAfterMutate(
AnnotatedType type) {
if (type.getType().getTypeName().equals("char")) {
return this::postMutateChars;
}
return (bytes, ignored) -> makeBytesToPrimitiveArrayConverter(type).apply(bytes);
}

public static Function<?, byte[]> makePrimitiveArrayToBytesConverter(AnnotatedType type) {
switch (type.getType().getTypeName()) {
case "int":
Expand Down
10 changes: 10 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,16 @@ java_fuzz_target_test(
],
)

java_fuzz_target_test(
name = "CharArrayFuzzer",
srcs = [
"src/test/java/com/example/CharArrayFuzzer.java",
],
allowed_findings = ["java.lang.RuntimeException"],
target_class = "com.example.CharArrayFuzzer",
verify_crash_reproducer = False,
)

filegroup(
name = "fuzz_test_lister_classes",
srcs = ["src/test/data/fuzz_test_lister_test"],
Expand Down
29 changes: 29 additions & 0 deletions tests/src/test/java/com/example/CharArrayFuzzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025 Code Intelligence GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example;

public class CharArrayFuzzer {
public static void fuzzerTestOneInput(char[] data) {
if (data == null) {
return;
}
String expression = new String(data);
if (expression.equals("中 Bös3r \uD801\uDC00 C0d3 中")) {
throw new RuntimeException("Found evil code");
}
}
}