File: macrocheck.c

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (570 lines) | stat: -rw-r--r-- 16,462 bytes parent folder | download
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
561
562
563
564
565
566
567
568
569
570
/*
  Copyright 2015-2016 David Anderson. All rights reserved.

  This program is free software; you can redistribute it and/or
  modify it under the terms of version 2 of the GNU General
  Public License as published by the Free Software Foundation.

  This program is distributed in the hope that it would be
  useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  Further, this software is distributed without any warranty
  that it is free of the rightful claim of any third person
  regarding infringement or the like.  Any license provided
  herein, whether implied or otherwise, applies only to this
  software file.  Patent licenses, if any, provided herein
  do not apply to combinations of this program with other
  software, or any other product whatsoever.

  You should have received a copy of the GNU General Public
  License along with this program; if not, write the Free
  Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  Boston MA 02110-1301, USA.

*/

#include "globals.h"
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#include "dwarf_tsearch.h"
#include "macrocheck.h"
#include "esb.h"

#define TRUE 1
#define FALSE 0
/*  WARNING: the tree walk functions will, if presented **tree
    when *tree is wanted, simply find nothing. No error,
    just bad results. So when a walk produces nothing
    suspect a code mistake here.
    The basic problem is void* is a terrible way to
    pass in a pointer. But it's how tsearch was defined
    long ago.
*/

void *  macro_check_tree;    /* DWARF5 macros */
void *  macinfo_check_tree; /* DWARF 2,3,4 macros */

static struct Macrocheck_Map_Entry_s * macrocheck_map_insert(
    Dwarf_Unsigned off,
    unsigned prim,unsigned sec, void **map);
static struct Macrocheck_Map_Entry_s * macrocheck_map_find(
    Dwarf_Unsigned offset,
    void **map);
static void macrocheck_map_destroy(void *map);
static Dwarf_Unsigned macro_count_recs(void **base);

#ifdef SELFTEST
int failcount = 0;
struct glflags_s glflags;
#endif /* SELFTEST */


static struct Macrocheck_Map_Entry_s *
macrocheck_map_create_entry(Dwarf_Unsigned offset,
    unsigned add_primary, unsigned add_secondary)
{
    struct Macrocheck_Map_Entry_s *mp =
        (struct Macrocheck_Map_Entry_s *)
        calloc(1,sizeof(struct Macrocheck_Map_Entry_s));
    if (!mp) {
        return 0;
    }
    mp->mp_key = offset;
    mp->mp_len = 0;
    mp->mp_printed = 0;
    mp->mp_refcount_primary = add_primary;
    mp->mp_refcount_secondary = add_secondary;
    return mp;
}
static void
macrocheck_map_free_func(void *mx)
{
    struct Macrocheck_Map_Entry_s *m = mx;
    free(m);
}

static int
macrocheck_map_compare_func(const void *l, const void *r)
{
    const struct Macrocheck_Map_Entry_s *ml = l;
    const struct Macrocheck_Map_Entry_s *mr = r;
    if (ml->mp_key < mr->mp_key) {
        return -1;
    }
    if (ml->mp_key > mr->mp_key) {
        return 1;
    }
    return 0;
}

static struct Macrocheck_Map_Entry_s *
macrocheck_map_insert(Dwarf_Unsigned offset,
    unsigned add_prim,unsigned add_sec,void **tree1)
{
    void *retval = 0;
    struct Macrocheck_Map_Entry_s *re = 0;
    struct Macrocheck_Map_Entry_s *e;
    e  = macrocheck_map_create_entry(offset,add_prim,add_sec);
    /*  tsearch records e's contents unless e
        is already present . We must not free it till
        destroy time if it got added to tree1.  */
    retval = dwarf_tsearch(e,tree1, macrocheck_map_compare_func);
    if (retval) {
        re = *(struct Macrocheck_Map_Entry_s **)retval;
        if (re != e) {
            /*  We returned an existing record, e not needed.
                Increment refcounts. */
            re->mp_refcount_primary += add_prim;
            re->mp_refcount_secondary += add_sec;
            macrocheck_map_free_func(e);
        } else {
            /* Record e got added to tree1, do not free record e. */
        }
    }
    return NULL;
}

static struct Macrocheck_Map_Entry_s *
macrocheck_map_find(Dwarf_Unsigned offset,void **tree1)
{
    void *retval = 0;
    struct Macrocheck_Map_Entry_s *re = 0;
    struct Macrocheck_Map_Entry_s *e = 0;

    e = macrocheck_map_create_entry(offset,0,0);
    retval = dwarf_tfind(e,tree1, macrocheck_map_compare_func);
    if (retval) {
        re = *(struct Macrocheck_Map_Entry_s **)retval;
    }
    /*  The one we created here must be deleted, it is dead.
        We look at the returned one instead. */
    macrocheck_map_free_func(e);
    return re;
}

static void
macrocheck_map_destroy(void *map)
{
    /* tdestroy is not part of Posix. */
    dwarf_tdestroy(map,macrocheck_map_free_func);
}


void
add_macro_import(void **base,Dwarf_Bool is_primary,Dwarf_Unsigned offset)
{
    Dwarf_Unsigned prim_count  = 0;
    Dwarf_Unsigned sec_count  = 0;

    if(is_primary) {
        prim_count = 1;
    } else {
        sec_count = 1;
    }
    macrocheck_map_insert(offset,prim_count,sec_count,base);
}
void
add_macro_import_sup(UNUSEDARG void **base,
    UNUSEDARG Dwarf_Unsigned offset)
{
    /* FIXME */
    return;
}
void
add_macro_area_len(void **base, Dwarf_Unsigned offset,
    Dwarf_Unsigned len)
{
    struct Macrocheck_Map_Entry_s *re = 0;

    re = macrocheck_map_find(offset,base);
    if (re) {
        re->mp_len = len;
    }
}

static Dwarf_Unsigned reccount = 0;

static void
macro_walk_count_recs(UNUSEDARG const void *nodep,
    const DW_VISIT which,
    UNUSEDARG const int depth)
{
    if (which == dwarf_postorder || which == dwarf_endorder) {
        return;
    }
    reccount += 1;
}
static Dwarf_Unsigned
macro_count_recs(void **base)
{
    reccount = 0;
    dwarf_twalk(*base,macro_walk_count_recs);
    return reccount;
}

static Dwarf_Unsigned lowestoff = 0;
static Dwarf_Bool lowestfound = FALSE;
static void
macro_walk_find_lowest(const void *nodep,const DW_VISIT  which,
    UNUSEDARG const int  depth)
{
    struct Macrocheck_Map_Entry_s * re =
        *(struct Macrocheck_Map_Entry_s**)nodep;

    if (which == dwarf_postorder || which == dwarf_endorder) {
        return;
    }
    if (!re->mp_printed) {
        if (lowestfound) {
            if (lowestoff > re->mp_key) {
                lowestoff = re->mp_key;
            }
        } else {
            lowestfound = TRUE;
            lowestoff = re->mp_key;
        }
    }
}

/* Never returns DW_DLV_ERROR */
int
get_next_unprinted_macro_offset(void **tree, Dwarf_Unsigned * off)
{
    lowestfound = FALSE;
    lowestoff = 0;

    /*  This walks the tree to find one entry.
        Which could get slow if the tree has lots of entries. */
    dwarf_twalk(*tree,macro_walk_find_lowest);
    if (!lowestfound) {
        return DW_DLV_NO_ENTRY;
    }
    *off = lowestoff;
    return DW_DLV_OK;
}

void
mark_macro_offset_printed(void **base, Dwarf_Unsigned offset)
{
    struct Macrocheck_Map_Entry_s *re = 0;

    re = macrocheck_map_find(offset,base);
    if (re) {
        re->mp_printed = TRUE;
    }
}


static struct Macrocheck_Map_Entry_s **mac_as_array = 0;
static unsigned mac_as_array_next = 0;
static void
macro_walk_to_array(const void *nodep,const DW_VISIT  which,
    UNUSEDARG const int  depth)
{
    struct Macrocheck_Map_Entry_s * re =
        *(struct Macrocheck_Map_Entry_s**)nodep;

    if (which == dwarf_postorder || which == dwarf_endorder) {
        return;
    }
    mac_as_array[mac_as_array_next] = re;
    mac_as_array_next++;
}

static int
qsort_compare(const void *lin, const void *rin)
{
    const struct Macrocheck_Map_Entry_s *l =
        *(const struct Macrocheck_Map_Entry_s **)lin;
    const struct Macrocheck_Map_Entry_s *r =
        *(const struct Macrocheck_Map_Entry_s **)rin;
    if (l->mp_key < r->mp_key) {
        return -1;
    }
    if (l->mp_key > r->mp_key) {
        return 1;
    }
    if (l->mp_len < r->mp_len) {
        return -1;
    }
    if (l->mp_len > r->mp_len) {
        return 1;
    }
    return 0;
}

int
print_macro_statistics(const char *name,void **tsbase,
    Dwarf_Unsigned section_size,
    UNUSEDARG Dwarf_Error * err)
{
    Dwarf_Unsigned count = 0;
    Dwarf_Unsigned lowest = -1ll;
    Dwarf_Unsigned highest = 0;
    Dwarf_Unsigned lastend = 0;
    Dwarf_Unsigned laststart = 0;
    Dwarf_Unsigned internalgap = 0;
    Dwarf_Unsigned wholegap = 0;
    Dwarf_Unsigned i = 0;

    if(! *tsbase) {
        return DW_DLV_NO_ENTRY;
    }
    count = macro_count_recs(tsbase);
    if (count < 1) {
        return DW_DLV_NO_ENTRY;
    }
    free(mac_as_array);
    mac_as_array = 0;
    mac_as_array_next = 0;
    mac_as_array = (struct Macrocheck_Map_Entry_s **)calloc(count,
        sizeof(struct Macrocheck_Map_Entry_s *));
    if(!mac_as_array) {
#ifdef SELFTEST
        ++failcount;
#endif
        glflags.gf_count_major_errors++;
        printf("\nERROR:  Macro checking %s: "
            "unable to allocate %" DW_PR_DUu "pointers\n",
            name,
            count);
        /*  Return OK so dwarfdump.c won't look for Dwarf_Error */
        return DW_DLV_OK;
    }
    dwarf_twalk(*tsbase,macro_walk_to_array);
    printf("  Macro unit count %s: %" DW_PR_DUu "\n",name,count);
    qsort(mac_as_array,
        count,sizeof(struct Macrocheck_Map_Entry_s *),
        qsort_compare);
    for (i = 0; i < count ; ++i) {
        Dwarf_Unsigned end = 0;
        struct Macrocheck_Map_Entry_s *r = mac_as_array[i];
#if 0
        printf("debugging: i %u off 0x%x len 0x%x printed? %u "
            " ref prim: %u  sec: %u\n",
            (unsigned)i,
            (unsigned)r->mp_key,
            (unsigned)r->mp_len,
            (unsigned)r->mp_printed,
            (unsigned)r->mp_refcount_primary,
            (unsigned)r->mp_refcount_secondary);
#endif
        if (r->mp_key < lowest) {
            lowest = r->mp_key;
        }
        end = r->mp_key + r->mp_len;
        if (end > highest) {
            highest = end;
        }
        if (r->mp_refcount_primary > 1) {
#ifdef SELFTEST
            ++failcount;
#endif
            glflags.gf_count_major_errors++;
            printf("\nERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
                " %" DW_PR_DUu
                " there is a primary reference count of "
                "0x%"  DW_PR_XZEROS DW_PR_DUx
                " %"  DW_PR_DUu "\n",
                r->mp_key,
                r->mp_key,
                r->mp_refcount_primary,
                r->mp_refcount_primary);
        }
        if (r->mp_refcount_primary && r->mp_refcount_secondary) {
#ifdef SELFTEST
            ++failcount;
#endif
            glflags.gf_count_major_errors++;
            printf("\nERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
                " %" DW_PR_DUu
                " there is a nonzero primary count of "
                "0x%"  DW_PR_XZEROS DW_PR_DUx
                " %" DW_PR_DUu
                " with a secondary count of "
                "0x%"  DW_PR_XZEROS DW_PR_DUx
                " %" DW_PR_DUu
                "\n",
                r->mp_key,
                r->mp_key,
                r->mp_refcount_primary,
                r->mp_refcount_primary,
                r->mp_refcount_secondary,
                r->mp_refcount_secondary);
        }
    }
    lastend =
        mac_as_array[0]->mp_key +
        mac_as_array[0]->mp_len;
    laststart = mac_as_array[0]->mp_key;
    printf("  Macro Offsets start at 0x%" DW_PR_XZEROS DW_PR_DUx
        " and end at 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
        lowest, highest);
    for (i = 1; i < count ; ++i) {
        struct Macrocheck_Map_Entry_s *r = mac_as_array[i];
#if 0
        printf("debugging i %u off 0x%x len 0x%x\n",
            (unsigned)i,
            (unsigned)r->mp_key,
            (unsigned)r->mp_len);
#endif
        if (r->mp_key > lastend) {
            internalgap += (r->mp_key - lastend);
        } else if (r->mp_key < lastend) {
            /* crazy overlap */
#ifdef SELFTEST
            ++failcount;
#endif
            glflags.gf_count_major_errors++;
            printf(" ERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
                " %" DW_PR_DUu
                " there is a crazy overlap with the previous end offset of "
                "0x%"  DW_PR_XZEROS DW_PR_DUx
                " %"  DW_PR_DUu
                " (previous start offset of 0x%"  DW_PR_XZEROS DW_PR_DUx ")"
                " %"  DW_PR_DUu
                "\n",
                r->mp_key,
                r->mp_key,
                lastend,
                lastend,
                laststart,
                laststart);
        }
        laststart = r->mp_key;
        lastend   = laststart + r->mp_len;
    }
    /*  wholegap is a) starting offset > 0 and b)
        space after used area before end of section. */
    wholegap = mac_as_array[0]->mp_key + internalgap;
    if (lastend > section_size) {
        /* Something seriously wrong */
#ifdef SELFTEST
        ++failcount;
#endif
        printf(" ERROR: For offset 0x%" DW_PR_XZEROS DW_PR_DUx
            " %" DW_PR_DUu
            " there is an overlap with the end of section "
            "0x%"  DW_PR_XZEROS DW_PR_DUx
            " %" DW_PR_DUu
            "\n",laststart,laststart,
            lastend, lastend);
    } else {
        wholegap += (section_size - lastend);
    }
    if(wholegap) {
        printf("  Macro Offsets internal unused space: "
            "0x%" DW_PR_XZEROS DW_PR_DUx
            "\n",
            internalgap);
        printf("  Macro Offsets total    unused space: "
            "0x%" DW_PR_XZEROS DW_PR_DUx
            "\n",
            wholegap);
    }
    free (mac_as_array);
    mac_as_array = 0;
    mac_as_array_next = 0;
    return DW_DLV_OK;
}

void
clear_macro_statistics(void **tsbase)
{
    if(! *tsbase) {
        return;
    }
    macrocheck_map_destroy(*tsbase);
    *tsbase = 0;
}


#ifdef SELFTEST
int
main()
{
    void * base = 0;
    Dwarf_Unsigned count = 0;
    int basefailcount = 0;
    Dwarf_Error err = 0;

    /* Test 1 */
    add_macro_import(&base,TRUE,200);
    count = macro_count_recs(&base);
    if (count != 1) {
        printf("FAIL: expect count 1, got %" DW_PR_DUu "\n",count);
        ++failcount;
    }
    print_macro_statistics("test1",&base,2000,&err);

    /* Test two */
    add_macro_area_len(&base,200,100);
    add_macro_import(&base,FALSE,350);
    add_macro_area_len(&base,350,100);
    count = macro_count_recs(&base);
    if (count != 2) {
        printf("FAIL: expect count 2, got %" DW_PR_DUu "\n",count);
        ++failcount;
    }
    print_macro_statistics("test 2",&base,2000,&err);
    clear_macro_statistics(&base);

    /* Test three */
    basefailcount = failcount;
    add_macro_import(&base,TRUE,0);
    add_macro_area_len(&base,0,1000);
    add_macro_import(&base,FALSE,2000);
    add_macro_area_len(&base,2000,100);
    mark_macro_offset_printed(&base,2000);
    add_macro_import(&base,FALSE,1000);
    add_macro_area_len(&base,1000,900);
    add_macro_import(&base,FALSE,1000);
    add_macro_area_len(&base,1000,900);
    count = macro_count_recs(&base);
    if (count != 3) {
        printf("FAIL: expect count 3, got %" DW_PR_DUu "\n",count);
        ++failcount;
    }
    printf("\n  Expect an ERROR about overlap with "
        "the end of section\n");
    print_macro_statistics("test 3",&base,2000,&err);
    clear_macro_statistics(&base);
    if ((basefailcount+1) != failcount) {
        printf("FAIL: Found no error in test 3 checking!\n");
        ++failcount;
    } else {
        failcount = basefailcount;
    }

    /* Test Four */
    basefailcount = failcount;
    add_macro_import(&base,TRUE,50);
    add_macro_import(&base,TRUE,50);
    add_macro_area_len(&base,50,50);
    add_macro_import(&base,FALSE,200);
    add_macro_import(&base,FALSE,50);
    add_macro_import(&base,FALSE,60);
    add_macro_area_len(&base,60,10);
    printf( "\n  Expect an ERROR about offset 50 having 2 primaries\n");
    printf( "  and Expect an ERROR about offset 50 having 2\n"
        "  primaries"
        " and a secondary\n");
    printf( "  and Expect an ERROR about crazy overlap 60\n\n");
    print_macro_statistics("test 4",&base,2000,&err);
    clear_macro_statistics(&base);
    if ((basefailcount + 3) != failcount) {
        printf("FAIL: Found wrong errors in test 4 checking!\n");
    } else {
        failcount = basefailcount;
    }
    if (failcount > 0) {
        printf("FAIL macrocheck selftest\n");
        exit(1);
    }
    printf("PASS macrocheck selftest\n");
    return 0;
}
#endif /* SELFTEST */