2020-06-01 16:35:34 +02:00
# include "hooks.h"
2020-06-06 22:12:18 +02:00
# include "globals.h"
2022-05-07 21:12:18 +02:00
# include "module/ModuleData.h"
# include "utils/logger.h"
# include <memory>
2022-02-04 21:44:03 +01:00
# include <wums.h>
2020-06-01 16:35:34 +02:00
2022-05-07 21:12:18 +02:00
# ifdef DEBUG
2022-02-04 21:44:03 +01:00
static const char * * hook_names = ( const char * [ ] ) {
2021-09-17 16:22:54 +02:00
" WUMS_HOOK_INIT_WUT_MALLOC " ,
" WUMS_HOOK_FINI_WUT_MALLOC " ,
" WUMS_HOOK_INIT_WUT_NEWLIB " ,
" WUMS_HOOK_FINI_WUT_NEWLIB " ,
" WUMS_HOOK_INIT_WUT_STDCPP " ,
" WUMS_HOOK_FINI_WUT_STDCPP " ,
" WUMS_HOOK_INIT_WUT_DEVOPTAB " ,
" WUMS_HOOK_FINI_WUT_DEVOPTAB " ,
" WUMS_HOOK_INIT_WUT_SOCKETS " ,
" WUMS_HOOK_FINI_WUT_SOCKETS " ,
2022-01-23 22:07:38 +01:00
" WUMS_HOOK_INIT_WRAPPER " ,
" WUMS_HOOK_FINI_WRAPPER " ,
2020-06-01 16:35:34 +02:00
" WUMS_HOOK_INIT " ,
" WUMS_HOOK_APPLICATION_STARTS " ,
" WUMS_HOOK_APPLICATION_ENDS " ,
2021-09-17 16:22:54 +02:00
" WUMS_HOOK_RELOCATIONS_DONE " ,
2023-01-06 18:04:26 +01:00
" WUMS_HOOK_APPLICATION_REQUESTS_EXIT " ,
" WUMS_HOOK_DEINIT " } ;
2022-05-07 21:12:18 +02:00
# endif
2020-06-01 16:35:34 +02:00
2022-05-07 21:12:18 +02:00
void CallHook ( const std : : vector < std : : shared_ptr < ModuleData > > & modules , wums_hook_type_t type , bool condition ) {
2022-01-21 19:14:57 +01:00
if ( condition ) {
CallHook ( modules , type ) ;
}
}
2022-05-07 21:12:18 +02:00
void CallHook ( const std : : vector < std : : shared_ptr < ModuleData > > & modules , wums_hook_type_t type ) {
DEBUG_FUNCTION_LINE ( " Calling hook of type %s [%d] for all modules " , hook_names [ type ] , type ) ;
2022-02-04 21:44:03 +01:00
for ( auto & curModule : modules ) {
2020-06-03 19:37:32 +02:00
CallHook ( curModule , type ) ;
}
}
2022-05-07 21:12:18 +02:00
void CallHook ( const std : : shared_ptr < ModuleData > & module , wums_hook_type_t type , bool condition ) {
2022-01-21 19:14:57 +01:00
if ( condition ) {
CallHook ( module , type ) ;
}
}
2022-05-07 21:12:18 +02:00
void CallHook ( const std : : shared_ptr < ModuleData > & module , wums_hook_type_t type ) {
2022-02-04 21:44:03 +01:00
for ( auto & curHook : module - > getHookDataList ( ) ) {
2021-12-07 18:23:18 +01:00
auto func_ptr = ( uint32_t ) curHook - > getTarget ( ) ;
2020-06-06 22:12:18 +02:00
if ( func_ptr = = 0 ) {
2022-05-07 21:12:18 +02:00
DEBUG_FUNCTION_LINE_ERR ( " Module %s: hook ptr was NULL " , module - > getExportName ( ) . c_str ( ) ) ;
2020-06-06 22:12:18 +02:00
break ;
}
2021-12-07 18:23:18 +01:00
if ( type = = curHook - > getType ( ) ) {
2020-06-07 13:59:02 +02:00
if ( ( type = = WUMS_HOOK_APPLICATION_STARTS | |
2020-06-06 22:12:18 +02:00
type = = WUMS_HOOK_APPLICATION_ENDS | |
2021-09-17 16:22:54 +02:00
type = = WUMS_HOOK_INIT_WUT_MALLOC | |
type = = WUMS_HOOK_FINI_WUT_MALLOC | |
type = = WUMS_HOOK_INIT_WUT_NEWLIB | |
type = = WUMS_HOOK_FINI_WUT_NEWLIB | |
type = = WUMS_HOOK_INIT_WUT_STDCPP | |
type = = WUMS_HOOK_FINI_WUT_STDCPP | |
type = = WUMS_HOOK_INIT_WUT_DEVOPTAB | |
type = = WUMS_HOOK_FINI_WUT_DEVOPTAB | |
type = = WUMS_HOOK_INIT_WUT_SOCKETS | |
2022-01-23 22:07:38 +01:00
type = = WUMS_HOOK_FINI_WUT_SOCKETS | |
2023-01-06 18:04:26 +01:00
type = = WUMS_HOOK_FINI_WUT_SOCKETS | |
2022-01-23 22:07:38 +01:00
type = = WUMS_HOOK_INIT_WRAPPER | |
2023-01-06 18:04:26 +01:00
type = = WUMS_HOOK_FINI_WRAPPER | |
type = = WUMS_HOOK_DEINIT ) ) {
2022-05-07 21:12:18 +02:00
DEBUG_FUNCTION_LINE ( " Calling hook of type %s [%d] %d for %s: %08X " , hook_names [ type ] , type , curHook - > getType ( ) , module - > getExportName ( ) . c_str ( ) , curHook - > getTarget ( ) ) ;
2022-02-04 21:44:03 +01:00
( ( void ( * ) ( ) ) ( ( uint32_t * ) func_ptr ) ) ( ) ;
2020-06-10 16:20:20 +02:00
break ;
} else if ( type = = WUMS_HOOK_INIT | |
type = = WUMS_HOOK_RELOCATIONS_DONE ) {
2022-05-07 21:12:18 +02:00
DEBUG_FUNCTION_LINE ( " Calling hook of type %s [%d] %d for %s: %08X " , hook_names [ type ] , type , curHook - > getType ( ) , module - > getExportName ( ) . c_str ( ) , curHook - > getTarget ( ) ) ;
2020-06-06 22:12:18 +02:00
wums_app_init_args_t args ;
2022-05-07 21:12:18 +02:00
args . module_information = & gModuleInformation ;
2022-02-04 21:44:03 +01:00
( ( void ( * ) ( wums_app_init_args_t * ) ) ( ( uint32_t * ) func_ptr ) ) ( & args ) ;
2021-09-17 16:22:54 +02:00
} else {
2022-05-07 21:12:18 +02:00
DEBUG_FUNCTION_LINE_ERR ( " ######################################### " ) ;
DEBUG_FUNCTION_LINE_ERR ( " #########HOOK NOT IMPLEMENTED %d######### " , type ) ;
DEBUG_FUNCTION_LINE_ERR ( " ######################################### " ) ;
2020-06-01 16:35:34 +02:00
}
2020-06-06 22:12:18 +02:00
break ;
2020-06-01 16:35:34 +02:00
}
}
2020-06-06 22:12:18 +02:00
}