Add distinction between p_filesz and p_memsz

It turns out that in ELF files there *is* a difference between
file size and mem size. Mem size is the size of the chunk of memory,
but file size is the size of what we copy into there from the
file. Most of the time they are the same, which is why previously
`spawn' only considered mem size, but this caused issues.

In particular, in the xous implementation of the rust standard
library, there whenever something is allocated it has to first check
an AtomicI32 LOCKED to see if it is 0 or 1, and set it to 1 when it is
allocating so that only one thing is allocating at a time. However,
the mem size for the section containing LOCKED is larger than the file
size, meaning that `spawn' copied more of the file that it should
have. This lead to junk being put into LOCKED, which caused the new
process to assume that something else was perpetually allocating.

Now, `spawn' puts 0's in the space between the end of the file size
and the end of the memsize, which makes everything work as expected.

The only problem remaining is that when an app registers for GAM, GAM
checks whether the app is allowed to register, which is based on what
apps were included in the image. Obviously, this does not work when
you are trying to load apps that are not already in the image
This commit is contained in:
Vihaan Jim 2023-08-04 17:54:06 -04:00
parent be58fbf0c3
commit 9eeed0d284
2 changed files with 29 additions and 20 deletions

View file

@ -28,16 +28,19 @@ pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -
let server = xous::SID::from_u32(server1, server2, server3, server4); let server = xous::SID::from_u32(server1, server2, server3, server4);
// recreate the extra sections that were cut out of the stub // recreate the extra sections that were cut out of the stub
let mut memory = xous::map_memory( {
None, let mut memory = xous::map_memory(
core::num::NonZeroUsize::new(0x40000000), None,
0x1000, core::num::NonZeroUsize::new(0x40000000),
xous::MemoryFlags::R | xous::MemoryFlags::W 0x1000,
).unwrap(); xous::MemoryFlags::R | xous::MemoryFlags::W
let connection = core::sync::atomic::AtomicU32::new(0); ).unwrap();
let slice = unsafe { core::slice::from_raw_parts(&connection as *const _ as *const u8, core::mem::size_of::<core::sync::atomic::AtomicU32>()) }; let connection = core::sync::atomic::AtomicU32::new(0);
for (dest, src) in memory.as_slice_mut::<u8>().iter_mut().skip(8).zip(slice) { let slice = unsafe { core::slice::from_raw_parts(&connection as *const _ as *const u8, core::mem::size_of::<core::sync::atomic::AtomicU32>()) };
*dest = *src; for (dest, src) in memory.as_slice_mut::<u8>().iter_mut().skip(8).zip(slice) {
*dest = *src;
}
// everything should drop now
} }
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
@ -119,6 +122,7 @@ fn read_elf(memory: Option<&xous::MemoryMessage>) -> usize {
let src_addr = to_usize(start+0x04, 4); let src_addr = to_usize(start+0x04, 4);
let vaddr = to_usize(start+0x08, 4); let vaddr = to_usize(start+0x08, 4);
let padding = if vaddr & 0xFFF == 0 { 0 } else { vaddr & 0xFFF }; let padding = if vaddr & 0xFFF == 0 { 0 } else { vaddr & 0xFFF };
let file_size = to_usize(start+0x10, 4);
let mem_size = to_usize(start+0x14, 4); let mem_size = to_usize(start+0x14, 4);
let mem_size = mem_size + padding; let mem_size = mem_size + padding;
let mem_size = mem_size + if mem_size & 0xFFF == 0 { 0 } else { 0x1000 - (mem_size & 0xFFF) }; let mem_size = mem_size + if mem_size & 0xFFF == 0 { 0 } else { 0x1000 - (mem_size & 0xFFF) };
@ -136,10 +140,15 @@ fn read_elf(memory: Option<&xous::MemoryMessage>) -> usize {
.unwrap(); .unwrap();
for (dest, src) in target_memory.as_slice_mut().iter_mut().skip(padding) for (dest, src) in target_memory.as_slice_mut().iter_mut().skip(padding)
.zip(bin[src_addr..core::cmp::min(max, src_addr+mem_size-padding)].iter()) .zip(bin[src_addr..core::cmp::min(max, src_addr+file_size)].iter())
{ {
*dest = *src; *dest = *src;
} }
for dest in target_memory.as_slice_mut().iter_mut().skip(padding+file_size)
.take(mem_size-file_size-padding)
{
*dest = 0;
}
} }
} }
log::info!("Finished writing"); log::info!("Finished writing");

View file

@ -77,7 +77,7 @@ fn main() -> ! {
log::info!("Result of ping: {:?}", result); log::info!("Result of ping: {:?}", result);
// load the app from the binary file // load the app from the binary file
let bin = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/release/hello"); let bin = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/debug/hello");
let bin_len = bin.len(); let bin_len = bin.len();
let bin_loc = bin.as_ptr() as usize; let bin_loc = bin.as_ptr() as usize;
let buf = unsafe { xous::MemoryRange::new(bin_loc & !0xFFF, bin_len + if bin_len & 0xFFF == 0 { 0 } else {0x1000 - (bin_len & 0xFFF)}).expect("Couldn't create a buffer from the segment") }; let buf = unsafe { xous::MemoryRange::new(bin_loc & !0xFFF, bin_len + if bin_len & 0xFFF == 0 { 0 } else {0x1000 - (bin_len & 0xFFF)}).expect("Couldn't create a buffer from the segment") };