Skip to content

Commit 198b6bd

Browse files
author
Sriya Pratipati
committed
[libc] Updated exp fuzz tests
Fuzz tests were previously in the wrong format, updated them to correct format.
1 parent 66850d0 commit 198b6bd

File tree

4 files changed

+99
-56
lines changed

4 files changed

+99
-56
lines changed

libc/fuzzing/math/exp10_fuzz.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,37 @@
1212

1313
#include "src/math/exp10.h"
1414
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
1518
#include <math.h>
1619

17-
extern "C" int LLVMFuzzerTestOneInput(double x) {
18-
// remove NaN and inf
19-
if (isnan(x) || isinf(x))
20-
return 0;
21-
// signed zeros already tested in unit tests
22-
if (signbit(x) && x == 0.0)
23-
return 0;
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2421
mpfr_t input;
2522
mpfr_init2(input, 53);
26-
mpfr_set_d(input, x, MPFR_RNDN);
27-
int output = mpfr_exp10(input, input, MPFR_RNDN);
28-
mpfr_subnormalize(input, output, MPFR_RNDN);
29-
double to_compare = mpfr_get_d(input, MPFR_RNDN);
23+
for (size_t i = 0; i < size / sizeof(double); ++i) {
24+
double x;
25+
std::memcpy(&x, data, sizeof(double)); // remove NaN and inf
26+
if (isnan(x) || isinf(x))
27+
return 0;
28+
// signed zeros already tested in unit tests
29+
if (signbit(x) && x == 0.0)
30+
return 0;
3031

31-
double result = LIBC_NAMESPACE::exp10(x);
32+
mpfr_set_d(input, x, MPFR_RNDN);
33+
int output = mpfr_exp10(input, input, MPFR_RNDN);
34+
mpfr_subnormalize(input, output, MPFR_RNDN);
35+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3236

33-
if (result != to_compare)
34-
__builtin_trap();
37+
double result = LIBC_NAMESPACE::exp10(x);
3538

39+
if (result != to_compare) {
40+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
41+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
42+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
43+
__builtin_trap();
44+
}
45+
}
3646
mpfr_clear(input);
3747
return 0;
3848
}

libc/fuzzing/math/exp2_fuzz.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,38 @@
1212

1313
#include "src/math/exp2.h"
1414
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
1518
#include <math.h>
1619

17-
extern "C" int LLVMFuzzerTestOneInput(double x) {
18-
// remove NaN and inf
19-
if (isnan(x) || isinf(x))
20-
return 0;
21-
// signed zeros already tested in unit tests
22-
if (signbit(x) && x == 0.0)
23-
return 0;
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2421
mpfr_t input;
2522
mpfr_init2(input, 53);
26-
mpfr_set_d(input, x, MPFR_RNDN);
27-
int output = mpfr_exp2(input, input, MPFR_RNDN);
28-
mpfr_subnormalize(input, output, MPFR_RNDN);
29-
double to_compare = mpfr_get_d(input, MPFR_RNDN);
23+
for (size_t i = 0; i < size / sizeof(double); ++i) {
24+
double x;
25+
std::memcpy(&x, data, sizeof(double));
26+
// remove NaN and inf
27+
if (isnan(x) || isinf(x))
28+
return 0;
29+
// signed zeros already tested in unit tests
30+
if (signbit(x) && x == 0.0)
31+
return 0;
3032

31-
double result = LIBC_NAMESPACE::exp2(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_exp2(input, input, MPFR_RNDN);
35+
mpfr_subnormalize(input, output, MPFR_RNDN);
36+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3237

33-
if (result != to_compare)
34-
__builtin_trap();
38+
double result = LIBC_NAMESPACE::exp2(x);
3539

40+
if (result != to_compare) {
41+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
42+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
43+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
44+
__builtin_trap();
45+
}
46+
}
3647
mpfr_clear(input);
3748
return 0;
3849
}

libc/fuzzing/math/exp_fuzz.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,38 @@
1212

1313
#include "src/math/exp.h"
1414
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
1518
#include <math.h>
1619

17-
extern "C" int LLVMFuzzerTestOneInput(double x) {
18-
// remove NaN and inf
19-
if (isnan(x) || isinf(x))
20-
return 0;
21-
// signed zeros already tested in unit tests
22-
if (signbit(x) && x == 0.0)
23-
return 0;
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2421
mpfr_t input;
2522
mpfr_init2(input, 53);
26-
mpfr_set_d(input, x, MPFR_RNDN);
27-
int output = mpfr_exp(input, input, MPFR_RNDN);
28-
mpfr_subnormalize(input, output, MPFR_RNDN);
29-
double to_compare = mpfr_get_d(input, MPFR_RNDN);
23+
for (size_t i = 0; i < size / sizeof(double); ++i) {
24+
double x;
25+
std::memcpy(&x, data, sizeof(double));
26+
// remove NaN and inf
27+
if (isnan(x) || isinf(x))
28+
return 0;
29+
// signed zeros already tested in unit tests
30+
if (signbit(x) && x == 0.0)
31+
return 0;
3032

31-
double result = LIBC_NAMESPACE::exp(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_exp(input, input, MPFR_RNDN);
35+
mpfr_subnormalize(input, output, MPFR_RNDN);
36+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3237

33-
if (result != to_compare)
34-
__builtin_trap();
38+
double result = LIBC_NAMESPACE::exp(x);
3539

40+
if (result != to_compare) {
41+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
42+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
43+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
44+
__builtin_trap();
45+
}
46+
}
3647
mpfr_clear(input);
3748
return 0;
3849
}

libc/fuzzing/math/expm1_fuzz.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,38 @@
1212

1313
#include "src/math/expm1.h"
1414
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
1518
#include <math.h>
1619

17-
extern "C" int LLVMFuzzerTestOneInput(double x) {
18-
// remove NaN and inf
19-
if (isnan(x) || isinf(x))
20-
return 0;
21-
// signed zeros already tested in unit tests
22-
if (signbit(x) && x == 0.0)
23-
return 0;
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2421
mpfr_t input;
2522
mpfr_init2(input, 53);
26-
mpfr_set_d(input, x, MPFR_RNDN);
27-
int output = mpfr_expm1(input, input, MPFR_RNDN);
28-
mpfr_subnormalize(input, output, MPFR_RNDN);
29-
double to_compare = mpfr_get_d(input, MPFR_RNDN);
23+
for (size_t i = 0; i < size / sizeof(double); ++i) {
24+
double x;
25+
std::memcpy(&x, data, sizeof(double));
26+
// remove NaN and inf
27+
if (isnan(x) || isinf(x))
28+
return 0;
29+
// signed zeros already tested in unit tests
30+
if (signbit(x) && x == 0.0)
31+
return 0;
3032

31-
double result = LIBC_NAMESPACE::expm1(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_expm1(input, input, MPFR_RNDN);
35+
mpfr_subnormalize(input, output, MPFR_RNDN);
36+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3237

33-
if (result != to_compare)
34-
__builtin_trap();
38+
double result = LIBC_NAMESPACE::expm1(x);
3539

40+
if (result != to_compare) {
41+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
42+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
43+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
44+
__builtin_trap();
45+
}
46+
}
3647
mpfr_clear(input);
3748
return 0;
3849
}

0 commit comments

Comments
 (0)