Skip to content

Commit

Permalink
stack-allocation: fail early when merging slices
Browse files Browse the repository at this point in the history
Fixes #54

(cherry picked from commit 6e6e80d)
(cherry picked from commit e32ed87)
  • Loading branch information
vbgl committed Mar 7, 2023
1 parent 2ce1448 commit 31e43af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
([PR #331](https://github.com/jasmin-lang/jasmin/pull/331);
fixes [#333](https://github.com/jasmin-lang/jasmin/issues/333)).

- Stack-allocation ensures that array slices are in bounds
([PR #363](https://github.com/jasmin-lang/jasmin/pull/363);
fixes [#54](https://github.com/jasmin-lang/jasmin/issues/54)).

# Jasmin 2022.04.0

This release is the result of more than two years of active development. It thus
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ let merge_slices params a s1 s2 =
else
let s1, s2 = if c < 0 then s1, s2 else s2, s1 in
let x = s1.in_var in
let y = s2.in_var in
let lo = fst s2.range - fst s1.range in
Mv.add x { s2 with range = lo, lo + size_of x.v_ty } a
let hi = lo + size_of x.v_ty in
if lo < 0 || size_of y.v_ty < hi
then hierror_no_loc "merging slices %a and %a may introduce invalid accesses; consider declaring variable %a smaller" pp_slice s1 pp_slice s2 pp_var x;
Mv.add x { s2 with range = lo, hi } a

(* Precondition: both maps are normalized *)
let merge params a1 a2 =
Expand Down
9 changes: 9 additions & 0 deletions compiler/tests/fail/stack_allocation/bug_54.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export
fn main () -> reg u64 {
reg u64 res;
stack u64[3] a, b;
b[1] = 42;
a[0:2] = b[1:2];
res = a[0];
return res;
}
10 changes: 10 additions & 0 deletions compiler/tests/success/bug_54.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export
fn main () -> reg u8 {
reg u8 res;
stack u8[3] a;
stack u8[4] b;
b[1] = 42;
a[0:2] = b[1:2];
res = a[0];
return res;
}

0 comments on commit 31e43af

Please sign in to comment.