| 1 |
//===-- vfabi-demangler-fuzzer.cpp - Fuzzer VFABI using lib/Fuzzer ------===// |
| 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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Build tool to fuzz the demangler for the vector function ABI names. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "llvm/AsmParser/Parser.h" |
| 14 |
#include "llvm/IR/Module.h" |
| 15 |
#include "llvm/IR/VFABIDemangler.h" |
| 16 |
#include "llvm/Support/SourceMgr.h" |
| 17 |
|
| 18 |
using namespace llvm; |
| 19 |
|
| 20 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
| 21 |
LLVMContext Ctx; |
| 22 |
SMDiagnostic Err; |
| 23 |
const std::unique_ptr<Module> M = |
| 24 |
parseAssemblyString("declare i32 @foo(i32 )\n", Err, Ctx); |
| 25 |
const StringRef MangledName((const char *)Data, Size); |
| 26 |
// Make sure that whatever symbol the demangler is operating on is |
| 27 |
// present in the module (the signature is not important). This is |
| 28 |
// because `tryDemangleForVFABI` fails if the function is not |
| 29 |
// present. We need to make sure we can even invoke |
| 30 |
// `getOrInsertFunction` because such method asserts on strings with |
| 31 |
// zeroes. |
| 32 |
// TODO: What is this actually testing? That we don't crash? |
| 33 |
if (!MangledName.empty() && MangledName.find_first_of(0) == StringRef::npos) { |
| 34 |
FunctionType *FTy = |
| 35 |
FunctionType::get(Type::getVoidTy(M->getContext()), false); |
| 36 |
const auto Info = VFABI::tryDemangleForVFABI(MangledName, FTy); |
| 37 |
|
| 38 |
// Do not optimize away the return value. Inspired by |
| 39 |
// https://github.com/google/benchmark/blob/main/include/benchmark/benchmark.h#L307-L345 |
| 40 |
asm volatile("" : : "r,m"(Info) : "memory"); |
| 41 |
} |
| 42 |
|
| 43 |
return 0; |
| 44 |
} |