在 minSDK=22 或以上运行 Android 应用程序后,调用
free()
(filepaths
) 的 free(filePaths[i]);
函数后出现以下错误。 minSDK=21 一切正常
传递给 free 的地址 0x71db7cb5e0 无效:值未分配 我只想知道 minSDK=22 或更高版本的 Android 会发生什么情况。内存分配是否不同?
static inline void parse_proc_maps_to_fetch_path(char **filepaths);
JNIEXPORT jboolean JNICALL Java_io_github_inflationx_calligraphy_Calligraphy_CalligraphyInterceptor_detectFrida(JNIEnv *env, jobject obj) {
char *filePaths[NUM_LIBS];
globalEnv = env;
parse_proc_maps_to_fetch_path(filePaths);
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Libc[%x][%x][%x][%x][%x][%x]", __NR_openat,
__NR_lseek, __NR_read, __NR_close, __NR_readlinkat, __NR_nanosleep);
for (int i = 0; i < NUM_LIBS; i++) {
fetch_checksum_of_library(filePaths[i], &elfSectionArr[i]);
if (filePaths[i] != NULL)
free(filePaths[i]);
}
bool result = false;
pthread_t t;
pthread_create(&t, NULL, (void *) detect_frida_loop, &result);
return result;
}
__attribute__((always_inline))
static inline void parse_proc_maps_to_fetch_path(char **filepaths) {
int fd = 0;
char map[MAX_LINE];
int counter = 0;
if ((fd = my_openat(AT_FDCWD, PROC_MAPS, O_RDONLY | O_CLOEXEC, 0)) != 0) {
while ((read_one_line(fd, map, MAX_LINE)) > 0) {
for (int i = 0; i < NUM_LIBS; i++) {
if (my_strstr(map, libstocheck[i]) != NULL) {
char tmp[MAX_LENGTH] = "";
char path[MAX_LENGTH] = "";
char buf[5] = "";
sscanf(map, "%s %s %s %s %s %s", tmp, buf, tmp, tmp, tmp, path);
if (buf[2] == 'x') {
size_t size = my_strlen(path) + 1;
filepaths[i] = malloc(size);
my_strlcpy(filepaths[i], path, size);
counter++;
}
}
}
if (counter == NUM_LIBS)
break;
}
my_close(fd);
}
}
在
Java_io_github_inflationx_calligraphy_Calligraphy_CalligraphyInterceptor_detectFrida
中,定义 filePaths
,使数组中的值保持未初始化状态:
char *filePaths[NUM_LIBS];
在
parse_proc_maps_to_fetch_path
中,如果某些条件为真,则仅将值分配给 filepaths[i]
,否则这些元素未初始化。
您似乎假设
filePaths
的元素将是 NULL
默认值,但函数中的局部变量并非如此。要解决此问题,您可以初始化 filePaths
:
char *filePaths[NUM_LIBS] = { 0 };
您也可以以这样的方式构造
parse_proc_maps_to_fetch_path
,即始终为 filePaths
的所有元素分配值。如果 parse_proc_maps_to_fetch_path
旨在“生成”整个数组,这可能是更好的选择。