From 29f8a4edbe4ed7d61ac61296959a063ef76b80fa Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Thu, 18 Jul 2024 00:18:14 +0200 Subject: Add checks --- bxgen.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 28 deletions(-) (limited to 'bxgen.c') diff --git a/bxgen.c b/bxgen.c index 06a2f3c..7285905 100755 --- a/bxgen.c +++ b/bxgen.c @@ -569,9 +569,10 @@ i64 pool_add(Pool *pool, Entity data) { } void pool_remove(Pool *pool, i64 entity, i16 type) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(pool->entities[entity].is_enabled, "Entity already removed",); - BX_CHECK(pool->entities[entity].type == type, "Invalid entity type",); + BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(entity >= 0 && entity < pool->num_entities, "Buffer overflow",); + BX_CHECK(pool->entities[entity].is_enabled, "Entity already removed",); + BX_CHECK(pool->entities[entity].type == type, "Invalid entity type",); pool->entities[entity].is_enabled = 1; } @@ -664,6 +665,7 @@ void proc_destroy(Pool *pool, i64 proc) { void proc_set_convention(Pool *pool, i64 proc, i16 convention) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); @@ -672,6 +674,7 @@ void proc_set_convention(Pool *pool, i64 proc, i16 convention) { void proc_set_name(Pool *pool, i64 proc, i64 name_size, c8 *name) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); @@ -687,6 +690,7 @@ void proc_set_name(Pool *pool, i64 proc, i64 name_size, c8 *name) { void proc_node_add(Pool *pool, i64 proc, i64 node) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[proc].is_enabled, "Proc does not exist",); BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); BX_CHECK(pool->entities[node].is_enabled, "Node does not exist",); @@ -717,6 +721,8 @@ void proc_node_add(Pool *pool, i64 proc, i64 node) { void proc_node_remove(Pool *pool, i64 proc, i64 node) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + BX_CHECK(node >= 0 && node < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); BX_CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity type",); @@ -752,6 +758,8 @@ void unit_destroy(Pool *pool, i64 unit) { void unit_proc_add(Pool *pool, i64 unit, i64 proc) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); BX_CHECK(pool->entities[proc].is_enabled, "Proc does not exist",); @@ -773,6 +781,8 @@ void unit_proc_add(Pool *pool, i64 unit, i64 proc) { void unit_proc_remove(Pool *pool, i64 unit, i64 proc) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); @@ -791,11 +801,13 @@ void unit_proc_remove(Pool *pool, i64 unit, i64 proc) { } void unit_link_add(Pool *pool, i64 unit, i64 link_unit) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(pool->entities[link_unit].is_enabled, "Link does not exist",); - BX_CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); + BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + BX_CHECK(link_unit >= 0 && link_unit < pool->num_entities, "Buffer overflow",); + BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + BX_CHECK(pool->entities[link_unit].is_enabled, "Link does not exist",); + BX_CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); Unit *u = &pool->entities[unit].unit; @@ -808,10 +820,12 @@ void unit_link_add(Pool *pool, i64 unit, i64 link_unit) { } void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); + BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + BX_CHECK(link_unit >= 0 && link_unit < pool->num_entities, "Buffer overflow",); + BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + BX_CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); Unit *u = &pool->entities[unit].unit; @@ -826,6 +840,7 @@ void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) { void unit_set_name(Pool *pool, i64 unit, i64 name_size, c8 *name) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); @@ -841,6 +856,7 @@ void unit_set_name(Pool *pool, i64 unit, i64 name_size, c8 *name) { void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) { BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid unit type",); @@ -851,6 +867,7 @@ void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) { return; } + BX_CHECK(entry_point_proc >= 0 && entry_point_proc < pool->num_entities, "Buffer overflow",); BX_CHECK(pool->entities[entry_point_proc].is_enabled, "Internal",); BX_CHECK(pool->entities[entry_point_proc].type == ENTITY_PROC, "Internal",); @@ -1256,7 +1273,7 @@ enum { SYM_DATA_COMMON, SYM_DATA_THREAD_LOCAL, SYM_SECTION, - SYM_LOOS, + SYM_SPECIFIC, BIND_LOCAL = 0, BIND_GLOBAL, @@ -1558,13 +1575,13 @@ Symbol_Entry elf_symbol( BX_CHECK(dst.size == 0 || sym_value + sym_size <= dst.size, "Buffer overflow", (Symbol_Entry) {0}); - u8 type = (sym_info & 0xf) == 1 ? SYM_DATA : + u8 type = (sym_info & 0xf) == 0 ? SYM_NONE : + (sym_info & 0xf) == 1 ? SYM_DATA : (sym_info & 0xf) == 2 ? SYM_PROC : (sym_info & 0xf) == 3 ? SYM_SECTION : (sym_info & 0xf) == 5 ? SYM_DATA_COMMON : (sym_info & 0xf) == 6 ? SYM_DATA_THREAD_LOCAL : - (sym_info & 0xf) == 10 ? SYM_LOOS : - SYM_NONE; + SYM_SPECIFIC; BX_CHECK(type != SYM_NONE || (sym_info & 0xf) == 0, "Unknown symbol type", (Symbol_Entry) {0}); @@ -1982,7 +1999,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data case SYM_DATA_COMMON: printf("\x1b[33mdata "); break; case SYM_DATA_THREAD_LOCAL: printf("\x1b[35mdata "); break; case SYM_SECTION: printf("\x1b[31msection"); break; - case SYM_LOOS: printf("\x1b[31mloos "); break; + case SYM_SPECIFIC: printf("\x1b[31mspec "); break; default: printf(" "); } printf("\x1b[37m"); @@ -2006,13 +2023,32 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data Rela_Entry rela = elf_rela(buf, symbols, symbol_names, section.data, dst, rela_index); printf( - " %-4d %08llx %-+5lld <= %08llx \"%s\"\n", + " %-4d %08llx %-+5lld <= ", rela.type_, rela.dst, - rela.addent, - rela.symbol.value.offset, - elf_name_from_offset(buf, rela.symbol.name) + rela.addent ); + if (rela.symbol.bind == BIND_WEAK) + printf("\x1b[33m"); + else + printf("\x1b[32m"); + printf("%08llx", rela.symbol.value.offset); + printf("\x1b[37m"); + if (rela.symbol.type == SYM_DATA || + rela.symbol.type == SYM_DATA_COMMON || + rela.symbol.type == SYM_DATA_THREAD_LOCAL) + printf(" \x1b[34mdata"); + else if (rela.symbol.type == SYM_PROC) + printf(" \x1b[34mproc"); + else if (rela.symbol.type == SYM_SECTION) + printf(" \x1b[36msect"); + else if (rela.symbol.type == SYM_SPECIFIC) + printf(" \x1b[34mspec"); + else + printf(" \x1b[33mnone"); + printf("\x1b[37m"); + printf(" \"%s\"", elf_name_from_offset(buf, rela.symbol.name)); + printf("\n"); } printf("\n"); @@ -2031,13 +2067,28 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data for (u32 rel_index = 0; rel_index < section.num_entries; ++rel_index) { Rel_Entry rel = elf_rel(buf, symbols, symbol_names, section.data, dst, rel_index); - printf( - " %-4d %08llx <= %08llx \"%s\"\n", - rel.type_, - rel.dst, - rel.symbol.value.offset, - elf_name_from_offset(buf, rel.symbol.name) - ); + printf(" %-4d %08llx <= ", rel.type_, rel.dst); + if (rel.symbol.bind == BIND_WEAK) + printf("\x1b[33m"); + else + printf("\x1b[32m"); + printf("%08llx", rel.symbol.value.offset); + printf("\x1b[37m"); + if (rel.symbol.type == SYM_DATA || + rel.symbol.type == SYM_DATA_COMMON || + rel.symbol.type == SYM_DATA_THREAD_LOCAL) + printf(" \x1b[34mdata"); + else if (rel.symbol.type == SYM_PROC) + printf(" \x1b[34mproc"); + else if (rel.symbol.type == SYM_SECTION) + printf(" \x1b[36msect"); + else if (rel.symbol.type == SYM_SPECIFIC) + printf(" \x1b[31mspec"); + else + printf(" \x1b[33mnone"); + printf("\x1b[37m"); + printf(" \"%s\"", elf_name_from_offset(buf, rel.symbol.name)); + printf("\n"); } printf("\n"); -- cgit v1.2.3