From 712b14df9fca734ddd487fe6f9876ba88b4be895 Mon Sep 17 00:00:00 2001 From: ami-chuu Date: Mon, 30 Mar 2026 16:18:14 +0300 Subject: [PATCH] Added Stack Overflow test --- tests/stack_overflow.rs | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/stack_overflow.rs diff --git a/tests/stack_overflow.rs b/tests/stack_overflow.rs new file mode 100644 index 0000000..af2538a --- /dev/null +++ b/tests/stack_overflow.rs @@ -0,0 +1,58 @@ +#![no_std] +#![no_main] +#![feature(abi_x86_interrupt)] + +use amix::{exit_qemu, serial_print, serial_println, QemuExitCode}; +use core::panic::PanicInfo; +use lazy_static::lazy_static; +use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; + +#[unsafe(no_mangle)] +pub extern "C" fn _start() -> ! { + serial_print!("stack_overflow::stack_overflow...\t"); + + amix::gdt::init(); + init_test_idt(); + + // trigger a stack overflow + stack_overflow(); + + panic!("Execution continued after stack overflow"); +} + +#[allow(unconditional_recursion)] +fn stack_overflow() { + stack_overflow(); // for each recursion, the return address is pushed + volatile::Volatile::new(0).read(); // prevent tail recursion optimizations +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + amix::test_panic_handler(info) +} + +lazy_static! { + static ref TEST_IDT: InterruptDescriptorTable = { + let mut idt = InterruptDescriptorTable::new(); + unsafe { + idt.double_fault + .set_handler_fn(test_double_fault_handler) + .set_stack_index(amix::gdt::DOUBLE_FAULT_IST_INDEX); + } + + idt + }; +} + +pub fn init_test_idt() { + TEST_IDT.load(); +} + +extern "x86-interrupt" fn test_double_fault_handler( + _stack_frame: InterruptStackFrame, + _error_code: u64, +) -> ! { + serial_println!("[ok]"); + exit_qemu(QemuExitCode::Success); + loop {} +}