tile: avoid undefined behavior with regs[TREG_TP] etc
authorChris Metcalf <cmetcalf@ezchip.com>
Wed, 12 Nov 2014 20:00:06 +0000 (15:00 -0500)
committerChris Metcalf <cmetcalf@ezchip.com>
Thu, 13 Nov 2014 14:39:26 +0000 (09:39 -0500)
Recent compilers warn about accesses to arrays past the end,
which is supported for pt_regs and sigcontext with the
intended alias past the end of regs[] to sp, tp, and lr
using TREG_SP, TREG_TP, and TREG_LR.

Make the intended usage explict by providing an anonymous
union of regs[56] on the one hand, and a short __regs[53]
on the other followed by the sp, tp, and lr fields, so the
aliasing is done explicitly and is visible to the compiler.

Reviewed-by: Jeff Epler <jepler@unpythonic.net>
Signed-off-by: Chris Metcalf <cmetcalf@ezchip.com>
arch/tile/include/uapi/asm/ptrace.h
arch/tile/include/uapi/asm/sigcontext.h

index 7757e19..d03b829 100644 (file)
@@ -52,12 +52,16 @@ typedef uint_reg_t pt_reg_t;
  * system call or exception.  "struct sigcontext" has the same shape.
  */
 struct pt_regs {
-       /* Saved main processor registers; 56..63 are special. */
-       /* tp, sp, and lr must immediately follow regs[] for aliasing. */
-       pt_reg_t regs[53];
-       pt_reg_t tp;            /* aliases regs[TREG_TP] */
-       pt_reg_t sp;            /* aliases regs[TREG_SP] */
-       pt_reg_t lr;            /* aliases regs[TREG_LR] */
+       union {
+               /* Saved main processor registers; 56..63 are special. */
+               pt_reg_t regs[56];
+               struct {
+                       pt_reg_t __regs[53];
+                       pt_reg_t tp;            /* aliases regs[TREG_TP] */
+                       pt_reg_t sp;            /* aliases regs[TREG_SP] */
+                       pt_reg_t lr;            /* aliases regs[TREG_LR] */
+               };
+       };
 
        /* Saved special registers. */
        pt_reg_t pc;            /* stored in EX_CONTEXT_K_0 */
index 6348e59..39ff5d1 100644 (file)
  * but is simplified since we know the fault is from userspace.
  */
 struct sigcontext {
-       __uint_reg_t gregs[53]; /* General-purpose registers.  */
-       __uint_reg_t tp;        /* Aliases gregs[TREG_TP].  */
-       __uint_reg_t sp;        /* Aliases gregs[TREG_SP].  */
-       __uint_reg_t lr;        /* Aliases gregs[TREG_LR].  */
+       __extension__ union {
+               /* General-purpose registers.  */
+               __uint_reg_t gregs[56];
+               __extension__ struct {
+                       __uint_reg_t __gregs[53];
+                       __uint_reg_t tp;        /* Aliases gregs[TREG_TP].  */
+                       __uint_reg_t sp;        /* Aliases gregs[TREG_SP].  */
+                       __uint_reg_t lr;        /* Aliases gregs[TREG_LR].  */
+               };
+       };
        __uint_reg_t pc;        /* Program counter.  */
        __uint_reg_t ics;       /* In Interrupt Critical Section?  */
        __uint_reg_t faultnum;  /* Fault number.  */