[libcxx-commits] [libcxx] [libc++][format] adds a basic fuzzer test. (PR #87883)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Apr 6 10:28:49 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

<details>
<summary>Changes</summary>

This adds an initial fuzzer. Different formatting arguments will execute different code paths. This will be tested by different fuzzer tests.

The code is based on a sample provided by Louis.

---
Full diff: https://github.com/llvm/llvm-project/pull/87883.diff


1 Files Affected:

- (added) libcxx/test/libcxx/fuzzing/format_no_args.pass.cpp (+26) 


``````````diff
diff --git a/libcxx/test/libcxx/fuzzing/format_no_args.pass.cpp b/libcxx/test/libcxx/fuzzing/format_no_args.pass.cpp
new file mode 100644
index 00000000000000..32e468eb54fc6c
--- /dev/null
+++ b/libcxx/test/libcxx/fuzzing/format_no_args.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: no-exceptions
+
+#include <cstdint>
+#include <format>
+#include <string_view>
+
+#include "fuzz.h"
+
+extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
+  try {
+    [[maybe_unused]] auto result = std::vformat(std::string_view{(const char*)(data), size}, std::make_format_args());
+  } catch (std::format_error const&) {
+    // If the fuzzing input isn't a valid thing we can format and we detect it, it's okay. We are looking for crashes.
+    return 0;
+  }
+  return 0;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/87883


More information about the libcxx-commits mailing list