Skip to content

Commit

Permalink
Add dec2hex tool (#307)
Browse files Browse the repository at this point in the history
* Add `dec2hex` tool

* Turn it into an API

* Print integers as hex in `rellic-xref`
  • Loading branch information
frabert authored Nov 9, 2022
1 parent cda2f22 commit 0167037
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 1 deletion.
29 changes: 29 additions & 0 deletions include/rellic/Dec2Hex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2021-present, Trail of Bits, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/

#pragma once

#include <clang/AST/ASTContext.h>
#include <llvm/ADT/APInt.h>
#include <llvm/Support/raw_ostream.h>

#include <functional>
#include <memory>

namespace rellic {
// Converts integer literals in `ast_ctx` to hexadecimal form when
// `shouldConvert` return true.
//
// Note: the context should have valid source range information, i.e. cannot be
// the one directly generated by Rellic. To obtain a context with valid source
// range information, serialize the AST to a string and parse it back into a
// context.
void ConvertIntegerLiteralsToHex(
clang::ASTContext& ast_ctx, llvm::raw_ostream& os,
std::function<bool(const llvm::APInt&)> shouldConvert);
} // namespace rellic
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ add_library("${PROJECT_NAME}" STATIC
${AST_SOURCES}
${BC_SOURCES}

Dec2Hex.cpp
Decompiler.cpp
Exception.cpp

Expand Down
98 changes: 98 additions & 0 deletions lib/Dec2Hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2022-present, Trail of Bits, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/

#include <clang/AST/Expr.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Rewrite/Core/Rewriter.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>

namespace rellic {
namespace {
using namespace clang;
using namespace clang::ast_matchers;

StatementMatcher intlit = integerLiteral().bind("intlit");

class IntegerReplacer : public MatchFinder::MatchCallback {
Rewriter &rw;
std::function<bool(const llvm::APInt &)> shouldConvert;

public:
IntegerReplacer(Rewriter &rw,
std::function<bool(const llvm::APInt &)> shouldConvert)
: rw(rw), shouldConvert(shouldConvert) {}
virtual void run(const MatchFinder::MatchResult &Result) {
if (auto lit = Result.Nodes.getNodeAs<IntegerLiteral>("intlit")) {
if (!shouldConvert(lit->getValue())) {
return;
}

llvm::SmallString<40> str;
lit->getValue().toString(
str, /*radix=*/16, /*isSigned=*/lit->getType()->isSignedIntegerType(),
/*formatAsCLiteral=*/true);
std::string res;
llvm::raw_string_ostream OS(res);
OS << str;
switch (lit->getType()->castAs<BuiltinType>()->getKind()) {
default:
llvm_unreachable("Unexpected type for integer literal!");
case BuiltinType::Char_S:
case BuiltinType::Char_U:
OS << "i8";
break;
case BuiltinType::UChar:
OS << "Ui8";
break;
case BuiltinType::Short:
OS << "i16";
break;
case BuiltinType::UShort:
OS << "Ui16";
break;
case BuiltinType::Int:
break; // no suffix.
case BuiltinType::UInt:
OS << 'U';
break;
case BuiltinType::Long:
OS << 'L';
break;
case BuiltinType::ULong:
OS << "UL";
break;
case BuiltinType::LongLong:
OS << "LL";
break;
case BuiltinType::ULongLong:
OS << "ULL";
break;
}
rw.ReplaceText(lit->getSourceRange(), res);
}
}
};
} // namespace

void ConvertIntegerLiteralsToHex(
clang::ASTContext &ast_ctx, llvm::raw_ostream &os,
std::function<bool(const llvm::APInt &)> shouldConvert) {
auto &sm{ast_ctx.getSourceManager()};
clang::Rewriter rewriter{sm, ast_ctx.getLangOpts()};
clang::ast_matchers::MatchFinder finder;
IntegerReplacer replacer{rewriter, shouldConvert};

finder.addMatcher(intlit, &replacer);
finder.matchAST(ast_ctx);
rewriter.getRewriteBufferFor(sm.getMainFileID())->write(os);
}
} // namespace rellic
18 changes: 18 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ target_link_libraries(${RELLIC_REPL}

set(RELLIC_REPL "${RELLIC_REPL}" PARENT_SCOPE)

#
# rellic-dec2hex
#
set(RELLIC_DEC2HEX "${PROJECT_NAME}-dec2hex")

add_executable(${RELLIC_DEC2HEX}
"dec2hex/dec2hex.cpp"
)

target_link_libraries(${RELLIC_DEC2HEX}
PRIVATE
"${PROJECT_NAME}_cxx_settings"
"${PROJECT_NAME}"
gflags::gflags
)

set(RELLIC_DEC2HEX "${RELLIC_DEC2HEX}" PARENT_SCOPE)

if(RELLIC_ENABLE_INSTALL)

install(
Expand Down
65 changes: 65 additions & 0 deletions tools/dec2hex/dec2hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2022-present, Trail of Bits, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/

#include <clang/Tooling/Tooling.h>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include <llvm/ADT/APInt.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <rellic/Dec2Hex.h>

#include <iostream>
#include <sstream>
#include <system_error>

DEFINE_string(input, "-", "Input C file.");
DEFINE_string(output, "", "Output C file.");

int main(int argc, char *argv[]) {
std::stringstream usage;
usage << std::endl
<< std::endl
<< " " << argv[0] << " \\" << std::endl
<< " --input INPUT_C_FILE \\" << std::endl
<< " --output OUTPUT_C_FILE \\" << std::endl
<< std::endl;

google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
google::SetUsageMessage(usage.str());
google::ParseCommandLineFlags(&argc, &argv, true);

auto input_file = llvm::MemoryBuffer::getFileOrSTDIN(FLAGS_input);
if (!input_file) {
LOG(FATAL) << input_file.getError().message();
}
auto ast_unit{clang::tooling::buildASTFromCodeWithArgs(
input_file.get()->getBuffer(), {}, FLAGS_input, "rellic-dec2hex")};
auto &ast_ctx{ast_unit->getASTContext()};

auto heuristic = [](const llvm::APInt &value) {
return value.getZExtValue() >= 16;
};

if (FLAGS_output.empty()) {
rellic::ConvertIntegerLiteralsToHex(ast_ctx, llvm::outs(), heuristic);
} else {
std::error_code ec;
llvm::raw_fd_ostream os(FLAGS_output, ec);
if (ec) {
LOG(FATAL) << ec.message();
}
rellic::ConvertIntegerLiteralsToHex(ast_ctx, os, heuristic);
}

google::ShutDownCommandLineFlags();
google::ShutdownGoogleLogging();

return EXIT_SUCCESS;
}
6 changes: 5 additions & 1 deletion tools/xref/StmtPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,11 @@ void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
return;
OS << "<span class=\"clang number integer-literal\">";
bool isSigned = Node->getType()->isSignedIntegerType();
OS << toString(Node->getValue(), 10, isSigned);
if (Node->getValue().getZExtValue() < 16) {
OS << toString(Node->getValue(), 10, isSigned);
} else {
OS << toString(Node->getValue(), 16, isSigned, /*formatAsCLiteral=*/true);
}

// Emit suffixes. Integer literals are always a builtin integer type.
switch (Node->getType()->castAs<BuiltinType>()->getKind()) {
Expand Down

0 comments on commit 0167037

Please sign in to comment.