Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 0 additions & 9 deletions libc/fuzzing/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 27 additions & 14 deletions libc/fuzzing/math/acos_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 27 additions & 14 deletions libc/fuzzing/math/asin_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 0 additions & 38 deletions libc/fuzzing/math/atan_fuzz.cpp

This file was deleted.

46 changes: 30 additions & 16 deletions libc/fuzzing/math/cos_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 30 additions & 16 deletions libc/fuzzing/math/sin_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 35 additions & 17 deletions libc/fuzzing/math/sincos_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,55 @@

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

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);
Expand Down
Loading
Loading