164 lines
4.3 KiB
C
164 lines
4.3 KiB
C
#ifndef HOOKS_H
|
|
#define HOOKS_H
|
|
|
|
////////////////////////////////
|
|
// Little Base Layer
|
|
|
|
#include <stdint.h>
|
|
typedef int8_t S8;
|
|
typedef int16_t S16;
|
|
typedef int32_t S32;
|
|
typedef int64_t S64;
|
|
typedef uint8_t U8;
|
|
typedef uint16_t U16;
|
|
typedef uint32_t U32;
|
|
typedef uint64_t U64;
|
|
typedef S8 B8;
|
|
typedef S16 B16;
|
|
typedef S32 B32;
|
|
typedef S64 B64;
|
|
typedef float F32;
|
|
typedef double F64;
|
|
|
|
typedef struct String8{
|
|
U8 *str;
|
|
U64 size;
|
|
} String8;
|
|
|
|
#define str8_const(S) {(U8*)(S), (U64)(sizeof(S) - 1)}
|
|
|
|
typedef struct Arena{
|
|
void *memory;
|
|
U64 pos;
|
|
U64 cap;
|
|
} Arena;
|
|
|
|
static Arena arena_alloc(U64 cap);
|
|
static void* arena_push(Arena *arena, U64 size);
|
|
static void arena_pop_to(Arena *arena, U64 pos);
|
|
static U64 arena_pos(Arena *arena);
|
|
|
|
#define push_array(a,T,c) (T*)arena_push((a), sizeof(T)*(c))
|
|
|
|
static void*copy_strided(void *dst, U32 dst_stride,
|
|
void *src, U32 src_stride,
|
|
U32 count);
|
|
|
|
static void*arena_push_symbol_set__raw(Arena *arena,
|
|
U32 size, void *src, U32 src_stride, U32 count);
|
|
|
|
#define arena_push_symbol_set(ar,S) (SyType(S)*) \
|
|
arena_push_symbol_set__raw((ar), sizeof(SyType(S)), SyFirst(S), SyStride(S), SyCount(S))
|
|
|
|
#define Min(a,b) ((a)<(b))?(a):(b)
|
|
|
|
////////////////////////////////
|
|
// Hooks
|
|
|
|
//// HOOK_POINT ////
|
|
|
|
typedef struct HookPoint{
|
|
String8 name;
|
|
} HookPoint;
|
|
|
|
#define SYMBOL_SET_DEFINE HOOK_POINT
|
|
#define HOOK_POINT_Type HookPoint
|
|
#define HOOK_POINT_elf_section ".sy.hookpoint"
|
|
#define HOOK_POINT_coff_a_section ".sy$hookpoint_a"
|
|
#define HOOK_POINT_coff_m_section ".sy$hookpoint_m"
|
|
#define HOOK_POINT_coff_z_section ".sy$hookpoint_z"
|
|
#define HOOK_POINT_marker hookpoint
|
|
#include "symbol_set.define.h"
|
|
|
|
#define HOOK_POINT_STRUCT(N) \
|
|
SyDefine(HOOK_POINT, N) = { .name = str8_const(#N) }; \
|
|
typedef struct HookPointType_##N HookPointType_##N; struct HookPointType_##N
|
|
|
|
#define HOOK_POINT_TYPE_DEF(N,T) \
|
|
SyDefine(HOOK_POINT, N) = { .name = str8_const(#N) }; \
|
|
typedef T HookPointType_##N
|
|
|
|
#define HOOK_POINT_TYPE(N) HookPointType_##N
|
|
|
|
//// PROCESS_TYPE ////
|
|
|
|
typedef struct ProcessType{
|
|
String8 name;
|
|
} ProcessType;
|
|
|
|
#define SYMBOL_SET_DEFINE PROCESS_TYPE
|
|
#define PROCESS_TYPE_Type ProcessType
|
|
#define PROCESS_TYPE_elf_section ".sy.processtype"
|
|
#define PROCESS_TYPE_coff_a_section ".sy$processtype_a"
|
|
#define PROCESS_TYPE_coff_m_section ".sy$processtype_m"
|
|
#define PROCESS_TYPE_coff_z_section ".sy$processtype_z"
|
|
#define PROCESS_TYPE_marker processtype
|
|
#include "symbol_set.define.h"
|
|
|
|
#define PROCESS_TYPE(N) SyDefine(PROCESS_TYPE, N) = { .name = str8_const(#N) }
|
|
|
|
//// PROCESS-HOOK ////
|
|
|
|
typedef struct Ctx Ctx;
|
|
typedef void HookFunc(Ctx *ctx, void *params);
|
|
|
|
typedef struct ProcessHook{
|
|
SY__UAddr process_type_id;
|
|
SY__UAddr hook_point_id;
|
|
HookFunc *func;
|
|
} ProcessHook;
|
|
|
|
#define SYMBOL_SET_DEFINE PROCESS_HOOK
|
|
#define PROCESS_HOOK_Type ProcessHook
|
|
#define PROCESS_HOOK_elf_section ".sy.processhook"
|
|
#define PROCESS_HOOK_coff_a_section ".sy$processhook_a"
|
|
#define PROCESS_HOOK_coff_m_section ".sy$processhook_m"
|
|
#define PROCESS_HOOK_coff_z_section ".sy$processhook_z"
|
|
#define PROCESS_HOOK_marker processhook
|
|
#include "symbol_set.define.h"
|
|
|
|
#define PROCESS_HOOK(PR,HP) \
|
|
static void processhook__##PR##_##HP(Ctx *ctx, HookPointType_##HP *params); \
|
|
SyDefineUnnamed(PROCESS_HOOK) = { \
|
|
.process_type_id = SyRaw(PROCESS_TYPE, PR), \
|
|
.hook_point_id = SyRaw(HOOK_POINT, HP), \
|
|
.func = (HookFunc*)processhook__##PR##_##HP }; \
|
|
static void processhook__##PR##_##HP(Ctx *ctx, HookPointType_##HP *params)
|
|
|
|
#if SY__MAIN
|
|
SY_INIT(PROCESS_HOOK){
|
|
for (SyEach(PROCESS_HOOK, link)){
|
|
link->process_type_id = SyIDFromRaw(PROCESS_TYPE, link->process_type_id);
|
|
link->hook_point_id = SyIDFromRaw(HOOK_POINT, link->hook_point_id);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
//// Rules ////
|
|
|
|
typedef struct Rules{
|
|
U32 hook_point_count;
|
|
U32 process_type_count;
|
|
HookPoint *hook_points;
|
|
ProcessType *process_types;
|
|
|
|
HookFunc **func__process_hook; // [process_type][hook_point]->func
|
|
} Rules;
|
|
|
|
static Rules rules_from_symbol_sets(Arena *arena);
|
|
static HookFunc* rules_func_from_process_hook(Rules *rules, U32 process_type_id, U32 hook_point_id);
|
|
|
|
//// CTX ////
|
|
|
|
struct Ctx{
|
|
Rules *rules;
|
|
|
|
U32 process_type_id[100];
|
|
U32 process_count;
|
|
};
|
|
|
|
static void ctx_new_process(Ctx *ctx, U32 process_type_id);
|
|
static void ctx_hook_all_processes(Ctx *ctx, U32 hook_id, void *params);
|
|
|
|
#endif //HOOKS_H
|