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

Fix handling of undef integer values. #158

Merged
merged 5 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion rellic/AST/ASTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ clang::Expr *ASTBuilder::CreateNull() {
return CreateCStyleCast(ctx.VoidPtrTy, lit);
};

clang::Expr *ASTBuilder::CreateUndef(clang::QualType type) {
clang::Expr *ASTBuilder::CreateUndefInteger(clang::QualType type) {
auto val{llvm::APInt::getNullValue(ctx.getTypeSize(type))};
auto lit{CreateIntLit(val)};
return lit;
};

clang::Expr *ASTBuilder::CreateUndefPointer(clang::QualType type) {
auto null{CreateNull()};
auto cast{CreateCStyleCast(ctx.getPointerType(type), null)};
return CreateDeref(cast);
Expand Down
3 changes: 2 additions & 1 deletion rellic/AST/ASTBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class ASTBuilder {
};
// Special values
clang::Expr *CreateNull();
clang::Expr *CreateUndef(clang::QualType type);
clang::Expr *CreateUndefPointer(clang::QualType type);
clang::Expr *CreateUndefInteger(clang::QualType type);
// Identifiers
clang::IdentifierInfo *CreateIdentifier(std::string name);
// Variable declaration
Expand Down
17 changes: 12 additions & 5 deletions rellic/AST/IRToASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,26 @@ clang::Expr *IRToASTVisitor::CreateLiteralExpr(llvm::Constant *constant) {
} break;
// Integers
case llvm::Type::IntegerTyID: {
auto val{llvm::cast<llvm::ConstantInt>(constant)->getValue()};
if (val.getBitWidth() == 1U) {
result = ast.CreateIntLit(val);
if (llvm::isa<llvm::ConstantInt>(constant)) {
auto ci = llvm::cast<llvm::ConstantInt>(constant);
auto val{ci->getValue()};
if (val.getBitWidth() == 1U) {
result = ast.CreateIntLit(val);
} else {
result = ast.CreateAdjustedIntLit(val);
}
} else if (llvm::isa<llvm::UndefValue>(constant)) {
result = ast.CreateUndefInteger(c_type);
} else {
result = ast.CreateAdjustedIntLit(val);
LOG(FATAL) << "Unsupported integer constant";
}
} break;

case llvm::Type::PointerTyID: {
if (llvm::isa<llvm::ConstantPointerNull>(constant)) {
result = ast.CreateNull();
} else if (llvm::isa<llvm::UndefValue>(constant)) {
result = ast.CreateUndef(c_type);
result = ast.CreateUndefPointer(c_type);
} else {
LOG(FATAL) << "Unsupported pointer constant";
}
Expand Down
22 changes: 20 additions & 2 deletions unittests/AST/ASTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,25 @@ TEST_SUITE("ASTBuilder::CreateNull") {
}
}

TEST_SUITE("ASTBuilder::CreateUndef") {
TEST_SUITE("ASTBuilder::CreateUndefInteger") {
artemdinaburg marked this conversation as resolved.
Show resolved Hide resolved
SCENARIO("Create clang::Expr to stand in for an undefined integer") {
GIVEN("Empty clang::ASTContext") {
auto unit{GetASTUnit()};
auto &ctx{unit->getASTContext()};
rellic::ASTBuilder ast(*unit);
GIVEN("an unsigned integer type") {
auto type{ctx.UnsignedIntTy};
THEN("return a value that satisfied LLVM's undef semantics (aka anything)") {
auto expr{ast.CreateUndefInteger(type)};
REQUIRE(expr != nullptr);
CHECK(expr->getType() == ctx.UnsignedIntTy);
}
}
}
}
}

TEST_SUITE("ASTBuilder::CreateUndefPointer") {
SCENARIO("Create clang::Expr whose value is undefined") {
GIVEN("Empty clang::ASTContext") {
auto unit{GetASTUnit()};
Expand All @@ -276,7 +294,7 @@ TEST_SUITE("ASTBuilder::CreateUndef") {
GIVEN("an arbitrary type t") {
auto type{ctx.DoubleTy};
THEN("return a null pointer dereference of type t") {
auto expr{ast.CreateUndef(type)};
auto expr{ast.CreateUndefPointer(type)};
REQUIRE(expr != nullptr);
CHECK(expr->getType() == ctx.DoubleTy);
auto deref{clang::dyn_cast<clang::UnaryOperator>(expr)};
Expand Down