51 lines · cpp
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-exception6//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 20extern "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 range28 if (isnan(x) || isinf(x) || x < -1)29 continue;30 // signed zeros already tested in unit tests31 if (signbit(x) && x == 0.0)32 continue;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}51