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-heap collision check to BrkSyscall execution #346

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/syscall/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,19 @@ class BrkSyscall : public BaseSyscall {

void execute() {
// Nothing to do - at the moment we allow infinite growth of the heap.
// @todo: Add checks to ensure that the heap doesn't grow into the stack
// segment...!

// Retrieves the argument of the brk syscall, the new program break
uint64_t newBreak = BaseSyscall::getArg(BaseSyscall::REG_FILE, 0);
// Retrieve the current stack pointer from the processor handler
uint64_t stackPointer =
ProcessorHandler::getProcessor()->getRegister(BaseSyscall::REG_FILE, 2);
if (newBreak >= stackPointer) {
SystemIO::printString(
"Error: Attempted to allocate memory overlapping stack segment\n");
BaseSyscall::setRet(BaseSyscall::REG_FILE, 0, -1); // Syscall error code
return;
}

BaseSyscall::setRet(BaseSyscall::REG_FILE, 0, 0);
}
};
Expand Down
35 changes: 35 additions & 0 deletions test/riscv-tests-64/brk_syscall.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.text
.globl _start
_start: nop

#-------------------------------------------------------------
# Stack-Heap Collision brk syscall Test
#-------------------------------------------------------------

test_0: # Test Ecall Return Value
mv a0, sp # Assign the ecall parameter to the stack pointer value
lw a7, BRK # Load the brk syscall immediate value
ecall

# If the return value of the ecall is zero, then the ecall
# was executed successfully and therefore the test failed
beqz a0, fail

bne x0, gp, pass

pass:
li a0, 42
li a7, 93
ecall


fail:
li a0, 0
li a7, 93
ecall

.data
.align 4

# Syscall immediate value
BRK: .word 214
27 changes: 27 additions & 0 deletions test/riscv-tests/brk_syscall.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.text

#-------------------------------------------------------------
# Stack-Heap Collision brk syscall Test
#-------------------------------------------------------------

test_0: # Test Ecall Return Value
mv a0, sp # Assign the ecall parameter to the stack pointer value
li a7, 214 # Load the brk syscall immediate value
ecall

# If the return value of the ecall is zero, then the ecall
# was executed successfully and therefore the test failed
beqz a0, fail

bne x0, gp, pass

pass:
li a0, 42
li a7, 93
ecall


fail:
li a0, 0
li a7, 93
ecall
Loading