Crate memory_pages

source ·
Expand description

memory_pages is a small crate providing a cross-platform API to request pages from kernel with certain permission modes set(read,write,execute). It provides an very safe API to aid in many use cases, mainly:

  1. Speeds up operating on large data sets: PagedVec provides allocation speed advantages over standard Vec for large data. types.
  2. Page alignment guarantee. Since the API returns memory pages, the first address inside Pages must be aligned to a page boundary. This means, that with a bit of careful selection of type sizes(powers of 2), a substantial speedup can be occurred(structures can be guaranteed to always reside entirely within 1 page). Those sorts of guarantees are not normally given by allocators.
  3. Simplifies dealing with page permissions and allows for additional levels of safety: Pages with DenyWrite cannot be written into without their permissions being changed, which allows for certain kinds of bugs to cause segfaults insted of overwriting data.
  4. Simplifies JITs - while dealing with memory pages is simple compared to difficulty of the task, which is writing a Just-In-Time compiler, this crate abstracts the platform specific differences away and adds additional measures to prevent some security issues, allowing you to focus on writing the compiler itself, without worrying about those low-level details.

Features

allow_exec - this feature allows access to everything related to executing code inside allocated pages. Off by default. deny_xw - default feature that prevents allowing both eXecution and Write permissions on a page. This is an additional security feature that prevents accidental misuse of the API-s locked behind allow_exec feature. Does noting without it, but is really usefull when allow_exec enabled.

Structs

  • Marks Pages as allowing execution. WARNING do NOT set this permission if not necessary!
  • Marks Pages as allowing to be read from.
  • Marks Pages as allowing to be modified.
  • Prevents data inside Pages from being executed. Do NOT change from this value if not 100% sure what you are doing.
  • Marks Pages as forbidding all reads(causing SIGSEGV if read attempted).
  • Marks Pages as forbidding all writes(causing SIGSEGV if write attempted).
  • A reference to a function inside Pages. It enforces that it may never outlive the Pages it is contained in, preventing lifetime related errors. Additionally, it enforces that if Pages permissions are changes, all FnRef referencing it will be invalidated, preventing exploits related to page permissions.
  • A Vec-like type located in memory pages acquired directly from the kernel. For big lengths a faster to allocate/deallocate than a normal Vec, but considerably slower for small sizes. Intended to be used for very large data sets, with a rough estimate of capacity known ahead of time.
  • Pages represents a collection of pages acquired from the kernel. Those pages share a common set of permissions and are laid out contiguously in the memory. The permissions on given Pages may be changed at runtime.

Traits