Skip to content

Reimplement memcpy() using slices, fix unaligned read/write, add test

Peter Limkilde Svendsen requested to merge plimkilde/relibc:memcpy-slices into master

This reimplements memcpy() to use MaybeUninit slices rather than raw pointers. This should help reason about safety and correctness.

I have tried to document the safety requirements and various caveats for this particular function.

I have also ditched the u64-based copying from the old implementation, since this relied on unaligned access (there is no alignment requirement for memcpy's input arguments). Attempting to copy using larger types is complicated by the fact that the s1 and s2 slices may decompose differently into (prefix, middle, suffix) with respect to the larger type. As an example, musl's implementation is quite convoluted. A possible alternative could be the compiler-builtins crate.

Edit: Now also includes addition of a C test, and copying in chunks of the largest possible Rust primitive type that is well-aligned for the middle part of the given input slices.

Edited by Peter Limkilde Svendsen

Merge request reports