Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stack overflow detection to sw-emulator #1785

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

clundin25
Copy link
Contributor

@clundin25 clundin25 commented Nov 12, 2024

Added stack monitoring and overflow detection to the SW emulator

If a stack overflow is detected, the emulator will panic and cause test failure.

This resolves #1735

@clundin25 clundin25 force-pushed the stack-overflow-monitor branch from 15c3882 to c1836a9 Compare November 18, 2024 18:00
@clundin25 clundin25 force-pushed the stack-overflow-monitor branch from c1836a9 to d6f8397 Compare December 2, 2024 17:58
@clundin25 clundin25 force-pushed the stack-overflow-monitor branch from d6f8397 to a99ab23 Compare December 3, 2024 01:42
@clundin25 clundin25 force-pushed the stack-overflow-monitor branch from a99ab23 to eca5e52 Compare December 3, 2024 18:06
@jhand2 jhand2 enabled auto-merge (rebase) December 4, 2024 18:37
@jhand2 jhand2 mentioned this pull request Dec 5, 2024
10 tasks
@jhand2 jhand2 merged commit 2f8f19b into chipsalliance:main Dec 5, 2024
11 checks passed
@@ -262,7 +401,24 @@ impl<TBus: Bus> Cpu<TBus> {
///
/// * `RvException` - Exception with cause `RvExceptionCause::IllegalRegister`
pub fn write_xreg(&mut self, reg: XReg, val: RvData) -> Result<(), RvException> {
self.xregs.write(reg, val)
// XReg::X2 is the sp register.
if reg == XReg::X2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, we can set the sp to another value as a temporary, or when initializing (we set it to 0 at the beginning of the ROM), and this isn't necessarily a stack overflow, so we might have to be a little careful here.

@@ -262,7 +401,24 @@ impl<TBus: Bus> Cpu<TBus> {
///
/// * `RvException` - Exception with cause `RvExceptionCause::IllegalRegister`
pub fn write_xreg(&mut self, reg: XReg, val: RvData) -> Result<(), RvException> {
self.xregs.write(reg, val)
// XReg::X2 is the sp register.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to document why this works: LLVM appears to always pre-allocate the stack by subtracting, and then using only positive relative offsets to SP. (Is that guaranteed though?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add stack overflow detection to SW emulator
4 participants