From 7cfc752383a3a966429251baa4ca4dff11c332ec Mon Sep 17 00:00:00 2001 From: Sriya Pratipati Date: Thu, 10 Jul 2025 20:31:33 +0000 Subject: [PATCH 1/3] added exp and exp10 fuzz tests --- libc/fuzzing/math/CMakeLists.txt | 18 +++++++++++++++ libc/fuzzing/math/exp10_fuzz.cpp | 38 ++++++++++++++++++++++++++++++++ libc/fuzzing/math/exp_fuzz.cpp | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 libc/fuzzing/math/exp10_fuzz.cpp create mode 100644 libc/fuzzing/math/exp_fuzz.cpp diff --git a/libc/fuzzing/math/CMakeLists.txt b/libc/fuzzing/math/CMakeLists.txt index e3c29651917fc..327e50b6afe21 100644 --- a/libc/fuzzing/math/CMakeLists.txt +++ b/libc/fuzzing/math/CMakeLists.txt @@ -62,6 +62,24 @@ add_libc_fuzzer( libc.src.math.nextafterl ) +add_libc_fuzzer( + exp_fuzz + NEED_MPFR + SRCS + exp_fuzz.cpp + DEPENDS + libc.src.math.exp +) + +add_libc_fuzzer( + exp10_fuzz + NEED_MPFR + SRCS + exp10_fuzz.cpp + DEPENDS + libc.src.math.exp10 +) + add_libc_fuzzer( asin_fuzz NEED_MPFR diff --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp new file mode 100644 index 0000000000000..2baef03a264a4 --- /dev/null +++ b/libc/fuzzing/math/exp10_fuzz.cpp @@ -0,0 +1,38 @@ +//===-- exp10_fuzz.cpp ----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Fuzzing test for llvm-libc exp10 implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/exp10.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include + +extern "C" int LLVMFuzzerTestOneInput(double x) { + // remove NaN and inf + if (isnan(x) || isinf(x)) + return 0; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + return 0; + mpfr_t input; + mpfr_init2(input, 53); + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_exp10(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::exp10(x); + + if (result != to_compare) + __builtin_trap(); + + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/exp_fuzz.cpp b/libc/fuzzing/math/exp_fuzz.cpp new file mode 100644 index 0000000000000..97bc12dfa64c9 --- /dev/null +++ b/libc/fuzzing/math/exp_fuzz.cpp @@ -0,0 +1,38 @@ +//===-- exp_fuzz.cpp ------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Fuzzing test for llvm-libc exp implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/exp.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include + +extern "C" int LLVMFuzzerTestOneInput(double x) { + // remove NaN and inf + if (isnan(x) || isinf(x)) + return 0; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + return 0; + mpfr_t input; + mpfr_init2(input, 53); + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_exp(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::exp(x); + + if (result != to_compare) + __builtin_trap(); + + mpfr_clear(input); + return 0; +} From 5a50991c3af05e5bbcfa58718cc395cda9c3d86f Mon Sep 17 00:00:00 2001 From: Sriya Pratipati Date: Thu, 10 Jul 2025 21:24:07 +0000 Subject: [PATCH 2/3] added exp2 fuzz test --- libc/fuzzing/math/exp2_fuzz.cpp | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 libc/fuzzing/math/exp2_fuzz.cpp diff --git a/libc/fuzzing/math/exp2_fuzz.cpp b/libc/fuzzing/math/exp2_fuzz.cpp new file mode 100644 index 0000000000000..8a2959047a6ca --- /dev/null +++ b/libc/fuzzing/math/exp2_fuzz.cpp @@ -0,0 +1,38 @@ +//===-- exp2_fuzz.cpp -----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Fuzzing test for llvm-libc exp2 implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/exp2.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include + +extern "C" int LLVMFuzzerTestOneInput(double x) { + // remove NaN and inf + if (isnan(x) || isinf(x)) + return 0; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + return 0; + mpfr_t input; + mpfr_init2(input, 53); + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_exp2(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::exp2(x); + + if (result != to_compare) + __builtin_trap(); + + mpfr_clear(input); + return 0; +} From f8f1a532e6820db2265bfea25f8f264da25ccb13 Mon Sep 17 00:00:00 2001 From: Sriya Pratipati Date: Thu, 10 Jul 2025 23:40:19 +0000 Subject: [PATCH 3/3] [libc] Fuzz tests for exp functions Created fuzz tests for exp functions --- libc/fuzzing/math/CMakeLists.txt | 18 +++++++++++++++ libc/fuzzing/math/expm1_fuzz.cpp | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 libc/fuzzing/math/expm1_fuzz.cpp diff --git a/libc/fuzzing/math/CMakeLists.txt b/libc/fuzzing/math/CMakeLists.txt index 327e50b6afe21..bf2be5c0b3cea 100644 --- a/libc/fuzzing/math/CMakeLists.txt +++ b/libc/fuzzing/math/CMakeLists.txt @@ -80,6 +80,24 @@ add_libc_fuzzer( libc.src.math.exp10 ) +add_libc_fuzzer( + exp2_fuzz + NEED_MPFR + SRCS + exp2_fuzz.cpp + DEPENDS + libc.src.math.exp2 +) + +add_libc_fuzzer( + expm1_fuzz + NEED_MPFR + SRCS + expm1_fuzz.cpp + DEPENDS + libc.src.math.expm1 +) + add_libc_fuzzer( asin_fuzz NEED_MPFR diff --git a/libc/fuzzing/math/expm1_fuzz.cpp b/libc/fuzzing/math/expm1_fuzz.cpp new file mode 100644 index 0000000000000..db507bb02b1d7 --- /dev/null +++ b/libc/fuzzing/math/expm1_fuzz.cpp @@ -0,0 +1,38 @@ +//===-- expm1_fuzz.cpp ----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Fuzzing test for llvm-libc expm1 implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/expm1.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include + +extern "C" int LLVMFuzzerTestOneInput(double x) { + // remove NaN and inf + if (isnan(x) || isinf(x)) + return 0; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + return 0; + mpfr_t input; + mpfr_init2(input, 53); + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_expm1(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::expm1(x); + + if (result != to_compare) + __builtin_trap(); + + mpfr_clear(input); + return 0; +}