Skip to content

Commit

Permalink
Fix memory leak on sys_exec
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed May 27, 2024
1 parent 288b0a4 commit 05d316d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
9 changes: 7 additions & 2 deletions cykusz-rs/src/arch/x86_64/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::mem::size_of;
use core::ptr::Unique;
Expand All @@ -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};
Expand Down Expand Up @@ -450,12 +450,14 @@ impl Task {
hdr: &ElfHeader,
vm: &VM,
tls_vm: Option<TlsVmInfo>,
path: String,
exe: DirEntryItem,
args: Option<ExeArgs>,
envs: Option<ExeArgs>,
) -> ! {
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));

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion cykusz-rs/src/kernel/fs/dirent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ impl Cacheable<CacheKey> 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<CacheKey, DirEntry>) {
logln!("deallocate {}", _me.data.lock().name);
Expand Down
7 changes: 5 additions & 2 deletions cykusz-rs/src/kernel/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -218,7 +221,7 @@ impl Task {
&elf_hdr,
self.vm(),
tls_vm,
exe.full_path(),
exe,
Some(args),
envs,
)
Expand Down Expand Up @@ -781,6 +784,6 @@ impl Task {

impl Drop for Task {
fn drop(&mut self) {
logln4!("drop task {}", self.tid());
logln!("drop task {}", self.tid());
}
}

0 comments on commit 05d316d

Please sign in to comment.