Commit: 4421c6c6fbfa37fb60c7d1b348c4bc8a210bbca2
Parent: cbf03635c7e1f678da8d1a3becb06b2d7a273929
Author: Randy Palamar
Date: Thu, 23 Jan 2025 08:21:00 -0700
test: better terminal memory cleanup
* don't actually unmap the terminal backing; instead mark it as
inaccessible and tell kernel that it can be freed. This way we
can catch any bugs related to global state or accidental reuse
* do unmap the ringbuffers since they can not be marked as free
since they need to be map as shared regions.
Diffstat:
4 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/platform_linux_aarch64.c b/platform_linux_aarch64.c
@@ -30,6 +30,7 @@
#define SYS_clone 220
#define SYS_execve 221
#define SYS_mmap 222
+#define SYS_mprotect 226
#define SYS_madvise 233
#define SYS_wait4 260
#define SYS_memfd_create 279
diff --git a/platform_linux_amd64.c b/platform_linux_amd64.c
@@ -12,6 +12,7 @@
#define SYS_write 1
#define SYS_close 3
#define SYS_mmap 9
+#define SYS_mprotect 10
#define SYS_munmap 11
#define SYS_ioctl 16
#define SYS_pwrite64 18
diff --git a/platform_linux_common.c b/platform_linux_common.c
@@ -5,6 +5,7 @@
#define PR_SET_NAME 15
+#define PROT_NONE 0x00
#define PROT_READ 0x01
#define PROT_RW 0x03
@@ -15,6 +16,7 @@
#define MAP_FIXED 0x10
#define MAP_ANON 0x20
+#define MADV_FREE 8
#define MADV_HUGEPAGE 14
#define O_RDONLY 0x00000
@@ -240,7 +242,14 @@ os_block_alloc(size requested_size)
static void
os_release_memory_block(MemoryBlock memory)
{
- syscall2(SYS_munmap, (iptr)memory.memory, memory.size);
+ syscall3(SYS_madvise, (iptr)memory.memory, memory.size, MADV_FREE);
+ syscall3(SYS_mprotect, (iptr)memory.memory, memory.size, PROT_NONE);
+}
+
+static void
+os_release_ring_buffer(RingBuf *rb)
+{
+ syscall2(SYS_munmap, (iptr)(rb->buf - rb->cap), rb->cap * 3);
}
static f64
diff --git a/tests/test.c b/tests/test.c
@@ -430,6 +430,15 @@ place_term_into_memory(MemoryBlock memory, i32 rows, i32 columns)
return t;
}
+static void
+release_term_memory(MemoryBlock backing)
+{
+ Term *t = backing.memory;
+ os_release_ring_buffer(&t->views[0].log);
+ os_release_ring_buffer(&t->views[1].log);
+ os_release_memory_block(backing);
+}
+
int
main(void)
{
@@ -461,7 +470,7 @@ main(void)
case SUCCESS: stream_push_s8(&log, success_string); break;
case UNSUPPORTED: stream_push_s8(&log, unsupported_string); break;
}
- os_release_memory_block(term_backing);
+ release_term_memory(term_backing);
}
stream_push_s8(&log, s8("FINISHED: ["));
stream_push_u64(&log, ARRAY_COUNT(tests) - failure_count);