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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions libc/fuzzing/math/acos_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 29 additions & 14 deletions libc/fuzzing/math/asin_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 31 additions & 16 deletions libc/fuzzing/math/cos_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions libc/fuzzing/math/log10_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions libc/fuzzing/math/log1p_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions libc/fuzzing/math/log2_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions libc/fuzzing/math/log_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 31 additions & 16 deletions libc/fuzzing/math/sin_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
53 changes: 36 additions & 17 deletions libc/fuzzing/math/sincos_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,56 @@

#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;

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);
Expand Down
4 changes: 2 additions & 2 deletions libc/fuzzing/math/sqrt_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading