-
Notifications
You must be signed in to change notification settings - Fork 37
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
Design: boxed preads #51
Comments
Do you have a real world example that would use this? |
Yes, similar to the example above, I was dealing with some C structs which had some header information, etc., then a large byte array containing image bytes, then some more stuff. |
I was hoping for an actual real world example. It seems to me that large fixed size arrays are quite uncommon, and even then you can usually use
|
It was the other way around, basically: struct Foo {
struct Header header;
uint8_t data [0x8000];
struct Footer footer;
} In rust land I then wanted to: let x = bytes.pread::<Foo>(0)?; // this fails because we allocate x on the stack.
// check x.header, do stuff, manipulate x.bytes, pwrite |
So I had something like this in mind 😈 , but it still overflows the stack (it also allocates twice which sucks): impl<'a, Ctx, T> TryFromCtx<'a, Ctx> for Box<T>
where
T: TryFromCtx<'a, Ctx>,
Ctx: Copy,
error::Error: From<<T as TryFromCtx<'a, Ctx>>::Error>
{
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], ctx: Ctx) -> result::Result<(Self, usize), Self::Error> {
let res = Box::new(TryFromCtx::try_from_ctx(src, ctx)?);
Ok((Box::new(res.0), res.1))
}
}
#[test]
fn pwrite_big_struct() {
use scroll::{LE, Pwrite};
#[derive(Pread, Pwrite, Clone)]
struct Big {
arr: [u8; 10000000],
}
let mut bytes = vec![0; 10000001];
let big = bytes.pread_with::<Box<Big>>(0, LE).unwrap();
let _ = bytes.pwrite_with(big.as_ref(), 0, LE).unwrap();
assert!(false);
} I'm wondering if its even possible to write in scroll |
If |
In anticipation of #47, the final remaining piece for working with arbitrary data safely is to figure out how to pread large objects without blowing the stack.
Most system languages accomplish this by boxing the object and manipulating it through the heap pointer.
I'm wondering if we can accomplish this in a semi-automated way, without compromising scroll's interesting type based reading/writing.
Doing this properly would allow us to round trip a large stack object within scroll.
Specifically, could we do something like:
In worst case, we should be able to expose a new
pread_with_box
method, that does the above, but pushes the box type information into the method name. This is fine, but i think its a more beautiful api design to allow the user to choose which type to read out via turbofish.cc @philipc @lzutao @willglynn @luser
What you all think?
The text was updated successfully, but these errors were encountered: