Module Name:    src
Committed By:   mrg
Date:           Sun Sep  3 21:41:45 UTC 2023

Modified Files:
        src/lib/libc/arch/sparc: Makefile.inc
        src/lib/libc/arch/sparc/gen: Makefile.inc longjmp.c setjmp.S
Added Files:
        src/lib/libc/arch/sparc: genassym.cf
        src/lib/libc/arch/sparc/gen: sparc_longjmp.h

Log Message:
avoid array-bounds issues by using more complete types.

also, don't cast to a type that includes an extra, unused, member.

while here, replace the hard coded offsets in setjmp.S and some
asserts in longjmp.c with assym.h and shared structures for all
the movings parts, and asserts based upon those structures.

avoids GCC 12 warnings.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/lib/libc/arch/sparc/Makefile.inc
cvs rdiff -u -r0 -r1.1 src/lib/libc/arch/sparc/genassym.cf
cvs rdiff -u -r1.23 -r1.24 src/lib/libc/arch/sparc/gen/Makefile.inc
cvs rdiff -u -r1.3 -r1.4 src/lib/libc/arch/sparc/gen/longjmp.c
cvs rdiff -u -r1.12 -r1.13 src/lib/libc/arch/sparc/gen/setjmp.S
cvs rdiff -u -r0 -r1.1 src/lib/libc/arch/sparc/gen/sparc_longjmp.h

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

Modified files:

Index: src/lib/libc/arch/sparc/Makefile.inc
diff -u src/lib/libc/arch/sparc/Makefile.inc:1.16 src/lib/libc/arch/sparc/Makefile.inc:1.17
--- src/lib/libc/arch/sparc/Makefile.inc:1.16	Sun Oct 31 22:24:35 2021
+++ src/lib/libc/arch/sparc/Makefile.inc	Sun Sep  3 21:41:45 2023
@@ -1,7 +1,9 @@
-#	$NetBSD: Makefile.inc,v 1.16 2021/10/31 22:24:35 thorpej Exp $
+#	$NetBSD: Makefile.inc,v 1.17 2023/09/03 21:41:45 mrg Exp $
 
 SRCS+=	__sigtramp2.S
 
+CPPFLAGS.assym.h+=	-I${LIBCDIR}/arch/sparc/gen
+
 .if ${MACHINE} != "sparc64"
 # `source' files built from m4 source
 # the name `div.o' is taken for the ANSI C `div' function, hence sdiv here

Index: src/lib/libc/arch/sparc/gen/Makefile.inc
diff -u src/lib/libc/arch/sparc/gen/Makefile.inc:1.23 src/lib/libc/arch/sparc/gen/Makefile.inc:1.24
--- src/lib/libc/arch/sparc/gen/Makefile.inc:1.23	Sat Jul 12 19:21:48 2014
+++ src/lib/libc/arch/sparc/gen/Makefile.inc	Sun Sep  3 21:41:45 2023
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.23 2014/07/12 19:21:48 nakayama Exp $
+#	$NetBSD: Makefile.inc,v 1.24 2023/09/03 21:41:45 mrg Exp $
 
 SRCS+=	fabs.S modf.S
 SRCS+=	flt_rounds.c fpgetmask.c fpgetround.c fpgetsticky.c fpsetmask.c \
@@ -29,6 +29,9 @@ SRCS+=	mul.S umul.S
 SRCS+=	fixunsdfsi.S saveregs.S
 SRCS+=	bswap16.c bswap32.c bswap64.c
 
+CPPFLAGS.setjmp.S+=	-I.
+CPPFLAGS.longjmp.c+=	-I.
+
 LSRCS.sparc.gen=	Lint_swapcontext.c
 LSRCS+=		${LSRCS.sparc.gen}
 DPSRCS+=	${LSRCS.sparc.gen}

Index: src/lib/libc/arch/sparc/gen/longjmp.c
diff -u src/lib/libc/arch/sparc/gen/longjmp.c:1.3 src/lib/libc/arch/sparc/gen/longjmp.c:1.4
--- src/lib/libc/arch/sparc/gen/longjmp.c:1.3	Sat Apr 30 23:41:12 2011
+++ src/lib/libc/arch/sparc/gen/longjmp.c	Sun Sep  3 21:41:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: longjmp.c,v 1.3 2011/04/30 23:41:12 martin Exp $	*/
+/*	$NetBSD: longjmp.c,v 1.4 2023/09/03 21:41:45 mrg Exp $	*/
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -41,20 +41,17 @@
 #include <setjmp.h>
 #include <compat/include/setjmp.h>
 
-struct __jmp_buf_regs_t {
-	__greg_t	g4;
-	__greg_t	g7;
-	__greg_t	save_mask;
-};
+#include "assym.h"
+#include "sparc_longjmp.h"
 
 /*
- * setjmp.S uses hard coded offsets into the jump_buf,
- * make sure any changes cause a compile failure here
+ * check that offsets in the above structures match their usage in the
+ * setjmp() side of this setup.  a jmp_buf is the 12-word contents of
+ * the sigcontexst structure, plus 2 more words for g4 and g7.
  */
-__CTASSERT(56 == offsetof(struct __jmp_buf_regs_t,save_mask) +
-	sizeof(struct sigcontext));
-__CTASSERT(sizeof(sigjmp_buf) >= sizeof(struct __jmp_buf_regs_t) +
-	sizeof(struct sigcontext));
+__CTASSERT(_SIZEOF_SC + _JB_G4 == offsetof(struct __jmp_buf,regs.g4));
+__CTASSERT(_SIZEOF_SC + _JB_G7 == offsetof(struct __jmp_buf,regs.g7));
+__CTASSERT(sizeof(jmp_buf) >= sizeof(struct __jmp_buf));
 
 /*
  * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
@@ -64,8 +61,9 @@ __CTASSERT(sizeof(sigjmp_buf) >= sizeof(
 void
 __longjmp14(jmp_buf env, int val)
 {
-	struct sigcontext *sc = (void *)env;
-	struct __jmp_buf_regs_t *r = (void*)&sc[1];
+	struct __jmp_buf *context = (void *)env;
+	struct sigcontext *sc = &context->sc;
+	struct __jmp_buf_regs_t *r = &context->regs;
 	ucontext_t uc;
 
 	/* Ensure non-zero SP */

Index: src/lib/libc/arch/sparc/gen/setjmp.S
diff -u src/lib/libc/arch/sparc/gen/setjmp.S:1.12 src/lib/libc/arch/sparc/gen/setjmp.S:1.13
--- src/lib/libc/arch/sparc/gen/setjmp.S:1.12	Sat Apr 30 23:41:13 2011
+++ src/lib/libc/arch/sparc/gen/setjmp.S	Sun Sep  3 21:41:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: setjmp.S,v 1.12 2011/04/30 23:41:13 martin Exp $	*/
+/*	$NetBSD: setjmp.S,v 1.13 2023/09/03 21:41:45 mrg Exp $	*/
 
 /*
  * Copyright (c) 1992, 1993
@@ -40,7 +40,7 @@
 #if 0
 	.asciz "@(#)setjmp.s	8.1 (Berkeley) 6/4/93"
 #else
-	RCSID("$NetBSD: setjmp.S,v 1.12 2011/04/30 23:41:13 martin Exp $")
+	RCSID("$NetBSD: setjmp.S,v 1.13 2023/09/03 21:41:45 mrg Exp $")
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -56,6 +56,7 @@
  */
 
 #include "SYS.h"
+#include "assym.h"
 
 ENTRY(__setjmp14)
 	save	%sp, -CCFSZ, %sp
@@ -74,15 +75,15 @@ ENTRY(__setjmp14)
 
 	ld	[%sp + 0x48 + 8], %o0	/* foo.ss_flags */
 	and	%o0, 1, %o1	/* onstack = foo.ss_flags & 1; */
-	st	%o1, [%i0 + 0]	/* sc.sc_onstack = current onstack; */
-	st	%fp, [%i0 + 8]	/* sc.sc_sp = (caller's) sp */
+	st	%o1, [%i0 + _SC_ONSTACK] /* sc.sc_onstack = current onstack; */
+	st	%fp, [%i0 + _SC_SP]	/* sc.sc_sp = (caller's) sp */
 	add	%i7, 8, %o0
-	st	%o0, [%i0 + 12]	/* sc.sc_pc = return_pc */
-	st	%g3, [%i0 + 16]	/* sc.sc_npc */
-	st	%g0, [%i0 + 20]	/* sc.sc_psr = (clean psr) */
-	st	%g2, [%i0 + 24]
-	st	%g4, [%i0 + 48]
-	st	%g7, [%i0 + 52]
+	st	%o0, [%i0 + _SC_PC]	/* sc.sc_pc = return_pc */
+	st	%g3, [%i0 + _SC_NPC]	/* sc.sc_npc */
+	st	%g0, [%i0 + _SC_PSR]	/* sc.sc_psr = (clean psr) */
+	st	%g2, [%i0 + _SC_G1]
+	st	%g4, [%i0 + _SIZEOF_SC + _JB_G4]
+	st	%g7, [%i0 + _SIZEOF_SC + _JB_G7]
 	ret			/* return 0 */
 	 restore	%g0, %g0, %o0
 

Added files:

Index: src/lib/libc/arch/sparc/genassym.cf
diff -u /dev/null src/lib/libc/arch/sparc/genassym.cf:1.1
--- /dev/null	Sun Sep  3 21:41:45 2023
+++ src/lib/libc/arch/sparc/genassym.cf	Sun Sep  3 21:41:45 2023
@@ -0,0 +1,41 @@
+# $NetBSD: genassym.cf,v 1.1 2023/09/03 21:41:45 mrg Exp $
+
+# Copyright (c) 2023 Matthew R. Green
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+include <sys/types.h>
+include <signal.h>
+include "sparc_longjmp.h"
+
+define _SC_ONSTACK	offsetof(struct sigcontext, sc_onstack)
+define _SC_SP		offsetof(struct sigcontext, sc_sp)
+define _SC_PC		offsetof(struct sigcontext, sc_pc)
+define _SC_NPC		offsetof(struct sigcontext, sc_npc)
+define _SC_PSR		offsetof(struct sigcontext, sc_psr)
+define _SC_G1		offsetof(struct sigcontext, sc_g1)
+
+define _SIZEOF_SC	sizeof(struct sigcontext)
+
+define _JB_G4		offsetof(struct __jmp_buf_regs_t, g4)
+define _JB_G7		offsetof(struct __jmp_buf_regs_t, g7)

Index: src/lib/libc/arch/sparc/gen/sparc_longjmp.h
diff -u /dev/null src/lib/libc/arch/sparc/gen/sparc_longjmp.h:1.1
--- /dev/null	Sun Sep  3 21:41:45 2023
+++ src/lib/libc/arch/sparc/gen/sparc_longjmp.h	Sun Sep  3 21:41:45 2023
@@ -0,0 +1,48 @@
+/*	$NetBSD: sparc_longjmp.h,v 1.1 2023/09/03 21:41:45 mrg Exp $	*/
+
+/*
+ * Copyright (c) 2023 Matthew R. Green
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _SPARC_LONGJMP_H_
+#define _SPARC_LONGJMP_H_
+
+/*
+ * This structure is written in setjmp.S and read by longjmp.c, and is
+ * here so that genassym can access it and we can ensure the right offsets
+ * are used in the right places.
+ */
+
+struct __jmp_buf_regs_t {
+	__greg_t	g4;
+	__greg_t	g7;
+};
+
+struct __jmp_buf {
+	struct sigcontext sc;
+	struct __jmp_buf_regs_t regs;
+};
+
+#endif /* _SPARC_LONGJMP_H_ */

Reply via email to