Skip to content

Commit 35d1756

Browse files
committed
Add bounds checking to solve_phi_insertion
- Replace magic numbers with configuration constants (PHI_WORKLIST_SIZE, DCE_WORKLIST_SIZE) - Add overflow protection for PHI worklist to prevent buffer overruns - Improve safety checks in dead code elimination
1 parent 5af8dec commit 35d1756

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/ssa.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
/* SCCP (Sparse Conditional Constant Propagation) optimization */
1515
#include "opt-sccp.c"
1616

17+
/* Configuration constants - replace magic numbers */
18+
#define PHI_WORKLIST_SIZE 64
19+
#define DCE_WORKLIST_SIZE 2048
20+
1721
/* cfront does not accept structure as an argument, pass pointer */
1822
void bb_forward_traversal(bb_traversal_args_t *args)
1923
{
@@ -606,12 +610,17 @@ void solve_phi_insertion(void)
606610
for (symbol_t *sym = func->global_sym_list.head; sym; sym = sym->next) {
607611
var_t *var = sym->var;
608612

609-
basic_block_t *work_list[64];
613+
basic_block_t *work_list[PHI_WORKLIST_SIZE];
610614
int work_list_idx = 0;
611615

612616
for (ref_block_t *ref = var->ref_block_list.head; ref;
613-
ref = ref->next)
617+
ref = ref->next) {
618+
if (work_list_idx >= PHI_WORKLIST_SIZE - 1) {
619+
printf("Error: PHI worklist overflow\n");
620+
abort();
621+
}
614622
work_list[work_list_idx++] = ref->bb;
623+
}
615624

616625
for (int i = 0; i < work_list_idx; i++) {
617626
basic_block_t *bb = work_list[i];
@@ -656,8 +665,13 @@ void solve_phi_insertion(void)
656665
break;
657666
}
658667
}
659-
if (!found)
668+
if (!found) {
669+
if (work_list_idx >= PHI_WORKLIST_SIZE - 1) {
670+
printf("Error: PHI worklist overflow\n");
671+
abort();
672+
}
660673
work_list[work_list_idx++] = df;
674+
}
661675
}
662676
}
663677
}
@@ -1515,14 +1529,14 @@ int dce_init_mark(insn_t *insn, insn_t *work_list[], int work_list_idx)
15151529
/* Dead Code Elimination (DCE) */
15161530
void dce_insn(basic_block_t *bb)
15171531
{
1518-
insn_t *work_list[2048];
1532+
insn_t *work_list[DCE_WORKLIST_SIZE];
15191533
int work_list_idx = 0;
15201534

15211535
/* initially analyze current bb*/
15221536
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
15231537
int mark_num = dce_init_mark(insn, work_list, work_list_idx);
15241538
work_list_idx += mark_num;
1525-
if (work_list_idx > 2048 - 1) {
1539+
if (work_list_idx > DCE_WORKLIST_SIZE - 1) {
15261540
printf("size of work_list in DCE is not enough\n");
15271541
abort();
15281542
}

0 commit comments

Comments
 (0)