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,18 +28,21 @@ pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -
let server = xous::SID::from_u32(server1, server2, server3, server4);
// recreate the extra sections that were cut out of the stub
let mut memory = xous::map_memory(
None,
core::num::NonZeroUsize::new(0x40000000),
0x1000,
xous::MemoryFlags::R | xous::MemoryFlags::W
).unwrap();
let connection = core::sync::atomic::AtomicU32::new(0);
let slice = unsafe { core::slice::from_raw_parts(&connection as *const _ as *const u8, core::mem::size_of::<core::sync::atomic::AtomicU32>()) };
for (dest, src) in memory.as_slice_mut::<u8>().iter_mut().skip(8).zip(slice) {
*dest = *src;
{
let mut memory = xous::map_memory(
None,
core::num::NonZeroUsize::new(0x40000000),
0x1000,
xous::MemoryFlags::R | xous::MemoryFlags::W
).unwrap();
let connection = core::sync::atomic::AtomicU32::new(0);
let slice = unsafe { core::slice::from_raw_parts(&connection as *const _ as *const u8, core::mem::size_of::<core::sync::atomic::AtomicU32>()) };
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::set_max_level(log::LevelFilter::Info);
log::info!("my PID is {}", xous::process::id());
@ -75,7 +78,7 @@ fn read_elf(memory: Option<&xous::MemoryMessage>) -> usize {
Some(s) => s,
None => panic!(),
};
// get the elf binary from the message
let mut bin = memory.buf.as_slice::<u8>();
@ -84,7 +87,7 @@ fn read_elf(memory: Option<&xous::MemoryMessage>) -> usize {
while !(bin[0] == 0x7F && bin[1] == 0x45 && bin[2] == 0x4c && bin[3] == 0x46) {
bin = &bin[1..];
}
let max = bin.len();
// make sure that this is a 32 bit
@ -119,6 +122,7 @@ fn read_elf(memory: Option<&xous::MemoryMessage>) -> usize {
let src_addr = to_usize(start+0x04, 4);
let vaddr = to_usize(start+0x08, 4);
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 = mem_size + padding;
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();
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;
}
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");

View file

@ -32,7 +32,7 @@ fn main() -> ! {
// the ticktimer
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
let gam = Gam::new(&xns).expect("can't connect to GAM");
let auth = gam.register_ux(UxRegistration {
app_name: xous_ipc::String::from_str(APP_NAME_APP_LOADER),
@ -45,7 +45,7 @@ fn main() -> ! {
focuschange_id: None,
rawkeys_id: None
}).expect("Couldn't register").expect("Didn't get an auth token");
let menu = menu_matic(Vec::new(), APP_MENU_0_APP_LOADER, Some(xous::create_server().unwrap())).expect("Couldn't create menu");
let mut apps = Vec::new();
@ -61,7 +61,7 @@ fn main() -> ! {
Opcode::LoadApp => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let load_app_req = buffer.to_original::<LoadAppRequest, _>().unwrap();
// create the spawn process
let stub = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/debug/spawn.bin");
let args = xous::ProcessArgs::new(stub, xous::MemoryAddress::new(0x2050_1000).unwrap(), xous::MemoryAddress::new(0x2050_1000).unwrap());
@ -75,9 +75,9 @@ fn main() -> ! {
)
.unwrap();
log::info!("Result of ping: {:?}", result);
// 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_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") };
@ -105,7 +105,7 @@ fn main() -> ! {
// for anyone who needs this I found this in Menu::key_event
gam.relinquish_focus().unwrap();
xous::yield_slice();
ticktimer.sleep_ms(100).ok(); // yield for a moment to allow the previous menu to close
// open the submenu