Skip to content

Commit b02c1bd

Browse files
authored
[fix] Native ASM compiler checks stack overflow (#1416)
All frame pointers are known at compile time. So we can check stack overflow at compile time.
1 parent 830053d commit b02c1bd

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

extensions/native/compiler/src/asm/compiler.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,28 @@ impl<F> Var<F> {
4141
/// Gets the frame pointer for a var.
4242
pub const fn fp(&self) -> i32 {
4343
// Vars are stored in stack positions 1, 2, 9, 10, 17, 18, ...
44-
STACK_TOP - (8 * (self.0 / 2) + 1 + (self.0 % 2)) as i32
44+
let offset = (8 * (self.0 / 2) + 1 + (self.0 % 2)) as i32;
45+
assert!(offset < STACK_TOP, "Var fp overflow");
46+
STACK_TOP - offset
4547
}
4648
}
4749

4850
impl<F> Felt<F> {
4951
/// Gets the frame pointer for a felt.
5052
pub const fn fp(&self) -> i32 {
5153
// Felts are stored in stack positions 3, 4, 11, 12, 19, 20, ...
52-
STACK_TOP - (((self.0 >> 1) << 3) + 3 + (self.0 & 1)) as i32
54+
let offset = (((self.0 >> 1) << 3) + 3 + (self.0 & 1)) as i32;
55+
assert!(offset < STACK_TOP, "Felt fp overflow");
56+
STACK_TOP - offset
5357
}
5458
}
5559

5660
impl<F, EF> Ext<F, EF> {
5761
/// Gets the frame pointer for an extension element
5862
pub const fn fp(&self) -> i32 {
5963
// Exts are stored in stack positions 5-8, 13-16, 21-24, ...
64+
let offset = 8 * self.0 as i32;
65+
assert!(offset < STACK_TOP, "Ext fp overflow");
6066
STACK_TOP - 8 * self.0 as i32
6167
}
6268
}

0 commit comments

Comments
 (0)