Skip to content

Commit

Permalink
Recognize strings referenced without GEP
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert authored and Ninja3047 committed Oct 6, 2022
1 parent b67c04c commit 40f3c5f
Showing 1 changed file with 35 additions and 27 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

0 comments on commit 40f3c5f

Please sign in to comment.