[insight-vmi] r1999 committed - C-sytle casts are the devil!

0 views
Skip to first unread message

insig...@googlecode.com

unread,
Jul 10, 2013, 7:14:59 AM7/10/13
to insight-v...@googlegroups.com
Revision: 1999
Author: vo...@in.tum.de
Date: Wed Jul 10 04:14:19 2013
Log: C-sytle casts are the devil!

http://code.google.com/p/insight-vmi/source/detail?r=1999

Modified:
/trunk/libinsight/detect.cpp
/trunk/libinsight/include/insight/detect.h
/trunk/libinsight/include/insight/memberlist.h
/trunk/libinsight/include/insight/memorymap.h
/trunk/libinsight/include/insight/memorymapbuildercs.h
/trunk/libinsight/memorymapbuildercs.cpp
/trunk/libinsight/structuredmember.cpp

=======================================
--- /trunk/libinsight/detect.cpp Mon Jun 24 02:44:11 2013
+++ /trunk/libinsight/detect.cpp Wed Jul 10 04:14:19 2013
@@ -3,6 +3,9 @@
#include <insight/variable.h>
#include <insight/console.h>

+#include <insight/memorymap.h>
+#include <insight/memorymapheuristics.h>
+
#include <QCryptographicHash>
#include <QRegExp>
#include <QProcess>
@@ -317,6 +320,87 @@
ExecutablePages = current;
}

+void Detect::verifyFunctionPointers(MemoryMap *map)
+{
+ assert(map);
+
+ quint64 validPointers = 0;
+ quint64 invalidPointers = 0;
+
+ const QList<FuncPointersInNode> funcPointers = map->funcPointers();
+
+ // Start the Operation
+ operationStarted();
+
+ for (int i = 0; i < funcPointers.size(); ++i) {
+ Instance node = funcPointers[i].node->toInstance();
+
+ if (!funcPointers[i].paths.empty()) {
+ // Reconstruct each function pointer by following the path
+ for (int j = 0; j < funcPointers[i].paths.size(); ++j) {
+
+ Instance funcPointer = node;
+
+ for (int k = 0; k < funcPointers[i].paths[j].size(); ++k) {
+ const BaseType *t =
funcPointers[i].paths[j].at(k).type;
+ int index = funcPointers[i].paths[j].at(k).index;
+
+ if (t->type() & StructOrUnion) {
+ funcPointer = funcPointer.member(index,
BaseType::trLexical, 0,
+ map->knowSrc());
+ }
+ else if (t->type() & rtArray) {
+ funcPointer = funcPointer.arrayElem(index);
+ }
+ else {
+ debugerr("This should be a struct or an array!");
+ }
+ }
+
+ // What we have now, should be a function pointer
+ if (!MemoryMapHeuristics::isFunctionPointer(funcPointer)) {
+ debugerr("The instance with name " <<
funcPointer.name()
+ << " and type " << funcPointer.typeName()
+ << "is not a function pointer!\n");
+ continue;
+ }
+
+ // Verify Function Pointer
+ if
(!MemoryMapHeuristics::isValidFunctionPointer(funcPointer))
+ invalidPointers++;
+ else
+ validPointers++;
+ }
+ }
+ else {
+ // The node itself must be a function pointer
+ if (!MemoryMapHeuristics::isFunctionPointer(node)) {
+ debugerr("The node with name " << node.name()
+ << " and type " << node.typeName()
+ << "is not a function pointer!\n");
+ continue;
+ }
+
+ // Verify Function Pointer
+ if (!MemoryMapHeuristics::isValidFunctionPointer(node))
+ invalidPointers++;
+ else
+ validPointers++;
+ }
+
+ }
+
+ // Finish
+ operationStopped();
+
+ // Print Stats
+ Console::out() << "\r\nProcessed " << Console::color(ctWarningLight)
+ << invalidPointers+validPointers <<
Console::color(ctReset)
+ << " Function Pointers in " << elapsedTime() << " min"
<< endl;
+ Console::out() << "\t Detected " << Console::color(ctError)
+ << invalidPointers << Console::color(ctReset) << "
invalid Function Pointers.\n" << endl;
+}
+

void Detect::hiddenCode(int index)
{
@@ -608,6 +692,10 @@

// Verify hashes
verifyHashes(currentHashes);
+
+ // Verify function pointers?
+ if (_sym.memDumps().at(index)->map())
+ verifyFunctionPointers(_sym.memDumps().at(index)->map());
}


=======================================
--- /trunk/libinsight/include/insight/detect.h Mon Jun 24 02:37:22 2013
+++ /trunk/libinsight/include/insight/detect.h Wed Jul 10 04:14:19 2013
@@ -70,6 +70,7 @@
void getExecutableSections(QString file);
quint64 inVmap(quint64 address, VirtualMemory *vmem);
void verifyHashes(QMultiHash<quint64, ExecutablePage> *current);
+ void verifyFunctionPointers(MemoryMap *map);
};

#endif // DETECT_H
=======================================
--- /trunk/libinsight/include/insight/memberlist.h Tue Jan 29 08:01:53 2013
+++ /trunk/libinsight/include/insight/memberlist.h Wed Jul 10 04:14:19 2013
@@ -4,6 +4,7 @@
#include <QList>

class StructuredMember;
+class BaseType;

/// A list of constant struct/union member objects
typedef QList<StructuredMember*> MemberList;
@@ -11,4 +12,19 @@
/// A list of constant struct/union member objects
typedef QList<const StructuredMember*> ConstMemberList;

+/// Holds VariableTypeContainer structs
+typedef QList<struct VariableTypeContainer> VariableTypeContainerList;
+
+struct VariableTypeContainer
+{
+ VariableTypeContainer() : type(0), index(-1) {}
+ VariableTypeContainer(const BaseType *type) :
+ type(type), index(-1) {}
+ VariableTypeContainer(const BaseType *type, int index) :
+ type(type), index(index) {}
+
+ const BaseType *type;
+ int index;
+};
+
#endif // MEMBERLIST_H
=======================================
--- /trunk/libinsight/include/insight/memorymap.h Mon Jun 24 10:25:15 2013
+++ /trunk/libinsight/include/insight/memorymap.h Wed Jul 10 04:14:19 2013
@@ -112,15 +112,16 @@
struct FuncPointersInNode
{
FuncPointersInNode() : node(0) {}
- FuncPointersInNode(MemoryMapNode *node, ConstMemberList *path) :
+ FuncPointersInNode(const MemoryMapNode *node,
+ VariableTypeContainerList *path) :
node(node)
{
if (path)
- funcPointers.append((*path));
+ paths.append((*path));
}

- MemoryMapNode *node;
- QList<ConstMemberList> funcPointers;
+ const MemoryMapNode *node;
+ QList<VariableTypeContainerList> paths;
};


=======================================
--- /trunk/libinsight/include/insight/memorymapbuildercs.h Mon Jun 24
10:25:15 2013
+++ /trunk/libinsight/include/insight/memorymapbuildercs.h Wed Jul 10
04:14:19 2013
@@ -10,6 +10,7 @@

#include "memorymapbuilder.h"
#include "memberlist.h"
+#include "structured.h"

// Forward declaration
class Instance;
@@ -44,19 +45,21 @@

void processNode(MemoryMapNode* node);
void processInstance(const Instance &inst, MemoryMapNode* node,
- ConstMemberList *path = 0);
+ VariableTypeContainerList *path = 0);
void processInstanceFromRule(const Instance &parent, const Instance&
member,
- int mbrIdx, MemoryMapNode* node,
ConstMemberList *path);
+ int mbrIdx, MemoryMapNode* node,
+ VariableTypeContainerList *path);
void processPointer(const Instance &inst, MemoryMapNode* node);
void processFunctionPointer(const Instance &inst, MemoryMapNode *node,
- ConstMemberList *path);
+ VariableTypeContainerList *path);
void processArray(const Instance& inst, MemoryMapNode* node,
- ConstMemberList *path);
+ VariableTypeContainerList *path);
void processStructured(const Instance &inst, MemoryMapNode* node,
- ConstMemberList *path);
+ VariableTypeContainerList *path);

private:
- void addMembers(const Instance &inst, MemoryMapNode* node,
ConstMemberList *path);
+ void addMembers(const Instance &inst, MemoryMapNode* node,
+ VariableTypeContainerList *path);
static int countInvalidChildren(const Instance &inst, int *total);
static int countInvalidChildrenRek(const Instance &inst, int *total,
int *userlandPtrs);
=======================================
--- /trunk/libinsight/memorymapbuildercs.cpp Mon Jun 24 10:25:15 2013
+++ /trunk/libinsight/memorymapbuildercs.cpp Wed Jul 10 04:14:19 2013
@@ -121,7 +121,7 @@


void MemoryMapBuilderCS::processInstance(const Instance &inst,
MemoryMapNode *node,
- ConstMemberList *path)
+ VariableTypeContainerList *path)
{
// Ignore user-land objects
if (inst.address() < _map->_vmem->memSpecs().pageOffset)
@@ -148,21 +148,14 @@

void MemoryMapBuilderCS::processFunctionPointer(const Instance &inst,
MemoryMapNode *node,
- ConstMemberList *path)
+ VariableTypeContainerList
*path)
{
assert(node);
-
- if (path)
- path->append((const StructuredMember *)inst.type());
- else {
- _map->_shared->functionPointersLock.lock();
- _map->_funcPointers.append(FuncPointersInNode(node, path));
- _map->_shared->functionPointersLock.unlock();
- return;
- }

_map->_shared->functionPointersLock.lock();
- if (_map->_funcPointers.size() == 0) {
+ if (_map->_funcPointers.size() == 0 || !path) {
+ // Insert if the list is empty or if we have no path, which means
that
+ // node is a function pointer
_map->_funcPointers.append(FuncPointersInNode(node, path));
}
else {
@@ -176,8 +169,9 @@
}
}

- if (position != -1)
- _map->_funcPointers[position].funcPointers.append((*path));
+ if (position != -1) {
+ _map->_funcPointers[position].paths.append((*path));
+ }
else
_map->_funcPointers.append(FuncPointersInNode(node, path));
}
@@ -209,7 +203,7 @@


void MemoryMapBuilderCS::processArray(const Instance& inst, MemoryMapNode
*node,
- ConstMemberList *path)
+ VariableTypeContainerList *path)
{
assert(inst.type()->type() == rtArray);

@@ -218,14 +212,23 @@
for (int i = 0; i < len; ++i) {
Instance e(inst.arrayElem(i).dereference(BaseType::trLexical));
// Pass the "nested" flag to all elements of this array
- processInstance(e, node, path);
+ // Create a copy/new List
+ VariableTypeContainerList l;
+
+ // Do we have a path yet?
+ if (path) {
+ l.append((*path));
+ }
+
+ l.append(VariableTypeContainer(inst.type(), i));
+ addMembers(inst, node, &l);
}
}


void MemoryMapBuilderCS::processStructured(const Instance& inst,
MemoryMapNode *node,
- ConstMemberList *path)
+ VariableTypeContainerList *path)
{
assert(inst.type()->type() & StructOrUnion);

@@ -234,20 +237,12 @@
if (!path && (inst.address() & 0x3ULL))
return;

- if (!path) {
- ConstMemberList p;
- addMembers(inst, node, &p);
- }
- else {
- ConstMemberList p((*path));
- p.append((const StructuredMember *)inst.type());
- addMembers(inst, node, &p);
- }
+ addMembers(inst, node, path);
}


void MemoryMapBuilderCS::addMembers(const Instance &inst, MemoryMapNode*
node,
- ConstMemberList *path)
+ VariableTypeContainerList *path)
{
const int cnt = inst.memberCount();

@@ -261,13 +256,23 @@
if (!mi.isValid() || mi.isNull())
continue;

+ // Create a copy/new List
+ VariableTypeContainerList l;
+
+ // Do we have a path yet?
+ if (path) {
+ l.append((*path));
+ }
+
+ l.append(VariableTypeContainer(inst.type(), i));
+
// Did the rules engine decide which instance to use?
if (TypeRuleEngine::useMatchedInst(result)) {
- processInstanceFromRule(inst, mi, i, node, path);
+ processInstanceFromRule(inst, mi, i, node, &l);
}
// Pass the "nested" flag to nested structs/unions
else {
- processInstance(mi, node, path);
+ processInstance(mi, node, &l);
}
}
catch (GenericException&) {
@@ -280,7 +285,7 @@
void MemoryMapBuilderCS::processInstanceFromRule(const Instance &parent,
const Instance &member,
int mbrIdx, MemoryMapNode
*node,
- ConstMemberList *path)
+ VariableTypeContainerList
*path)
{
if (!member.isNull()) {
// How does the returned instance relate to the given one?
=======================================
--- /trunk/libinsight/structuredmember.cpp Tue Jan 29 08:01:53 2013
+++ /trunk/libinsight/structuredmember.cpp Wed Jul 10 04:14:19 2013
@@ -60,13 +60,12 @@
else
return QString("(unresolved type 0x%1) %2").arg((uint)_refTypeId,
0, 16).arg(_name);
}
-

Instance StructuredMember::toInstance(size_t structAddress,
VirtualMemory* vmem, const Instance* parent,
int resolveTypes, int maxPtrDeref) const
{
- Instance inst = createRefInstance(structAddress + _offset, vmem, _name,
+ Instance inst = createRefInstance(structAddress + _offset, vmem, _name,
parent ? parent->fullNameComponents() : QStringList(),
resolveTypes, maxPtrDeref);
// Is this a bit-field with bit-size/offset?
Reply all
Reply to author
Forward
0 new messages