[libc-commits] [libc] [libc] Updated exp fuzz tests (PR #148912)

via libc-commits libc-commits at lists.llvm.org
Wed Jul 16 09:49:17 PDT 2025


https://github.com/sribee8 updated https://github.com/llvm/llvm-project/pull/148912

>From 198b6bd917af160ac94a594c84c5f01679824b66 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Tue, 15 Jul 2025 17:52:55 +0000
Subject: [PATCH 1/4] [libc] Updated exp fuzz tests

Fuzz tests were previously in the wrong format, updated them to correct format.
---
 libc/fuzzing/math/exp10_fuzz.cpp | 38 +++++++++++++++++++------------
 libc/fuzzing/math/exp2_fuzz.cpp  | 39 ++++++++++++++++++++------------
 libc/fuzzing/math/exp_fuzz.cpp   | 39 ++++++++++++++++++++------------
 libc/fuzzing/math/expm1_fuzz.cpp | 39 ++++++++++++++++++++------------
 4 files changed, 99 insertions(+), 56 deletions(-)

diff --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp
index 2baef03a264a4..3dba9b129521f 100644
--- a/libc/fuzzing/math/exp10_fuzz.cpp
+++ b/libc/fuzzing/math/exp10_fuzz.cpp
@@ -12,27 +12,37 @@
 
 #include "src/math/exp10.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
-  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_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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double)); // 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;
 
-  double result = LIBC_NAMESPACE::exp10(x);
+    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);
 
-  if (result != to_compare)
-    __builtin_trap();
+    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
index 8a2959047a6ca..3c2f58915fa50 100644
--- a/libc/fuzzing/math/exp2_fuzz.cpp
+++ b/libc/fuzzing/math/exp2_fuzz.cpp
@@ -12,27 +12,38 @@
 
 #include "src/math/exp2.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
-  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_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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    // 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;
 
-  double result = LIBC_NAMESPACE::exp2(x);
+    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);
 
-  if (result != to_compare)
-    __builtin_trap();
+    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
index 97bc12dfa64c9..13a81623d5c02 100644
--- a/libc/fuzzing/math/exp_fuzz.cpp
+++ b/libc/fuzzing/math/exp_fuzz.cpp
@@ -12,27 +12,38 @@
 
 #include "src/math/exp.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
-  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_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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    // 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;
 
-  double result = LIBC_NAMESPACE::exp(x);
+    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);
 
-  if (result != to_compare)
-    __builtin_trap();
+    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
index db507bb02b1d7..464d799565747 100644
--- a/libc/fuzzing/math/expm1_fuzz.cpp
+++ b/libc/fuzzing/math/expm1_fuzz.cpp
@@ -12,27 +12,38 @@
 
 #include "src/math/expm1.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
-  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_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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    // 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;
 
-  double result = LIBC_NAMESPACE::expm1(x);
+    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);
 
-  if (result != to_compare)
-    __builtin_trap();
+    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;
 }

>From 44cedcd507694af1801e59fd0a3f2bfb8655e58a Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Tue, 15 Jul 2025 19:59:34 +0000
Subject: [PATCH 2/4] updating data pointer

---
 libc/fuzzing/math/exp10_fuzz.cpp | 5 ++++-
 libc/fuzzing/math/exp2_fuzz.cpp  | 2 ++
 libc/fuzzing/math/exp_fuzz.cpp   | 2 ++
 libc/fuzzing/math/expm1_fuzz.cpp | 2 ++
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp
index 3dba9b129521f..4cad0437a69f9 100644
--- a/libc/fuzzing/math/exp10_fuzz.cpp
+++ b/libc/fuzzing/math/exp10_fuzz.cpp
@@ -22,7 +22,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   mpfr_init2(input, 53);
   for (size_t i = 0; i < size / sizeof(double); ++i) {
     double x;
-    std::memcpy(&x, data, sizeof(double)); // remove NaN and inf
+    std::memcpy(&x, data, sizeof(double));
+    data += sizeof(double);
+
+    // remove NaN and inf
     if (isnan(x) || isinf(x))
       return 0;
     // signed zeros already tested in unit tests
diff --git a/libc/fuzzing/math/exp2_fuzz.cpp b/libc/fuzzing/math/exp2_fuzz.cpp
index 3c2f58915fa50..755b16f9b5def 100644
--- a/libc/fuzzing/math/exp2_fuzz.cpp
+++ b/libc/fuzzing/math/exp2_fuzz.cpp
@@ -23,6 +23,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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))
       return 0;
diff --git a/libc/fuzzing/math/exp_fuzz.cpp b/libc/fuzzing/math/exp_fuzz.cpp
index 13a81623d5c02..19e15e8231075 100644
--- a/libc/fuzzing/math/exp_fuzz.cpp
+++ b/libc/fuzzing/math/exp_fuzz.cpp
@@ -23,6 +23,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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))
       return 0;
diff --git a/libc/fuzzing/math/expm1_fuzz.cpp b/libc/fuzzing/math/expm1_fuzz.cpp
index 464d799565747..eeba0179ddd27 100644
--- a/libc/fuzzing/math/expm1_fuzz.cpp
+++ b/libc/fuzzing/math/expm1_fuzz.cpp
@@ -23,6 +23,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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))
       return 0;

>From f69d8ab2156436a8fe3e3f8a23417fc4ef357670 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Wed, 16 Jul 2025 16:41:16 +0000
Subject: [PATCH 3/4] changed return inside for loop to continue

---
 libc/fuzzing/math/exp10_fuzz.cpp | 4 ++--
 libc/fuzzing/math/exp2_fuzz.cpp  | 4 ++--
 libc/fuzzing/math/exp_fuzz.cpp   | 4 ++--
 libc/fuzzing/math/expm1_fuzz.cpp | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp
index 4cad0437a69f9..d939948b723a5 100644
--- a/libc/fuzzing/math/exp10_fuzz.cpp
+++ b/libc/fuzzing/math/exp10_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf
     if (isnan(x) || isinf(x))
-      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_exp10(input, input, MPFR_RNDN);
diff --git a/libc/fuzzing/math/exp2_fuzz.cpp b/libc/fuzzing/math/exp2_fuzz.cpp
index 755b16f9b5def..a29d3c00da672 100644
--- a/libc/fuzzing/math/exp2_fuzz.cpp
+++ b/libc/fuzzing/math/exp2_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf
     if (isnan(x) || isinf(x))
-      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_exp2(input, input, MPFR_RNDN);
diff --git a/libc/fuzzing/math/exp_fuzz.cpp b/libc/fuzzing/math/exp_fuzz.cpp
index 19e15e8231075..e3b53aa08fea8 100644
--- a/libc/fuzzing/math/exp_fuzz.cpp
+++ b/libc/fuzzing/math/exp_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf
     if (isnan(x) || isinf(x))
-      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_exp(input, input, MPFR_RNDN);
diff --git a/libc/fuzzing/math/expm1_fuzz.cpp b/libc/fuzzing/math/expm1_fuzz.cpp
index eeba0179ddd27..0690e449c3d23 100644
--- a/libc/fuzzing/math/expm1_fuzz.cpp
+++ b/libc/fuzzing/math/expm1_fuzz.cpp
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf
     if (isnan(x) || isinf(x))
-      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_expm1(input, input, MPFR_RNDN);

>From 28a3ba19746e6501a239081c2b6ae79e59664947 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Wed, 16 Jul 2025 16:49:00 +0000
Subject: [PATCH 4/4] fixed formatting

---
 libc/fuzzing/math/exp_fuzz.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/fuzzing/math/exp_fuzz.cpp b/libc/fuzzing/math/exp_fuzz.cpp
index e3b53aa08fea8..66823596dc6fa 100644
--- a/libc/fuzzing/math/exp_fuzz.cpp
+++ b/libc/fuzzing/math/exp_fuzz.cpp
@@ -27,7 +27,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     // remove NaN and inf
     if (isnan(x) || isinf(x))
-       continue;
+      continue;
     // signed zeros already tested in unit tests
     if (signbit(x) && x == 0.0)
       continue;



More information about the libc-commits mailing list