[libc-commits] [libc] 892ed1f - [libc] Add differential fuzzers for ldexp and remquo.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Thu Nov 19 08:32:28 PST 2020


Author: Siva Chandra Reddy
Date: 2020-11-19T08:32:05-08:00
New Revision: 892ed1f15076251dd94d1e4783033d34ed69a3ed

URL: https://github.com/llvm/llvm-project/commit/892ed1f15076251dd94d1e4783033d34ed69a3ed
DIFF: https://github.com/llvm/llvm-project/commit/892ed1f15076251dd94d1e4783033d34ed69a3ed.diff

LOG: [libc] Add differential fuzzers for ldexp and remquo.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D91763

Added: 
    libc/fuzzing/math/CMakeLists.txt
    libc/fuzzing/math/LdExpDiff.h
    libc/fuzzing/math/RemQuoDiff.h
    libc/fuzzing/math/ldexp_differential_fuzz.cpp
    libc/fuzzing/math/ldexpf_differential_fuzz.cpp
    libc/fuzzing/math/ldexpl_differential_fuzz.cpp
    libc/fuzzing/math/remquo_differential_fuzz.cpp
    libc/fuzzing/math/remquof_differential_fuzz.cpp
    libc/fuzzing/math/remquol_differential_fuzz.cpp

Modified: 
    libc/fuzzing/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/fuzzing/CMakeLists.txt b/libc/fuzzing/CMakeLists.txt
index 1b950ad9b721..5d8922bcc22b 100644
--- a/libc/fuzzing/CMakeLists.txt
+++ b/libc/fuzzing/CMakeLists.txt
@@ -2,4 +2,5 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer")
 add_custom_target(libc-fuzzer)
 add_dependencies(check-libc libc-fuzzer)
 
+add_subdirectory(math)
 add_subdirectory(string)

diff  --git a/libc/fuzzing/math/CMakeLists.txt b/libc/fuzzing/math/CMakeLists.txt
new file mode 100644
index 000000000000..147e2c70733d
--- /dev/null
+++ b/libc/fuzzing/math/CMakeLists.txt
@@ -0,0 +1,65 @@
+add_libc_fuzzer(
+  ldexp_
diff erential_fuzz
+  SRCS
+    ldexp_
diff erential_fuzz.cpp
+  HDRS
+    LdExpDiff.h
+  DEPENDS
+    libc.src.math.ldexp
+    libc.utils.FPUtil.fputil
+)
+
+add_libc_fuzzer(
+  ldexpf_
diff erential_fuzz
+  SRCS
+    ldexpf_
diff erential_fuzz.cpp
+  HDRS
+    LdExpDiff.h
+  DEPENDS
+    libc.src.math.ldexpf
+    libc.utils.FPUtil.fputil
+)
+
+add_libc_fuzzer(
+  ldexpl_
diff erential_fuzz
+  SRCS
+    ldexpl_
diff erential_fuzz.cpp
+  HDRS
+    LdExpDiff.h
+  DEPENDS
+    libc.src.math.ldexpl
+    libc.utils.FPUtil.fputil
+)
+
+add_libc_fuzzer(
+  remquo_
diff erential_fuzz
+  SRCS
+    remquo_
diff erential_fuzz.cpp
+  HDRS
+    RemQuoDiff.h
+  DEPENDS
+    libc.src.math.remquo
+    libc.utils.FPUtil.fputil
+)
+
+add_libc_fuzzer(
+  remquof_
diff erential_fuzz
+  SRCS
+    remquof_
diff erential_fuzz.cpp
+  HDRS
+    RemQuoDiff.h
+  DEPENDS
+    libc.src.math.remquof
+    libc.utils.FPUtil.fputil
+)
+
+add_libc_fuzzer(
+  remquol_
diff erential_fuzz
+  SRCS
+    remquol_
diff erential_fuzz.cpp
+  HDRS
+    RemQuoDiff.h
+  DEPENDS
+    libc.src.math.remquol
+    libc.utils.FPUtil.fputil
+)

diff  --git a/libc/fuzzing/math/LdExpDiff.h b/libc/fuzzing/math/LdExpDiff.h
new file mode 100644
index 000000000000..403429ff5c71
--- /dev/null
+++ b/libc/fuzzing/math/LdExpDiff.h
@@ -0,0 +1,44 @@
+//===-- Template for 
diff ing ldexp results ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "utils/FPUtil/FPBits.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+template <typename T> using LdExpFunc = T (*)(T, int);
+
+template <typename T>
+void LdExpDiff(LdExpFunc<T> func1, LdExpFunc<T> func2, const uint8_t *data,
+               size_t size) {
+  constexpr size_t typeSize = sizeof(T);
+  if (size < typeSize + sizeof(int))
+    return;
+
+  T x = *reinterpret_cast<const T *>(data);
+  T i = *reinterpret_cast<const int *>(data + typeSize);
+
+  T result1 = func1(x, i);
+  T result2 = func2(x, i);
+  if (isnan(result1)) {
+    if (!isnan(result2))
+      __builtin_trap();
+    return;
+  }
+  if (isinf(result1)) {
+    if (isinf(result2) != isinf(result1))
+      __builtin_trap();
+    return;
+  }
+
+  __llvm_libc::fputil::FPBits<T> bits1(result1);
+  __llvm_libc::fputil::FPBits<T> bits2(result2);
+  if (bits1.bitsAsUInt() != bits2.bitsAsUInt())
+    __builtin_trap();
+}

diff  --git a/libc/fuzzing/math/RemQuoDiff.h b/libc/fuzzing/math/RemQuoDiff.h
new file mode 100644
index 000000000000..c0091a4f65de
--- /dev/null
+++ b/libc/fuzzing/math/RemQuoDiff.h
@@ -0,0 +1,50 @@
+//===-- Template for 
diff ing remquo results ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "utils/FPUtil/FPBits.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+template <typename T> using RemQuoFunc = T (*)(T, T, int *);
+
+template <typename T>
+void RemQuoDiff(RemQuoFunc<T> func1, RemQuoFunc<T> func2, const uint8_t *data,
+                size_t size) {
+  constexpr size_t typeSize = sizeof(T);
+  if (size < 2 * typeSize)
+    return;
+
+  T x = *reinterpret_cast<const T *>(data);
+  T y = *reinterpret_cast<const T *>(data + typeSize);
+
+  int q1, q2;
+  T remainder1 = func1(x, y, &q1);
+  T remainder2 = func2(x, y, &q2);
+
+  if (isnan(remainder1)) {
+    if (!isnan(remainder2))
+      __builtin_trap();
+    return;
+  }
+  if (isinf(remainder1)) {
+    if (isinf(remainder2) != isinf(remainder1))
+      __builtin_trap();
+    return;
+  }
+
+  // Compare only the 3 LS bits of the quotient.
+  if ((q1 & 0x7) != (q2 & 0x7))
+    __builtin_trap();
+
+  __llvm_libc::fputil::FPBits<T> bits1(remainder1);
+  __llvm_libc::fputil::FPBits<T> bits2(remainder2);
+  if (bits1.bitsAsUInt() != bits2.bitsAsUInt())
+    __builtin_trap();
+}

diff  --git a/libc/fuzzing/math/ldexp_
diff erential_fuzz.cpp b/libc/fuzzing/math/ldexp_
diff erential_fuzz.cpp
new file mode 100644
index 000000000000..573625f21481
--- /dev/null
+++ b/libc/fuzzing/math/ldexp_
diff erential_fuzz.cpp
@@ -0,0 +1,23 @@
+//===-- ldexp_
diff erential_fuzz.cpp ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Differential fuzz test for llvm-libc ldexp implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzing/math/LdExpDiff.h"
+#include "src/math/ldexp.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+  LdExpDiff<double>(&__llvm_libc::ldexp, &::ldexp, data, size);
+  return 0;
+}

diff  --git a/libc/fuzzing/math/ldexpf_
diff erential_fuzz.cpp b/libc/fuzzing/math/ldexpf_
diff erential_fuzz.cpp
new file mode 100644
index 000000000000..fc8d5c96a570
--- /dev/null
+++ b/libc/fuzzing/math/ldexpf_
diff erential_fuzz.cpp
@@ -0,0 +1,23 @@
+//===-- ldexpf_
diff erential_fuzz.cpp --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Differential fuzz test for llvm-libc ldexpf implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzing/math/LdExpDiff.h"
+#include "src/math/ldexpf.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+  LdExpDiff<float>(&__llvm_libc::ldexpf, &::ldexpf, data, size);
+  return 0;
+}

diff  --git a/libc/fuzzing/math/ldexpl_
diff erential_fuzz.cpp b/libc/fuzzing/math/ldexpl_
diff erential_fuzz.cpp
new file mode 100644
index 000000000000..e042fa972884
--- /dev/null
+++ b/libc/fuzzing/math/ldexpl_
diff erential_fuzz.cpp
@@ -0,0 +1,23 @@
+//===-- ldexpl_
diff erential_fuzz.cpp --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Differential fuzz test for llvm-libc ldexpl implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzing/math/LdExpDiff.h"
+#include "src/math/ldexpl.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+  LdExpDiff<long double>(&__llvm_libc::ldexpl, &::ldexpl, data, size);
+  return 0;
+}

diff  --git a/libc/fuzzing/math/remquo_
diff erential_fuzz.cpp b/libc/fuzzing/math/remquo_
diff erential_fuzz.cpp
new file mode 100644
index 000000000000..a64c30db5d3d
--- /dev/null
+++ b/libc/fuzzing/math/remquo_
diff erential_fuzz.cpp
@@ -0,0 +1,23 @@
+//===-- remquo_
diff erential_fuzz.cpp ---------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Differential fuzz test for llvm-libc remquo implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzing/math/RemQuoDiff.h"
+#include "src/math/remquo.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+  RemQuoDiff<double>(&__llvm_libc::remquo, &::remquo, data, size);
+  return 0;
+}

diff  --git a/libc/fuzzing/math/remquof_
diff erential_fuzz.cpp b/libc/fuzzing/math/remquof_
diff erential_fuzz.cpp
new file mode 100644
index 000000000000..78ec6ea057b7
--- /dev/null
+++ b/libc/fuzzing/math/remquof_
diff erential_fuzz.cpp
@@ -0,0 +1,23 @@
+//===-- remquof_
diff erential_fuzz.cpp --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Differential fuzz test for llvm-libc remquof implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzing/math/RemQuoDiff.h"
+#include "src/math/remquof.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+  RemQuoDiff<float>(&__llvm_libc::remquof, &::remquof, data, size);
+  return 0;
+}

diff  --git a/libc/fuzzing/math/remquol_
diff erential_fuzz.cpp b/libc/fuzzing/math/remquol_
diff erential_fuzz.cpp
new file mode 100644
index 000000000000..ab25fa196631
--- /dev/null
+++ b/libc/fuzzing/math/remquol_
diff erential_fuzz.cpp
@@ -0,0 +1,23 @@
+//===-- remquol_
diff erential_fuzz.cpp --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// Differential fuzz test for llvm-libc remquol implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzing/math/RemQuoDiff.h"
+#include "src/math/remquol.h"
+
+#include <math.h>
+#include <stddef.h>
+#include <stdint.h>
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+  RemQuoDiff<long double>(&__llvm_libc::remquol, &::remquol, data, size);
+  return 0;
+}


        


More information about the libc-commits mailing list