Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Remove clang format so code is readable i hate clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
emesare committed Oct 4, 2024
1 parent fdaecdf commit 59aeecc
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 364 deletions.
86 changes: 0 additions & 86 deletions .clang-format

This file was deleted.

184 changes: 97 additions & 87 deletions include/rtti.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,117 @@

#include "binaryninjaapi.h"

constexpr const char* VIEW_METADATA_MSVC = "msvc";
constexpr const char *VIEW_METADATA_MSVC = "msvc";

namespace BinaryNinja {
struct BaseClassArray
{
uint32_t length;
std::vector<uint64_t> descriptors;

BaseClassArray(BinaryView* view, uint64_t address, uint32_t length);
};

struct ClassHierarchyDescriptor
{
uint32_t signature;
uint32_t attributes;
uint32_t numBaseClasses;
int32_t pBaseClassArray;

ClassHierarchyDescriptor(BinaryView* view, uint64_t address);
};

struct BaseClassDescriptor
{
int32_t pTypeDescriptor;
uint32_t numContainedBases;
int32_t where_mdisp;
int32_t where_pdisp;
int32_t where_vdisp;
uint32_t attributes;
int32_t pClassHierarchyDescriptor;

BaseClassDescriptor(BinaryView* view, uint64_t address);
};

struct TypeDescriptor
{
uint64_t pVFTable;
uint64_t spare;
std::string name;

TypeDescriptor(BinaryView* view, uint64_t address);
};

struct CompleteObjectLocator
{
uint32_t signature;
uint32_t offset;
uint32_t cdOffset;
int32_t pTypeDescriptor;
int32_t pClassHeirarchyDescriptor;
// Only on 64 bit
int32_t pSelf;

CompleteObjectLocator(BinaryView *view, uint64_t address);
};
struct BaseClassArray
{
uint32_t length;
std::vector<uint64_t> descriptors;

BaseClassArray(BinaryView *view, uint64_t address, uint32_t length);
};

struct ClassHierarchyDescriptor
{
uint32_t signature;
uint32_t attributes;
uint32_t numBaseClasses;
int32_t pBaseClassArray;

ClassHierarchyDescriptor(BinaryView *view, uint64_t address);
};

struct BaseClassDescriptor
{
int32_t pTypeDescriptor;
uint32_t numContainedBases;
int32_t where_mdisp;
int32_t where_pdisp;
int32_t where_vdisp;
uint32_t attributes;
int32_t pClassHierarchyDescriptor;

BaseClassDescriptor(BinaryView *view, uint64_t address);
};

struct TypeDescriptor
{
uint64_t pVFTable;
uint64_t spare;
std::string name;

TypeDescriptor(BinaryView *view, uint64_t address);
};

struct CompleteObjectLocator
{
uint32_t signature;
uint32_t offset;
uint32_t cdOffset;
int32_t pTypeDescriptor;
int32_t pClassHeirarchyDescriptor;
// Only on 64 bit
int32_t pSelf;

CompleteObjectLocator(BinaryView *view, uint64_t address);
};

struct VirtualFunctionInfo
{
uint64_t funcAddr;

Ref<Metadata> SerializedMetadata();
static VirtualFunctionInfo DeserializedMetadata(const Ref<Metadata>& metadata);

static VirtualFunctionInfo DeserializedMetadata(const Ref<Metadata> &metadata);
};

struct VirtualFunctionTableInfo
{
uint64_t address;
std::vector<VirtualFunctionInfo> virtualFunctions;
std::vector<VirtualFunctionInfo> virtualFunctions;

Ref<Metadata> SerializedMetadata();
static VirtualFunctionTableInfo DeserializedMetadata(const Ref<Metadata>& metadata);

static VirtualFunctionTableInfo DeserializedMetadata(const Ref<Metadata> &metadata);
};

struct ClassInfo
{
std::string className;
std::optional<std::string> baseClassName;
std::optional<uint64_t> classOffset;
std::optional<VirtualFunctionTableInfo> vft;

Ref<Metadata> SerializedMetadata();
static ClassInfo DeserializedMetadata(const Ref<Metadata>& metadata);
};

class MicrosoftRTTIProcessor
{
Ref<BinaryView> m_view;
Ref<Logger> m_logger;
bool allowMangledClassNames;
bool checkWritableRData;

std::map<uint64_t, ClassInfo> m_classInfo;

void DeserializedMetadata(const Ref<Metadata>& metadata);
std::optional<std::string> DemangleName(const std::string& mangledName);
std::optional<ClassInfo> ProcessRTTI(uint64_t coLocatorAddr);
std::optional<VirtualFunctionTableInfo> ProcessVFT(uint64_t vftAddr, const ClassInfo& classInfo);
public:
MicrosoftRTTIProcessor(const Ref<BinaryView>& view, bool useMangled = true, bool checkRData = true);
Ref<Metadata> SerializedMetadata();
void ProcessRTTI();
void ProcessVFT();
};
}
struct ClassInfo
{
std::string className;
std::optional<std::string> baseClassName;
std::optional<uint64_t> classOffset;
std::optional<VirtualFunctionTableInfo> vft;

Ref<Metadata> SerializedMetadata();

static ClassInfo DeserializedMetadata(const Ref<Metadata> &metadata);
};

class MicrosoftRTTIProcessor
{
Ref<BinaryView> m_view;
Ref<Logger> m_logger;
bool allowMangledClassNames;
bool checkWritableRData;

std::map<uint64_t, ClassInfo> m_classInfo;

void DeserializedMetadata(const Ref<Metadata> &metadata);

std::optional<std::string> DemangleName(const std::string &mangledName);

std::optional<ClassInfo> ProcessRTTI(uint64_t coLocatorAddr);

std::optional<VirtualFunctionTableInfo> ProcessVFT(uint64_t vftAddr, const ClassInfo &classInfo);

public:
MicrosoftRTTIProcessor(const Ref<BinaryView> &view, bool useMangled = true, bool checkRData = true);

Ref<Metadata> SerializedMetadata();

void ProcessRTTI();

void ProcessVFT();
};
}
9 changes: 4 additions & 5 deletions scripts/class_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
# '5368823856': {'className': 'type_info'}
# }
# }
import sys
from time import sleep
from typing import List

from binaryninja.types import BaseStructure
from binaryninja import BinaryView, BaseStructure, NamedTypeReferenceClass, StructureVariant, NamedTypeReferenceType, \
StructureBuilder, PointerType, PluginCommandContext, PluginCommand
from binaryninja import BinaryView, PluginCommandContext, PluginCommand

import sys
if len(sys.argv) != 2:
print("Usage: python class_dump.py <path_to_file>")
sys.exit(1)
Expand All @@ -36,10 +33,12 @@
sleep(1)
data = view.query_metadata("msvc")


class ClassInfo:
def __init__(self, class_name: str, base_classes: dict[int, str]):
self.class_name = class_name
self.base_classes = base_classes

def __repr__(self):
return f"ClassInfo(class_name={self.class_name}, base_classes={self.base_classes})"

Expand Down
8 changes: 4 additions & 4 deletions scripts/class_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
# }


from PySide6.QtWidgets import QApplication, QDialog, QVBoxLayout
from PySide6.QtGui import QFont
from PySide6.QtCore import QSize
# FYI we do not have this in binja
from PySide6.QtCharts import QChart, QChartView, QLineSeries
from PySide6.QtWidgets import QDialog, QVBoxLayout

data = {
'classes': {
Expand All @@ -26,6 +24,7 @@
}
}


class GraphDialog(QDialog):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -70,9 +69,10 @@ def add_data_to_series(self):

y += 10


def create_window():
dialog = GraphDialog()
dialog.exec_()


execute_on_main_thread(create_window)
execute_on_main_thread(create_window)
5 changes: 4 additions & 1 deletion scripts/class_graphviz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from time import sleep

import graphviz
import sys
from binaryninja import BinaryView, PluginCommandContext, PluginCommand

# Whether to open the png after it is created.
Expand Down Expand Up @@ -29,6 +30,7 @@
print("Creating graph...")
data = view.query_metadata("msvc")


def create_graph(data):
dot = graphviz.Digraph()
classes = data.get('classes', {})
Expand All @@ -40,6 +42,7 @@ def create_graph(data):
dot.edge(base_class_name, class_name)
return dot


# data = {
# 'classes': {
# '5368823328': {'className': 'Animal'},
Expand Down
1 change: 0 additions & 1 deletion scripts/class_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# }
from typing import List

from binaryninja.types import BaseStructure
from binaryninja import BinaryView, BaseStructure, NamedTypeReferenceClass, StructureVariant, NamedTypeReferenceType, \
StructureBuilder, PointerType

Expand Down
Loading

0 comments on commit 59aeecc

Please sign in to comment.