Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence clang warn #305

Merged
merged 16 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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