Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
tests: Add tests which triggers stack canary
Browse files Browse the repository at this point in the history
The change to `canary.rs` it to be able to detect the stack overflow.
  • Loading branch information
Urhengulas committed Mar 1, 2023
1 parent bad542f commit 0d29b4f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
5 changes: 1 addition & 4 deletions src/canary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ impl Canary {
// When measuring stack consumption, we have to color the whole stack.
stack_available as usize
} else {
// We consider >90% stack usage a potential stack overflow, but don't go beyond 1 kb
// since filling a lot of RAM is slow (and 1 kb should be "good enough" for what we're
// doing).
round_up(1024.min(stack_available / 10), 4) as usize
round_up(stack_available / 10, 4) as usize
};

log::debug!(
Expand Down
1 change: 1 addition & 0 deletions tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn truncate_output(probe_run_output: String) -> String {
#[case::panic_verbose("panic-rzcobs --verbose", false)]
#[case::unsuccessful_run_can_suppress_backtrace("panic-rzcobs --backtrace=never", false)]
#[case::stack_overflow_can_suppress_backtrace("overflow-rzcobs --backtrace=never", false)]
#[case::canary("overflow-no-flip-link", false)]
#[serial]
#[ignore = "requires the target hardware to be present"]
fn snapshot_test(#[case] args: &str, #[case] success: bool) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/snapshot.rs
assertion_line: 113
assertion_line: 114
expression: run_result.output

---
Expand All @@ -23,6 +23,7 @@ ack(m=9, n=0, SP=2000ba18)
ack(m=8, n=1, SP=200079f8)
ack(m=8, n=0, SP=200039d8)
────────────────────────────────────────────────────────────────────────────────
(HOST) WARN program has used at least 240.62/254.93 KiB (94.4%) of stack space
stack backtrace:
0: HardFaultTrampoline
<exception entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/snapshot.rs
assertion_line: 113
assertion_line: 114
expression: run_result.output

---
Expand All @@ -23,5 +23,6 @@ ack(m=9, n=0, SP=2000ba18)
ack(m=8, n=1, SP=200079f8)
ack(m=8, n=0, SP=200039d8)
────────────────────────────────────────────────────────────────────────────────
(HOST) WARN program has used at least 240.62/254.93 KiB (94.4%) of stack space
(HOST) ERROR the program has overflowed its stack

33 changes: 33 additions & 0 deletions tests/snapshots/snapshot__case_9_canary.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: tests/snapshot.rs
assertion_line: 114
expression: run_result.output

---
(HOST) INFO flashing program (2 pages / 8.00 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
ack(m=10, n=10, SP=2003bfd8)
ack(m=10, n=9, SP=20037fb8)
ack(m=10, n=8, SP=20033f98)
ack(m=10, n=7, SP=2002ff78)
ack(m=10, n=6, SP=2002bf58)
ack(m=10, n=5, SP=20027f38)
ack(m=10, n=4, SP=20023f18)
ack(m=10, n=3, SP=2001fef8)
ack(m=10, n=2, SP=2001bed8)
ack(m=10, n=1, SP=20017eb8)
ack(m=10, n=0, SP=20013e98)
ack(m=9, n=1, SP=2000fe78)
ack(m=9, n=0, SP=2000be58)
ack(m=8, n=1, SP=20007e38)
ack(m=8, n=0, SP=20003e18)
────────────────────────────────────────────────────────────────────────────────
(HOST) WARN program has used at least 240.62/254.93 KiB (94.4%) of stack space
(HOST) WARN data segments might be corrupted due to stack overflow
stack backtrace:
0: HardFaultTrampoline
<exception entry>
(HOST) WARN call stack was corrupted; unwinding could not be completed
(HOST) ERROR the program has overflowed its stack

Binary file added tests/test_elfs/overflow-no-flip-link
Binary file not shown.

0 comments on commit 0d29b4f

Please sign in to comment.