Skip to content

Commit

Permalink
Migrate to clang type parser and make ffi.h a valid C file. Fix Vecto…
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed Jun 26, 2023
1 parent c4fcc4a commit 6e7617e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 48 deletions.
114 changes: 67 additions & 47 deletions api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,51 @@ extern "C"
#endif // _MSC_VER
#endif // __GNUC__C

struct BNDebuggerController;
struct BNDebugAdapterType;
struct BNDebugAdapter;
struct BNDebuggerState;

struct BNBinaryView;
struct BNArchitecture;
struct BNDataBuffer;
struct BNMetadata;
enum BNFunctionGraphType;

struct BNDebugProcess
typedef struct BNDebuggerController BNDebuggerController;
typedef struct BNDebugAdapterType BNDebugAdapterType;
typedef struct BNDebugAdapter BNDebugAdapter;
typedef struct BNDebuggerState BNDebuggerState;

typedef struct BNBinaryView BNBinaryView;
typedef struct BNArchitecture BNArchitecture;
typedef struct BNDataBuffer BNDataBuffer;
typedef struct BNMetadata BNMetadata;

// When `ffi.h` gets parsed by clang type parser, the binaryninjacore.h is NOT included so this enum will become not
// defined. As a workaround, I duplicate its definition here. When the code gets compiled, the `BN_TYPE_PARSER` is
// not defined so the enum will not be redefined.
#ifdef BN_TYPE_PARSER
typedef enum BNFunctionGraphType
{
InvalidILViewType = -1,
NormalFunctionGraph = 0,
LowLevelILFunctionGraph = 1,
LiftedILFunctionGraph = 2,
LowLevelILSSAFormFunctionGraph = 3,
MediumLevelILFunctionGraph = 4,
MediumLevelILSSAFormFunctionGraph = 5,
MappedMediumLevelILFunctionGraph = 6,
MappedMediumLevelILSSAFormFunctionGraph = 7,
HighLevelILFunctionGraph = 8,
HighLevelILSSAFormFunctionGraph = 9,
HighLevelLanguageRepresentationFunctionGraph = 10,
} BNFunctionGraphType;
#endif

typedef struct BNDebugProcess
{
uint32_t m_pid;
char* m_processName;
};
} BNDebugProcess;

struct BNDebugThread
typedef struct BNDebugThread
{
uint32_t m_tid;
uint64_t m_rip;
bool m_isFrozen;
};
} BNDebugThread;

struct BNDebugFrame
typedef struct BNDebugFrame
{
size_t m_index;
uint64_t m_pc;
Expand All @@ -76,47 +96,47 @@ extern "C"
char* m_functionName;
uint64_t m_functionStart;
char* m_module;
};
} BNDebugFrame;


struct BNDebugModule
typedef struct BNDebugModule
{
char* m_name;
char* m_short_name;
uint64_t m_address;
size_t m_size;
bool m_loaded;
};
} BNDebugModule;


struct BNDebugRegister
typedef struct BNDebugRegister
{
char* m_name;
uint64_t m_value;
size_t m_width;
size_t m_registerIndex;
char* m_hint;
};
} BNDebugRegister;


struct BNDebugBreakpoint
typedef struct BNDebugBreakpoint
{
// TODO: we should add an absolute address to this, along with a boolean telling whether it is valid
char* module;
uint64_t offset;
uint64_t address;
bool enabled;
};
} BNDebugBreakpoint;


struct BNModuleNameAndOffset
typedef struct BNModuleNameAndOffset
{
char* module;
uint64_t offset;
};
} BNModuleNameAndOffset;


enum BNDebugStopReason
typedef enum BNDebugStopReason
{
UnknownReason = 0,
InitialBreakpoint,
Expand Down Expand Up @@ -172,27 +192,27 @@ extern "C"
UserRequestedBreak,

OperationNotSupported
};
} BNDebugStopReason;


enum BNDebugAdapterConnectionStatus
typedef enum BNDebugAdapterConnectionStatus
{
DebugAdapterNotConnectedStatus,
DebugAdapterConnectingStatus,
DebugAdapterConnectedStatus,
};
} BNDebugAdapterConnectionStatus;


enum BNDebugAdapterTargetStatus
typedef enum BNDebugAdapterTargetStatus
{
// Target is not created yet, or not connected to yet
DebugAdapterInvalidStatus,
DebugAdapterRunningStatus,
DebugAdapterPausedStatus,
};
} BNDebugAdapterTargetStatus;


enum BNDebuggerEventType
typedef enum BNDebuggerEventType
{
LaunchEventType,
ResumeEventType,
Expand Down Expand Up @@ -239,57 +259,57 @@ extern "C"

ForceMemoryCacheUpdateEvent,
ModuleLoadedEvent,
};
} BNDebuggerEventType;


struct BNTargetStoppedEventData
typedef struct BNTargetStoppedEventData
{
BNDebugStopReason reason;
uint32_t lastActiveThread;
size_t exitCode;
void* data;
};
} BNTargetStoppedEventData;


struct BNErrorEventData
typedef struct BNErrorEventData
{
char* error;
char* shortError;
void* data;
};
} BNErrorEventData;


struct BNTargetExitedEventData
typedef struct BNTargetExitedEventData
{
uint64_t exitCode;
};
} BNTargetExitedEventData;


struct BNStdoutMessageEventData
typedef struct BNStdoutMessageEventData
{
char* message;
};
} BNStdoutMessageEventData;


// This should really be a union, but gcc complains...
struct BNDebuggerEventData
typedef struct BNDebuggerEventData
{
BNTargetStoppedEventData targetStoppedData;
BNErrorEventData errorData;
uint64_t absoluteAddress;
BNModuleNameAndOffset relativeAddress;
BNTargetExitedEventData exitData;
BNStdoutMessageEventData messageData;
};
} BNDebuggerEventData;


struct BNDebuggerEvent
typedef struct BNDebuggerEvent
{
BNDebuggerEventType type;
BNDebuggerEventData data;
};
} BNDebuggerEvent;

enum BNDebuggerAdapterOperation
typedef enum BNDebuggerAdapterOperation
{
DebugAdapterLaunch,
DebugAdapterAttach,
Expand All @@ -301,7 +321,7 @@ extern "C"
DebugAdapterPause,
DebugAdapterQuit,
DebugAdapterDetach
};
} BNDebuggerAdapterOperation;


DEBUGGER_FFI_API char* BNDebuggerAllocString(const char* string);
Expand Down
2 changes: 1 addition & 1 deletion api/python/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ int main(int argc, char* argv[])

// Enable ephemeral settings
Settings::Instance()->LoadSettingsFile("");
Settings::Instance()->Set("analysis.types.parserName", "CoreTypeParser");
Settings::Instance()->Set("analysis.types.parserName", "ClangTypeParser");
bool ok = arch->GetStandalonePlatform()->ParseTypesFromSourceFile(argv[1], types, vars, funcs, errors);

if (!ok)
Expand Down

0 comments on commit 6e7617e

Please sign in to comment.