Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Misc fixes #42

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
18 changes: 8 additions & 10 deletions fcd/ast/ast_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,16 @@ class InstToExpr : public llvm::InstVisitor<InstToExpr, Expression*>
{
if (auto constantInt = dyn_cast<ConstantInt>(&constant))
{
assert(constantInt->getValue().ule(numeric_limits<uint64_t>::max()));
return ctx.numeric(ctx.getIntegerType(false, (unsigned short)constantInt->getBitWidth()), constantInt->getLimitedValue());
assert(constantInt->getBitWidth() <= numeric_limits<unsigned short>::max());
return ctx.numeric(ctx.getIntegerType(false, (unsigned short)constantInt->getBitWidth()), constantInt->getValue());
}

if (auto expression = dyn_cast<ConstantExpr>(&constant))
{
return ctx.uncachedExpressionFor(*expression->getAsInstruction());
auto inst = expression->getAsInstruction();
auto res = ctx.uncachedExpressionFor(*inst);
inst->deleteValue();
return res;
}

if (auto structure = dyn_cast<ConstantStruct>(&constant))
Expand Down Expand Up @@ -311,13 +314,8 @@ class InstToExpr : public llvm::InstVisitor<InstToExpr, Expression*>
{
// special case for a + -const
const auto& type = constant->getExpressionType(ctx);
unsigned idleBits = 64 - type.getBits();
int64_t signedValue = (constant->si64 << idleBits) >> idleBits;
if (signedValue < 0)
{
// I'm pretty sure that we don't need to check for the minimum value for that type
// since a + INT_MIN is the same as a - INT_MIN.
auto positiveRight = ctx.numeric(type, static_cast<uint64_t>(-signedValue));
if (constant->value.isNegative()) {
auto positiveRight = ctx.numeric(type, -constant->value);
return ctx.nary(NAryOperatorExpression::Subtract, left, positiveRight);
}
}
Expand Down
2 changes: 1 addition & 1 deletion fcd/ast/ast_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class AstContext
return allocate<true, TernaryExpression>(3, cond, ifTrue, ifFalse);
}

NumericExpression* numeric(const IntegerExpressionType& type, uint64_t ui)
NumericExpression* numeric(const IntegerExpressionType& type, llvm::APInt ui)
{
return allocate<false, NumericExpression>(0, type, ui);
}
Expand Down
2 changes: 1 addition & 1 deletion fcd/ast/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ bool NumericExpression::operator==(const Expression& that) const
{
if (auto token = llvm::dyn_cast<NumericExpression>(&that))
{
return this->ui64 == token->ui64;
return this->value == token->value;
}
return false;
}
Expand Down
20 changes: 6 additions & 14 deletions fcd/ast/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "not_null.h"

#include <llvm/ADT/iterator_range.h>
#include <llvm/ADT/APInt.h>

#include <string>

Expand Down Expand Up @@ -312,29 +313,20 @@ class TernaryExpression final : public Expression
struct NumericExpression final : public Expression
{
const IntegerExpressionType& expressionType;
union
{
int64_t si64;
uint64_t ui64;
};
llvm::APInt value;

static bool classof(const ExpressionUser* node)
{
return node->getUserType() == Numeric;
}

NumericExpression(AstContext& ctx, unsigned uses, const IntegerExpressionType& type, uint64_t ui)
: Expression(Numeric, ctx, uses), expressionType(type), ui64(ui)
{
assert(uses == 0);
}

NumericExpression(AstContext& ctx, unsigned uses, const IntegerExpressionType& type, int64_t si)
: Expression(Numeric, ctx, uses), expressionType(type), si64(si)
NumericExpression(AstContext& ctx, unsigned uses, const IntegerExpressionType& type, llvm::APInt val)
: Expression(Numeric, ctx, uses), expressionType(type), value(val)
{
assert(uses == 0);
assert(value.getBitWidth() == expressionType.getBits());
}

virtual const IntegerExpressionType& getExpressionType(AstContext&) const override { return expressionType; }
virtual bool operator==(const Expression& that) const override;
};
Expand Down
2 changes: 1 addition & 1 deletion fcd/ast/pass_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ bool AstBackEnd::runOnModule(llvm::Module &m)
}

// sort outputNodes by virtual address, then by name
sort(outputNodes.begin(), outputNodes.end(), [](unique_ptr<FunctionNode>& a, unique_ptr<FunctionNode>& b)
std::sort(outputNodes.begin(), outputNodes.end(), [](unique_ptr<FunctionNode>& a, unique_ptr<FunctionNode>& b)
{
auto virtA = getVirtualAddress(*a);
auto virtB = getVirtualAddress(*b);
Expand Down
2 changes: 1 addition & 1 deletion fcd/ast/pass_simplifyexpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ namespace

if (auto addressOf = match(subscript.getPointer(), UnaryOperatorExpression::AddressOf))
if (auto constantIndex = dyn_cast<NumericExpression>(subscript.getIndex()))
if (constantIndex->ui64 == 0)
if (constantIndex->value == 0)
{
subscript.replaceAllUsesWith(addressOf->getOperand());
subscript.dropAllReferences();
Expand Down
6 changes: 3 additions & 3 deletions fcd/ast/pre_ast_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void PreAstContext::generateBlocks(Function& fn)
{
auto bits = static_cast<unsigned short>(caseValue->getType()->getIntegerBitWidth());
const IntegerExpressionType& type = ctx.getIntegerType(false, bits);
Expression* numericConstant = ctx.numeric(type, caseValue->getLimitedValue());
Expression* numericConstant = ctx.numeric(type, caseValue->getValue());
caseCondition = ctx.nary(NAryOperatorExpression::Equal, testVariable, numericConstant);
}
if (dest == &bbRef)
Expand Down Expand Up @@ -210,7 +210,7 @@ PreAstBasicBlock& PreAstContext::createRedirectorBlock(ArrayRef<PreAstBasicBlock
auto iter = caseConditions.find(edge->to);
if (iter == caseConditions.end())
{
Expression* numericConstant = ctx.numeric(ctx.getIntegerType(false, 32), caseConditions.size());
Expression* numericConstant = ctx.numeric(ctx.getIntegerType(false, 32), llvm::APInt(32, caseConditions.size()));
auto condition = ctx.nary(NAryOperatorExpression::Equal, sythesizedVariable, numericConstant);
iter = caseConditions.insert({edge->to, condition}).first;

Expand All @@ -231,4 +231,4 @@ void PreAstContext::view() const
{
ViewGraph(const_cast<PreAstContext*>(this), "Pre-AST Basic Block Graph");
}
#endif
#endif
6 changes: 3 additions & 3 deletions fcd/ast/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ void StatementPrintVisitor::visitNumeric(const NumericExpression& numeric)
// 2- the parent expression is is a bitwise operator and the number is greater than 9.
if (auto nary = dyn_cast_or_null<NAryOperatorExpression>(parentExpression))
{
if (numeric.ui64 > 9)
if (numeric.value.ugt(9))
{
switch (nary->getType())
{
Expand All @@ -461,11 +461,11 @@ void StatementPrintVisitor::visitNumeric(const NumericExpression& numeric)

if (formatAsHex)
{
(os << "0x").write_hex(numeric.ui64);
os << "0x" << numeric.value.toString(16, false);
}
else
{
os << numeric.si64;
os << numeric.value.toString(10, true);
}
}

Expand Down
2 changes: 2 additions & 0 deletions fcd/ast/print_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace
}
}

PrintableItem::~PrintableItem() {}

void PrintableItem::dump() const
{
print(errs(), 0);
Expand Down
1 change: 1 addition & 0 deletions fcd/ast/print_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PrintableItem
PrintableScope* parent;

public:
virtual ~PrintableItem();
PrintableItem(Type type, PrintableScope* parent)
: discriminant(type), parent(parent)
{
Expand Down
9 changes: 5 additions & 4 deletions fcd/codegen/translation_context_remill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <llvm/IR/Verifier.h>

#include <llvm/Analysis/AliasAnalysis.h>
#include <llvm/Transforms/Utils.h>
#include <llvm/Transforms/InstCombine/InstCombine.h>

#include <sstream>
#include <string>
Expand Down Expand Up @@ -277,11 +279,9 @@ RemillTranslationContext::RemillTranslationContext(llvm::LLVMContext &ctx,
target_arch = remill::GetTargetArch();
module = std::unique_ptr<llvm::Module>(remill::LoadTargetSemantics(&ctx));
target_arch->PrepareModule(module);
auto word_type = llvm::Type::getIntNTy(
module->getContext(), static_cast<unsigned>(target_arch->address_size));

intrinsics = std::make_unique<remill::IntrinsicTable>(module.get());
lifter =
std::make_unique<remill::InstructionLifter>(word_type, intrinsics.get());
lifter = std::make_unique<remill::InstructionLifter>(target_arch, intrinsics.get());
}

uint64_t RemillTranslationContext::FindFunctionAddr(llvm::Function *func) {
Expand Down Expand Up @@ -557,6 +557,7 @@ const StubInfo *RemillTranslationContext::GetStubInfo(
if (auto int2ptr = llvm::dyn_cast<llvm::IntToPtrInst>(inst)) {
addr = llvm::dyn_cast<llvm::ConstantInt>(int2ptr->getOperand(0));
}
inst->deleteValue();
} else {
addr = llvm::dyn_cast<llvm::ConstantInt>(read_op);
}
Expand Down
Loading