Skip to content

Commit

Permalink
Silence clang warn (#305)
Browse files Browse the repository at this point in the history
* Bring `ast_unit`, `ast_ctx`, `ast` into `dec_ctx`

* Move `GetQualType` to `DecompilationContext`

* Add type provider

* Add type provider for `main`

* Update copyright

* Revert formatting change

* Emit non-signed `char`s

* Allow adding type providers from decomp API

* Fix merge error

* More merge errors

* silence clang warning for arm

* Recognize strings referenced without GEP

* replace function if address is not taken

* rename replacement function:

Co-authored-by: Francesco Bertolaccini <[email protected]>
Co-authored-by: William Tan <[email protected]>
Co-authored-by: 2over12 <[email protected]>
  • Loading branch information
4 people authored Oct 25, 2022
1 parent 59b4751 commit d51bed7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
62 changes: 35 additions & 27 deletions lib/AST/IRToASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,42 @@ void ExprGen::VisitGlobalVar(llvm::GlobalVariable &gvar) {
}
}

static bool IsGVarAString(llvm::GlobalVariable *gvar) {
if (!gvar->hasInitializer()) {
return false;
}

auto constant{gvar->getInitializer()};
// Check if constant can be considered a string literal
auto arr_type{llvm::dyn_cast<llvm::ArrayType>(constant->getType())};
if (!arr_type) {
return false;
}

auto elm_type{arr_type->getElementType()};
if (!elm_type->isIntegerTy(8U)) {
return false;
}

auto arr{llvm::dyn_cast<llvm::ConstantDataArray>(constant)};
if (!arr) {
return false;
}

auto init{arr->getAsString().str()};
if (init.find('\0') != init.size() - 1) {
return false;
}

return true;
}

clang::Expr *ExprGen::CreateConstantExpr(llvm::Constant *constant) {
if (auto gvar = llvm::dyn_cast<llvm::GlobalVariable>(constant)) {
if (IsGVarAString(gvar)) {
auto arr{llvm::cast<llvm::ConstantDataArray>(gvar->getInitializer())};
return ast.CreateStrLit(arr->getAsString().str().c_str());
}
VisitGlobalVar(*gvar);
}

Expand Down Expand Up @@ -518,33 +552,7 @@ clang::Expr *ExprGen::visitGetElementPtrInst(llvm::GetElementPtrInst &inst) {
return false;
}

if (!gvar->hasInitializer()) {
return false;
}

auto constant{gvar->getInitializer()};
// Check if constant can be considered a string literal
auto arr_type{llvm::dyn_cast<llvm::ArrayType>(constant->getType())};
if (!arr_type) {
return false;
}

auto elm_type{arr_type->getElementType()};
if (!elm_type->isIntegerTy(8U)) {
return false;
}

auto arr{llvm::dyn_cast<llvm::ConstantDataArray>(constant)};
if (!arr) {
return false;
}

auto init{arr->getAsString().str()};
if (init.find('\0') != init.size() - 1) {
return false;
}

return true;
return IsGVarAString(gvar);
};

// Maybe we're inspecting a string reference
Expand Down
19 changes: 13 additions & 6 deletions lib/BC/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void ConvertArrayArguments(llvm::Module &m) {
std::unordered_map<llvm::Type *, llvm::Type *> conv_types;
std::vector<unsigned> indices;
indices.push_back(0);
std::vector<llvm::Function *> funcs_to_remove;
std::vector<std::pair<llvm::Function *, llvm::Function *>> funcs_to_remove;
auto &ctx{m.getContext()};
auto ConvertType = [&](llvm::Type *t) -> llvm::Type * {
if (!t->isArrayTy()) {
Expand Down Expand Up @@ -364,7 +364,7 @@ void ConvertArrayArguments(llvm::Module &m) {
ret->eraseFromParent();
}
}
funcs_to_remove.push_back(orig_func);
funcs_to_remove.push_back(std::make_pair(orig_func, new_func));
return new_func;
};

Expand Down Expand Up @@ -425,12 +425,19 @@ void ConvertArrayArguments(llvm::Module &m) {
}
}

for (auto func : funcs_to_remove) {
for (auto [func_to_remove, replacement] : funcs_to_remove) {
// TODO(frabert): Sometimes uses stick around which are not calls (e.g.
// references in globals). How do we replace those? Cannot use
// `func->replaceAllUsesWith` because types don't match.
if (func->use_empty()) {
func->eraseFromParent();
// `func->replaceAllUsesWith` because types don't match
const llvm::User *user;
if (!func_to_remove->hasAddressTaken(&user, false, false, true, false)) {
auto orig_name = func_to_remove->getName().str();
func_to_remove->replaceAllUsesWith(replacement);
func_to_remove->eraseFromParent();
replacement->setName(orig_name);
} else {
DLOG(ERROR) << "Keeping around old array function: "
<< func_to_remove->getName().str();
}
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Decompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Result<DecompilationResult, DecompilationError> Decompile(
std::vector<std::string> args{"-Wno-pointer-to-int-cast",
"-Wno-pointer-sign", "-target",
module->getTargetTriple()};
// Silence clang warning
// warning: unknown platform, assumming -mfloat-abi=soft
const auto& triple{llvm::Triple(module->getTargetTriple())};
if (triple.isARM()) {
args.push_back("-mfloat-abi=soft");
}
auto ast_unit{clang::tooling::buildASTFromCodeWithArgs("", args, "out.c")};
rellic::DecompilationContext dec_ctx(*ast_unit);

Expand Down Expand Up @@ -167,4 +173,4 @@ Result<DecompilationResult, DecompilationError> Decompile(
return Result<DecompilationResult, DecompilationError>(std::move(error));
}
}
} // namespace rellic
} // namespace rellic

0 comments on commit d51bed7

Please sign in to comment.