Skip to content

Commit 81614e5

Browse files
sribee8Sriya Pratipati
andauthored
[libc] sincos fuzz test (#147855)
Created fuzz test for sincos --------- Co-authored-by: Sriya Pratipati <sriyap@google.com>
1 parent 8e7461e commit 81614e5

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

libc/fuzzing/math/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,12 @@ add_libc_fuzzer(
8888
DEPENDS
8989
libc.src.math.tan
9090
)
91+
92+
add_libc_fuzzer(
93+
sincos_fuzz
94+
NEED_MPFR
95+
SRCS
96+
sincos_fuzz.cpp
97+
DEPENDS
98+
libc.src.math.sincos
99+
)

libc/fuzzing/math/sincos_fuzz.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- sincos_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 cos implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/sincos.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 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;
24+
mpfr_t input;
25+
mpfr_t sin_x;
26+
mpfr_t cos_x;
27+
28+
mpfr_init2(input, 53);
29+
mpfr_init2(sin_x, 53);
30+
mpfr_init2(cos_x, 53);
31+
32+
mpfr_set_d(input, x, MPFR_RNDN);
33+
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+
38+
double to_compare_sin = mpfr_get_d(sin_x, MPFR_RNDN);
39+
double to_compare_cos = mpfr_get_d(cos_x, MPFR_RNDN);
40+
41+
double sin_res, cos_res;
42+
LIBC_NAMESPACE::sincos(x, &sin_res, &cos_res);
43+
44+
if (sin_res != to_compare_sin || cos_res != to_compare_cos)
45+
__builtin_trap();
46+
47+
mpfr_clear(input);
48+
mpfr_clear(sin_x);
49+
mpfr_clear(cos_x);
50+
return 0;
51+
}

0 commit comments

Comments
 (0)