diff options
-rw-r--r-- | libc/fuzzing/math/acos_fuzz.cpp | 42 | ||||
-rw-r--r-- | libc/fuzzing/math/asin_fuzz.cpp | 43 | ||||
-rw-r--r-- | libc/fuzzing/math/cos_fuzz.cpp | 47 | ||||
-rw-r--r-- | libc/fuzzing/math/log10_fuzz.cpp | 4 | ||||
-rw-r--r-- | libc/fuzzing/math/log1p_fuzz.cpp | 4 | ||||
-rw-r--r-- | libc/fuzzing/math/log2_fuzz.cpp | 4 | ||||
-rw-r--r-- | libc/fuzzing/math/log_fuzz.cpp | 4 | ||||
-rw-r--r-- | libc/fuzzing/math/sin_fuzz.cpp | 47 | ||||
-rw-r--r-- | libc/fuzzing/math/sincos_fuzz.cpp | 53 | ||||
-rw-r--r-- | libc/fuzzing/math/sqrt_fuzz.cpp | 4 | ||||
-rw-r--r-- | libc/fuzzing/math/tan_fuzz.cpp | 47 |
11 files changed, 196 insertions, 103 deletions
diff --git a/libc/fuzzing/math/acos_fuzz.cpp b/libc/fuzzing/math/acos_fuzz.cpp index d2b5456..48fb4ea 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 94ae5c7..e27d179 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 5b5ba0f..6ed1e9e 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 23134f4..369408c 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 5e138a6..e02c61a 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 aa19649..c3e53c6 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 03aa678..9618acc 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 a5f0fa9..f6d59c7 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 fd3dfae..3d33067 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 e81cf1a..969b4f5 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 2a462fa..63d3b12 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; |