Skip to content

Commit 82451d0

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Updated exp fuzz tests (#148912)
Fuzz tests were previously in the wrong format, updated them to correct format. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 056f0a1 commit 82451d0

File tree

4 files changed

+108
-56
lines changed

4 files changed

+108
-56
lines changed

libc/fuzzing/math/exp10_fuzz.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,40 @@
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));
26+
data += sizeof(double);
3027

31-
double result = LIBC_NAMESPACE::exp10(x);
28+
// remove NaN and inf
29+
if (isnan(x) || isinf(x))
30+
continue;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
continue;
3234

33-
if (result != to_compare)
34-
__builtin_trap();
35+
mpfr_set_d(input, x, MPFR_RNDN);
36+
int output = mpfr_exp10(input, input, MPFR_RNDN);
37+
mpfr_subnormalize(input, output, MPFR_RNDN);
38+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3539

40+
double result = LIBC_NAMESPACE::exp10(x);
41+
42+
if (result != to_compare) {
43+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
44+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
45+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
46+
__builtin_trap();
47+
}
48+
}
3649
mpfr_clear(input);
3750
return 0;
3851
}

libc/fuzzing/math/exp2_fuzz.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,40 @@
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+
data += sizeof(double);
3027

31-
double result = LIBC_NAMESPACE::exp2(x);
28+
// remove NaN and inf
29+
if (isnan(x) || isinf(x))
30+
continue;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
continue;
3234

33-
if (result != to_compare)
34-
__builtin_trap();
35+
mpfr_set_d(input, x, MPFR_RNDN);
36+
int output = mpfr_exp2(input, input, MPFR_RNDN);
37+
mpfr_subnormalize(input, output, MPFR_RNDN);
38+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3539

40+
double result = LIBC_NAMESPACE::exp2(x);
41+
42+
if (result != to_compare) {
43+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
44+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
45+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
46+
__builtin_trap();
47+
}
48+
}
3649
mpfr_clear(input);
3750
return 0;
3851
}

libc/fuzzing/math/exp_fuzz.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,40 @@
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+
data += sizeof(double);
3027

31-
double result = LIBC_NAMESPACE::exp(x);
28+
// remove NaN and inf
29+
if (isnan(x) || isinf(x))
30+
continue;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
continue;
3234

33-
if (result != to_compare)
34-
__builtin_trap();
35+
mpfr_set_d(input, x, MPFR_RNDN);
36+
int output = mpfr_exp(input, input, MPFR_RNDN);
37+
mpfr_subnormalize(input, output, MPFR_RNDN);
38+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3539

40+
double result = LIBC_NAMESPACE::exp(x);
41+
42+
if (result != to_compare) {
43+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
44+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
45+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
46+
__builtin_trap();
47+
}
48+
}
3649
mpfr_clear(input);
3750
return 0;
3851
}

libc/fuzzing/math/expm1_fuzz.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,40 @@
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+
data += sizeof(double);
3027

31-
double result = LIBC_NAMESPACE::expm1(x);
28+
// remove NaN and inf
29+
if (isnan(x) || isinf(x))
30+
continue;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
continue;
3234

33-
if (result != to_compare)
34-
__builtin_trap();
35+
mpfr_set_d(input, x, MPFR_RNDN);
36+
int output = mpfr_expm1(input, input, MPFR_RNDN);
37+
mpfr_subnormalize(input, output, MPFR_RNDN);
38+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3539

40+
double result = LIBC_NAMESPACE::expm1(x);
41+
42+
if (result != to_compare) {
43+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
44+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
45+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
46+
__builtin_trap();
47+
}
48+
}
3649
mpfr_clear(input);
3750
return 0;
3851
}

0 commit comments

Comments
 (0)