Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[4d808a]: opjitconv / jitsymbol.c Maximize Restore History

Download this file

jitsymbol.c    561 lines (502 with data), 14.6 kB

  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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/**
* @file jitsymbol.c
* Handle symbols found in jitted code dump
*
* @remark Copyright 2007 OProfile authors
* @remark Read the file COPYING
*
* @author Jens Wilke
* @Modifications Maynard Johnson
* @Modifications Philippe Elie
* @Modifications Daniel Hansel
*
* Copyright IBM Corporation 2007
*
*/
#include "opjitconv.h"
#include "opd_printf.h"
#include "op_libiberty.h"
#include "op_types.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
typedef int (*compare_symbol)(void const *, void const *);
/* count the entries in the jitentry_list */
static u32 count_entries(void)
{
struct jitentry const * entry;
u32 cnt = 0;
for (entry = jitentry_list; entry; entry = entry->next)
cnt++;
return cnt;
}
static void fill_entry_array(struct jitentry * entries[])
{
int i = 0;
struct jitentry * entry;
for (entry = jitentry_list; entry; entry = entry->next)
entries[i++] = entry;
}
/* create an array pointing to the jitentry structures which is sorted
* according to the comparator rule given by parameter compar
*/
static struct jitentry ** create_sorted_array(compare_symbol compare)
{
struct jitentry ** array =
xmalloc(sizeof(struct jitentry *) * entry_count);
fill_entry_array(array);
qsort(array, entry_count, sizeof(struct jitentry *), compare);
return array;
}
/* comparator method for qsort which sorts jitentries by symbol_name */
static int cmp_symbolname(void const * a, void const * b)
{
struct jitentry * a0 = *(struct jitentry **) a;
struct jitentry * b0 = *(struct jitentry **) b;
return strcmp(a0->symbol_name, b0->symbol_name);
}
/* comparator method for qsort which sorts jitentries by address */
static int cmp_address(void const * a, void const * b)
{
struct jitentry * a0 = *(struct jitentry **) a;
struct jitentry * b0 = *(struct jitentry **) b;
if (a0->vma < b0->vma)
return -1;
if (a0->vma == b0->vma)
return 0;
return 1;
}
/* resort address_ascending array */
static void resort_address(void)
{
u32 i;
qsort(entries_address_ascending, entry_count,
sizeof(struct jitentry *), cmp_address);
// lower entry_count if entries are invalidated
for (i = 0; i < entry_count; ++i) {
if (entries_address_ascending[i]->vma)
break;
}
if (i) {
entry_count -= i;
memmove(&entries_address_ascending[0],
&entries_address_ascending[i],
sizeof(struct jitentry *) * entry_count);
}
}
/* Copy address_ascending array to entries_symbols_ascending and resort it. */
static void resort_symbol(void)
{
memcpy(entries_symbols_ascending, entries_address_ascending,
sizeof(struct jitentry *) * entry_count);
qsort(entries_symbols_ascending, entry_count,
sizeof(struct jitentry *), cmp_symbolname);
}
/* allocate, populate and sort the jitentry arrays */
void create_arrays(void)
{
max_entry_count = entry_count = count_entries();
entries_symbols_ascending = create_sorted_array(cmp_symbolname);
entries_address_ascending = create_sorted_array(cmp_address);
}
/* add a new create jitentry to the array. mallocs new arrays if space is
* needed */
static void insert_entry(struct jitentry * entry)
{
if (entry_count == max_entry_count) {
if (max_entry_count < UINT32_MAX - 18)
max_entry_count += 18;
else if (max_entry_count < UINT32_MAX)
max_entry_count += 1;
else {
fprintf(stderr, "Amount of JIT dump file entries is too large.\n");
exit(EXIT_FAILURE);
}
entries_symbols_ascending = (struct jitentry **)
xrealloc(entries_symbols_ascending,
sizeof(struct jitentry *) * max_entry_count);
entries_address_ascending = (struct jitentry **)
xrealloc(entries_address_ascending,
sizeof(struct jitentry *) * max_entry_count);
}
entries_address_ascending[entry_count++] = entry;
}
/* add a suffix to the name to differenciate it */
static char * replacement_name(char * s, int i)
{
int cnt = 1;
int j = i;
char * res;
while ((j = j/10))
cnt++;
cnt += 2 + strlen(s);
res = xmalloc(cnt);
snprintf(res, cnt, "%s~%i", s, i);
return res;
}
/*
* Mark the entry so it is not included in the ELF file. We do this by
* writing a 0 address as magic vma and sorting
* it out later
*/
static void invalidate_entry(struct jitentry * e)
{
verbprintf(debug, "invalidate_entry: addr=%llx, name=%s\n",
e->vma, e->symbol_name);
e->vma = 0;
}
/*
* Invalidate all symbols that are not alive at sampling start time.
*/
static void invalidate_earlybirds(unsigned long long start_time)
{
u32 i;
int flag;
struct jitentry * a;
flag = 0;
for (i = 0; i < entry_count; i++) {
a = entries_address_ascending[i];
if (a->life_end < start_time) {
invalidate_entry(a);
flag = 1;
}
}
if (flag) {
resort_address();
resort_symbol();
}
}
/* select the symbol with the longest life time in the index range */
static int select_one(int start_idx, int end_idx)
{
int i;
int candidate = OP_JIT_CONV_FAIL;
unsigned long long lifetime = 0;
unsigned long long x;
struct jitentry const * e;
for (i = start_idx; i <= end_idx; i++) {
e = entries_address_ascending[i];
x = e->life_end - e->life_start;
if (candidate == -1 || x > lifetime) {
candidate = i;
lifetime = x;
}
}
return candidate;
}
/*
* We decided to keep one entry in favor of the other. Instead of dropping
* the overlapping entry we split or truncate it to not overlap any more.
*
* Looking on the address regions, we may have the following situation:
*
* split: |------------|
* keep: |---|
*
* The split entry may be splitted in a left part and a right part. E.g.:
*
* split0/1: |--| |---|
* keep: |---|
*
* However, both parts may or may not exist.
*/
static void split_entry(struct jitentry * split, struct jitentry const * keep)
{
unsigned long long start_addr_keep = keep->vma;
unsigned long long end_addr_keep = keep->vma + keep->code_size;
unsigned long long end_addr_split = split->vma + split->code_size;
unsigned long long start_addr_split = split->vma;
// do we need a right part?
if (end_addr_split > end_addr_keep) {
struct jitentry * new_entry =
xcalloc(1, sizeof(struct jitentry));
char * s = NULL;
/* Check for max. length to avoid possible integer overflow. */
if (strlen(split->symbol_name) > SIZE_MAX - 3) {
fprintf(stderr, "Length of symbol name is too large.\n");
exit(EXIT_FAILURE);
} else {
s = xmalloc(strlen(split->symbol_name) + 3);
strcpy(s, split->symbol_name);
strcat(s, "#1");
}
new_entry->vma = end_addr_keep;
new_entry->code_size = end_addr_split - end_addr_keep;
new_entry->symbol_name = s;
new_entry->sym_name_malloced = 1;
new_entry->life_start = split->life_start;
new_entry->life_end = split->life_end;
// the right part does not have an associated code, because we
// don't know whether the split part begins at an opcode
new_entry->code = NULL;
verbprintf(debug, "split right (new) name=%s, start=%llx,"
" end=%llx\n", new_entry->symbol_name,
new_entry->vma,
new_entry->vma + new_entry->code_size);
insert_entry(new_entry);
}
// do we need a left part?
if (start_addr_split < start_addr_keep) {
char * s = NULL;
/* Check for max. length to avoid possible integer overflow. */
if (strlen(split->symbol_name) > SIZE_MAX - 3) {
fprintf(stderr, "Length of symbol name is too large.\n");
exit(EXIT_FAILURE);
} else {
s = xmalloc(strlen(split->symbol_name) + 3);
strcpy(s, split->symbol_name);
strcat(s, "#0");
}
split->code_size = start_addr_keep - start_addr_split;
if (split->sym_name_malloced)
free(split->symbol_name);
split->symbol_name = s;
split->sym_name_malloced = 1;
verbprintf(debug, "split left name=%s, start=%llx, end=%llx\n",
split->symbol_name, split->vma,
split->vma + split->code_size);
} else {
invalidate_entry(split);
}
}
/*
* Scans through the index range and splits/truncates entries that overlap
* with the one indexed by keep_idx. Returns the total lifetime of the symbols
* found to overlap.
* Returns ULONG_MAX on error.
*/
static unsigned long long eliminate_overlaps(int start_idx, int end_idx,
int keep_idx)
{
unsigned long long retval;
struct jitentry const * keep = entries_address_ascending[keep_idx];
struct jitentry * e;
unsigned long long start_addr_keep = keep->vma;
unsigned long long end_addr_keep = keep->vma + keep->code_size;
unsigned long long start_addr_entry, end_addr_entry;
int i;
unsigned long long min_start = keep->life_start;
unsigned long long max_end = keep->life_end;
for (i = start_idx; i <= end_idx; i++) {
if (i == keep_idx)
continue;
e = entries_address_ascending[i];
start_addr_entry = e->vma;
end_addr_entry = e->vma + e->code_size;
if (debug) {
if (!(start_addr_entry <= end_addr_entry)) {
verbprintf(debug, "assert failed:"
" start_addr_entry <="
" end_addr_entry\n");
retval = ULONG_MAX;
goto out;
}
if (!(start_addr_keep <= end_addr_keep)) {
verbprintf(debug, "assert failed: "
"start_addr_keep <="
" end_addr_keep\n");
retval = ULONG_MAX;
goto out;
}
}
if (start_addr_entry < end_addr_keep &&
end_addr_entry > start_addr_keep) {
if (e->life_start < min_start)
min_start = e->life_start;
if (e->life_end > max_end)
max_end = e->life_end;
split_entry(e, keep);
}
}
retval = max_end - min_start;
out:
return retval;
}
/*
* Within the index range (into the array entries_address_ascending), find the
* symbol with the maximal lifetime and split/truncate all symbols that overlap
* with it (i.e. that there won't be any overlaps anymore).
*/
static int handle_overlap_region(int start_idx, int end_idx)
{
int rc = OP_JIT_CONV_OK;
int idx;
struct jitentry * e;
int cnt;
char * name;
int i, j;
unsigned long long totaltime;
if (debug) {
for (i = start_idx; i <= end_idx; i++) {
e = entries_address_ascending[i];
verbprintf(debug, "overlap idx=%i, name=%s, "
"start=%llx, end=%llx, life_start=%lli, "
"life_end=%lli, lifetime=%lli\n",
i, e->symbol_name, e->vma,
e->vma + e->code_size, e->life_start,
e->life_end, e->life_end - e->life_start);
}
}
idx = select_one(start_idx, end_idx);
// This can't happen, but we check anyway, just to silence Coverity
if (idx == OP_JIT_CONV_FAIL) {
rc = OP_JIT_CONV_FAIL;
goto out;
}
totaltime = eliminate_overlaps(start_idx, end_idx, idx);
if (totaltime == ULONG_MAX) {
rc = OP_JIT_CONV_FAIL;
goto out;
}
e = entries_address_ascending[idx];
cnt = 1;
j = (e->life_end - e->life_start) * 100 / totaltime;
while ((j = j/10))
cnt++;
// Mark symbol name with a %% to indicate the overlap.
cnt += strlen(e->symbol_name) + 2 + 1;
name = xmalloc(cnt);
snprintf(name, cnt, "%s%%%llu", e->symbol_name,
(e->life_end - e->life_start) * 100 / totaltime);
if (e->sym_name_malloced)
free(e->symbol_name);
e->symbol_name = name;
e->sym_name_malloced = 1;
verbprintf(debug, "selected idx=%i, name=%s\n", idx, e->symbol_name);
out:
return rc;
}
/*
* one scan through the symbols to find overlaps.
* return 1 if an overlap is found. this is repeated until no more overlap
* is there.
* Process: There may be more than two overlapping symbols with each other.
* The index range of symbols found to overlap are passed to
* handle_overlap_region.
*/
static int scan_overlaps(void)
{
int i, j;
unsigned long long end_addr, end_addr2;
struct jitentry const * a;
int flag = 0;
// entry_count can be incremented by split_entry() during the loop,
// save the inital value as loop count
int loop_count = entry_count;
verbprintf(debug,"count=%i, scan overlaps...\n", entry_count);
i = 0;
end_addr = 0;
for (j = 1; j < loop_count; j++) {
/**
* Take a symbol [i] and look for the next symbol(s) [j] that are overlapping
* symbol [i]. If a symbol [j] is found that is not overlapping symbol [i] the
* symbols [i]..[j-1] are handled to be not overlapping anymore.
* See descriptions of handle_overlap_region() and eliminate_overlaps() for
* further details of this handling.
* E.g. possible cases to be handled could be:
*
* sym1 |-----------|
* sym2 |---|
*
* sym1 |--------|
* sym2 |--------|
*
* sym1 |--------|
* sym2 |-------|
* sym3 |---------|
*
* But in the following case
*
* sym1 |--------|
* sym2 |-------------|
* sym3 |----------|
*
* sym3 would not overlap with sym1. Therefore handle_overlap_regio() would
* only be called for sym1 up to sym2.
*/
a = entries_address_ascending[j - 1];
end_addr2 = a->vma + a->code_size;
if (end_addr2 > end_addr)
end_addr = end_addr2;
a = entries_address_ascending[j];
if (end_addr <= a->vma) {
if (i != j - 1) {
if (handle_overlap_region(i, j - 1) ==
OP_JIT_CONV_FAIL) {
flag = OP_JIT_CONV_FAIL;
goto out;
}
flag = 1;
}
i = j;
}
}
if (i != j - 1) {
if (handle_overlap_region(i, j - 1) == OP_JIT_CONV_FAIL)
flag = OP_JIT_CONV_FAIL;
else
flag = 1;
}
out:
return flag;
}
/* search for symbols that have overlapping address ranges and decide for
* one */
int resolve_overlaps(unsigned long long start_time)
{
int rc = OP_JIT_CONV_OK;
int cnt = 0;
invalidate_earlybirds(start_time);
while ((rc = scan_overlaps()) && rc != OP_JIT_CONV_FAIL) {
resort_address();
if (cnt == 0) {
verbprintf(debug, "WARNING: overlaps detected. "
"Removing overlapping JIT methods\n");
}
cnt++;
}
if (cnt > 0 && rc != OP_JIT_CONV_FAIL)
resort_symbol();
return rc;
}
/*
* scan through the sorted array and replace identical symbol names by unique
* ones by adding a counter value.
*/
void disambiguate_symbol_names(void)
{
u32 j;
int cnt, rep_cnt;
struct jitentry * a;
struct jitentry * b;
rep_cnt = 0;
for (j = 1; j < entry_count; j++) {
a = entries_symbols_ascending[j - 1];
cnt = 1;
do {
b = entries_symbols_ascending[j];
if (strcmp(a->symbol_name, b->symbol_name) == 0) {
if (b->sym_name_malloced)
free(b->symbol_name);
b->symbol_name =
replacement_name(a->symbol_name, cnt);
b->sym_name_malloced = 1;
j++;
cnt++;
rep_cnt++;
} else {
break;
}
} while (j < entry_count);
}
/* recurse to avoid that the added suffix also creates a collision */
if (rep_cnt) {
qsort(entries_symbols_ascending, entry_count,
sizeof(struct jitentry *), cmp_symbolname);
disambiguate_symbol_names();
}
}