Skip to content

Commit

Permalink
llvm-undname: Add a -raw-file flag to pass a raw buffer to microsoftD…
Browse files Browse the repository at this point in the history
…emangle

The default handling splits input into lines. Since
llvm-microsoft-demangle-fuzzer doesn't do this, oss-fuzz produces inputs
that only trigger crashes if the input isn't split into lines. This adds
a hidden flag -raw-file which passes file contents to microsoftDemangle() in
the same way the fuzzer does, for reproducing oss-fuzz reports.

Also change llvm-undname to have a non-0 exit code for invalid symbols.

Differential Revision: https://reviews.llvm.org/D60771

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358485 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
nico committed Apr 16, 2019
1 parent a2389c7 commit b06fa34
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tools/llvm-undname/llvm-undname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
Expand All @@ -29,10 +31,12 @@ using namespace llvm;
cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
cl::desc("dump backreferences"), cl::Hidden,
cl::init(false));
cl::opt<std::string> RawFile("raw-file", cl::Optional,
cl::desc("for fuzzer data"), cl::Hidden);
cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
cl::ZeroOrMore);

static void msDemangle(const std::string &S) {
static bool msDemangle(const std::string &S) {
int Status;
MSDemangleFlags Flags = MSDF_None;
if (DumpBackReferences)
Expand All @@ -47,13 +51,26 @@ static void msDemangle(const std::string &S) {
WithColor::error() << "Invalid mangled name\n";
}
std::free(ResultBuf);
return Status == llvm::demangle_success;
}

int main(int argc, char **argv) {
InitLLVM X(argc, argv);

cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");

if (!RawFile.empty()) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(RawFile);
if (std::error_code EC = FileOrErr.getError()) {
WithColor::error() << "Could not open input file \'" << RawFile
<< "\': " << EC.message() << '\n';
return 1;
}
return msDemangle(FileOrErr->get()->getBuffer()) ? 0 : 1;
}

bool Success = true;
if (Symbols.empty()) {
while (true) {
std::string LineStr;
Expand All @@ -74,17 +91,19 @@ int main(int argc, char **argv) {
outs() << Line << "\n";
outs().flush();
}
msDemangle(Line);
if (!msDemangle(Line))
Success = false;
outs() << "\n";
}
} else {
for (StringRef S : Symbols) {
outs() << S << "\n";
outs().flush();
msDemangle(S);
if (!msDemangle(S))
Success = false;
outs() << "\n";
}
}

return 0;
return Success ? 0 : 1;
}

0 comments on commit b06fa34

Please sign in to comment.