File: malloc_check.c

package info (click to toggle)
dwarfutils 20180809-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,228 kB
  • sloc: ansic: 63,382; sh: 5,222; cpp: 4,041; makefile: 548; python: 445; awk: 11
file content (327 lines) | stat: -rw-r--r-- 9,503 bytes parent folder | download | duplicates (3)
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
/*

  Copyright (C) 2005 Silicon Graphics, Inc.  All Rights Reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser 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 Lesser 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.

*/

/* malloc_check.c For checking dealloc completeness.

   This code is as simple as possible and works ok for
   reasonable size allocation counts.

   It treats allocation as global, and so will not
   work very well if an application opens more than one
   Dwarf_Debug.

*/

#include <stdio.h>
#include <stdlib.h>             /* for exit() and various malloc
    prototypes */
#include "config.h"
#include "dwarf_incl.h"
#include "malloc_check.h"
#ifdef  WANT_LIBBDWARF_MALLOC_CHECK

/*  To turn off printing every entry, just change the define
    to set PRINT_MALLOC_DETAILS 0.
*/
#define PRINT_MALLOC_DETAILS 0

#define MC_TYPE_UNKNOWN 0
#define MC_TYPE_ALLOC 1
#define MC_TYPE_DEALLOC 2

struct mc_data_s {
    struct mc_data_s *mc_prev;
    unsigned long mc_address;   /* Assumes this is large enough to hold
        a pointer! */

    long mc_alloc_number;       /* Assigned in order by when record
        created. */
    unsigned char mc_alloc_code;        /* Allocation code, libdwarf. */
    unsigned char mc_type;
    unsigned char mc_dealloc_noted;     /* Used on an ALLOC node. */
    unsigned char mc_dealloc_noted_count;       /* Used on an ALLOC
        node. */
};

/*

*/
#define HASH_TABLE_SIZE 10501
static struct mc_data_s *mc_data_hash[HASH_TABLE_SIZE];
static long mc_data_list_size = 0;

static char *alloc_type_name[MAX_DW_DLA + 1] = {
    "",
    "DW_DLA_STRING",
    "DW_DLA_LOC",
    "DW_DLA_LOCDESC",
    "DW_DLA_ELLIST",
    "DW_DLA_BOUNDS",
    "DW_DLA_BLOCK",
    "DW_DLA_DEBUG",
    "DW_DLA_DIE",
    "DW_DLA_LINE",
    "DW_DLA_ATTR",
    "DW_DLA_TYPE",
    "DW_DLA_SUBSCR",
    "DW_DLA_GLOBAL",
    "DW_DLA_ERROR",
    "DW_DLA_LIST",
    "DW_DLA_LINEBUF",
    "DW_DLA_ARANGE",
    "DW_DLA_ABBREV",
    "DW_DLA_FRAME_OP",
    "DW_DLA_CIE",
    "DW_DLA_FDE",
    "DW_DLA_LOC_BLOCK",
    "DW_DLA_FRAME_BLOCK",
    "DW_DLA_FUNC",
    "DW_DLA_TYPENAME",
    "DW_DLA_VAR",
    "DW_DLA_WEAK",
    "DW_DLA_ADDR",
    "DW_DLA_ABBREV_LIST",
    "DW_DLA_CHAIN",
    "DW_DLA_CU_CONTEXT",
    "DW_DLA_FRAME",
    "DW_DLA_GLOBAL_CONTEXT",
    "DW_DLA_FILE_ENTRY",
    "DW_DLA_LINE_CONTEXT",
    "DW_DLA_LOC_CHAIN",
    "DW_DLA_HASH_TABLE",
    "DW_DLA_FUNC_CONTEXT",
    "DW_DLA_TYPENAME_CONTEXT",
    "DW_DLA_VAR_CONTEXT",
    "DW_DLA_WEAK_CONTEXT",
    "DW_DLA_PUBTYPES_CONTEXT"
    /*  Don't forget to expand this list if the list of codes
        expands. */
};

static unsigned
hash_address(unsigned long addr)
{
    unsigned long a = addr >> 2;

    return a % HASH_TABLE_SIZE;
}

#if PRINT_MALLOC_DETAILS
static void
print_alloc_dealloc_detail(unsigned long addr,
    int code, char *whichisit)
{
    fprintf(stderr,
        "%s  addr 0x%lx code %d (%s) entry %ld\n",
        whichisit, addr, code, alloc_type_name[code],
        mc_data_list_size);
}
#else
#define  print_alloc_dealloc_detail(a,b,c)      /* nothing */
#endif

/* Create a zeroed struct or die. */
static void *
newone(void)
{
    struct mc_data_s *newd = malloc(sizeof(struct mc_data_s));

    if (newd == 0) {
        fprintf(stderr, "out of memory , # %ld\n", mc_data_list_size);
        exit(1);
    }
    memset(newd, 0, sizeof(struct mc_data_s));
    return newd;
}

/* Notify checker that get_alloc has allocated user data. */
void
dwarf_malloc_check_alloc_data(void *addr_in, unsigned char code)
{
    struct mc_data_s *newd = newone();
    unsigned long addr = (unsigned long) addr_in;
    struct mc_data_s **base = &mc_data_hash[hash_address(addr)];

    print_alloc_dealloc_detail(addr, code, "alloc   ");
    newd->mc_address = addr;
    newd->mc_alloc_code = code;
    newd->mc_type = MC_TYPE_ALLOC;
    newd->mc_alloc_number = mc_data_list_size;
    newd->mc_prev = *base;
    *base = newd;
    newd->mc_alloc_number = mc_data_list_size;
    mc_data_list_size += 1;
}

static void
print_entry(char *msg, struct mc_data_s *data)
{
    fprintf(stderr,
        "%s: 0x%08lx code %2d (%s) type %s dealloc noted %u ct %u\n",
        msg,
        (long) data->mc_address,
        data->mc_alloc_code,
        alloc_type_name[data->mc_alloc_code],
        (data->mc_type == MC_TYPE_ALLOC) ? "alloc  " :
        (data->mc_type == MC_TYPE_DEALLOC) ? "dealloc" : "unknown",
        (unsigned) data->mc_dealloc_noted,
        (unsigned) data->mc_dealloc_noted_count);
}

/* newd is a 'dealloc'.
*/
static long
balanced_by_alloc_p(struct mc_data_s *newd,
    long *addr_match_num,
    struct mc_data_s **addr_match,
    struct mc_data_s *base)
{
    struct mc_data_s *cur = base;

    for (; cur; cur = cur->mc_prev) {
        if (cur->mc_address == newd->mc_address) {
            if (cur->mc_type == MC_TYPE_ALLOC) {
                if (cur->mc_alloc_code == newd->mc_alloc_code) {
                    *addr_match = cur;
                    *addr_match_num = cur->mc_alloc_number;
                    return cur->mc_alloc_number;
                } else {
                    /* code mismatch */
                    *addr_match = cur;
                    *addr_match_num = cur->mc_alloc_number;
                    return -1;
                }
            } else {
                /* Unbalanced new/del */
                *addr_match = cur;
                *addr_match_num = cur->mc_alloc_number;
                return -1;
            }
        }
    }
    return -1;
}

/*  A dealloc is to take place. Ensure it balances an alloc.
*/
void
dwarf_malloc_check_dealloc_data(void *addr_in, unsigned char code)
{
    struct mc_data_s *newd = newone();
    long prev;
    long addr_match_num = -1;
    struct mc_data_s *addr_match = 0;
    unsigned long addr = (unsigned long) addr_in;
    struct mc_data_s **base = &mc_data_hash[hash_address(addr)];


    print_alloc_dealloc_detail(addr, code, "dealloc ");
    newd->mc_address = (unsigned long) addr;
    newd->mc_alloc_code = code;
    newd->mc_type = MC_TYPE_DEALLOC;
    newd->mc_prev = *base;
    prev =
        balanced_by_alloc_p(newd, &addr_match_num, &addr_match, *base);
    if (prev < 0) {
        fprintf(stderr,
            "Unbalanced dealloc at index %ld\n", mc_data_list_size);
        print_entry("new", newd);
        fprintf(stderr, "addr-match_num? %ld\n", addr_match_num);
        if (addr_match) {
            print_entry("prev entry", addr_match);
            if (addr_match->mc_dealloc_noted > 1) {
                fprintf(stderr, "Above is Duplicate dealloc!\n");
            }
        }
        abort();
        exit(3);
    }
    addr_match->mc_dealloc_noted = 1;
    addr_match->mc_dealloc_noted_count += 1;
    if (addr_match->mc_dealloc_noted_count > 1) {
        fprintf(stderr, "Double dealloc entry %ld\n", addr_match_num);
        print_entry("new dealloc entry", newd);
        print_entry("bad alloc entry", addr_match);
    }
    *base = newd;
    mc_data_list_size += 1;
}

/* Final check for leaks.
*/
void
dwarf_malloc_check_complete(char *msg)
{
    long i = 0;
    long total = mc_data_list_size;
    long hash_slots_used = 0;
    long max_chain_length = 0;

    fprintf(stderr, "Run complete, %s. %ld entries\n", msg, total);
    for (; i < HASH_TABLE_SIZE; ++i) {
        struct mc_data_s *cur = mc_data_hash[i];
        long cur_chain_length = 0;

        if (cur == 0)
            continue;
        ++hash_slots_used;
        for (; cur; cur = cur->mc_prev) {
            ++cur_chain_length;
            if (cur->mc_type == MC_TYPE_ALLOC) {
                if (cur->mc_dealloc_noted) {
                    if (cur->mc_dealloc_noted > 1) {
                        fprintf(stderr,
                            " Duplicate dealloc! entry %ld\n",
                            cur->mc_alloc_number);
                        print_entry("duplicate dealloc", cur);

                    }
                    continue;
                } else {
                    fprintf(stderr, "malloc no dealloc, entry %ld\n",
                        cur->mc_alloc_number);
                    print_entry("dangle", cur);
                }
            } else {
                /* mc_type is MC_TYPE_DEALLOC, already checked */

            }
        }
        if (cur_chain_length > max_chain_length) {
            max_chain_length = cur_chain_length;
        }
    }
    fprintf(stderr, "mc hash table slots=%ld, "
        "used=%ld,  maxchain=%ld\n",
        (long) HASH_TABLE_SIZE, hash_slots_used, max_chain_length);
    return;
}

#else

extern void *libdwarf_an_unused_function_so_not_empty_c_file(void);

#endif /* WANT_LIBBDWARF_MALLOC_CHECK */