Skip to content

Commit ba95df1

Browse files
sribee8Sriya Pratipati
andauthored
[libc] sqrt and log functions fuzz tests (#148006)
added fuzz tests for sqrt and log functions --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent e5bc7e7 commit ba95df1

File tree

8 files changed

+298
-48
lines changed

8 files changed

+298
-48
lines changed

libc/fuzzing/math/CMakeLists.txt

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,6 @@ add_libc_fuzzer(
134134
libc.src.math.cos
135135
)
136136

137-
add_libc_fuzzer(
138-
atan_fuzz
139-
NEED_MPFR
140-
SRCS
141-
atan_fuzz.cpp
142-
DEPENDS
143-
libc.src.math.atan
144-
)
145-
146137
add_libc_fuzzer(
147138
tan_fuzz
148139
NEED_MPFR
@@ -160,3 +151,48 @@ add_libc_fuzzer(
160151
DEPENDS
161152
libc.src.math.sincos
162153
)
154+
155+
add_libc_fuzzer(
156+
log_fuzz
157+
NEED_MPFR
158+
SRCS
159+
log_fuzz.cpp
160+
DEPENDS
161+
libc.src.math.log
162+
)
163+
164+
add_libc_fuzzer(
165+
log10_fuzz
166+
NEED_MPFR
167+
SRCS
168+
log10_fuzz.cpp
169+
DEPENDS
170+
libc.src.math.log10
171+
)
172+
173+
add_libc_fuzzer(
174+
log1p_fuzz
175+
NEED_MPFR
176+
SRCS
177+
log1p_fuzz.cpp
178+
DEPENDS
179+
libc.src.math.log1p
180+
)
181+
182+
add_libc_fuzzer(
183+
log2_fuzz
184+
NEED_MPFR
185+
SRCS
186+
log2_fuzz.cpp
187+
DEPENDS
188+
libc.src.math.log2
189+
)
190+
191+
add_libc_fuzzer(
192+
sqrt_fuzz
193+
NEED_MPFR
194+
SRCS
195+
sqrt_fuzz.cpp
196+
DEPENDS
197+
libc.src.__support.FPUtil.generic.sqrt
198+
)

libc/fuzzing/math/atan_fuzz.cpp

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

libc/fuzzing/math/log10_fuzz.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- log10_fuzz.cpp ----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// Fuzzing test for llvm-libc log10 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/log10.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
18+
#include <math.h>
19+
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21+
mpfr_t input;
22+
mpfr_init2(input, 53);
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+
28+
// remove NaN and inf and values outside accepted range
29+
if (isnan(x) || isinf(x) || x < 0)
30+
return 0;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
return 0;
34+
35+
mpfr_set_d(input, x, MPFR_RNDN);
36+
int output = mpfr_log10(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::log10(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+
}
49+
mpfr_clear(input);
50+
return 0;
51+
}

libc/fuzzing/math/log1p_fuzz.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- log1p_fuzz.cpp ----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// Fuzzing test for llvm-libc log1p implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/log1p.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
18+
#include <math.h>
19+
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21+
mpfr_t input;
22+
mpfr_init2(input, 53);
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)
29+
return 0;
30+
// signed zeros already tested in unit tests
31+
if (signbit(x) && x == 0.0)
32+
return 0;
33+
34+
mpfr_set_d(input, x, MPFR_RNDN);
35+
int output = mpfr_log1p(input, input, MPFR_RNDN);
36+
mpfr_subnormalize(input, output, MPFR_RNDN);
37+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
38+
39+
double result = LIBC_NAMESPACE::log1p(x);
40+
41+
if (result != to_compare) {
42+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
43+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
44+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
45+
__builtin_trap();
46+
}
47+
}
48+
mpfr_clear(input);
49+
return 0;
50+
}

libc/fuzzing/math/log2_fuzz.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- log2_fuzz.cpp -----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// Fuzzing test for llvm-libc log2 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/log2.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
18+
#include <math.h>
19+
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21+
mpfr_t input;
22+
mpfr_init2(input, 53);
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+
28+
// remove NaN and inf and values outside accepted range
29+
if (isnan(x) || isinf(x) || x < 0)
30+
return 0;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
return 0;
34+
35+
mpfr_set_d(input, x, MPFR_RNDN);
36+
int output = mpfr_log2(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::log2(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+
}
49+
mpfr_clear(input);
50+
return 0;
51+
}

libc/fuzzing/math/log_fuzz.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- log_fuzz.cpp ------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// Fuzzing test for llvm-libc log implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/log.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
18+
#include <math.h>
19+
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21+
mpfr_t input;
22+
mpfr_init2(input, 53);
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+
28+
// remove NaN and inf and values outside accepted range
29+
if (isnan(x) || isinf(x) || x < 0)
30+
return 0;
31+
// signed zeros already tested in unit tests
32+
if (signbit(x) && x == 0.0)
33+
return 0;
34+
mpfr_set_d(input, x, MPFR_RNDN);
35+
int output = mpfr_log(input, input, MPFR_RNDN);
36+
mpfr_subnormalize(input, output, MPFR_RNDN);
37+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
38+
39+
double result = LIBC_NAMESPACE::log(x);
40+
41+
if (result != to_compare) {
42+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
43+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
44+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
45+
__builtin_trap();
46+
}
47+
}
48+
mpfr_clear(input);
49+
return 0;
50+
}

libc/fuzzing/math/sincos_fuzz.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88
///
9-
/// Fuzzing test for llvm-libc cos implementation.
9+
/// Fuzzing test for llvm-libc sincos implementation.
1010
///
1111
//===----------------------------------------------------------------------===//
1212

libc/fuzzing/math/sqrt_fuzz.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- sqrt_fuzz.cpp -----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// Fuzzing test for llvm-libc sqrt implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/__support/FPUtil/generic/sqrt.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <cstdint>
16+
#include <cstring>
17+
#include <iostream>
18+
#include <math.h>
19+
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21+
mpfr_t input;
22+
mpfr_init2(input, 53);
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 < 0)
29+
return 0;
30+
// signed zeros already tested in unit tests
31+
if (signbit(x) && x == 0.0)
32+
return 0;
33+
34+
mpfr_set_d(input, x, MPFR_RNDN);
35+
int output = mpfr_sqrt(input, input, MPFR_RNDN);
36+
mpfr_subnormalize(input, output, MPFR_RNDN);
37+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
38+
39+
double result = LIBC_NAMESPACE::fputil::sqrt<double>(x);
40+
41+
if (result != to_compare) {
42+
std::cout << std::hexfloat << "Failing input: " << x << std::endl;
43+
std::cout << std::hexfloat << "Failing output: " << result << std::endl;
44+
std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
45+
__builtin_trap();
46+
}
47+
}
48+
mpfr_clear(input);
49+
return 0;
50+
}

0 commit comments

Comments
 (0)