From 05d316d0bc3705e1e3c47ecdfbad4f4da3d9879d Mon Sep 17 00:00:00 2001 From: Rafal Mielniczuk Date: Mon, 27 May 2024 16:06:46 +0100 Subject: [PATCH] Fix memory leak on sys_exec --- cykusz-rs/src/arch/x86_64/task/mod.rs | 9 +++++++-- cykusz-rs/src/kernel/fs/dirent.rs | 5 ++++- cykusz-rs/src/kernel/task/mod.rs | 7 +++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cykusz-rs/src/arch/x86_64/task/mod.rs b/cykusz-rs/src/arch/x86_64/task/mod.rs index 1bcf1f04..6da81b57 100644 --- a/cykusz-rs/src/arch/x86_64/task/mod.rs +++ b/cykusz-rs/src/arch/x86_64/task/mod.rs @@ -1,4 +1,3 @@ -use alloc::string::String; use alloc::vec::Vec; use core::mem::size_of; use core::ptr::Unique; @@ -19,6 +18,7 @@ use crate::arch::raw::segmentation::SegmentSelector; use crate::arch::syscall::SyscallFrame; use crate::arch::utils::StackHelper; use crate::drivers::elf::ElfHeader; +use crate::kernel::fs::dirent::DirEntryItem; use crate::kernel::mm::virt::PageFlags; use crate::kernel::mm::{allocate, VirtAddr, PAGE_SIZE}; use crate::kernel::mm::{Frame, PhysAddr}; @@ -450,12 +450,14 @@ impl Task { hdr: &ElfHeader, vm: &VM, tls_vm: Option, - path: String, + exe: DirEntryItem, args: Option, envs: Option, ) -> ! { logln!("entry point: {}", entry); + let path = exe.full_path(); + let args = args.map(|a| args::Args::new(a)); let envs = envs.map(|e| args::Args::new(e)); @@ -538,9 +540,12 @@ impl Task { helper.write(argp.len()); // int argc } + // !!! Prevent memory leaks as we are not running destructors here! drop(envp); drop(argp); drop(tls_vm); + drop(path); + drop(exe); logln!("exec user stack: {:#x}", helper.current()); assert_eq!(helper.current() % 16, 0); diff --git a/cykusz-rs/src/kernel/fs/dirent.rs b/cykusz-rs/src/kernel/fs/dirent.rs index cff5ede5..002c37c8 100644 --- a/cykusz-rs/src/kernel/fs/dirent.rs +++ b/cykusz-rs/src/kernel/fs/dirent.rs @@ -27,7 +27,10 @@ impl Cacheable for DirEntry { data.parent = None; } - fn notify_used(&self) {} + fn notify_used(&self) { + let data = self.write(); + logln!("mark used: {}", data.name); + } fn deallocate(&self, _me: &CacheItem) { logln!("deallocate {}", _me.data.lock().name); diff --git a/cykusz-rs/src/kernel/task/mod.rs b/cykusz-rs/src/kernel/task/mod.rs index 11d2c386..eed0b2fa 100644 --- a/cykusz-rs/src/kernel/task/mod.rs +++ b/cykusz-rs/src/kernel/task/mod.rs @@ -210,6 +210,9 @@ impl Task { self.filetable().close_on_exec(); self.filetable().debug(); + // !!! Prevent memory leak as we are not running destructors here! + drop(vm); + unsafe { // EXEC! self.arch_task_mut().exec( @@ -218,7 +221,7 @@ impl Task { &elf_hdr, self.vm(), tls_vm, - exe.full_path(), + exe, Some(args), envs, ) @@ -781,6 +784,6 @@ impl Task { impl Drop for Task { fn drop(&mut self) { - logln4!("drop task {}", self.tid()); + logln!("drop task {}", self.tid()); } }