我试图从我们的代码库中提取一些函数,以便它们可以加密存储.text
,然后只能使用正确的许可证密钥进行解密和访问.为此,我们需要(1)提取函数,(2)加载函数和(3)成功执行函数.
为了提取函数,我们依赖于gcc
将函数放在彼此之后似乎有效,因为它FUNCTION_LENGTH
似乎导致了正确的长度.但到目前为止,我一直无法加载函数并成功执行它们,我遇到了一个没有可理解信息的分段错误.
如何执行从char* buffer
?加载到内存中的代码?
example.cpp
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#define _FNAME_CAT(name, suffix) name##suffix
#define FNAME_CAT(name, suffix) _FNAME_CAT(name, suffix)
#define FUNCTION_START(ftype, fname, ...) \
typedef ftype (*FNAME_CAT(func_, fname))(__VA_ARGS__); \
ftype fname(__VA_ARGS__)
#define FUNCTION_END(ftype, fname) \
void FNAME_CAT(fname, _end)() { }
#define FUNCTION_LENGTH(fname) \
(int) ((intptr_t) FNAME_CAT(fname, _end) - (intptr_t) fname)
FUNCTION_START(void, test, int a, int b) {
printf("a(%d), b(%d)\n", a, b);
} FUNCTION_END(void, test);
char* function_code(void* func, int flen) …
Run Code Online (Sandbox Code Playgroud)