Skip to content

Commit 037d348

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Updated fuzz tests for trig functions (#148891)
Fuzz tests were set up incorrectly so updated trig functions to match the correct format. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 55b417a commit 037d348

File tree

11 files changed

+196
-103
lines changed

11 files changed

+196
-103
lines changed

libc/fuzzing/math/acos_fuzz.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,40 @@
1212

1313
#include "src/math/acos.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 and values outside accepted range
19-
if (isnan(x) || isinf(x) || x > 1 || x < -1)
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_acos(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);
27+
// remove NaN and inf and values outside accepted range
28+
if (isnan(x) || isinf(x) || x > 1 || x < -1)
29+
continue;
3030

31-
double result = LIBC_NAMESPACE::acos(x);
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_acos(input, input, MPFR_RNDN);
37+
mpfr_subnormalize(input, output, MPFR_RNDN);
38+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
39+
40+
double result = LIBC_NAMESPACE::acos(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+
}
3549

3650
mpfr_clear(input);
3751
return 0;

libc/fuzzing/math/asin_fuzz.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,41 @@
1212

1313
#include "src/math/asin.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 and values outside accepted range
19-
if (isnan(x) || isinf(x) || x > 1 || x < -1)
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_asin(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::asin(x);
28+
// remove NaN and inf and values outside accepted range
29+
if (isnan(x) || isinf(x) || x > 1 || x < -1)
30+
continue;
3231

33-
if (result != to_compare)
34-
__builtin_trap();
32+
// signed zeros already tested in unit tests
33+
if (signbit(x) && x == 0.0)
34+
continue;
35+
36+
mpfr_set_d(input, x, MPFR_RNDN);
37+
int output = mpfr_asin(input, input, MPFR_RNDN);
38+
mpfr_subnormalize(input, output, MPFR_RNDN);
39+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
40+
41+
double result = LIBC_NAMESPACE::asin(x);
42+
43+
if (result != to_compare) {
44+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
45+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
46+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
47+
__builtin_trap();
48+
}
49+
}
3550

3651
mpfr_clear(input);
3752
return 0;

libc/fuzzing/math/cos_fuzz.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,43 @@
1212

1313
#include "src/math/cos.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(const double x) {
18-
// remove NaN and inf as preconditions
19-
if (isnan(x))
20-
return 0;
21-
if (isinf(x))
22-
return 0;
23-
// signed zeros already tested in unit tests
24-
if (signbit(x) && x == 0.0)
25-
return 0;
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2621
mpfr_t input;
2722
mpfr_init2(input, 53);
28-
mpfr_set_d(input, x, MPFR_RNDN);
29-
int output = mpfr_cos(input, input, MPFR_RNDN);
30-
mpfr_subnormalize(input, output, MPFR_RNDN);
31-
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);
3227

33-
double result = LIBC_NAMESPACE::cos(x);
28+
// remove NaN and inf as preconditions
29+
if (isnan(x))
30+
continue;
31+
if (isinf(x))
32+
continue;
3433

35-
if (result != to_compare)
36-
__builtin_trap();
34+
// signed zeros already tested in unit tests
35+
if (signbit(x) && x == 0.0)
36+
continue;
37+
38+
mpfr_set_d(input, x, MPFR_RNDN);
39+
int output = mpfr_cos(input, input, MPFR_RNDN);
40+
mpfr_subnormalize(input, output, MPFR_RNDN);
41+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
42+
43+
double result = LIBC_NAMESPACE::cos(x);
44+
45+
if (result != to_compare) {
46+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
47+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
48+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
49+
__builtin_trap();
50+
}
51+
}
3752

3853
mpfr_clear(input);
3954
return 0;

libc/fuzzing/math/log10_fuzz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2727

2828
// remove NaN and inf and values outside accepted range
2929
if (isnan(x) || isinf(x) || x < 0)
30-
return 0;
30+
continue;
3131
// signed zeros already tested in unit tests
3232
if (signbit(x) && x == 0.0)
33-
return 0;
33+
continue;
3434

3535
mpfr_set_d(input, x, MPFR_RNDN);
3636
int output = mpfr_log10(input, input, MPFR_RNDN);

libc/fuzzing/math/log1p_fuzz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2626
data += sizeof(double);
2727
// remove NaN and inf and values outside accepted range
2828
if (isnan(x) || isinf(x) || x < -1)
29-
return 0;
29+
continue;
3030
// signed zeros already tested in unit tests
3131
if (signbit(x) && x == 0.0)
32-
return 0;
32+
continue;
3333

3434
mpfr_set_d(input, x, MPFR_RNDN);
3535
int output = mpfr_log1p(input, input, MPFR_RNDN);

libc/fuzzing/math/log2_fuzz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2727

2828
// remove NaN and inf and values outside accepted range
2929
if (isnan(x) || isinf(x) || x < 0)
30-
return 0;
30+
continue;
3131
// signed zeros already tested in unit tests
3232
if (signbit(x) && x == 0.0)
33-
return 0;
33+
continue;
3434

3535
mpfr_set_d(input, x, MPFR_RNDN);
3636
int output = mpfr_log2(input, input, MPFR_RNDN);

libc/fuzzing/math/log_fuzz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2727

2828
// remove NaN and inf and values outside accepted range
2929
if (isnan(x) || isinf(x) || x < 0)
30-
return 0;
30+
continue;
3131
// signed zeros already tested in unit tests
3232
if (signbit(x) && x == 0.0)
33-
return 0;
33+
continue;
3434
mpfr_set_d(input, x, MPFR_RNDN);
3535
int output = mpfr_log(input, input, MPFR_RNDN);
3636
mpfr_subnormalize(input, output, MPFR_RNDN);

libc/fuzzing/math/sin_fuzz.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,43 @@
1212

1313
#include "src/math/sin.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(const double x) {
18-
// remove NaN and inf as preconditions
19-
if (isnan(x))
20-
return 0;
21-
if (isinf(x))
22-
return 0;
23-
// signed zeros already tested in unit tests
24-
if (signbit(x) && x == 0.0)
25-
return 0;
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2621
mpfr_t input;
2722
mpfr_init2(input, 53);
28-
mpfr_set_d(input, x, MPFR_RNDN);
29-
int output = mpfr_sin(input, input, MPFR_RNDN);
30-
mpfr_subnormalize(input, output, MPFR_RNDN);
31-
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);
3227

33-
double result = LIBC_NAMESPACE::sin(x);
28+
// remove NaN and inf as preconditions
29+
if (isnan(x))
30+
continue;
31+
if (isinf(x))
32+
continue;
3433

35-
if (result != to_compare)
36-
__builtin_trap();
34+
// signed zeros already tested in unit tests
35+
if (signbit(x) && x == 0.0)
36+
continue;
37+
38+
mpfr_set_d(input, x, MPFR_RNDN);
39+
int output = mpfr_sin(input, input, MPFR_RNDN);
40+
mpfr_subnormalize(input, output, MPFR_RNDN);
41+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
42+
43+
double result = LIBC_NAMESPACE::sin(x);
44+
45+
if (result != to_compare) {
46+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
47+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
48+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
49+
__builtin_trap();
50+
}
51+
}
3752

3853
mpfr_clear(input);
3954
return 0;

libc/fuzzing/math/sincos_fuzz.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,56 @@
1212

1313
#include "src/math/sincos.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 as preconditions
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_t sin_x;
2623
mpfr_t cos_x;
2724

2825
mpfr_init2(input, 53);
2926
mpfr_init2(sin_x, 53);
3027
mpfr_init2(cos_x, 53);
28+
for (size_t i = 0; i < size / sizeof(double); ++i) {
29+
double x;
30+
std::memcpy(&x, data, sizeof(double));
31+
data += sizeof(double);
3132

32-
mpfr_set_d(input, x, MPFR_RNDN);
33+
// remove NaN and inf as preconditions
34+
if (isnan(x) || isinf(x))
35+
continue;
3336

34-
int output = mpfr_sin_cos(sin_x, cos_x, input, MPFR_RNDN);
35-
mpfr_subnormalize(sin_x, output, MPFR_RNDN);
36-
mpfr_subnormalize(cos_x, output, MPFR_RNDN);
37+
// signed zeros already tested in unit tests
38+
if (signbit(x) && x == 0.0)
39+
continue;
3740

38-
double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN);
39-
double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN);
41+
mpfr_set_d(input, x, MPFR_RNDN);
42+
int output = mpfr_sin_cos(sin_x, cos_x, input, MPFR_RNDN);
43+
mpfr_subnormalize(sin_x, output, MPFR_RNDN);
44+
mpfr_subnormalize(cos_x, output, MPFR_RNDN);
4045

41-
double sin_res, cos_res;
42-
LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res);
46+
double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN);
47+
double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN);
4348

44-
if (sin_res != to_compare_sin || cos_res != to_compare_cos)
45-
__builtin_trap();
49+
double sin_res, cos_res;
50+
LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res);
51+
52+
if (sin_res != to_compare_sin || cos_res != to_compare_cos) {
53+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
54+
std::cout << std::hexfloat << "Failing sin output: " << sin_res
55+
<< std::endl;
56+
std::cout << std::hexfloat << "Expected sin: " << to_compare_sin
57+
<< std::endl;
58+
std::cout << std::hexfloat << "Failing cos output: " << cos_res
59+
<< std::endl;
60+
std::cout << std::hexfloat << "Expected cos: " << to_compare_cos
61+
<< std::endl;
62+
__builtin_trap();
63+
}
64+
}
4665

4766
mpfr_clear(input);
4867
mpfr_clear(sin_x);

libc/fuzzing/math/sqrt_fuzz.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2626
data += sizeof(double);
2727
// remove NaN and inf and values outside accepted range
2828
if (isnan(x) || isinf(x) || x < 0)
29-
return 0;
29+
continue;
3030
// signed zeros already tested in unit tests
3131
if (signbit(x) && x == 0.0)
32-
return 0;
32+
continue;
3333

3434
mpfr_set_d(input, x, MPFR_RNDN);
3535
int output = mpfr_sqrt(input, input, MPFR_RNDN);

0 commit comments

Comments
 (0)