Skip to content

Commit

Permalink
Merge #388
Browse files Browse the repository at this point in the history
388: Refactoring and Unsafe Reasoning in inode.rs r=jeehoonkang a=Medowhill



Co-authored-by: Jaemin Hong <hjm0901@gmail.com>
  • Loading branch information
kaist-cp-bors[bot] and Medowhill authored Feb 1, 2021
2 parents 372458f + a5a0e90 commit a15fa0c
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 184 deletions.
25 changes: 9 additions & 16 deletions kernel-rs/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
param::MAXARG,
proc::myproc,
riscv::{pgroundup, PGSIZE},
vm::{KVAddr, PAddr, UVAddr, UserMemory, VAddr},
vm::{PAddr, UVAddr, UserMemory, VAddr},
};
use bitflags::bitflags;
use core::{cmp, mem};
Expand Down Expand Up @@ -106,12 +106,10 @@ impl Kernel {

// Check ELF header
let mut elf: ElfHdr = Default::default();
let bytes_read = ip.read(
KVAddr::new(&mut elf as *mut _ as _),
0,
mem::size_of::<ElfHdr>() as _,
)?;
if !(bytes_read == mem::size_of::<ElfHdr>() && elf.is_valid()) {
// It is safe becuase ElfHdr can be safely transmuted to [u8; _], as it
// contains only integers, which do not have internal structures.
unsafe { ip.read_kernel(&mut elf, 0) }?;
if !elf.is_valid() {
return Err(());
}

Expand All @@ -125,20 +123,15 @@ impl Kernel {
let off = elf.phoff + i * mem::size_of::<ProgHdr>();

let mut ph: ProgHdr = Default::default();
let bytes_read = ip.read(
KVAddr::new(&mut ph as *mut _ as _),
off as _,
mem::size_of::<ProgHdr>() as _,
)?;
if bytes_read != mem::size_of::<ProgHdr>() {
return Err(());
}
// It is safe becuase ProgHdr can be safely transmuted to [u8; _], as it
// contains only integers, which do not have internal structures.
unsafe { ip.read_kernel(&mut ph, off as _) }?;
if ph.is_prog_load() {
if ph.memsz < ph.filesz || ph.vaddr % PGSIZE != 0 {
return Err(());
}
let _ = mem.alloc(ph.vaddr.checked_add(ph.memsz).ok_or(())?)?;
mem.read_file(UVAddr::new(ph.vaddr), &mut ip, ph.off as _, ph.filesz as _)?;
mem.load_file(UVAddr::new(ph.vaddr), &mut ip, ph.off as _, ph.filesz as _)?;
}
}
drop(ip);
Expand Down
4 changes: 2 additions & 2 deletions kernel-rs/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl File {
FileType::Inode { ip, off } => {
let mut ip = ip.deref().lock();
let curr_off = unsafe { *off.get() };
let ret = ip.read(addr, curr_off, n as u32);
let ret = ip.read_user(addr, curr_off, n as u32);
if let Ok(v) = ret {
unsafe { *off.get() = curr_off.wrapping_add(v as u32) };
}
Expand Down Expand Up @@ -143,7 +143,7 @@ impl File {
let mut ip = ip.deref().lock();
let curr_off = unsafe { *off.get() };
let r = ip
.write(addr + bytes_written, curr_off, bytes_to_write as u32, &tx)
.write_user(addr + bytes_written, curr_off, bytes_to_write as u32, &tx)
.map(|v| {
unsafe { *off.get() = curr_off.wrapping_add(v as u32) };
v
Expand Down
Loading

0 comments on commit a15fa0c

Please sign in to comment.