45 lines · cpp
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-exception6//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 18using namespace llvm;19 20extern "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 is27 // present in the module (the signature is not important). This is28 // because `tryDemangleForVFABI` fails if the function is not29 // present. We need to make sure we can even invoke30 // `getOrInsertFunction` because such method asserts on strings with31 // 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 by39 // https://github.com/google/benchmark/blob/main/include/benchmark/benchmark.h#L307-L34540 asm volatile("" : : "r,m"(Info) : "memory");41 }42 43 return 0;44}45