[libc-commits] [libc] 037d348 - [libc] Updated fuzz tests for trig functions (#148891)

via libc-commits libc-commits at lists.llvm.org
Wed Jul 16 10:57:50 PDT 2025


Author: sribee8
Date: 2025-07-16T17:57:47Z
New Revision: 037d34815efc6b2c0b3f9f4d19945e49aac831d1

URL: https://github.com/llvm/llvm-project/commit/037d34815efc6b2c0b3f9f4d19945e49aac831d1
DIFF: https://github.com/llvm/llvm-project/commit/037d34815efc6b2c0b3f9f4d19945e49aac831d1.diff

LOG: [libc] Updated fuzz tests for trig functions (#148891)

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

---------

Co-authored-by: Sriya Pratipati <sriyap at google.com>

Added: 
    

Modified: 
    libc/fuzzing/math/acos_fuzz.cpp
    libc/fuzzing/math/asin_fuzz.cpp
    libc/fuzzing/math/cos_fuzz.cpp
    libc/fuzzing/math/log10_fuzz.cpp
    libc/fuzzing/math/log1p_fuzz.cpp
    libc/fuzzing/math/log2_fuzz.cpp
    libc/fuzzing/math/log_fuzz.cpp
    libc/fuzzing/math/sin_fuzz.cpp
    libc/fuzzing/math/sincos_fuzz.cpp
    libc/fuzzing/math/sqrt_fuzz.cpp
    libc/fuzzing/math/tan_fuzz.cpp

Removed: 
    


################################################################################
diff  --git a/libc/fuzzing/math/acos_fuzz.cpp b/libc/fuzzing/math/acos_fuzz.cpp
index d2b5456026839..48fb4eacc3a79 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 94ae5c7bfdeee..e27d179606824 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/cos_fuzz.cpp b/libc/fuzzing/math/cos_fuzz.cpp
index 5b5ba0f7de717..6ed1e9ed8f309 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/log10_fuzz.cpp b/libc/fuzzing/math/log10_fuzz.cpp
index 23134f4903a45..369408cc288b5 100644
--- a/libc/fuzzing/math/log10_fuzz.cpp
+++ b/libc/fuzzing/math/log10_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf and values outside accepted range
     if (isnan(x) || isinf(x) || x < 0)
-      return 0;
+      continue;
     // signed zeros already tested in unit tests
     if (signbit(x) && x == 0.0)
-      return 0;
+      continue;
 
     mpfr_set_d(input, x, MPFR_RNDN);
     int output = mpfr_log10(input, input, MPFR_RNDN);

diff  --git a/libc/fuzzing/math/log1p_fuzz.cpp b/libc/fuzzing/math/log1p_fuzz.cpp
index 5e138a65e3716..e02c61a352c1f 100644
--- a/libc/fuzzing/math/log1p_fuzz.cpp
+++ b/libc/fuzzing/math/log1p_fuzz.cpp
@@ -26,10 +26,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
     data += sizeof(double);
     // remove NaN and inf and values outside accepted range
     if (isnan(x) || isinf(x) || x < -1)
-      return 0;
+      continue;
     // signed zeros already tested in unit tests
     if (signbit(x) && x == 0.0)
-      return 0;
+      continue;
 
     mpfr_set_d(input, x, MPFR_RNDN);
     int output = mpfr_log1p(input, input, MPFR_RNDN);

diff  --git a/libc/fuzzing/math/log2_fuzz.cpp b/libc/fuzzing/math/log2_fuzz.cpp
index aa19649b95126..c3e53c639cba9 100644
--- a/libc/fuzzing/math/log2_fuzz.cpp
+++ b/libc/fuzzing/math/log2_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf and values outside accepted range
     if (isnan(x) || isinf(x) || x < 0)
-      return 0;
+      continue;
     // signed zeros already tested in unit tests
     if (signbit(x) && x == 0.0)
-      return 0;
+      continue;
 
     mpfr_set_d(input, x, MPFR_RNDN);
     int output = mpfr_log2(input, input, MPFR_RNDN);

diff  --git a/libc/fuzzing/math/log_fuzz.cpp b/libc/fuzzing/math/log_fuzz.cpp
index 03aa678d1f16c..9618accf3db26 100644
--- a/libc/fuzzing/math/log_fuzz.cpp
+++ b/libc/fuzzing/math/log_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf and values outside accepted range
     if (isnan(x) || isinf(x) || x < 0)
-      return 0;
+      continue;
     // signed zeros already tested in unit tests
     if (signbit(x) && x == 0.0)
-      return 0;
+      continue;
     mpfr_set_d(input, x, MPFR_RNDN);
     int output = mpfr_log(input, input, MPFR_RNDN);
     mpfr_subnormalize(input, output, MPFR_RNDN);

diff  --git a/libc/fuzzing/math/sin_fuzz.cpp b/libc/fuzzing/math/sin_fuzz.cpp
index a5f0fa95c1581..f6d59c7e496bc 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 fd3dfae23168c..3d3306721fc47 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,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
index e81cf1afd3728..969b4f58e342c 100644
--- a/libc/fuzzing/math/sqrt_fuzz.cpp
+++ b/libc/fuzzing/math/sqrt_fuzz.cpp
@@ -26,10 +26,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
     data += sizeof(double);
     // remove NaN and inf and values outside accepted range
     if (isnan(x) || isinf(x) || x < 0)
-      return 0;
+      continue;
     // signed zeros already tested in unit tests
     if (signbit(x) && x == 0.0)
-      return 0;
+      continue;
 
     mpfr_set_d(input, x, MPFR_RNDN);
     int output = mpfr_sqrt(input, input, MPFR_RNDN);

diff  --git a/libc/fuzzing/math/tan_fuzz.cpp b/libc/fuzzing/math/tan_fuzz.cpp
index 2a462fa34fce4..63d3b12866a0e 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;


        


More information about the libc-commits mailing list