Skip to content

Commit 5b5e4e6

Browse files
author
Sriya Pratipati
committed
updated tests to correct format
1 parent b83fcf3 commit 5b5e4e6

File tree

7 files changed

+125
-118
lines changed

7 files changed

+125
-118
lines changed

libc/fuzzing/math/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ add_libc_fuzzer(
9898
libc.src.math.cos
9999
)
100100

101-
add_libc_fuzzer(
102-
atan_fuzz
103-
NEED_MPFR
104-
SRCS
105-
atan_fuzz.cpp
106-
DEPENDS
107-
libc.src.math.atan
108-
)
109-
110101
add_libc_fuzzer(
111102
tan_fuzz
112103
NEED_MPFR

libc/fuzzing/math/atan_fuzz.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

libc/fuzzing/math/log10_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/log10.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 < 0)
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_log10(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 and values outside accepted range
27+
if (isnan(x) || isinf(x) || x < 0)
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::log10(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_log10(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::log10(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/log1p_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/log1p.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)
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_log1p(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 and values outside accepted range
27+
if (isnan(x) || isinf(x) || x < -1)
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::log1p(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_log1p(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::log1p(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/log2_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/log2.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 < 0)
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_log2(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 and values outside accepted range
27+
if (isnan(x) || isinf(x) || x < 0)
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::log2(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_log2(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::log2(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/log_fuzz.cpp

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

1313
#include "src/math/log.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 < 0)
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_log(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 and values outside accepted range
27+
if (isnan(x) || isinf(x) || x < 0)
28+
return 0;
29+
// signed zeros already tested in unit tests
30+
if (signbit(x) && x == 0.0)
31+
return 0;
32+
mpfr_set_d(input, x, MPFR_RNDN);
33+
int output = mpfr_log(input, input, MPFR_RNDN);
34+
mpfr_subnormalize(input, output, MPFR_RNDN);
35+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
3036

31-
double result = LIBC_NAMESPACE::log(x);
32-
33-
if (result != to_compare)
34-
__builtin_trap();
37+
double result = LIBC_NAMESPACE::log(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/sqrt_fuzz.cpp

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

1313
#include "src/__support/FPUtil/generic/sqrt.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 < 0)
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_sqrt(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 and values outside accepted range
27+
if (isnan(x) || isinf(x) || x < 0)
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::fputil::sqrt<double>(x);
33+
mpfr_set_d(input, x, MPFR_RNDN);
34+
int output = mpfr_sqrt(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::fputil::sqrt<double>(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)