Skip to content

Commit 07c7e0c

Browse files
committed
fixed brk syscall
1 parent 7b93d03 commit 07c7e0c

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

src/memory.hh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,9 @@ public:
5252
}
5353

5454
[[nodiscard]] void* get_initial_program_break() const {
55+
// TODO: align to next page
5556
auto& [addr, len] = m_mapped_segments.back();
56-
size_t program_break = reinterpret_cast<size_t>(addr) + len;
57-
// set the program break to the next full page, to avoid address collisions with the
58-
// previous page from the data segment
59-
// TODO: dont jump to the next page if program_break is perfectly page aligned
60-
size_t aligned = align_to_page_size(program_break) + getpagesize();
61-
return reinterpret_cast<void*>(aligned);
57+
return reinterpret_cast<char*>(addr) + len;
6258
}
6359

6460
private:

src/probe/test.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,24 @@ void dynarray_push_back(DynamicArray *dynarray, Item item) {
3434

3535
int main(void) {
3636

37-
int *mem = malloc(20);
38-
if (mem == NULL)
39-
printf("is null\n");
40-
41-
// DynamicArray dynarray = {0};
42-
// dynarray_init(&dynarray);
43-
// dynarray_push_back(&dynarray, 1);
37+
// int *mem = malloc(20);
38+
// if (mem == NULL)
39+
// printf("is null\n");
40+
41+
DynamicArray dynarray = {0};
42+
dynarray_init(&dynarray);
43+
dynarray_push_back(&dynarray, 1);
44+
dynarray_push_back(&dynarray, 2);
45+
dynarray_push_back(&dynarray, 3);
46+
dynarray_push_back(&dynarray, 4);
47+
dynarray_push_back(&dynarray, 5);
48+
dynarray_push_back(&dynarray, 6);
49+
dynarray_push_back(&dynarray, 7);
50+
dynarray_push_back(&dynarray, 8);
51+
dynarray_push_back(&dynarray, 9);
52+
53+
for (size_t i=0; i < dynarray.length; ++i) {
54+
printf("%d\n", dynarray.items[i]);
55+
}
4456

4557
}

src/syscalls.hh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ SYSCALL_NODISCARD inline int close(int fd) {
3232
}
3333
}
3434

35-
// TODO: doesnt quite work yet
3635
SYSCALL_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

src/util.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void log(std::format_string<Args...> fmt, Args&& ...args) {
3434
std::println(stderr, fmt, std::forward<Args>(args)...);
3535
}
3636

37-
[[nodiscard]] inline size_t align_to_page_size(size_t address) {
37+
[[nodiscard]] constexpr inline size_t align_to_page_size(size_t address) {
3838
return address - address % getpagesize();
3939
}
4040

0 commit comments

Comments
 (0)