diff options
author | Adrian Kummerlaender | 2016-02-16 23:55:40 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2016-02-16 23:55:40 +0100 |
commit | 98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb (patch) | |
tree | 226264f5c53e851e0ff69ee1903f0d88eff4413e /src/actual.h | |
parent | e0b000a61f54db41286ededcc795318c79715d6e (diff) | |
download | change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.tar change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.tar.gz change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.tar.bz2 change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.tar.lz change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.tar.xz change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.tar.zst change-98f68dd9bb0318acfaaf7f2e7ad571a19729a8bb.zip |
Move actual function pointers to function local static variables
The previous approach of storing them in static variables of the `actual` namespace and initializing them statically did not work out as it is not guaranteed that they are initialized before any interposed function is called.
Diffstat (limited to 'src/actual.h')
-rw-r--r-- | src/actual.h | 46 |
1 files changed, 9 insertions, 37 deletions
diff --git a/src/actual.h b/src/actual.h index af64ec3..cb08d42 100644 --- a/src/actual.h +++ b/src/actual.h @@ -12,49 +12,21 @@ #include <memory> #include <cstring> -namespace { - template <class Result, typename... Arguments> - using function_ptr = Result(*)(Arguments...); - - template <typename FunctionPtr> - FunctionPtr get_actual_function(const std::string& symbol_name) { - const void* symbol_address{ dlsym(RTLD_NEXT, symbol_name.c_str()) }; - - FunctionPtr actual_function{}; - std::memcpy(&actual_function, &symbol_address, sizeof(symbol_address)); - - return actual_function; - } -} - namespace actual { - static auto write = get_actual_function< - function_ptr<ssize_t, int, const void*, size_t> - >("write"); - - static auto writev = get_actual_function< - function_ptr<ssize_t, int, const iovec*, int> - >("writev"); - static auto rename = get_actual_function< - function_ptr<int, const char*, const char*> - >("rename"); +template <class Result, typename... Arguments> +using ptr = Result(*)(Arguments...); - static auto rmdir = get_actual_function< - function_ptr<int, const char*> - >("rmdir"); +template <typename FunctionPtr> +FunctionPtr get_ptr(const std::string& symbol_name) { + const void* symbol_address{ dlsym(RTLD_NEXT, symbol_name.c_str()) }; - static auto unlink = get_actual_function< - function_ptr<int, const char*> - >("unlink"); + FunctionPtr actual_function{}; + std::memcpy(&actual_function, &symbol_address, sizeof(symbol_address)); - static auto unlinkat = get_actual_function< - function_ptr<int, int, const char*, int> - >("unlinkat"); + return actual_function; +} - static auto mmap = get_actual_function< - function_ptr<void*, void*, size_t, int, int, int, off_t> - >("mmap"); } #endif // CHANGE_SRC_ACTUAL_FUNCTION_H_ |