-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc] Updated fuzz tests for trig functions #148891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fuzz tests were set up incorrectly so updated trig functions to match the correct format.
@llvm/pr-subscribers-libc Author: None (sribee8) ChangesFuzz tests were set up incorrectly so updated trig functions to match the correct format. Full diff: https://github.com/llvm/llvm-project/pull/148891.diff 8 Files Affected:
diff --git a/libc/fuzzing/math/CMakeLists.txt b/libc/fuzzing/math/CMakeLists.txt
index e3c29651917fc..64cbf0f301982 100644
--- a/libc/fuzzing/math/CMakeLists.txt
+++ b/libc/fuzzing/math/CMakeLists.txt
@@ -98,15 +98,6 @@ add_libc_fuzzer(
libc.src.math.cos
)
-add_libc_fuzzer(
- atan_fuzz
- NEED_MPFR
- SRCS
- atan_fuzz.cpp
- DEPENDS
- libc.src.math.atan
-)
-
add_libc_fuzzer(
tan_fuzz
NEED_MPFR
diff --git a/libc/fuzzing/math/acos_fuzz.cpp b/libc/fuzzing/math/acos_fuzz.cpp
index d2b5456026839..f01e9ef9ed1a8 100644
--- a/libc/fuzzing/math/acos_fuzz.cpp
+++ b/libc/fuzzing/math/acos_fuzz.cpp
@@ -12,26 +12,39 @@
#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));
+ // remove NaN and inf and values outside accepted range
+ if (isnan(x) || isinf(x) || x > 1 || x < -1)
+ return 0;
- double result = LIBC_NAMESPACE::acos(x);
+ // signed zeros already tested in unit tests
+ if (signbit(x) && x == 0.0)
+ return 0;
- 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 94ae5c7bfdeee..9d8852f22f1f5 100644
--- a/libc/fuzzing/math/asin_fuzz.cpp
+++ b/libc/fuzzing/math/asin_fuzz.cpp
@@ -12,26 +12,39 @@
#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));
+ // remove NaN and inf and values outside accepted range
+ if (isnan(x) || isinf(x) || x > 1 || x < -1)
+ return 0;
- double result = LIBC_NAMESPACE::asin(x);
+ // signed zeros already tested in unit tests
+ if (signbit(x) && x == 0.0)
+ return 0;
- if (result != to_compare)
- __builtin_trap();
+ 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 3b485786e3a63..0000000000000
--- 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 5b5ba0f7de717..4fc4a83303adc 100644
--- a/libc/fuzzing/math/cos_fuzz.cpp
+++ b/libc/fuzzing/math/cos_fuzz.cpp
@@ -12,28 +12,42 @@
#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));
- double result = LIBC_NAMESPACE::cos(x);
+ // remove NaN and inf as preconditions
+ if (isnan(x))
+ return 0;
+ if (isinf(x))
+ return 0;
- if (result != to_compare)
- __builtin_trap();
+ // signed zeros already tested in unit tests
+ if (signbit(x) && x == 0.0)
+ return 0;
+
+ 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/sin_fuzz.cpp b/libc/fuzzing/math/sin_fuzz.cpp
index a5f0fa95c1581..cb2fc0772410a 100644
--- a/libc/fuzzing/math/sin_fuzz.cpp
+++ b/libc/fuzzing/math/sin_fuzz.cpp
@@ -12,28 +12,42 @@
#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));
- double result = LIBC_NAMESPACE::sin(x);
+ // remove NaN and inf as preconditions
+ if (isnan(x))
+ return 0;
+ if (isinf(x))
+ return 0;
- if (result != to_compare)
- __builtin_trap();
+ // signed zeros already tested in unit tests
+ if (signbit(x) && x == 0.0)
+ return 0;
+
+ 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 8cc6f7291a3df..35e6ddc632523 100644
--- a/libc/fuzzing/math/sincos_fuzz.cpp
+++ b/libc/fuzzing/math/sincos_fuzz.cpp
@@ -12,15 +12,12 @@
#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,42 @@ 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));
- mpfr_set_d(input, x, MPFR_RNDN);
+ // remove NaN and inf as preconditions
+ if (isnan(x) || isinf(x))
+ return 0;
- 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)
+ return 0;
- 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/tan_fuzz.cpp b/libc/fuzzing/math/tan_fuzz.cpp
index 2a462fa34fce4..1f3456555f003 100644
--- a/libc/fuzzing/math/tan_fuzz.cpp
+++ b/libc/fuzzing/math/tan_fuzz.cpp
@@ -12,28 +12,42 @@
#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));
- double result = LIBC_NAMESPACE::tan(x);
+ // remove NaN and inf as preconditions
+ if (isnan(x))
+ return 0;
+ if (isinf(x))
+ return 0;
- if (result != to_compare)
- __builtin_trap();
+ // signed zeros already tested in unit tests
+ if (signbit(x) && x == 0.0)
+ return 0;
+
+ 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;
|
lntue
reviewed
Jul 15, 2025
lntue
approved these changes
Jul 15, 2025
lntue
reviewed
Jul 16, 2025
lntue
reviewed
Jul 16, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fuzz tests were set up incorrectly so updated trig functions to match the correct format.