Skip to content

Commit ad7db31

Browse files
committed
Replace arena_alloc with arena_calloc
Changed all structure allocations to use arena_calloc instead of arena_alloc to ensure zero-initialization and eliminate undefined behavior from accessing uninitialized memory. This fixes sanitizer errors related to: - block_t, var_t, basic_block_t structures in parsing - insn_t structures in SSA passes - ph2_ir_t structures in register allocation - constant_t, alias_t, macro_t structures in symbol management
1 parent 61bc355 commit ad7db31

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/globals.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,17 @@ symbol_t *arena_alloc_symbol(void)
308308

309309
constant_t *arena_alloc_constant(void)
310310
{
311-
return arena_alloc(GENERAL_ARENA, sizeof(constant_t));
311+
return arena_calloc(GENERAL_ARENA, 1, sizeof(constant_t));
312312
}
313313

314314
alias_t *arena_alloc_alias(void)
315315
{
316-
return arena_alloc(GENERAL_ARENA, sizeof(alias_t));
316+
return arena_calloc(GENERAL_ARENA, 1, sizeof(alias_t));
317317
}
318318

319319
macro_t *arena_alloc_macro(void)
320320
{
321-
return arena_alloc(GENERAL_ARENA, sizeof(macro_t));
321+
return arena_calloc(GENERAL_ARENA, 1, sizeof(macro_t));
322322
}
323323

324324
bb_traversal_args_t *arena_alloc_traversal_args(void)
@@ -614,7 +614,7 @@ ph2_ir_t *add_existed_ph2_ir(ph2_ir_t *ph2_ir)
614614

615615
ph2_ir_t *add_ph2_ir(opcode_t op)
616616
{
617-
ph2_ir_t *ph2_ir = arena_alloc(BB_ARENA, sizeof(ph2_ir_t));
617+
ph2_ir_t *ph2_ir = arena_calloc(BB_ARENA, 1, sizeof(ph2_ir_t));
618618
ph2_ir->op = op;
619619
/* Set safe defaults; arch-lowering may annotate later */
620620
ph2_ir->next = NULL;
@@ -631,7 +631,7 @@ void set_var_liveout(var_t *var, int end)
631631

632632
block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
633633
{
634-
block_t *blk = arena_alloc(BLOCK_ARENA, sizeof(block_t));
634+
block_t *blk = arena_calloc(BLOCK_ARENA, 1, sizeof(block_t));
635635

636636
blk->parent = parent;
637637
blk->func = func;
@@ -885,7 +885,7 @@ func_t *find_func(char *func_name)
885885
/* Create a basic block and set the scope of variables to 'parent' block */
886886
basic_block_t *bb_create(block_t *parent)
887887
{
888-
basic_block_t *bb = arena_alloc(BB_ARENA, sizeof(basic_block_t));
888+
basic_block_t *bb = arena_calloc(BB_ARENA, 1, sizeof(basic_block_t));
889889

890890
for (int i = 0; i < MAX_BB_PRED; i++) {
891891
bb->prev[i].bb = NULL;
@@ -1001,7 +1001,7 @@ void add_insn(block_t *block,
10011001

10021002
bb->scope = block;
10031003

1004-
insn_t *n = arena_alloc(INSN_ARENA, sizeof(insn_t));
1004+
insn_t *n = arena_calloc(INSN_ARENA, 1, sizeof(insn_t));
10051005
n->opcode = op;
10061006
n->rd = rd;
10071007
n->rs1 = rs1;

src/parser.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var_t *require_var(block_t *blk)
5050
var_list->elements = new_locals;
5151
}
5252

53-
var_t *var = arena_alloc(BLOCK_ARENA, sizeof(var_t));
53+
var_t *var = arena_calloc(BLOCK_ARENA, 1, sizeof(var_t));
5454
var_list->elements[var_list->size++] = var;
5555
var->consumed = -1;
5656
var->base = var;
@@ -3792,7 +3792,7 @@ void parse_internal(void)
37923792
/* set starting point of global stack manually */
37933793
GLOBAL_FUNC = add_func("", true);
37943794
GLOBAL_FUNC->stack_size = 4;
3795-
GLOBAL_FUNC->bbs = arena_alloc(BB_ARENA, sizeof(basic_block_t));
3795+
GLOBAL_FUNC->bbs = arena_calloc(BB_ARENA, 1, sizeof(basic_block_t));
37963796

37973797
/* built-in types */
37983798
TY_void = add_named_type("void");
@@ -3829,7 +3829,7 @@ void parse_internal(void)
38293829
func->return_def.type = TY_int;
38303830
func->num_params = 0;
38313831
func->va_args = 1;
3832-
func->bbs = arena_alloc(BB_ARENA, sizeof(basic_block_t));
3832+
func->bbs = arena_calloc(BB_ARENA, 1, sizeof(basic_block_t));
38333833

38343834
/* lexer initialization */
38353835
SOURCE->size = 0;

src/reg-alloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void refresh(basic_block_t *bb, insn_t *insn)
5353

5454
ph2_ir_t *bb_add_ph2_ir(basic_block_t *bb, opcode_t op)
5555
{
56-
ph2_ir_t *n = arena_alloc(BB_ARENA, sizeof(ph2_ir_t));
56+
ph2_ir_t *n = arena_calloc(BB_ARENA, 1, sizeof(ph2_ir_t));
5757
n->op = op;
5858
/* Ensure deterministic defaults for newly created IR nodes */
5959
n->next = NULL; /* well-formed singly linked list */

src/ssa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ bool insert_phi_insn(basic_block_t *bb, var_t *var)
583583
return false;
584584

585585
insn_t *head = bb->insn_list.head;
586-
insn_t *n = arena_alloc(INSN_ARENA, sizeof(insn_t));
586+
insn_t *n = arena_calloc(INSN_ARENA, 1, sizeof(insn_t));
587587
n->opcode = OP_phi;
588588
n->rd = var;
589589
n->rs1 = var;
@@ -805,7 +805,7 @@ void solve_phi_params(void)
805805

806806
void append_unwound_phi_insn(basic_block_t *bb, var_t *dest, var_t *rs)
807807
{
808-
insn_t *n = arena_alloc(INSN_ARENA, sizeof(insn_t));
808+
insn_t *n = arena_calloc(INSN_ARENA, 1, sizeof(insn_t));
809809
n->opcode = OP_unwound_phi;
810810
n->rd = dest;
811811
n->rs1 = rs;

0 commit comments

Comments
 (0)