Convert a Vec<&'static mut [u8]> to a &[u8] without copy?

In system programming, we usually use seperate page tables in different privileges to avoid vulnerability caused by Meltdown and Spectre.

Thus a demand occurs as following:

  • A syscall handler receives a pointer saved in a register with the type of usize.

  • The kernel tries to get the buffer starting at this pointer.

  • The buffer may be larger than a page, and the contiguous virtual address range may be translated into a disconguous list of physical address ranges.

When I tried to import a third party crate and use the functions, I found that all these functions receives &[u8] as a buffer. So I need to convert a list of ranges such as Vec<&'static mut [u8]> into a &[u8] without performance influenced by Copy of u8.

Consider that slices must not span separately allocated objects, and note in particular the incorrect usage which is a less general version of what you're trying to do.

In order for it to be sound, you need to at least

  • Start with a single object (such as a static _: [u8; N]) that you split up into different &'static [u8]
  • Keep track of the original range so you don't go outside of it
  • Ensure continuity when going from multiple &[u8] down to one
  • Encapsulate all this in a newtype to preserve invariants instead of just using Vec
5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.