Skip to content

Commit

Permalink
Add callback system
Browse files Browse the repository at this point in the history
- Added code for notifying, adding, creating, removing, and destroying callback objects
- PreCallbacks are now AurieEntryEx
  • Loading branch information
Archie-osu committed Apr 7, 2024
1 parent da8a377 commit fa61cc1
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 17 deletions.
1 change: 0 additions & 1 deletion Aurie/source/AurieMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ void ArProcessAttach(HINSTANCE Instance)
}

// Now we have to wait until the current process has finished initializating

// Query the process subsystem
unsigned short current_process_subsystem = 0;
PpGetImageSubsystem(
Expand Down
24 changes: 23 additions & 1 deletion Aurie/source/framework/Memory Manager/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,51 @@

namespace Aurie
{
// Queries the version of the running framework
EXPORTED void MmGetFrameworkVersion(
OUT OPTIONAL short* Major,
OUT OPTIONAL short* Minor,
OUT OPTIONAL short* Patch
);

// Allocates memory that may be freed only after the Aurie Framework gets unloaded.
// Allocates memory that is freed after the Aurie Framework and all modules unload
EXPORTED PVOID MmAllocatePersistentMemory(
IN size_t Size
);

// Allocates memory that is automatically freed upon a module's unloading
EXPORTED PVOID MmAllocateMemory(
IN AurieModule* Owner,
IN size_t Size
);

// Frees persistent memory allocated via MmAllocatePersistentMemory
EXPORTED AurieStatus MmFreePersistentMemory(
IN PVOID AllocationBase
);

// Frees memory allocated via MmAllocateMemory
EXPORTED AurieStatus MmFreeMemory(
IN AurieModule* Owner,
IN PVOID AllocationBase
);

// Scans a module's memory for an array of bytes
EXPORTED size_t MmSigscanModule(
IN const wchar_t* ModuleName,
IN const unsigned char* Pattern,
IN const char* PatternMask
);

// Scans a memory region for an array of bytes
EXPORTED size_t MmSigscanRegion(
IN const unsigned char* RegionBase,
IN const size_t RegionSize,
IN const unsigned char* Pattern,
IN const char* PatternMask
);

// Creates a trampoline hook
EXPORTED AurieStatus MmCreateHook(
IN AurieModule* Module,
IN std::string_view HookIdentifier,
Expand All @@ -51,48 +58,57 @@ namespace Aurie
OUT OPTIONAL PVOID* Trampoline
);

// Checks if a given trampoline hook exists
EXPORTED AurieStatus MmHookExists(
IN AurieModule* Module,
IN std::string_view HookIdentifier
);

// Removes an already existing trampoline hook
EXPORTED AurieStatus MmRemoveHook(
IN AurieModule* Module,
IN std::string_view HookIdentifier
);

// Retrieves the address of the trampoline for a given hook
EXPORTED PVOID MmGetHookTrampoline(
IN AurieModule* Module,
IN std::string_view HookIdentifier
);

namespace Internal
{
// Internal function for allocating memory
AurieMemoryAllocation MmpAllocateMemory(
IN const size_t AllocationSize,
IN AurieModule* const OwnerModule
);

// Verifies that a callback resides in an expected module region
AurieStatus MmpVerifyCallback(
IN HMODULE Module,
IN PVOID CallbackRoutine
);

// Internal routine for freeing memory
void MmpFreeMemory(
IN AurieModule* OwnerModule,
IN PVOID AllocationBase,
IN bool RemoveTableEntry
);

// Adds an AurieMemoryAllocation to the internal table
AurieMemoryAllocation* MmpAddAllocationToTable(
IN const AurieMemoryAllocation& Allocation
);

// Checks whether a memory is allocated by a certain module
EXPORTED bool MmpIsAllocatedMemory(
IN AurieModule* Module,
IN PVOID AllocationBase
);

// Internal function for signature scanning a memory region
EXPORTED AurieStatus MmpSigscanRegion(
IN const unsigned char* RegionBase,
IN const size_t RegionSize,
Expand All @@ -101,33 +117,39 @@ namespace Aurie
OUT uintptr_t& PatternBase
);

// Removes a memory allocation from the module's table without freeing the memory
void MmpRemoveAllocationsFromTable(
IN AurieModule* OwnerModule,
IN const PVOID AllocationBase
);

// Adds an AurieHook object to the module's hook table
AurieHook* MmpAddHookToTable(
IN AurieModule* OwnerModule,
IN AurieHook&& Hook
);

// Internal routine for removing trampoline hooks
AurieStatus MmpRemoveHook(
IN AurieModule* Module,
IN std::string_view HookIdentifier,
IN bool RemoveFromTable
);

// Removes an AurieHook object from the module's hook table
void MmpRemoveHookFromTable(
IN AurieModule* Module,
IN AurieHook* Hook
);

// Internal method for looking up hook objects
AurieStatus MmpLookupHookByName(
IN AurieModule* Module,
IN std::string_view HookIdentifier,
OUT AurieHook*& Hook
);

// Internal method for creating trampoline hooks
AurieHook* MmpCreateHook(
IN AurieModule* Module,
IN std::string_view HookIdentifier,
Expand Down
23 changes: 22 additions & 1 deletion Aurie/source/framework/Module Manager/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,36 @@
namespace Aurie
{
// Maps an image into memory and returns the pointer to its AurieModule struct
// Used for runtime loading only
EXPORTED AurieStatus MdMapImage(
IN const fs::path& ImagePath,
OUT AurieModule*& Module
);

// Maps an image into memory and returns the pointer to its AurieModule struct
// Used to load both runtime and preloaded modules
AurieStatus MdMapImageEx(
IN const fs::path& ImagePath,
IN bool IsRuntimeLoad,
OUT AurieModule*& Module
);

// Checks whether an image's ModulePreinitialize method ran
EXPORTED bool MdIsImagePreinitialized(
IN AurieModule* Module
);

// Checks whether an image is initialized or not
// Checks whether an image's ModuleInitialize method ran
EXPORTED bool MdIsImageInitialized(
IN AurieModule* Module
);

// Checks whether an image was loaded at runtime
EXPORTED bool MdIsImageRuntimeLoaded(
IN AurieModule* Module
);

// Maps every module in a given folder
EXPORTED AurieStatus MdMapFolder(
IN const fs::path& FolderPath,
IN bool Recursive
Expand All @@ -43,6 +49,7 @@ namespace Aurie
OUT std::wstring& Filename
);

// Unmaps and deallocates every image
EXPORTED AurieStatus MdUnmapImage(
IN AurieModule* Module
);
Expand All @@ -59,14 +66,17 @@ namespace Aurie
OUT AurieModule& Module
);

// Internal method to check whether a module is marked for purge
bool MdpIsModuleMarkedForPurge(
IN AurieModule* Module
);

// Internal method to mark a module for purge
void MdpMarkModuleForPurge(
IN AurieModule* Module
);

// Internal method that unloads all modules marked for purge
void MdpPurgeMarkedModules();

// Internal routine responsible for ensuring module compatibility
Expand All @@ -75,6 +85,8 @@ namespace Aurie
OUT HMODULE& ImageBase
);

// Internal routine that builds a list of modules to be loaded from a folder
// given a predicate routine that decides whether or not to include them in the list
void MdpBuildModuleList(
IN const fs::path& BaseFolder,
IN bool Recursive,
Expand All @@ -95,46 +107,55 @@ namespace Aurie
OPTIONAL OUT PVOID* EntryPoint
);

// Queries the full path for a given AurieModule
EXPORTED fs::path& MdpGetImagePath(
IN AurieModule* Module
);

// Queries the folder path for a given AurieModule
EXPORTED AurieStatus MdpGetImageFolder(
IN AurieModule* Module,
OUT fs::path& Path
);

// Walks the linked list of loaded modules
EXPORTED AurieStatus MdpGetNextModule(
IN AurieModule* Module,
OUT AurieModule*& NextModule
);

// Queries the base address of a given AurieModule
EXPORTED PVOID MdpGetModuleBaseAddress(
IN AurieModule* Module
);

// Looks up the AurieModule given a path to the module
EXPORTED AurieStatus MdpLookupModuleByPath(
IN const fs::path& ModulePath,
OUT AurieModule*& Module
);

// Internal method responsible for initializing AurieModule function pointers
AurieStatus MdpProcessImageExports(
IN const fs::path& ImagePath,
IN HMODULE ImageBaseAddress,
OUT AurieModule* ModuleImage
);

// Internal method for unmapping images
AurieStatus MdpUnmapImage(
IN AurieModule* Module,
IN bool RemoveFromList,
IN bool CallUnloadRoutine
);

// Internal method for dispatching AurieEntry function pointers
AurieStatus MdpDispatchEntry(
IN AurieModule* Module,
IN AurieEntry Entry
);

// Internal routine that maps every module in a given folder
void MdpMapFolder(
IN const fs::path& Folder,
IN bool Recursive,
Expand Down
Loading

0 comments on commit fa61cc1

Please sign in to comment.