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

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Sat Apr 6 10:28:17 PDT 2024


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

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.

>From 448eaca1f76a249fedf064423282afac0a83f859 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Sat, 6 Apr 2024 19:25:04 +0200
Subject: [PATCH] [libc++][format] adds a basic fuzzer test.

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.
---
 .../libcxx/fuzzing/format_no_args.pass.cpp    | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 libcxx/test/libcxx/fuzzing/format_no_args.pass.cpp

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



More information about the libcxx-commits mailing list