Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys New macro ALIGNED_POINTER_LOAD.



details:   https://anonhg.NetBSD.org/src/rev/188a417e1ab3
branches:  trunk
changeset: 1002906:188a417e1ab3
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Tue Aug 20 12:33:04 2019 +0000

description:
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.

diffstat:

 sys/arch/amd64/include/param.h |   5 +++--
 sys/arch/i386/include/param.h  |   5 +++--
 sys/sys/param.h                |  24 ++++++++++++++++++++----
 3 files changed, 26 insertions(+), 8 deletions(-)

diffs (83 lines):

diff -r 2b634ce9af05 -r 188a417e1ab3 sys/arch/amd64/include/param.h
--- a/sys/arch/amd64/include/param.h    Tue Aug 20 12:26:38 2019 +0000
+++ b/sys/arch/amd64/include/param.h    Tue Aug 20 12:33:04 2019 +0000
@@ -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
diff -r 2b634ce9af05 -r 188a417e1ab3 sys/arch/i386/include/param.h
--- a/sys/arch/i386/include/param.h     Tue Aug 20 12:26:38 2019 +0000
+++ b/sys/arch/i386/include/param.h     Tue Aug 20 12:33:04 2019 +0000
@@ -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 */
diff -r 2b634ce9af05 -r 188a417e1ab3 sys/sys/param.h
--- a/sys/sys/param.h   Tue Aug 20 12:26:38 2019 +0000
+++ b/sys/sys/param.h   Tue Aug 20 12:33:04 2019 +0000
@@ -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



Home | Main Index | Thread Index | Old Index