[libc-commits] [libc] [libc] Updated fuzz tests for trig functions (PR #148891)
via libc-commits
libc-commits at lists.llvm.org
Tue Jul 15 11:24:24 PDT 2025
https://github.com/sribee8 updated https://github.com/llvm/llvm-project/pull/148891
>From ab68b155832fc45c3b38ae657d75fa23512041ae Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Tue, 15 Jul 2025 16:56:22 +0000
Subject: [PATCH 1/2] [libc] Updated fuzz tests for trig functions
Fuzz tests were set up incorrectly so updated trig functions to match the correct format.
---
libc/fuzzing/math/CMakeLists.txt | 9 ------
libc/fuzzing/math/acos_fuzz.cpp | 41 +++++++++++++++---------
libc/fuzzing/math/asin_fuzz.cpp | 41 +++++++++++++++---------
libc/fuzzing/math/atan_fuzz.cpp | 38 ----------------------
libc/fuzzing/math/cos_fuzz.cpp | 46 +++++++++++++++++----------
libc/fuzzing/math/sin_fuzz.cpp | 46 +++++++++++++++++----------
libc/fuzzing/math/sincos_fuzz.cpp | 52 +++++++++++++++++++++----------
libc/fuzzing/math/tan_fuzz.cpp | 46 +++++++++++++++++----------
8 files changed, 179 insertions(+), 140 deletions(-)
delete mode 100644 libc/fuzzing/math/atan_fuzz.cpp
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;
>From 17f2697347d14be5de17d9263401e9457d92abb6 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Tue, 15 Jul 2025 18:24:10 +0000
Subject: [PATCH 2/2] added data pointer incrementing
---
libc/fuzzing/math/acos_fuzz.cpp | 1 +
libc/fuzzing/math/asin_fuzz.cpp | 2 ++
libc/fuzzing/math/cos_fuzz.cpp | 1 +
libc/fuzzing/math/sin_fuzz.cpp | 1 +
libc/fuzzing/math/sincos_fuzz.cpp | 1 +
libc/fuzzing/math/tan_fuzz.cpp | 1 +
6 files changed, 7 insertions(+)
diff --git a/libc/fuzzing/math/acos_fuzz.cpp b/libc/fuzzing/math/acos_fuzz.cpp
index f01e9ef9ed1a8..7c82724a513e6 100644
--- a/libc/fuzzing/math/acos_fuzz.cpp
+++ b/libc/fuzzing/math/acos_fuzz.cpp
@@ -23,6 +23,7 @@ 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 and values outside accepted range
if (isnan(x) || isinf(x) || x > 1 || x < -1)
return 0;
diff --git a/libc/fuzzing/math/asin_fuzz.cpp b/libc/fuzzing/math/asin_fuzz.cpp
index 9d8852f22f1f5..5c447b720256e 100644
--- a/libc/fuzzing/math/asin_fuzz.cpp
+++ b/libc/fuzzing/math/asin_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 and values outside accepted range
if (isnan(x) || isinf(x) || x > 1 || x < -1)
return 0;
diff --git a/libc/fuzzing/math/cos_fuzz.cpp b/libc/fuzzing/math/cos_fuzz.cpp
index 4fc4a83303adc..ab2078500c70d 100644
--- a/libc/fuzzing/math/cos_fuzz.cpp
+++ b/libc/fuzzing/math/cos_fuzz.cpp
@@ -23,6 +23,7 @@ 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 as preconditions
if (isnan(x))
diff --git a/libc/fuzzing/math/sin_fuzz.cpp b/libc/fuzzing/math/sin_fuzz.cpp
index cb2fc0772410a..09060b1c6b82a 100644
--- a/libc/fuzzing/math/sin_fuzz.cpp
+++ b/libc/fuzzing/math/sin_fuzz.cpp
@@ -23,6 +23,7 @@ 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 as preconditions
if (isnan(x))
diff --git a/libc/fuzzing/math/sincos_fuzz.cpp b/libc/fuzzing/math/sincos_fuzz.cpp
index 35e6ddc632523..0c2e68a1a9680 100644
--- a/libc/fuzzing/math/sincos_fuzz.cpp
+++ b/libc/fuzzing/math/sincos_fuzz.cpp
@@ -28,6 +28,7 @@ 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 as preconditions
if (isnan(x) || isinf(x))
diff --git a/libc/fuzzing/math/tan_fuzz.cpp b/libc/fuzzing/math/tan_fuzz.cpp
index 1f3456555f003..201984c1f6383 100644
--- a/libc/fuzzing/math/tan_fuzz.cpp
+++ b/libc/fuzzing/math/tan_fuzz.cpp
@@ -23,6 +23,7 @@ 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 as preconditions
if (isnan(x))
More information about the libc-commits
mailing list