Skip to content

Commit

Permalink
Moving to standard integer types
Browse files Browse the repository at this point in the history
This is mostly an internal change, with some artifacts being visible.

PiperOrigin-RevId: 342591888
Change-Id: Ifbce44a7bd69be9e00e81cb6a03aa70d74b75122
  • Loading branch information
cblichmann authored and copybara-github committed Nov 16, 2020
1 parent c7d6d28 commit b886b8f
Show file tree
Hide file tree
Showing 28 changed files with 128 additions and 167 deletions.
8 changes: 3 additions & 5 deletions base_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

#include "third_party/zynamics/binexport/base_types.h"

#include <assert.h>
#include <algorithm>
#include <cassert>
#include <cstddef>

namespace {

Expand All @@ -38,9 +39,7 @@ uint32_t BaseType::NextTypeId() {
return type_id++;
}

uint32_t BaseType::GetId() const {
return id_;
}
uint32_t BaseType::GetId() const { return id_; }

void BaseType::SetName(const std::string& name) { name_ = name; }

Expand Down Expand Up @@ -140,4 +139,3 @@ uint32_t MemberType::NextTypeId() {
static uint32_t member_id = 1;
return member_id++;
}

24 changes: 9 additions & 15 deletions base_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef BASE_TYPES_H_
#define BASE_TYPES_H_

#include <cstdint>
#include <string>
#include <vector>

Expand All @@ -43,8 +44,6 @@ class BaseType {

BaseType()
: id_(NextTypeId()),
size_(0),
is_signed_(false),
pointer_(nullptr),
category_(kAtomic) {}

Expand Down Expand Up @@ -77,21 +76,22 @@ class BaseType {
static const MemberType* ResolveMember(const BaseType* base_type, int offset);

private:
static uint32_t NextTypeId();

uint32_t id_;
// The name of this type.
std::string name_;
// The size of this type in bits.
size_t size_;
size_t size_ = 0;
// Is this type able to represent signed values?
bool is_signed_;
bool is_signed_ = false;
// If this is a pointer type we also have a parent relation
// (e.g. int** -> int* -> int).
const BaseType* pointer_;
// The list of members that belong to this base type. The base type does not
// own the MemberType instances. Instead, the types container owns them.
MemberTypes members_;
TypeCategory category_;
static uint32_t NextTypeId();
};

// Represents an element of a compound type.
Expand All @@ -100,13 +100,7 @@ struct MemberType {
// Represents a null value in the database schema.
enum { DB_NULL_VALUE = -1 };

MemberType()
: id(NextTypeId()),
type(nullptr),
parent_type(nullptr),
offset(DB_NULL_VALUE),
argument(DB_NULL_VALUE),
num_elements(DB_NULL_VALUE) {}
MemberType() : id(NextTypeId()), type(nullptr), parent_type(nullptr) {}

// The corresponding id in the database.
uint32_t id;
Expand All @@ -118,15 +112,15 @@ struct MemberType {
const BaseType* parent_type;
// The offset of this member within a structure type.
// DB_NULL_VALUE if this should be NULL in the database (for arrays).
int offset;
int offset = DB_NULL_VALUE;
// The ith argument if this is a function pointer.
// DB_NULL_VALUE if this should be NULL in the database.
// (anything but function pointers).
int argument;
int argument = DB_NULL_VALUE;
// Number of elements if this is an array.
// DB_NULL_VALUE if this should be NULL in the database
// (anything but arrays).
int num_elements;
int num_elements = DB_NULL_VALUE;

private:
static uint32_t NextTypeId();
Expand Down
9 changes: 5 additions & 4 deletions binexport2_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <string>
#include <fstream>

#include "base/integral_types.h"
#include "base/logging.h"
#include "third_party/absl/container/flat_hash_map.h"
#include "third_party/absl/strings/str_cat.h"
Expand All @@ -51,8 +50,9 @@ namespace {

// Sorts by descending occurrence count then by mnemonic string. Don't be
// confused by operator > - this function is called as an operator less-than.
bool SortMnemonicsByOccurrenceCount(const std::pair<std::string, int32_t>& one,
const std::pair<std::string, int32_t>& two) {
bool SortMnemonicsByOccurrenceCount(
const std::pair<std::string, int32_t>& one,
const std::pair<std::string, int32_t>& two) {
if (one.second != two.second) {
return one.second > two.second;
}
Expand Down Expand Up @@ -455,7 +455,8 @@ bool SortByAddress(const BinExport2::CallGraph::Vertex& one,
// particular address.
// It is a fatal error to look for an address that is not actually contained in
// the graph.
int32_t GetVertexIndex(const BinExport2::CallGraph& call_graph, uint64_t address) {
int32_t GetVertexIndex(const BinExport2::CallGraph& call_graph,
uint64_t address) {
BinExport2::CallGraph::Vertex vertex;
vertex.set_address(address);
const auto& it =
Expand Down
18 changes: 9 additions & 9 deletions database/postgresql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Parameters& Parameters::operator<<(const int64_t value) {

Parameters& Parameters::operator<<(double value) {
parameters_.push_back(static_cast<int>(data_.size()));
const uint8_t* p = (reinterpret_cast<const uint8_t*>(&value) + 7);
const auto* p = (reinterpret_cast<const uint8_t*>(&value) + 7);
for (int i = 0; i < 8; ++i) {
data_.push_back(*(p - i));
}
Expand Down Expand Up @@ -156,7 +156,7 @@ Database::Database(const char* connection_string)

// Set a custom handler for log messages coming from the server.
PQsetNoticeProcessor(connection_, &PostgresNoticeProcessor,
NULL /*user data*/);
nullptr /*user data*/);
}

Database::~Database() {
Expand Down Expand Up @@ -248,7 +248,7 @@ Database& Database::operator>>(bool& value) {
assert(!PQgetisnull(result_, result_index_ / num_columns,
result_index_ % num_columns) &&
"null values not supported");
const uint8_t* source = reinterpret_cast<const uint8_t*>(PQgetvalue(
const auto* source = reinterpret_cast<const uint8_t*>(PQgetvalue(
result_, result_index_ / num_columns, result_index_ % num_columns));
value = *source != 0;
++result_index_;
Expand All @@ -260,8 +260,8 @@ Database& Database::operator>>(int32_t& value) {
assert(!PQgetisnull(result_, result_index_ / num_columns,
result_index_ % num_columns) &&
"null values not supported");
uint8_t* dest = reinterpret_cast<uint8_t*>(&value);
const uint8_t* source =
auto* dest = reinterpret_cast<uint8_t*>(&value);
const auto* source =
reinterpret_cast<const uint8_t*>(PQgetvalue(
result_, result_index_ / num_columns, result_index_ % num_columns)) +
3;
Expand All @@ -278,8 +278,8 @@ Database& Database::operator>>(int64_t& value) {
assert(!PQgetisnull(result_, result_index_ / num_columns,
result_index_ % num_columns) &&
"null values not supported");
uint8_t* dest = reinterpret_cast<uint8_t*>(&value);
const uint8_t* source =
auto* dest = reinterpret_cast<uint8_t*>(&value);
const auto* source =
reinterpret_cast<const uint8_t*>(PQgetvalue(
result_, result_index_ / num_columns, result_index_ % num_columns)) +
7;
Expand All @@ -300,8 +300,8 @@ Database& Database::operator>>(double& value) {
assert(!PQgetisnull(result_, result_index_ / num_columns,
result_index_ % num_columns) &&
"null values not supported");
uint8_t* dest = reinterpret_cast<uint8_t*>(&value);
const uint8_t* source =
auto* dest = reinterpret_cast<uint8_t*>(&value);
const auto* source =
reinterpret_cast<const uint8_t*>(PQgetvalue(
result_, result_index_ / num_columns, result_index_ % num_columns)) +
7;
Expand Down
17 changes: 10 additions & 7 deletions database/postgresql.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef DATABASE_POSTGRESQL_H_
#define DATABASE_POSTGRESQL_H_

#include <cstdint>
#include <string>
#include <vector>

Expand Down Expand Up @@ -57,6 +58,10 @@ struct pg_result;
class Database {
public:
explicit Database(const char* connection_string);

Database(const Database&) = delete;
Database& operator=(const Database&) = delete;

~Database();

Database& Execute(const char* query,
Expand All @@ -70,16 +75,13 @@ class Database {

operator bool() const;
Database& operator>>(bool& value); // NOLINT
Database& operator>>(int32_t& value); // NOLINT
Database& operator>>(int64_t& value); // NOLINT
Database& operator>>(int32_t& value); // NOLINT
Database& operator>>(int64_t& value); // NOLINT
Database& operator>>(double& value); // NOLINT
Database& operator>>(std::string& value); // NOLINT
Database& operator>>(Blob& value); // NOLINT

private:
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;

pg_conn* connection_;
pg_result* result_;
int result_index_;
Expand All @@ -89,12 +91,13 @@ class Transaction {
public:
// Does not take ownership
explicit Transaction(Database* database);
~Transaction();

private:
Transaction(const Transaction&) = delete;
Transaction& operator=(const Transaction&) = delete;

~Transaction();

private:
Database* database_;
};

Expand Down
4 changes: 2 additions & 2 deletions database/postgresql_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ void DatabaseWriter::InsertExpressionSubstitutions(
}
if (expression->IsImmediate() && !expression->GetSymbol().empty()) {
query_builder
<< "(" << static_cast<int64_t>(instruction.GetAddress()) << ","
<< operand_num << "," << expression->GetId() << ","
<< "(" << static_cast<int64_t>(instruction.GetAddress())
<< "," << operand_num << "," << expression->GetId() << ","
<< database_.EscapeLiteral(expression->GetSymbol()) << "),"
<< kFlushQuery;
}
Expand Down
6 changes: 4 additions & 2 deletions database/query_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef DATABASE_QUERY_BUILDER_H_
#define DATABASE_QUERY_BUILDER_H_

#include <cstddef>
#include <sstream>
#include <string>
#include <vector>
Expand All @@ -30,12 +31,13 @@ class QueryBuilder {
// Does not take ownership.
QueryBuilder(Database* database, const std::string& base_query,
size_t query_size);
void Execute();

private:
QueryBuilder(const QueryBuilder&) = delete;
QueryBuilder& operator=(const QueryBuilder&) = delete;

void Execute();

private:
friend QueryBuilder& operator<<(QueryBuilder&, const std::string&);
friend QueryBuilder& operator<<(QueryBuilder&, int64_t);
friend QueryBuilder& operator<<(QueryBuilder&, const Terminator&);
Expand Down
8 changes: 4 additions & 4 deletions edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#define EDGE_H_

#include <cstddef>
#include <cstdint>

#include "base/integral_types.h"
#include "third_party/zynamics/binexport/types.h"

struct FlowGraphEdge {
Expand All @@ -38,11 +38,11 @@ struct FlowGraphEdge {
};

struct FlowGraphEdgeHash {
std::size_t operator()(const FlowGraphEdge& fge) const;
size_t operator()(const FlowGraphEdge& fge) const;
};

// For easy use with std::set. Sorts by source address first, target address
// second.
// For easy use with set containers. Sorts by source address first, target
// address second.
bool operator<(const FlowGraphEdge& one, const FlowGraphEdge& two);
bool operator==(const FlowGraphEdge& lhs, const FlowGraphEdge& rhs);

Expand Down
31 changes: 16 additions & 15 deletions expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ Expression* Expression::Create(const Expression* parent,
ExpressionCache::iterator i = expression_cache_.find(signature);
if (i != expression_cache_.end()) {
return &i->second;
} else {
// Id should simply be a count of how many objects are already in the cache.
expression.id_ = ++global_id_;
return &expression_cache_.insert(std::make_pair(signature, expression))
.first->second;
}
// Id should simply be a count of how many objects are already in the cache.
expression.id_ = ++global_id_;
return &expression_cache_.insert(std::make_pair(signature, expression))
.first->second;
}

void Expression::EmptyCache() {
Expand Down Expand Up @@ -109,22 +108,24 @@ std::string Expression::CreateSignature() {
}

std::ostream& operator<<(std::ostream& stream, const Expression& expression) {
if (expression.IsDereferenceOperator())
if (expression.IsDereferenceOperator()) {
stream << "[";
else if (!expression.GetSymbol().empty())
} else if (!expression.GetSymbol().empty()) {
stream << expression.GetSymbol();
else if (expression.GetImmediate() >= 0)
} else if (expression.GetImmediate() >= 0) {
stream << std::hex << expression.GetImmediate();
else
} else {
stream << "-" << std::hex << -expression.GetImmediate();
}

// TODO(soerenme) Re-implement using renderExpression in instruction.cpp
// for (std::list< CExpression * >::const_iterator i =
// expression.m_Children.begin(); i != expression.m_Children.end(); ++i )
// stream << "(" << **i << ")";

if (expression.IsDereferenceOperator()) stream << "]";
// TODO(cblichmann): Reimplement using RenderExpression in instruction.cc
// for (auto it =
// expression.children.begin(); it != expression.children.end(); ++it)
// stream << "(" << **it << ")";

if (expression.IsDereferenceOperator()) {
stream << "]";
}
return stream;
}

Expand Down
11 changes: 7 additions & 4 deletions expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Expression {
TYPE_SIZEPREFIX = 6,
TYPE_DEREFERENCE = 7,

// These are only used by the IDA plugin
TYPE_NEWOPERAND = 8,
TYPE_STACKVARIABLE = 9,
TYPE_GLOBALVARIABLE = 10,
Expand All @@ -67,9 +68,10 @@ class Expression {
const Expression* GetParent() const;
std::string CreateSignature();
static Expression* Create(const Expression* parent,
const std::string& symbol = "", int64_t immediate = 0,
Type type = TYPE_IMMEDIATE_INT, uint16_t position = 0,
bool relocatable = false);
const std::string& symbol = "",
int64_t immediate = 0,
Type type = TYPE_IMMEDIATE_INT,
uint16_t position = 0, bool relocatable = false);
static void EmptyCache();
static const ExpressionCache& GetExpressions();

Expand Down Expand Up @@ -141,14 +143,15 @@ class Expression {
};

private:
static const std::string* CacheString(const std::string& value);

// The constructor is private for two reasons:
// - We want to make sure expressions can only be created on the heap.
// - We want to avoid creating duplicate expressions so we just
// refer to the expression_cache_ if someone tries (that way archiving
// compression/de-duping)
Expression(const Expression* parent, const std::string& symbol,
int64_t immediate, Type type, uint16_t position, bool relocatable);
static const std::string* CacheString(const std::string& value);

const std::string* symbol_;
int64_t immediate_;
Expand Down
Loading

0 comments on commit b886b8f

Please sign in to comment.