git: 17602eb3eae9 - main - CHERI: make mem{cpy,move}(9) CHERI compatible
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 01 Jul 2026 11:49:36 UTC
The branch main has been updated by brooks:
URL: https://cgit.FreeBSD.org/src/commit/?id=17602eb3eae95a53c973a5ceb4fc3134a770e8d6
commit 17602eb3eae95a53c973a5ceb4fc3134a770e8d6
Author: Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2026-07-01 11:46:55 +0000
Commit: Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-07-01 11:49:10 +0000
CHERI: make mem{cpy,move}(9) CHERI compatible
- Use intptr_t in place of long as the word type in the core copying
loop where aligned words a copied. This preserved the provenance of
any copied pointers.
- When working with the address of src or dst use ptraddr_t rather than
uintptr_t. This avoid ambigious provenance in expressions involving
multiple addresses.
As a minor tweak, rename the function to memmove since that is the
interface it implements (overlapping src and dst are permitted) and make
memcpy the alias rather than the other way around.
Reviewed by: kib, markj
Effort: CHERI upstreaming
Sponsored by: Innovate UK
Differential Revision: https://reviews.freebsd.org/D57965
---
sys/libkern/bcopy.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/sys/libkern/bcopy.c b/sys/libkern/bcopy.c
index 8df260809a02..be26443ef884 100644
--- a/sys/libkern/bcopy.c
+++ b/sys/libkern/bcopy.c
@@ -35,7 +35,9 @@
#include <sys/param.h>
#ifdef _KERNEL
#include <sys/systm.h>
+#include <sys/stddef.h>
#else
+#include <stddef.h>
#include <string.h>
#endif
@@ -46,7 +48,7 @@
* sizeof(word) MUST BE A POWER OF TWO
* SO THAT wmask BELOW IS ALL ONES
*/
-typedef long word; /* "word" used for optimal copy speed */
+typedef intptr_t word; /* "word" used for optimal copy speed */
#define wsize sizeof(word)
#define wmask (wsize - 1)
@@ -57,7 +59,7 @@ typedef long word; /* "word" used for optimal copy speed */
* (the portable versions of) bcopy, memcpy, and memmove.
*/
void *
-memcpy(void *dst0, const void *src0, size_t length)
+memmove(void *dst0, const void *src0, size_t length)
{
char *dst;
const char *src;
@@ -76,18 +78,18 @@ memcpy(void *dst0, const void *src0, size_t length)
#define TLOOP(s) if (t) TLOOP1(s)
#define TLOOP1(s) do { s; } while (--t)
- if ((unsigned long)dst < (unsigned long)src) {
+ if ((ptraddr_t)dst < (ptraddr_t)src) {
/*
* Copy forward.
*/
t = (size_t)src; /* only need low bits */
- if ((t | (uintptr_t)dst) & wmask) {
+ if ((t | (ptraddr_t)dst) & wmask) {
/*
* Try to align operands. This cannot be done
* unless the low bits match.
*/
- if ((t ^ (uintptr_t)dst) & wmask || length < wsize) {
+ if ((t ^ (ptraddr_t)dst) & wmask || length < wsize) {
t = length;
} else {
t = wsize - (t & wmask);
@@ -112,10 +114,10 @@ memcpy(void *dst0, const void *src0, size_t length)
*/
src += length;
dst += length;
- t = (uintptr_t)src;
+ t = (size_t)src;
- if ((t | (uintptr_t)dst) & wmask) {
- if ((t ^ (uintptr_t)dst) & wmask || length <= wsize) {
+ if ((t | (ptraddr_t)dst) & wmask) {
+ if ((t ^ (ptraddr_t)dst) & wmask || length <= wsize) {
t = length;
} else {
t &= wmask;
@@ -134,4 +136,4 @@ done:
return (dst0);
}
-__strong_reference(memcpy, memmove);
+__strong_reference(memmove, memcpy);