Skip to content

Commit a9d8843

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Fuzz tests for acos and atan (#147843)
created fuzz tests for acos and atan --------- Co-authored-by: Sriya Pratipati <sriyap@google.com>
1 parent f60cc63 commit a9d8843

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

libc/fuzzing/math/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ add_libc_fuzzer(
7171
libc.src.math.sin
7272
)
7373

74+
add_libc_fuzzer(
75+
acos_fuzz
76+
NEED_MPFR
77+
SRCS
78+
acos_fuzz.cpp
79+
DEPENDS
80+
libc.src.math.acos
81+
)
82+
7483
add_libc_fuzzer(
7584
cos_fuzz
7685
NEED_MPFR
@@ -80,6 +89,15 @@ add_libc_fuzzer(
8089
libc.src.math.cos
8190
)
8291

92+
add_libc_fuzzer(
93+
atan_fuzz
94+
NEED_MPFR
95+
SRCS
96+
atan_fuzz.cpp
97+
DEPENDS
98+
libc.src.math.atan
99+
)
100+
83101
add_libc_fuzzer(
84102
tan_fuzz
85103
NEED_MPFR

libc/fuzzing/math/acos_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- acos_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 acos implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/acos.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <math.h>
16+
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;
24+
mpfr_t input;
25+
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);
30+
31+
double result = LIBC_NAMESPACE::acos(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/atan_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- atan_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 atan implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/atan.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <math.h>
16+
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;
24+
mpfr_t input;
25+
mpfr_init2(input, 53);
26+
mpfr_set_d(input, x, MPFR_RNDN);
27+
int output = mpfr_atan(input, input, MPFR_RNDN);
28+
mpfr_subnormalize(input, output, MPFR_RNDN);
29+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
30+
31+
double result = LIBC_NAMESPACE::atan(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

0 commit comments

Comments
 (0)