Skip to content

Commit 1f97c94

Browse files
sribee8Sriya Pratipati
andauthored
[libc] exp fuzz tests (#148086)
Created fuzz tests for exp functions --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent a76dfde commit 1f97c94

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

libc/fuzzing/math/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ add_libc_fuzzer(
6262
libc.src.math.nextafterl
6363
)
6464

65+
add_libc_fuzzer(
66+
exp_fuzz
67+
NEED_MPFR
68+
SRCS
69+
exp_fuzz.cpp
70+
DEPENDS
71+
libc.src.math.exp
72+
)
73+
74+
add_libc_fuzzer(
75+
exp10_fuzz
76+
NEED_MPFR
77+
SRCS
78+
exp10_fuzz.cpp
79+
DEPENDS
80+
libc.src.math.exp10
81+
)
82+
83+
add_libc_fuzzer(
84+
exp2_fuzz
85+
NEED_MPFR
86+
SRCS
87+
exp2_fuzz.cpp
88+
DEPENDS
89+
libc.src.math.exp2
90+
)
91+
92+
add_libc_fuzzer(
93+
expm1_fuzz
94+
NEED_MPFR
95+
SRCS
96+
expm1_fuzz.cpp
97+
DEPENDS
98+
libc.src.math.expm1
99+
)
100+
65101
add_libc_fuzzer(
66102
asin_fuzz
67103
NEED_MPFR

libc/fuzzing/math/exp10_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- exp10_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 exp10 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/exp10.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_exp10(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::exp10(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/exp2_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- exp2_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 exp2 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/exp2.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_exp2(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::exp2(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/exp_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- exp_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 exp implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/exp.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_exp(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::exp(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/expm1_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- expm1_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 expm1 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/expm1.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_expm1(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::expm1(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

0 commit comments

Comments
 (0)