Skip to content

[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 4 commits into from
Jul 16, 2025

Conversation

sribee8
Copy link
Contributor

@sribee8 sribee8 commented Jul 15, 2025

Fuzz tests were set up incorrectly so updated trig functions to match the correct format.

Fuzz tests were set up incorrectly so updated trig functions to match the correct format.
@sribee8 sribee8 requested a review from lntue July 15, 2025 16:58
@llvmbot llvmbot added the libc label Jul 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-libc

Author: None (sribee8)

Changes

Fuzz 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:

  • (modified) libc/fuzzing/math/CMakeLists.txt (-9)
  • (modified) libc/fuzzing/math/acos_fuzz.cpp (+27-14)
  • (modified) libc/fuzzing/math/asin_fuzz.cpp (+27-14)
  • (removed) libc/fuzzing/math/atan_fuzz.cpp (-38)
  • (modified) libc/fuzzing/math/cos_fuzz.cpp (+30-16)
  • (modified) libc/fuzzing/math/sin_fuzz.cpp (+30-16)
  • (modified) libc/fuzzing/math/sincos_fuzz.cpp (+35-17)
  • (modified) libc/fuzzing/math/tan_fuzz.cpp (+30-16)
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;

@sribee8 sribee8 merged commit 037d348 into llvm:main Jul 16, 2025
19 checks passed
@sribee8 sribee8 deleted the fuzz-formatting-fixes branch July 16, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants