author | mbaesken |
Thu, 05 Sep 2019 09:59:43 +0200 | |
changeset 58017 | 9726449d2644 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
10565 | 1 |
/* |
58017
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
10565 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
#include <unistd.h> |
|
26 |
#include <search.h> |
|
27 |
#include <stdlib.h> |
|
28 |
#include <string.h> |
|
29 |
#include <db.h> |
|
30 |
#include <fcntl.h> |
|
16351 | 31 |
|
32 |
#include "libproc_impl.h" |
|
10565 | 33 |
#include "symtab.h" |
16351 | 34 |
#ifndef __APPLE__ |
10565 | 35 |
#include "salibelf.h" |
16351 | 36 |
#endif // __APPLE__ |
10565 | 37 |
|
38 |
||
39 |
// ---------------------------------------------------- |
|
40 |
// functions for symbol lookups |
|
41 |
// ---------------------------------------------------- |
|
42 |
||
16351 | 43 |
typedef struct symtab_symbol { |
44 |
char *name; // name like __ZThread_... |
|
45 |
uintptr_t offset; // to loaded address |
|
46 |
uintptr_t size; // size strlen |
|
47 |
} symtab_symbol; |
|
48 |
||
49 |
typedef struct symtab { |
|
50 |
char *strs; // all symbols "__symbol1__'\0'__symbol2__...." |
|
51 |
size_t num_symbols; |
|
52 |
DB* hash_table; |
|
53 |
symtab_symbol* symbols; |
|
54 |
} symtab_t; |
|
55 |
||
56 |
#ifdef __APPLE__ |
|
57 |
||
58 |
void build_search_table(symtab_t *symtab) { |
|
59 |
int i; |
|
60 |
for (i = 0; i < symtab->num_symbols; i++) { |
|
61 |
DBT key, value; |
|
62 |
key.data = symtab->symbols[i].name; |
|
63 |
key.size = strlen(key.data) + 1; |
|
64 |
value.data = &(symtab->symbols[i]); |
|
65 |
value.size = sizeof(symtab_symbol); |
|
66 |
(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0); |
|
67 |
||
68 |
// check result |
|
69 |
if (is_debug()) { |
|
70 |
DBT rkey, rvalue; |
|
71 |
char* tmp = (char *)malloc(strlen(symtab->symbols[i].name) + 1); |
|
58017
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
72 |
if (tmp == NULL) { |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
73 |
print_debug("error allocating array in build_search_table\n"); |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
74 |
} else { |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
75 |
strcpy(tmp, symtab->symbols[i].name); |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
76 |
rkey.data = tmp; |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
77 |
rkey.size = strlen(tmp) + 1; |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
78 |
(*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0); |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
79 |
// we may get a copy back so compare contents |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
80 |
symtab_symbol *res = (symtab_symbol *)rvalue.data; |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
81 |
if (strcmp(res->name, symtab->symbols[i].name) || |
16351 | 82 |
res->offset != symtab->symbols[i].offset || |
83 |
res->size != symtab->symbols[i].size) { |
|
58017
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
84 |
print_debug("error to get hash_table value!\n"); |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
85 |
} |
9726449d2644
8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents:
47216
diff
changeset
|
86 |
free(tmp); |
16351 | 87 |
} |
88 |
} |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
// read symbol table from given fd. |
|
93 |
struct symtab* build_symtab(int fd) { |
|
94 |
symtab_t* symtab = NULL; |
|
95 |
int i; |
|
96 |
mach_header_64 header; |
|
97 |
off_t image_start; |
|
98 |
||
99 |
if (!get_arch_off(fd, CPU_TYPE_X86_64, &image_start)) { |
|
100 |
print_debug("failed in get fat header\n"); |
|
101 |
return NULL; |
|
102 |
} |
|
103 |
lseek(fd, image_start, SEEK_SET); |
|
104 |
if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) { |
|
105 |
print_debug("reading header failed!\n"); |
|
106 |
return NULL; |
|
107 |
} |
|
108 |
// header |
|
109 |
if (header.magic != MH_MAGIC_64) { |
|
110 |
print_debug("not a valid .dylib file\n"); |
|
111 |
return NULL; |
|
112 |
} |
|
113 |
||
114 |
load_command lcmd; |
|
115 |
symtab_command symtabcmd; |
|
116 |
nlist_64 lentry; |
|
117 |
||
118 |
bool lcsymtab_exist = false; |
|
119 |
||
120 |
long filepos = ltell(fd); |
|
121 |
for (i = 0; i < header.ncmds; i++) { |
|
122 |
lseek(fd, filepos, SEEK_SET); |
|
123 |
if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) { |
|
124 |
print_debug("read load_command failed for file\n"); |
|
125 |
return NULL; |
|
126 |
} |
|
127 |
filepos += lcmd.cmdsize; // next command position |
|
128 |
if (lcmd.cmd == LC_SYMTAB) { |
|
129 |
lseek(fd, -sizeof(load_command), SEEK_CUR); |
|
130 |
lcsymtab_exist = true; |
|
131 |
break; |
|
132 |
} |
|
133 |
} |
|
134 |
if (!lcsymtab_exist) { |
|
135 |
print_debug("No symtab command found!\n"); |
|
136 |
return NULL; |
|
137 |
} |
|
138 |
if (read(fd, (void *)&symtabcmd, sizeof(symtab_command)) != sizeof(symtab_command)) { |
|
139 |
print_debug("read symtab_command failed for file"); |
|
140 |
return NULL; |
|
141 |
} |
|
142 |
symtab = (symtab_t *)malloc(sizeof(symtab_t)); |
|
143 |
if (symtab == NULL) { |
|
144 |
print_debug("out of memory: allocating symtab\n"); |
|
145 |
return NULL; |
|
146 |
} |
|
147 |
||
148 |
// create hash table, we use berkeley db to |
|
149 |
// manipulate the hash table. |
|
150 |
symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL); |
|
151 |
if (symtab->hash_table == NULL) |
|
152 |
goto quit; |
|
153 |
||
154 |
symtab->num_symbols = symtabcmd.nsyms; |
|
155 |
symtab->symbols = (symtab_symbol *)malloc(sizeof(symtab_symbol) * symtab->num_symbols); |
|
156 |
symtab->strs = (char *)malloc(sizeof(char) * symtabcmd.strsize); |
|
157 |
if (symtab->symbols == NULL || symtab->strs == NULL) { |
|
158 |
print_debug("out of memory: allocating symtab.symbol or symtab.strs\n"); |
|
159 |
goto quit; |
|
160 |
} |
|
161 |
lseek(fd, image_start + symtabcmd.symoff, SEEK_SET); |
|
162 |
for (i = 0; i < symtab->num_symbols; i++) { |
|
163 |
if (read(fd, (void *)&lentry, sizeof(nlist_64)) != sizeof(nlist_64)) { |
|
164 |
print_debug("read nlist_64 failed at %i\n", i); |
|
165 |
goto quit; |
|
166 |
} |
|
167 |
symtab->symbols[i].offset = lentry.n_value; |
|
168 |
symtab->symbols[i].size = lentry.n_un.n_strx; // index |
|
169 |
} |
|
170 |
||
171 |
// string table |
|
172 |
lseek(fd, image_start + symtabcmd.stroff, SEEK_SET); |
|
173 |
int size = read(fd, (void *)(symtab->strs), symtabcmd.strsize * sizeof(char)); |
|
174 |
if (size != symtabcmd.strsize * sizeof(char)) { |
|
175 |
print_debug("reading string table failed\n"); |
|
176 |
goto quit; |
|
177 |
} |
|
178 |
||
179 |
for (i = 0; i < symtab->num_symbols; i++) { |
|
180 |
symtab->symbols[i].name = symtab->strs + symtab->symbols[i].size; |
|
181 |
if (i > 0) { |
|
182 |
// fix size |
|
183 |
symtab->symbols[i - 1].size = symtab->symbols[i].size - symtab->symbols[i - 1].size; |
|
184 |
print_debug("%s size = %d\n", symtab->symbols[i - 1].name, symtab->symbols[i - 1].size); |
|
185 |
||
186 |
} |
|
187 |
||
188 |
if (i == symtab->num_symbols - 1) { |
|
189 |
// last index |
|
190 |
symtab->symbols[i].size = |
|
191 |
symtabcmd.strsize - symtab->symbols[i].size; |
|
192 |
print_debug("%s size = %d\n", symtab->symbols[i].name, symtab->symbols[i].size); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
// build a hashtable for fast query |
|
197 |
build_search_table(symtab); |
|
198 |
return symtab; |
|
199 |
quit: |
|
200 |
if (symtab) destroy_symtab(symtab); |
|
201 |
return NULL; |
|
202 |
} |
|
203 |
||
204 |
#else // __APPLE__ |
|
205 |
||
10565 | 206 |
struct elf_section { |
207 |
ELF_SHDR *c_shdr; |
|
208 |
void *c_data; |
|
209 |
}; |
|
210 |
||
211 |
// read symbol table from given fd. |
|
212 |
struct symtab* build_symtab(int fd) { |
|
213 |
ELF_EHDR ehdr; |
|
214 |
struct symtab* symtab = NULL; |
|
215 |
||
216 |
// Reading of elf header |
|
217 |
struct elf_section *scn_cache = NULL; |
|
218 |
int cnt = 0; |
|
219 |
ELF_SHDR* shbuf = NULL; |
|
220 |
ELF_SHDR* cursct = NULL; |
|
221 |
ELF_PHDR* phbuf = NULL; |
|
222 |
int symtab_found = 0; |
|
223 |
int dynsym_found = 0; |
|
224 |
uint32_t symsection = SHT_SYMTAB; |
|
225 |
||
226 |
uintptr_t baseaddr = (uintptr_t)-1; |
|
227 |
||
228 |
lseek(fd, (off_t)0L, SEEK_SET); |
|
229 |
if (! read_elf_header(fd, &ehdr)) { |
|
230 |
// not an elf |
|
231 |
return NULL; |
|
232 |
} |
|
233 |
||
234 |
// read ELF header |
|
235 |
if ((shbuf = read_section_header_table(fd, &ehdr)) == NULL) { |
|
236 |
goto quit; |
|
237 |
} |
|
238 |
||
239 |
baseaddr = find_base_address(fd, &ehdr); |
|
240 |
||
241 |
scn_cache = calloc(ehdr.e_shnum, sizeof(*scn_cache)); |
|
242 |
if (scn_cache == NULL) { |
|
243 |
goto quit; |
|
244 |
} |
|
245 |
||
246 |
for (cursct = shbuf, cnt = 0; cnt < ehdr.e_shnum; cnt++) { |
|
247 |
scn_cache[cnt].c_shdr = cursct; |
|
248 |
if (cursct->sh_type == SHT_SYMTAB || |
|
249 |
cursct->sh_type == SHT_STRTAB || |
|
250 |
cursct->sh_type == SHT_DYNSYM) { |
|
251 |
if ( (scn_cache[cnt].c_data = read_section_data(fd, &ehdr, cursct)) == NULL) { |
|
252 |
goto quit; |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
if (cursct->sh_type == SHT_SYMTAB) |
|
257 |
symtab_found++; |
|
258 |
||
259 |
if (cursct->sh_type == SHT_DYNSYM) |
|
260 |
dynsym_found++; |
|
261 |
||
262 |
cursct++; |
|
263 |
} |
|
264 |
||
265 |
if (!symtab_found && dynsym_found) |
|
266 |
symsection = SHT_DYNSYM; |
|
267 |
||
268 |
for (cnt = 1; cnt < ehdr.e_shnum; cnt++) { |
|
269 |
ELF_SHDR *shdr = scn_cache[cnt].c_shdr; |
|
270 |
||
271 |
if (shdr->sh_type == symsection) { |
|
272 |
ELF_SYM *syms; |
|
10739 | 273 |
int j, n; |
10565 | 274 |
size_t size; |
275 |
||
276 |
// FIXME: there could be multiple data buffers associated with the |
|
277 |
// same ELF section. Here we can handle only one buffer. See man page |
|
278 |
// for elf_getdata on Solaris. |
|
279 |
||
280 |
// guarantee(symtab == NULL, "multiple symtab"); |
|
281 |
symtab = calloc(1, sizeof(*symtab)); |
|
282 |
if (symtab == NULL) { |
|
283 |
goto quit; |
|
284 |
} |
|
285 |
// the symbol table |
|
286 |
syms = (ELF_SYM *)scn_cache[cnt].c_data; |
|
287 |
||
288 |
// number of symbols |
|
289 |
n = shdr->sh_size / shdr->sh_entsize; |
|
290 |
||
291 |
// create hash table, we use berkeley db to |
|
292 |
// manipulate the hash table. |
|
293 |
symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL); |
|
294 |
// guarantee(symtab->hash_table, "unexpected failure: dbopen"); |
|
10739 | 295 |
if (symtab->hash_table == NULL) |
296 |
goto bad; |
|
10565 | 297 |
|
298 |
// shdr->sh_link points to the section that contains the actual strings |
|
299 |
// for symbol names. the st_name field in ELF_SYM is just the |
|
300 |
// string table index. we make a copy of the string table so the |
|
301 |
// strings will not be destroyed by elf_end. |
|
302 |
size = scn_cache[shdr->sh_link].c_shdr->sh_size; |
|
303 |
symtab->strs = malloc(size); |
|
10739 | 304 |
if (symtab->strs == NULL) |
305 |
goto bad; |
|
10565 | 306 |
memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size); |
307 |
||
308 |
// allocate memory for storing symbol offset and size; |
|
309 |
symtab->num_symbols = n; |
|
310 |
symtab->symbols = calloc(n , sizeof(*symtab->symbols)); |
|
10739 | 311 |
if (symtab->symbols == NULL) |
312 |
goto bad; |
|
10565 | 313 |
|
314 |
// copy symbols info our symtab and enter them info the hash table |
|
315 |
for (j = 0; j < n; j++, syms++) { |
|
316 |
DBT key, value; |
|
317 |
char *sym_name = symtab->strs + syms->st_name; |
|
318 |
||
319 |
// skip non-object and non-function symbols |
|
320 |
int st_type = ELF_ST_TYPE(syms->st_info); |
|
321 |
if ( st_type != STT_FUNC && st_type != STT_OBJECT) |
|
322 |
continue; |
|
323 |
// skip empty strings and undefined symbols |
|
324 |
if (*sym_name == '\0' || syms->st_shndx == SHN_UNDEF) continue; |
|
325 |
||
326 |
symtab->symbols[j].name = sym_name; |
|
327 |
symtab->symbols[j].offset = syms->st_value - baseaddr; |
|
328 |
symtab->symbols[j].size = syms->st_size; |
|
329 |
||
330 |
key.data = sym_name; |
|
331 |
key.size = strlen(sym_name) + 1; |
|
332 |
value.data = &(symtab->symbols[j]); |
|
16351 | 333 |
value.size = sizeof(symtab_symbol); |
10565 | 334 |
(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0); |
335 |
} |
|
336 |
} |
|
337 |
} |
|
10739 | 338 |
goto quit; |
339 |
||
340 |
bad: |
|
341 |
destroy_symtab(symtab); |
|
342 |
symtab = NULL; |
|
10565 | 343 |
|
344 |
quit: |
|
345 |
if (shbuf) free(shbuf); |
|
346 |
if (phbuf) free(phbuf); |
|
347 |
if (scn_cache) { |
|
348 |
for (cnt = 0; cnt < ehdr.e_shnum; cnt++) { |
|
349 |
if (scn_cache[cnt].c_data != NULL) { |
|
350 |
free(scn_cache[cnt].c_data); |
|
351 |
} |
|
352 |
} |
|
353 |
free(scn_cache); |
|
354 |
} |
|
355 |
return symtab; |
|
356 |
} |
|
357 |
||
16351 | 358 |
#endif // __APPLE__ |
359 |
||
360 |
void destroy_symtab(symtab_t* symtab) { |
|
10565 | 361 |
if (!symtab) return; |
16351 | 362 |
free(symtab->strs); |
363 |
free(symtab->symbols); |
|
10565 | 364 |
free(symtab); |
365 |
} |
|
366 |
||
16351 | 367 |
uintptr_t search_symbol(struct symtab* symtab, uintptr_t base, const char *sym_name, int *sym_size) { |
10565 | 368 |
DBT key, value; |
369 |
int ret; |
|
370 |
||
371 |
// library does not have symbol table |
|
16351 | 372 |
if (!symtab || !symtab->hash_table) { |
10565 | 373 |
return 0; |
16351 | 374 |
} |
10565 | 375 |
|
376 |
key.data = (char*)(uintptr_t)sym_name; |
|
377 |
key.size = strlen(sym_name) + 1; |
|
378 |
ret = (*symtab->hash_table->get)(symtab->hash_table, &key, &value, 0); |
|
379 |
if (ret == 0) { |
|
16351 | 380 |
symtab_symbol *sym = value.data; |
10565 | 381 |
uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset); |
382 |
if (sym_size) *sym_size = sym->size; |
|
383 |
return rslt; |
|
384 |
} |
|
385 |
||
386 |
return 0; |
|
387 |
} |
|
388 |
||
389 |
const char* nearest_symbol(struct symtab* symtab, uintptr_t offset, |
|
390 |
uintptr_t* poffset) { |
|
391 |
int n = 0; |
|
392 |
if (!symtab) return NULL; |
|
393 |
for (; n < symtab->num_symbols; n++) { |
|
16351 | 394 |
symtab_symbol* sym = &(symtab->symbols[n]); |
10739 | 395 |
if (sym->name != NULL && |
396 |
offset >= sym->offset && offset < sym->offset + sym->size) { |
|
397 |
if (poffset) *poffset = (offset - sym->offset); |
|
398 |
return sym->name; |
|
399 |
} |
|
10565 | 400 |
} |
401 |
return NULL; |
|
402 |
} |