debug_map.c 5.1 KiB raw
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
#include "debug_map.h"
6
#include "hosted.h"
7
#include "io.h"
8
9
u8 *debug_read_sidecar(const char *program, const char *suffix, size_t *size) {
10
    size_t length = strlen(program) + strlen(suffix) + 1;
11
    char  *path   = malloc(length);
12
    if (!path)
13
        bail("cannot allocate sidecar path");
14
    snprintf(path, length, "%s%s", program, suffix);
15
    FILE *file = fopen(path, "rb");
16
    free(path);
17
    if (!file)
18
        return NULL;
19
    long length_on_disk = -1;
20
    if (fseek(file, 0, SEEK_END) == 0)
21
        length_on_disk = ftell(file);
22
    u8 *data = NULL;
23
    if (length_on_disk >= 0 && fseek(file, 0, SEEK_SET) == 0) {
24
        *size = (size_t)length_on_disk;
25
        data  = malloc(*size ? *size : 1);
26
        if (!data)
27
            bail("cannot allocate sidecar");
28
        if (fread(data, 1, *size, file) != *size) {
29
            free(data);
30
            data = NULL;
31
        }
32
    }
33
    fclose(file);
34
    return data;
35
}
36
37
static bool load_paths(struct debug_map *map, size_t size, u32 code_bytes) {
38
    const u8 *data = map->data;
39
    if (size < 4)
40
        return false;
41
    u32    count = decode_u32_le(data);
42
    size_t pos   = 4;
43
    if (count > (size - pos) / 4)
44
        return false;
45
    /* Validate lengths and locations before allocating paths. */
46
    for (u32 i = 0; i < count; i++) {
47
        if (size - pos < 4)
48
            return false;
49
        u32 len  = decode_u32_le(data + pos);
50
        pos     += 4;
51
        if (!len || len > size - pos || memchr(data + pos, 0, len))
52
            return false;
53
        pos += len;
54
    }
55
    if ((size - pos) % 12)
56
        return false;
57
    for (size_t i = pos; i < size; i += 12) {
58
        u32 pc = decode_u32_le(data + i);
59
        if (pc > code_bytes || pc % INSTR_SIZE ||
60
            decode_u32_le(data + i + 8) >= count)
61
            return false;
62
    }
63
64
    map->path_count = count;
65
    map->count      = (size - pos) / 12;
66
    map->paths      = calloc(count, sizeof(*map->paths));
67
    map->entries    = calloc(map->count, sizeof(*map->entries));
68
    if ((count && !map->paths) || (map->count && !map->entries))
69
        bail("cannot allocate source map");
70
    pos = 4;
71
    for (u32 i = 0; i < count; i++) {
72
        u32 len        = decode_u32_le(data + pos);
73
        pos           += 4;
74
        map->paths[i]  = malloc((size_t)len + 1);
75
        if (!map->paths[i])
76
            bail("cannot allocate source path");
77
        memcpy(map->paths[i], data + pos, len);
78
        map->paths[i][len]  = 0;
79
        pos                += len;
80
    }
81
    for (size_t i = 0; i < map->count; i++, pos += 12) {
82
        map->entries[i] =
83
            (struct debug_entry){ decode_u32_le(data + pos),
84
                                  decode_u32_le(data + pos + 4),
85
                                  map->paths[decode_u32_le(data + pos + 8)] };
86
    }
87
    return true;
88
}
89
90
static bool load_legacy(struct debug_map *map, size_t size, u32 code_bytes) {
91
    size_t count = 0;
92
    for (size_t pos = 0; pos < size; count++) {
93
        if (size - pos < 10)
94
            return false;
95
        u32 pc = decode_u32_le(map->data + pos);
96
        if (pc > code_bytes || pc % INSTR_SIZE)
97
            return false;
98
        const u8 *path = map->data + pos + 8;
99
        const u8 *end  = memchr(path, 0, size - pos - 8);
100
        if (!end || end == path)
101
            return false;
102
        pos = (size_t)(end - map->data) + 1;
103
    }
104
    map->entries = calloc(count, sizeof(*map->entries));
105
    if (count && !map->entries)
106
        bail("cannot allocate source map");
107
    map->count = count;
108
    size_t pos = 0;
109
    for (size_t i = 0; i < count; i++) {
110
        const char *path = (const char *)map->data + pos + 8;
111
        map->entries[i] =
112
            (struct debug_entry){ decode_u32_le(map->data + pos),
113
                                  decode_u32_le(map->data + pos + 4),
114
                                  path };
115
        pos += 9 + strlen(path);
116
    }
117
    return true;
118
}
119
120
static int compare_pc(const void *a, const void *b) {
121
    u32 left  = ((const struct debug_entry *)a)->pc;
122
    u32 right = ((const struct debug_entry *)b)->pc;
123
    return (left > right) - (left < right);
124
}
125
126
bool debug_map_load(
127
    struct debug_map *map, const char *program, u32 code_bytes
128
) {
129
    size_t size = 0;
130
    map->data   = debug_read_sidecar(program, ".debug", &size);
131
    if (!map->data)
132
        return true;
133
    if (!load_paths(map, size, code_bytes) &&
134
        !load_legacy(map, size, code_bytes)) {
135
        debug_map_free(map);
136
        return false;
137
    }
138
    if (map->count > 1)
139
        qsort(map->entries, map->count, sizeof(*map->entries), compare_pc);
140
    return true;
141
}
142
143
const struct debug_entry *debug_map_lookup(
144
    const struct debug_map *map, u32 pc
145
) {
146
    size_t start = 0, end = map->count;
147
    while (start < end) {
148
        size_t middle = start + (end - start) / 2;
149
        if (map->entries[middle].pc <= pc)
150
            start = middle + 1;
151
        else
152
            end = middle;
153
    }
154
    return start ? &map->entries[start - 1] : NULL;
155
}
156
157
void debug_map_free(struct debug_map *map) {
158
    for (size_t i = 0; i < map->path_count; i++)
159
        free(map->paths[i]);
160
    free(map->paths);
161
    free(map->entries);
162
    free(map->data);
163
    *map = (struct debug_map){ 0 };
164
}