Giter VIP home page Giter VIP logo

Comments (6)

philipc avatar philipc commented on May 28, 2024

Do you have a real world example that would use this?

from scroll.

m4b avatar m4b commented on May 28, 2024

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 wanted to pread the struct, match on some header values, validate, then do something with the bytes, then write them out to disk with slightly different header values.
Scroll would have been extremely fast way to prototype/work with the data, but because I couldn't box the pread data, it exploded my stack; testing with the pwrite version taking references, I was able to write the data, which was one half of the problem.
Hope that's clear?
I think this will generally come up in FFI with large C structs of byte arrays where the api expects a malloc(sizeof(LargeStruct)) to construct the initial type.

from scroll.

philipc avatar philipc commented on May 28, 2024

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 Vec<u8> anyway. So the use case is:

  • you have a struct containing a large fixed size array
  • you need to pass this struct to C with FFI so you can't use Vec?

from scroll.

m4b avatar m4b commented on May 28, 2024

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

from scroll.

m4b avatar m4b commented on May 28, 2024

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 TryFromCtx as is without blowing up the stack?

from scroll.

philipc avatar philipc commented on May 28, 2024

If Big::try_from_ctx(src, ctx) overflows the stack, then wrapping it in Box::new won't change anything. You need placement new or an equivalent (pass in a preallocated pointer for the destination of the read, instead of returning a value), which is incompatible with returning by value.

from scroll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.