#include #include #include #include "debug_map.h" #include "hosted.h" #include "io.h" u8 *debug_read_sidecar(const char *program, const char *suffix, size_t *size) { size_t length = strlen(program) + strlen(suffix) + 1; char *path = malloc(length); if (!path) bail("cannot allocate sidecar path"); snprintf(path, length, "%s%s", program, suffix); FILE *file = fopen(path, "rb"); free(path); if (!file) return NULL; long length_on_disk = -1; if (fseek(file, 0, SEEK_END) == 0) length_on_disk = ftell(file); u8 *data = NULL; if (length_on_disk >= 0 && fseek(file, 0, SEEK_SET) == 0) { *size = (size_t)length_on_disk; data = malloc(*size ? *size : 1); if (!data) bail("cannot allocate sidecar"); if (fread(data, 1, *size, file) != *size) { free(data); data = NULL; } } fclose(file); return data; } static bool load_paths(struct debug_map *map, size_t size, u32 code_bytes) { const u8 *data = map->data; if (size < 4) return false; u32 count = decode_u32_le(data); size_t pos = 4; if (count > (size - pos) / 4) return false; /* Validate lengths and locations before allocating paths. */ for (u32 i = 0; i < count; i++) { if (size - pos < 4) return false; u32 len = decode_u32_le(data + pos); pos += 4; if (!len || len > size - pos || memchr(data + pos, 0, len)) return false; pos += len; } if ((size - pos) % 12) return false; for (size_t i = pos; i < size; i += 12) { u32 pc = decode_u32_le(data + i); if (pc > code_bytes || pc % INSTR_SIZE || decode_u32_le(data + i + 8) >= count) return false; } map->path_count = count; map->count = (size - pos) / 12; map->paths = calloc(count, sizeof(*map->paths)); map->entries = calloc(map->count, sizeof(*map->entries)); if ((count && !map->paths) || (map->count && !map->entries)) bail("cannot allocate source map"); pos = 4; for (u32 i = 0; i < count; i++) { u32 len = decode_u32_le(data + pos); pos += 4; map->paths[i] = malloc((size_t)len + 1); if (!map->paths[i]) bail("cannot allocate source path"); memcpy(map->paths[i], data + pos, len); map->paths[i][len] = 0; pos += len; } for (size_t i = 0; i < map->count; i++, pos += 12) { map->entries[i] = (struct debug_entry){ decode_u32_le(data + pos), decode_u32_le(data + pos + 4), map->paths[decode_u32_le(data + pos + 8)] }; } return true; } static bool load_legacy(struct debug_map *map, size_t size, u32 code_bytes) { size_t count = 0; for (size_t pos = 0; pos < size; count++) { if (size - pos < 10) return false; u32 pc = decode_u32_le(map->data + pos); if (pc > code_bytes || pc % INSTR_SIZE) return false; const u8 *path = map->data + pos + 8; const u8 *end = memchr(path, 0, size - pos - 8); if (!end || end == path) return false; pos = (size_t)(end - map->data) + 1; } map->entries = calloc(count, sizeof(*map->entries)); if (count && !map->entries) bail("cannot allocate source map"); map->count = count; size_t pos = 0; for (size_t i = 0; i < count; i++) { const char *path = (const char *)map->data + pos + 8; map->entries[i] = (struct debug_entry){ decode_u32_le(map->data + pos), decode_u32_le(map->data + pos + 4), path }; pos += 9 + strlen(path); } return true; } static int compare_pc(const void *a, const void *b) { u32 left = ((const struct debug_entry *)a)->pc; u32 right = ((const struct debug_entry *)b)->pc; return (left > right) - (left < right); } bool debug_map_load( struct debug_map *map, const char *program, u32 code_bytes ) { size_t size = 0; map->data = debug_read_sidecar(program, ".debug", &size); if (!map->data) return true; if (!load_paths(map, size, code_bytes) && !load_legacy(map, size, code_bytes)) { debug_map_free(map); return false; } if (map->count > 1) qsort(map->entries, map->count, sizeof(*map->entries), compare_pc); return true; } const struct debug_entry *debug_map_lookup( const struct debug_map *map, u32 pc ) { size_t start = 0, end = map->count; while (start < end) { size_t middle = start + (end - start) / 2; if (map->entries[middle].pc <= pc) start = middle + 1; else end = middle; } return start ? &map->entries[start - 1] : NULL; } void debug_map_free(struct debug_map *map) { for (size_t i = 0; i < map->path_count; i++) free(map->paths[i]); free(map->paths); free(map->entries); free(map->data); *map = (struct debug_map){ 0 }; }