-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add struct_util module from linux-loader #88
Comments
Fixes #77 Signed-off-by: Alexandra Iordache <[email protected]>
There is some overlap between this and vm-memory's Bytes and ByteValued traits. Can you try instead implementing ByteValued on the structs and using the read_obj/write_obj methods from the Bytes trait? The main limitation is that you need a mutable reference to slice but it shouldn't be a problem for linux-loader. |
Indeed we can We'd be replacing let mut ehdr: elf::Elf64_Ehdr = Default::default();
unsafe { struct_util::read_struct(kernel_image, &mut ehdr).map_err(|_| Error::ReadElfHeader)?; } with let mut ehdr_bytes = vec![0u8; mem::size_of::<elf::Elf64_Ehdr>()];
kernel_image.read_exact(ehdr_bytes.as_mut_slice()).map_err(|_| Error::ReadElfHeader)?;
let ehdr = elf::Elf64_Ehdr::from_mut_slice(ehdr_bytes.as_mut_slice()).ok_or(Error::ReadElfHeader)?; which I don't necessarily find more elegant, but personally prefer rather than move |
Yes, it's not super nice but at least it doesn't have unsafe. You can also put let mut ehdr_bytes = [0u8; mem::size_of::<elf::Elf64_Ehdr>()];
kernel_image.read_exact(&mut ehdr_bytes[..]).map_err(|_| Error::ReadElfHeader)?;
let ehdr = elf::Elf64_Ehdr::from_mut_slice(&mut ehdr_bytes[..]).ok_or(Error::ReadElfHeader)?; ? |
Also: let mut ehdr_bytes = [0u8; mem::size_of::<elf::Elf64_Ehdr>()];
ehdr_bytes.as_volatile_slice().read_from(0, kernel_image, ehdr_bytes.len()).map_err(|_| Error::ReadElfHeader)?;
let ehdr = elf::Elf64_Ehdr::from_mut_slice(&mut ehdr_bytes[..]).ok_or(Error::ReadElfHeader)?; ... and finally, we could implement let mut ehdr: elf::Elf64_Ehdr = Default::default();
ehdr.as_volatile_slice().read_from(0, kernel_image, ehdr.len()).map_err(|_| Error::ReadElfHeader)?; |
The last solution looks great and I find it to be the most elegant alternative to |
Fixes rust-vmm#20 Closes rust-vmm/vmm-sys-util#77 Signed-off-by: Alexandra Iordache <[email protected]>
Fixes rust-vmm#20 Closes rust-vmm/vmm-sys-util#77 Signed-off-by: Alexandra Iordache <[email protected]>
Transferred this issue from vmm-sys-util to vm-memory as the implementation is in vm-memory now. Fix introduced in: #83 |
See also rust-vmm/linux-loader#20, rust-vmm/linux-loader#23 (comment)
struct_util
is a generic utility module that reads structs from bytes. It should be included in this utility crate and consumed bylinux-loader
from here.The text was updated successfully, but these errors were encountered: