diff options
Diffstat (limited to 'libc/fuzzing/math')
| -rw-r--r-- | libc/fuzzing/math/CMakeLists.txt | 90 | ||||
| -rw-r--r-- | libc/fuzzing/math/acos_fuzz.cpp | 42 | ||||
| -rw-r--r-- | libc/fuzzing/math/asin_fuzz.cpp | 43 | ||||
| -rw-r--r-- | libc/fuzzing/math/atan_fuzz.cpp | 38 | ||||
| -rw-r--r-- | libc/fuzzing/math/cos_fuzz.cpp | 47 | ||||
| -rw-r--r-- | libc/fuzzing/math/exp10_fuzz.cpp | 51 | ||||
| -rw-r--r-- | libc/fuzzing/math/exp2_fuzz.cpp | 51 | ||||
| -rw-r--r-- | libc/fuzzing/math/exp_fuzz.cpp | 51 | ||||
| -rw-r--r-- | libc/fuzzing/math/expm1_fuzz.cpp | 51 | ||||
| -rw-r--r-- | libc/fuzzing/math/log10_fuzz.cpp | 51 | ||||
| -rw-r--r-- | libc/fuzzing/math/log1p_fuzz.cpp | 50 | ||||
| -rw-r--r-- | libc/fuzzing/math/log2_fuzz.cpp | 51 | ||||
| -rw-r--r-- | libc/fuzzing/math/log_fuzz.cpp | 50 | ||||
| -rw-r--r-- | libc/fuzzing/math/sin_fuzz.cpp | 47 | ||||
| -rw-r--r-- | libc/fuzzing/math/sincos_fuzz.cpp | 55 | ||||
| -rw-r--r-- | libc/fuzzing/math/sqrt_fuzz.cpp | 50 | ||||
| -rw-r--r-- | libc/fuzzing/math/tan_fuzz.cpp | 47 |
17 files changed, 724 insertions, 141 deletions
diff --git a/libc/fuzzing/math/CMakeLists.txt b/libc/fuzzing/math/CMakeLists.txt index e3c29651917f..c1a93058764b 100644 --- a/libc/fuzzing/math/CMakeLists.txt +++ b/libc/fuzzing/math/CMakeLists.txt @@ -63,6 +63,42 @@ add_libc_fuzzer( ) 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( + 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 SRCS @@ -99,15 +135,6 @@ add_libc_fuzzer( ) add_libc_fuzzer( - atan_fuzz - NEED_MPFR - SRCS - atan_fuzz.cpp - DEPENDS - libc.src.math.atan -) - -add_libc_fuzzer( tan_fuzz NEED_MPFR SRCS @@ -124,3 +151,48 @@ add_libc_fuzzer( DEPENDS libc.src.math.sincos ) + +add_libc_fuzzer( + log_fuzz + NEED_MPFR + SRCS + log_fuzz.cpp + DEPENDS + libc.src.math.log +) + +add_libc_fuzzer( + log10_fuzz + NEED_MPFR + SRCS + log10_fuzz.cpp + DEPENDS + libc.src.math.log10 +) + +add_libc_fuzzer( + log1p_fuzz + NEED_MPFR + SRCS + log1p_fuzz.cpp + DEPENDS + libc.src.math.log1p +) + +add_libc_fuzzer( + log2_fuzz + NEED_MPFR + SRCS + log2_fuzz.cpp + DEPENDS + libc.src.math.log2 +) + +add_libc_fuzzer( + sqrt_fuzz + NEED_MPFR + SRCS + sqrt_fuzz.cpp + DEPENDS + libc.src.__support.FPUtil.generic.sqrt +) diff --git a/libc/fuzzing/math/acos_fuzz.cpp b/libc/fuzzing/math/acos_fuzz.cpp index d2b545602683..48fb4eacc3a7 100644 --- a/libc/fuzzing/math/acos_fuzz.cpp +++ b/libc/fuzzing/math/acos_fuzz.cpp @@ -12,26 +12,40 @@ #include "src/math/acos.h" #include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> #include <math.h> -extern "C" int LLVMFuzzerTestOneInput(double x) { - // remove NaN and inf and values outside accepted range - if (isnan(x) || isinf(x) || x > 1 || x < -1) - return 0; - // signed zeros already tested in unit tests - if (signbit(x) && x == 0.0) - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mpfr_t input; mpfr_init2(input, 53); - mpfr_set_d(input, x, MPFR_RNDN); - int output = mpfr_acos(input, input, MPFR_RNDN); - mpfr_subnormalize(input, output, MPFR_RNDN); - double to_compare = mpfr_get_d(input, MPFR_RNDN); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x > 1 || x < -1) + continue; - double result = LIBC_NAMESPACE::acos(x); + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; - if (result != to_compare) - __builtin_trap(); + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_acos(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::acos(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } mpfr_clear(input); return 0; diff --git a/libc/fuzzing/math/asin_fuzz.cpp b/libc/fuzzing/math/asin_fuzz.cpp index 94ae5c7bfdee..e27d17960682 100644 --- a/libc/fuzzing/math/asin_fuzz.cpp +++ b/libc/fuzzing/math/asin_fuzz.cpp @@ -12,26 +12,41 @@ #include "src/math/asin.h" #include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> #include <math.h> -extern "C" int LLVMFuzzerTestOneInput(double x) { - // remove NaN and inf and values outside accepted range - if (isnan(x) || isinf(x) || x > 1 || x < -1) - return 0; - // signed zeros already tested in unit tests - if (signbit(x) && x == 0.0) - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mpfr_t input; mpfr_init2(input, 53); - mpfr_set_d(input, x, MPFR_RNDN); - int output = mpfr_asin(input, input, MPFR_RNDN); - mpfr_subnormalize(input, output, MPFR_RNDN); - double to_compare = mpfr_get_d(input, MPFR_RNDN); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); - double result = LIBC_NAMESPACE::asin(x); + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x > 1 || x < -1) + continue; - if (result != to_compare) - __builtin_trap(); + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_asin(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::asin(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } mpfr_clear(input); return 0; diff --git a/libc/fuzzing/math/atan_fuzz.cpp b/libc/fuzzing/math/atan_fuzz.cpp deleted file mode 100644 index 3b485786e3a6..000000000000 --- a/libc/fuzzing/math/atan_fuzz.cpp +++ /dev/null @@ -1,38 +0,0 @@ -//===-- atan_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 atan implementation. -/// -//===----------------------------------------------------------------------===// - -#include "src/math/atan.h" -#include "utils/MPFRWrapper/mpfr_inc.h" -#include <math.h> - -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_atan(input, input, MPFR_RNDN); - mpfr_subnormalize(input, output, MPFR_RNDN); - double to_compare = mpfr_get_d(input, MPFR_RNDN); - - double result = LIBC_NAMESPACE::atan(x); - - if (result != to_compare) - __builtin_trap(); - - mpfr_clear(input); - return 0; -} diff --git a/libc/fuzzing/math/cos_fuzz.cpp b/libc/fuzzing/math/cos_fuzz.cpp index 5b5ba0f7de71..6ed1e9ed8f30 100644 --- a/libc/fuzzing/math/cos_fuzz.cpp +++ b/libc/fuzzing/math/cos_fuzz.cpp @@ -12,28 +12,43 @@ #include "src/math/cos.h" #include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> #include <math.h> -extern "C" int LLVMFuzzerTestOneInput(const double x) { - // remove NaN and inf as preconditions - if (isnan(x)) - return 0; - if (isinf(x)) - return 0; - // signed zeros already tested in unit tests - if (signbit(x) && x == 0.0) - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mpfr_t input; mpfr_init2(input, 53); - mpfr_set_d(input, x, MPFR_RNDN); - int output = mpfr_cos(input, input, MPFR_RNDN); - mpfr_subnormalize(input, output, MPFR_RNDN); - double to_compare = mpfr_get_d(input, MPFR_RNDN); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); - double result = LIBC_NAMESPACE::cos(x); + // remove NaN and inf as preconditions + if (isnan(x)) + continue; + if (isinf(x)) + continue; - if (result != to_compare) - __builtin_trap(); + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_cos(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::cos(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } mpfr_clear(input); return 0; diff --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp new file mode 100644 index 000000000000..d939948b723a --- /dev/null +++ b/libc/fuzzing/math/exp10_fuzz.cpp @@ -0,0 +1,51 @@ +//===-- 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 <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf + if (isnan(x) || isinf(x)) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + 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) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/exp2_fuzz.cpp b/libc/fuzzing/math/exp2_fuzz.cpp new file mode 100644 index 000000000000..a29d3c00da67 --- /dev/null +++ b/libc/fuzzing/math/exp2_fuzz.cpp @@ -0,0 +1,51 @@ +//===-- 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 <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf + if (isnan(x) || isinf(x)) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + 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) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __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 000000000000..66823596dc6f --- /dev/null +++ b/libc/fuzzing/math/exp_fuzz.cpp @@ -0,0 +1,51 @@ +//===-- 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 <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf + if (isnan(x) || isinf(x)) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + 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) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/expm1_fuzz.cpp b/libc/fuzzing/math/expm1_fuzz.cpp new file mode 100644 index 000000000000..0690e449c3d2 --- /dev/null +++ b/libc/fuzzing/math/expm1_fuzz.cpp @@ -0,0 +1,51 @@ +//===-- 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 <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf + if (isnan(x) || isinf(x)) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + 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) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/log10_fuzz.cpp b/libc/fuzzing/math/log10_fuzz.cpp new file mode 100644 index 000000000000..369408cc288b --- /dev/null +++ b/libc/fuzzing/math/log10_fuzz.cpp @@ -0,0 +1,51 @@ +//===-- log10_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 log10 implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/log10.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x < 0) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_log10(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::log10(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/log1p_fuzz.cpp b/libc/fuzzing/math/log1p_fuzz.cpp new file mode 100644 index 000000000000..e02c61a352c1 --- /dev/null +++ b/libc/fuzzing/math/log1p_fuzz.cpp @@ -0,0 +1,50 @@ +//===-- log1p_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 log1p implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/log1p.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x < -1) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_log1p(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::log1p(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/log2_fuzz.cpp b/libc/fuzzing/math/log2_fuzz.cpp new file mode 100644 index 000000000000..c3e53c639cba --- /dev/null +++ b/libc/fuzzing/math/log2_fuzz.cpp @@ -0,0 +1,51 @@ +//===-- log2_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 log2 implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/log2.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x < 0) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_log2(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::log2(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/log_fuzz.cpp b/libc/fuzzing/math/log_fuzz.cpp new file mode 100644 index 000000000000..9618accf3db2 --- /dev/null +++ b/libc/fuzzing/math/log_fuzz.cpp @@ -0,0 +1,50 @@ +//===-- log_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 log implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/math/log.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x < 0) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_log(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::log(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/sin_fuzz.cpp b/libc/fuzzing/math/sin_fuzz.cpp index a5f0fa95c158..f6d59c7e496b 100644 --- a/libc/fuzzing/math/sin_fuzz.cpp +++ b/libc/fuzzing/math/sin_fuzz.cpp @@ -12,28 +12,43 @@ #include "src/math/sin.h" #include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> #include <math.h> -extern "C" int LLVMFuzzerTestOneInput(const double x) { - // remove NaN and inf as preconditions - if (isnan(x)) - return 0; - if (isinf(x)) - return 0; - // signed zeros already tested in unit tests - if (signbit(x) && x == 0.0) - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mpfr_t input; mpfr_init2(input, 53); - mpfr_set_d(input, x, MPFR_RNDN); - int output = mpfr_sin(input, input, MPFR_RNDN); - mpfr_subnormalize(input, output, MPFR_RNDN); - double to_compare = mpfr_get_d(input, MPFR_RNDN); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); - double result = LIBC_NAMESPACE::sin(x); + // remove NaN and inf as preconditions + if (isnan(x)) + continue; + if (isinf(x)) + continue; - if (result != to_compare) - __builtin_trap(); + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_sin(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::sin(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } mpfr_clear(input); return 0; diff --git a/libc/fuzzing/math/sincos_fuzz.cpp b/libc/fuzzing/math/sincos_fuzz.cpp index 8cc6f7291a3d..3d3306721fc4 100644 --- a/libc/fuzzing/math/sincos_fuzz.cpp +++ b/libc/fuzzing/math/sincos_fuzz.cpp @@ -6,21 +6,18 @@ // //===----------------------------------------------------------------------===// /// -/// Fuzzing test for llvm-libc cos implementation. +/// Fuzzing test for llvm-libc sincos implementation. /// //===----------------------------------------------------------------------===// #include "src/math/sincos.h" #include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> #include <math.h> -extern "C" int LLVMFuzzerTestOneInput(double x) { - // remove NaN and inf as preconditions - if (isnan(x) || isinf(x)) - return 0; - // signed zeros already tested in unit tests - if (signbit(x) && x == 0.0) - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mpfr_t input; mpfr_t sin_x; mpfr_t cos_x; @@ -28,21 +25,43 @@ extern "C" int LLVMFuzzerTestOneInput(double x) { mpfr_init2(input, 53); mpfr_init2(sin_x, 53); mpfr_init2(cos_x, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); - mpfr_set_d(input, x, MPFR_RNDN); + // remove NaN and inf as preconditions + if (isnan(x) || isinf(x)) + continue; - int output = mpfr_sin_cos(sin_x, cos_x, input, MPFR_RNDN); - mpfr_subnormalize(sin_x, output, MPFR_RNDN); - mpfr_subnormalize(cos_x, output, MPFR_RNDN); + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; - double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN); - double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN); + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_sin_cos(sin_x, cos_x, input, MPFR_RNDN); + mpfr_subnormalize(sin_x, output, MPFR_RNDN); + mpfr_subnormalize(cos_x, output, MPFR_RNDN); - double sin_res, cos_res; - LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res); + double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN); + double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN); - if (sin_res != to_compare_sin || cos_res != to_compare_cos) - __builtin_trap(); + double sin_res, cos_res; + LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res); + + if (sin_res != to_compare_sin || cos_res != to_compare_cos) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing sin output: " << sin_res + << std::endl; + std::cout << std::hexfloat << "Expected sin: " << to_compare_sin + << std::endl; + std::cout << std::hexfloat << "Failing cos output: " << cos_res + << std::endl; + std::cout << std::hexfloat << "Expected cos: " << to_compare_cos + << std::endl; + __builtin_trap(); + } + } mpfr_clear(input); mpfr_clear(sin_x); diff --git a/libc/fuzzing/math/sqrt_fuzz.cpp b/libc/fuzzing/math/sqrt_fuzz.cpp new file mode 100644 index 000000000000..969b4f58e342 --- /dev/null +++ b/libc/fuzzing/math/sqrt_fuzz.cpp @@ -0,0 +1,50 @@ +//===-- sqrt_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 sqrt implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/__support/FPUtil/generic/sqrt.h" +#include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> +#include <math.h> + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + mpfr_t input; + mpfr_init2(input, 53); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); + // remove NaN and inf and values outside accepted range + if (isnan(x) || isinf(x) || x < 0) + continue; + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_sqrt(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::fputil::sqrt<double>(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } + mpfr_clear(input); + return 0; +} diff --git a/libc/fuzzing/math/tan_fuzz.cpp b/libc/fuzzing/math/tan_fuzz.cpp index 2a462fa34fce..63d3b12866a0 100644 --- a/libc/fuzzing/math/tan_fuzz.cpp +++ b/libc/fuzzing/math/tan_fuzz.cpp @@ -12,28 +12,43 @@ #include "src/math/tan.h" #include "utils/MPFRWrapper/mpfr_inc.h" +#include <cstdint> +#include <cstring> +#include <iostream> #include <math.h> -extern "C" int LLVMFuzzerTestOneInput(const double x) { - // remove NaN and inf as preconditions - if (isnan(x)) - return 0; - if (isinf(x)) - return 0; - // signed zeros already tested in unit tests - if (signbit(x) && x == 0.0) - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mpfr_t input; mpfr_init2(input, 53); - mpfr_set_d(input, x, MPFR_RNDN); - int output = mpfr_tan(input, input, MPFR_RNDN); - mpfr_subnormalize(input, output, MPFR_RNDN); - double to_compare = mpfr_get_d(input, MPFR_RNDN); + for (size_t i = 0; i < size / sizeof(double); ++i) { + double x; + std::memcpy(&x, data, sizeof(double)); + data += sizeof(double); - double result = LIBC_NAMESPACE::tan(x); + // remove NaN and inf as preconditions + if (isnan(x)) + continue; + if (isinf(x)) + continue; - if (result != to_compare) - __builtin_trap(); + // signed zeros already tested in unit tests + if (signbit(x) && x == 0.0) + continue; + + mpfr_set_d(input, x, MPFR_RNDN); + int output = mpfr_tan(input, input, MPFR_RNDN); + mpfr_subnormalize(input, output, MPFR_RNDN); + double to_compare = mpfr_get_d(input, MPFR_RNDN); + + double result = LIBC_NAMESPACE::tan(x); + + if (result != to_compare) { + std::cout << std::hexfloat << "Failing input: " << x << std::endl; + std::cout << std::hexfloat << "Failing output: " << result << std::endl; + std::cout << std::hexfloat << "Expected: " << to_compare << std::endl; + __builtin_trap(); + } + } mpfr_clear(input); return 0; |
