1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| static void xh_core_refresh_impl() { char line[512]; FILE *fp; uintptr_t base_addr; uintptr_t prev_base_addr = 0; char perm[5]; char prev_perm[5] = "---p"; unsigned long offset; unsigned long prev_offset = 0; int pathname_pos; char *pathname; char prev_pathname[512] = {0}; size_t pathname_len; xh_core_map_info_t *mi, *mi_tmp; xh_core_map_info_t mi_key; xh_core_hook_info_t *hi; xh_core_ignore_info_t *ii; int match; xh_core_map_info_tree_t map_info_refreshed = RB_INITIALIZER(&map_info_refreshed);
if(NULL == (fp = fopen("/proc/self/maps", "r"))) { XH_LOG_ERROR("fopen /proc/self/maps failed"); return; }
while(fgets(line, sizeof(line), fp)) { if(sscanf(line, "%"PRIxPTR"-%*lx %4s %lx %*x:%*x %*d%n", &base_addr, perm, &offset, &pathname_pos) != 3) continue;
if (perm[3] != 'p') continue;
if (perm[0] == '-' && perm[1] == '-' && perm[2] == '-') continue;
while(isspace(line[pathname_pos]) && pathname_pos < (int)(sizeof(line) - 1)) pathname_pos += 1; if(pathname_pos >= (int)(sizeof(line) - 1)) continue; pathname = line + pathname_pos; pathname_len = strlen(pathname); if(0 == pathname_len) continue; if(pathname[pathname_len - 1] == '\n') { pathname[pathname_len - 1] = '\0'; pathname_len -= 1; } if(0 == pathname_len) continue; if('[' == pathname[0]) continue;
if (perm[2] != 'x') { prev_offset = offset; prev_base_addr = base_addr; memcpy(prev_perm, perm, sizeof(prev_perm)); strcpy(prev_pathname, pathname); continue; }
if (offset != 0) { if (strcmp(prev_pathname, pathname) || prev_offset != 0 || prev_perm[0] != 'r') { continue; } base_addr = prev_base_addr; }
match = 0; TAILQ_FOREACH(hi, &xh_core_hook_info, link) { if(0 == regexec(&(hi->pathname_regex), pathname, 0, NULL, 0)) { TAILQ_FOREACH(ii, &xh_core_ignore_info, link) { if(0 == regexec(&(ii->pathname_regex), pathname, 0, NULL, 0)) { if(NULL == ii->symbol) goto check_finished;
if(0 == strcmp(ii->symbol, hi->symbol)) goto check_continue; } }
match = 1; check_continue: break; } } check_finished: if(0 == match) continue;
if(0 != xh_core_check_elf_header(base_addr, pathname)) continue; mi_key.pathname = pathname; if(NULL != (mi = RB_FIND(xh_core_map_info_tree, &xh_core_map_info, &mi_key))) { RB_REMOVE(xh_core_map_info_tree, &xh_core_map_info, mi); if(NULL != RB_INSERT(xh_core_map_info_tree, &map_info_refreshed, mi)) { #if XH_CORE_DEBUG XH_LOG_DEBUG("repeated map info when update: %s", line); #endif free(mi->pathname); free(mi); continue; }
if(mi->base_addr != base_addr) { mi->base_addr = base_addr; xh_core_hook(mi); } } else { if(NULL == (mi = (xh_core_map_info_t *)malloc(sizeof(xh_core_map_info_t)))) continue; if(NULL == (mi->pathname = strdup(pathname))) { free(mi); continue; } mi->base_addr = base_addr;
if(NULL != RB_INSERT(xh_core_map_info_tree, &map_info_refreshed, mi)) { #if XH_CORE_DEBUG XH_LOG_DEBUG("repeated map info when create: %s", line); #endif free(mi->pathname); free(mi); continue; }
xh_core_hook(mi); } } fclose(fp);
RB_FOREACH_SAFE(mi, xh_core_map_info_tree, &xh_core_map_info, mi_tmp) { #if XH_CORE_DEBUG XH_LOG_DEBUG("remove missing map info: %s", mi->pathname); #endif RB_REMOVE(xh_core_map_info_tree, &xh_core_map_info, mi); if(mi->pathname) free(mi->pathname); free(mi); }
xh_core_map_info = map_info_refreshed;
XH_LOG_INFO("map refreshed"); #if XH_CORE_DEBUG RB_FOREACH(mi, xh_core_map_info_tree, &xh_core_map_info) XH_LOG_DEBUG(" %"PRIxPTR" %s\n", mi->base_addr, mi->pathname); #endif }
static void *xh_core_refresh_thread_func(void *arg) { (void)arg; pthread_setname_np(pthread_self(), "xh_refresh_loop");
while(xh_core_refresh_thread_running) { pthread_mutex_lock(&xh_core_mutex); while(!xh_core_refresh_thread_do && xh_core_refresh_thread_running) { pthread_cond_wait(&xh_core_cond, &xh_core_mutex); } if(!xh_core_refresh_thread_running) { pthread_mutex_unlock(&xh_core_mutex); break; } xh_core_refresh_thread_do = 0; pthread_mutex_unlock(&xh_core_mutex);
pthread_mutex_lock(&xh_core_refresh_mutex); xh_core_refresh_impl(); pthread_mutex_unlock(&xh_core_refresh_mutex); }
return NULL; }
static void xh_core_init_once() { if(xh_core_inited) return;
pthread_mutex_lock(&xh_core_mutex);
if(xh_core_inited) goto end;
xh_core_inited = 1; XH_LOG_INFO("%s\n", xh_version_str_full()); #if XH_CORE_DEBUG xh_core_hook_info_t *hi; TAILQ_FOREACH(hi, &xh_core_hook_info, link) XH_LOG_INFO(" hook: %s @ %s, (%p, %p)\n", hi->symbol, hi->pathname_regex_str, hi->new_func, hi->old_func); xh_core_ignore_info_t *ii; TAILQ_FOREACH(ii, &xh_core_ignore_info, link) XH_LOG_INFO(" ignore: %s @ %s\n", ii->symbol ? ii->symbol : "ALL ", ii->pathname_regex_str); #endif if(0 != xh_core_add_sigsegv_handler()) goto end;
xh_core_init_ok = 1;
end: pthread_mutex_unlock(&xh_core_mutex); }
|