Skip to content

Commit ae58c58

Browse files
committed
feat: add big decimal sanitizer
1 parent 7f77be4 commit ae58c58

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

sanitizers/sanitizers.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ _sanitizer_package_prefix = "com.code_intelligence.jazzer.sanitizers."
1818

1919
_sanitizer_class_names = [
2020
# keep sorted
21+
"BigDecimal",
2122
"ClojureLangHooks",
2223
"Deserialization",
2324
"ExpressionLanguageInjection",

sanitizers/src/main/java/com/code_intelligence/jazzer/sanitizers/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ java_library(
5252
],
5353
)
5454

55+
java_library(
56+
name = "big_decimal",
57+
srcs = ["BigDecimal.java"],
58+
deps = [
59+
"//src/main/java/com/code_intelligence/jazzer/api:hooks",
60+
],
61+
)
62+
5563
java_library(
5664
name = "unsafe_sanitizer",
5765
srcs = ["UnsafeSanitizer.java"],
@@ -79,6 +87,7 @@ kt_jvm_library(
7987
"//sanitizers/src/test/java/com/code_intelligence/jazzer/sanitizers:__pkg__",
8088
],
8189
runtime_deps = [
90+
":big_decimal",
8291
":clojure_lang_hooks",
8392
":file_path_traversal",
8493
":regex_roadblocks",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.code_intelligence.jazzer.sanitizers;
18+
19+
import com.code_intelligence.jazzer.api.HookType;
20+
import com.code_intelligence.jazzer.api.Jazzer;
21+
import com.code_intelligence.jazzer.api.MethodHook;
22+
import java.lang.invoke.MethodHandle;
23+
24+
/**
25+
* Guides inputs passed to {@link java.math.BigDecimal} constructors towards forms with huge
26+
* exponents (e.g., 1e1000000) to trigger performance issues like timeouts or OOMs.
27+
*/
28+
public final class BigDecimal {
29+
30+
private static final String HUGE_EXPONENT = "1e1000000";
31+
32+
@MethodHook(
33+
type = HookType.BEFORE,
34+
targetClassName = "java.math.BigDecimal",
35+
targetMethod = "<init>")
36+
public static void bigDecimalConstructorHook(
37+
MethodHandle method, Object thisObject, Object[] args, int hookId) {
38+
if (args.length == 0 || args[0] == null) {
39+
return;
40+
}
41+
42+
String s = null;
43+
Object first = args[0];
44+
if (first instanceof String) {
45+
s = (String) first;
46+
} else if (first instanceof char[]) {
47+
s = new String((char[]) first);
48+
}
49+
50+
if (s == null || s.isEmpty()) {
51+
return;
52+
}
53+
54+
// Nudge the fuzzer towards a BigDecimal string with a huge exponent.
55+
Jazzer.guideTowardsEquality(s, HUGE_EXPONENT, hookId);
56+
}
57+
}

tests/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,19 @@ java_fuzz_target_test(
10761076
],
10771077
)
10781078

1079+
java_fuzz_target_test(
1080+
name = "BigDecimalFuzzer",
1081+
srcs = [
1082+
"src/test/java/com/example/BigDecimalFuzzer.java",
1083+
],
1084+
allowed_findings = ["timeout"],
1085+
fuzzer_args = [
1086+
"-timeout=1",
1087+
],
1088+
target_class = "com.example.BigDecimalFuzzer",
1089+
verify_crash_reproducer = False,
1090+
)
1091+
10791092
java_fuzz_target_test(
10801093
name = "JUnitInvalidJavaSeedTest",
10811094
timeout = "short",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.code_intelligence.jazzer.mutation.annotation.NotNull;
20+
import java.math.BigDecimal;
21+
22+
public class BigDecimalFuzzer {
23+
public static void fuzzerTestOneInput(@NotNull String value) {
24+
try {
25+
BigDecimal bd = new BigDecimal(value);
26+
bd.toBigInteger();
27+
28+
} catch (NumberFormatException | ArithmeticException ignored) {
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)