Module Name:    src
Committed By:   riastradh
Date:           Tue Aug 20 12:33:05 UTC 2019

Modified Files:
        src/sys/arch/amd64/include: param.h
        src/sys/arch/i386/include: param.h
        src/sys/sys: param.h

Log Message:
New macro ALIGNED_POINTER_LOAD.

To be used with ALIGNED_POINTER(p,t) instead of writing *(const t *)p
directly.  This way, on machines without strict alignment, we can use
memcpy to pacify sanitizers, while getting the same compiled code in
the end with a single (say) MOV instruction.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/amd64/include/param.h
cvs rdiff -u -r1.84 -r1.85 src/sys/arch/i386/include/param.h
cvs rdiff -u -r1.609 -r1.610 src/sys/sys/param.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/include/param.h
diff -u src/sys/arch/amd64/include/param.h:1.30 src/sys/arch/amd64/include/param.h:1.31
--- src/sys/arch/amd64/include/param.h:1.30	Sat Mar 16 11:50:48 2019
+++ src/sys/arch/amd64/include/param.h	Tue Aug 20 12:33:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.30 2019/03/16 11:50:48 rin Exp $	*/
+/*	$NetBSD: param.h,v 1.31 2019/08/20 12:33:04 riastradh Exp $	*/
 
 #ifdef __x86_64__
 
@@ -21,7 +21,8 @@
 #define	MACHINE_ARCH	"x86_64"
 #define MID_MACHINE	MID_X86_64
 
-#define ALIGNED_POINTER(p,t)	1
+#define ALIGNED_POINTER(p,t)		1
+#define ALIGNED_POINTER_LOAD(q,p,t)	memcpy((q), (p), sizeof(t))
 
 /*
  * Align stack as required by AMD64 System V ABI. This is because

Index: src/sys/arch/i386/include/param.h
diff -u src/sys/arch/i386/include/param.h:1.84 src/sys/arch/i386/include/param.h:1.85
--- src/sys/arch/i386/include/param.h:1.84	Mon Jan  7 22:00:31 2019
+++ src/sys/arch/i386/include/param.h	Tue Aug 20 12:33:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.84 2019/01/07 22:00:31 jdolecek Exp $	*/
+/*	$NetBSD: param.h,v 1.85 2019/08/20 12:33:04 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -58,7 +58,8 @@
 #define	MACHINE_ARCH	"i386"
 #define	MID_MACHINE	MID_I386
 
-#define ALIGNED_POINTER(p,t)	1
+#define ALIGNED_POINTER(p,t)		1
+#define ALIGNED_POINTER_LOAD(q,p,t)	memcpy((q), (p), sizeof(t))
 
 #define	PGSHIFT		12		/* LOG2(NBPG) */
 #define	NBPG		(1 << PGSHIFT)	/* bytes/page */

Index: src/sys/sys/param.h
diff -u src/sys/sys/param.h:1.609 src/sys/sys/param.h:1.610
--- src/sys/sys/param.h:1.609	Tue Aug 20 06:37:06 2019
+++ src/sys/sys/param.h	Tue Aug 20 12:33:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.609 2019/08/20 06:37:06 mrg Exp $	*/
+/*	$NetBSD: param.h,v 1.610 2019/08/20 12:33:04 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -253,9 +253,22 @@
  * any desired pointer type.
  *
  * ALIGNED_POINTER is a boolean macro that checks whether an address
- * is valid to fetch data elements of type t from on this architecture.
- * This does not reflect the optimal alignment, just the possibility
- * (within reasonable limits).
+ * is valid to fetch data elements of type t from on this architecture
+ * using ALIGNED_POINTER_LOAD.  This does not reflect the optimal
+ * alignment, just the possibility (within reasonable limits).
+ *
+ *	uint32_t x;
+ *	unsigned char *p = ...;
+ *
+ *	if (ALIGNED_POINTER(p, uint32_t)) {
+ *		uint32_t t;
+ *		ALIGNED_POINTER_LOAD(&t, p, uint32_t);
+ *		x = t;
+ *	} else {
+ *		uint32_t t;
+ *		memcpy(&t, p, sizeof(t));
+ *		x = t;
+ *	}
  *
  */
 #define ALIGNBYTES	__ALIGNBYTES
@@ -265,6 +278,9 @@
 #ifndef ALIGNED_POINTER
 #define	ALIGNED_POINTER(p,t)	((((uintptr_t)(p)) & (sizeof(t) - 1)) == 0)
 #endif
+#ifndef ALIGNED_POINTER_LOAD
+#define	ALIGNED_POINTER_LOAD(q,p,t)	(*(q) = *((const t *)(p)))
+#endif
 
 /*
  * Historic priority levels.  These are meaningless and remain only

Reply via email to