Skip to content

[libc++][format] adds a basic fuzzer test. #87883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

mordante
Copy link
Member

@mordante mordante commented Apr 6, 2024

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.

@mordante mordante requested a review from a team as a code owner April 6, 2024 17:28
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 6, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 6, 2024

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

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 --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;
+}

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.
@mordante mordante force-pushed the review/format_fuzzing_test branch from 448eaca to e2a24e3 Compare April 7, 2024 15:54
@mordante mordante merged commit 1381645 into llvm:main Apr 9, 2024
@mordante mordante deleted the review/format_fuzzing_test branch April 9, 2024 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants