@@ -32,16 +32,15 @@ SYSCALL_NODISCARD inline int close(int fd) {
3232 }
3333}
3434
35- // TODO: doesnt quite work yet
3635SYSCALL_NODISCARD inline Word brk (CPU& cpu, Word new_brk) {
3736
38- std::println (" new brk: {:#x}" , new_brk);
39-
37+ // TODO: create a util.hh wrapper for aligning to the NEXT page boundary
4038 auto new_addr = reinterpret_cast <void *>(align_to_page_size (new_brk)+getpagesize ());
4139 auto old_addr = reinterpret_cast <void *>(align_to_page_size (reinterpret_cast <size_t >(cpu.get_program_break ()))+getpagesize ());
4240
4341 // brk() syscall differs from the libc wrapper, in that it returns
4442 // the current program break when calling it with NULL
43+ // `man 2 brk` documents the libc wrapper, not the raw syscall!
4544 if (new_brk == 0 )
4645 return reinterpret_cast <Word>(old_addr);
4746
@@ -50,7 +49,8 @@ SYSCALL_NODISCARD inline Word brk(CPU& cpu, Word new_brk) {
5049
5150 ptrdiff_t size = static_cast <char *>(new_addr) - static_cast <char *>(old_addr);
5251
53- auto ret = ::mmap (
52+ // TODO: handle mmap() failure
53+ [[gnu::unused]] void * unused_ret = mmap (
5454 old_addr,
5555 size,
5656 PROT_WRITE | PROT_READ,
@@ -59,13 +59,10 @@ SYSCALL_NODISCARD inline Word brk(CPU& cpu, Word new_brk) {
5959 0
6060 );
6161
62- if (ret == MAP_FAILED) {
63- return -1 ;
64- }
65-
6662 cpu.set_program_break (new_addr);
6763
6864 } else if (new_addr < old_addr) {
65+ // TODO:
6966 // shrink the heap
7067
7168 // ptrdiff_t size = static_cast<char*>(old_addr) - static_cast<char*>(new_addr);
@@ -76,12 +73,9 @@ SYSCALL_NODISCARD inline Word brk(CPU& cpu, Word new_brk) {
7673 // return -1;
7774 // }
7875
79- } else if (new_addr == old_addr) {
80- // do nothing
81-
8276 }
8377
84- return 0 ;
78+ return new_brk ;
8579}
8680
8781} // namespace syscalls
0 commit comments