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/Analysis/VectorUtils.h"
14 #include "llvm/AsmParser/Parser.h"
15 
16 using namespace llvm;
17 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)18 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
19   LLVMContext Ctx;
20   SMDiagnostic Err;
21   const std::unique_ptr<Module> M =
22       parseAssemblyString("declare i32 @foo(i32 )\n", Err, Ctx);
23   const StringRef MangledName((const char *)Data, Size);
24   // Make sure that whatever symbol the demangler is operating on is
25   // present in the module (the signature is not important). This is
26   // because `tryDemangleForVFABI` fails if the function is not
27   // present. We need to make sure we can even invoke
28   // `getOrInsertFunction` because such method asserts on strings with
29   // zeroes.
30   if (!MangledName.empty() && MangledName.find_first_of(0) == StringRef::npos)
31     M->getOrInsertFunction(
32         MangledName,
33         FunctionType::get(Type::getVoidTy(M->getContext()), false));
34   const auto Info = VFABI::tryDemangleForVFABI(MangledName, *M);
35 
36   // Do not optimize away the return value. Inspired by
37   // https://github.com/google/benchmark/blob/master/include/benchmark/benchmark.h#L307-L345
38   asm volatile("" : : "r,m"(Info) : "memory");
39 
40   return 0;
41 }
42