git: bd8bde138531 - stable/13 - Merge bmake-20220208

From: Simon J. Gerraty <sjg_at_FreeBSD.org>
Date: Wed, 16 Feb 2022 05:38:59 UTC
The branch stable/13 has been updated by sjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=bd8bde138531bc28f6be861ac3f39b7ac89c64b6

commit bd8bde138531bc28f6be861ac3f39b7ac89c64b6
Author:     Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2022-02-09 17:25:03 +0000
Commit:     Simon J. Gerraty <sjg@FreeBSD.org>
CommitDate: 2022-02-16 05:37:08 +0000

    Merge bmake-20220208
    
            * unit-tests/Makefile: disable opt-debug-x-trace on Linux if there
            is any chance we have dash as .SHELL
    
            * meta.c: use a variable to hold command line to be filtered
            to avoid any side effects from content of command line.
    
    Merge commit '535c59a6a9214436f5d6643775d29808e4b3408d'
    
    (cherry picked from commit a6f0e10b24b6011dd5f71c99848ddeef264d9ac9)
---
 contrib/bmake/ChangeLog                     | 11 +++++++
 contrib/bmake/FILES                         |  2 ++
 contrib/bmake/VERSION                       |  2 +-
 contrib/bmake/meta.c                        | 25 ++++++---------
 contrib/bmake/mk/ChangeLog                  | 11 +++++++
 contrib/bmake/mk/cc-wrap.mk                 |  7 +++-
 contrib/bmake/mk/install-mk                 |  4 +--
 contrib/bmake/mk/sys.vars.mk                | 20 ++++++++++--
 contrib/bmake/os.sh                         |  0
 contrib/bmake/parse.c                       |  5 ++-
 contrib/bmake/unit-tests/Makefile           | 16 +++++++--
 contrib/bmake/unit-tests/suff-use.exp       |  7 ++++
 contrib/bmake/unit-tests/suff-use.mk        | 50 +++++++++++++++++++++++++++++
 contrib/bmake/unit-tests/var-scope-local.mk | 31 +++++++++++++++++-
 usr.bin/bmake/Makefile.config               |  2 +-
 usr.bin/bmake/unit-tests/Makefile           | 16 +++++++--
 16 files changed, 178 insertions(+), 31 deletions(-)

diff --git a/contrib/bmake/ChangeLog b/contrib/bmake/ChangeLog
index a87f39fc49c9..0cf33bc6e039 100644
--- a/contrib/bmake/ChangeLog
+++ b/contrib/bmake/ChangeLog
@@ -1,3 +1,14 @@
+2022-02-08  Simon J Gerraty  <sjg@beast.crufty.net>
+
+	* unit-tests/Makefile: disable opt-debug-x-trace on Linux if there
+	is any chance we have dash as .SHELL
+
+	* VERSION (_MAKE_VERSION): 20220208
+	Merge with NetBSD make, pick up
+	o more unit tests
+	o meta.c: use a variable to hold command line to be filtered
+	to avoid any side effects from content of command line.
+
 2022-02-04  Simon J Gerraty  <sjg@beast.crufty.net>
 
 	* VERSION (_MAKE_VERSION): 20220204
diff --git a/contrib/bmake/FILES b/contrib/bmake/FILES
index 8193debe5032..d421c1734fa2 100644
--- a/contrib/bmake/FILES
+++ b/contrib/bmake/FILES
@@ -616,6 +616,8 @@ unit-tests/suff-transform-expand.exp
 unit-tests/suff-transform-expand.mk
 unit-tests/suff-transform-select.exp
 unit-tests/suff-transform-select.mk
+unit-tests/suff-use.exp
+unit-tests/suff-use.mk
 unit-tests/sunshcmd.exp
 unit-tests/sunshcmd.mk
 unit-tests/ternary.exp
diff --git a/contrib/bmake/VERSION b/contrib/bmake/VERSION
index ae363192cf1c..f1627a3f7373 100644
--- a/contrib/bmake/VERSION
+++ b/contrib/bmake/VERSION
@@ -1,2 +1,2 @@
 # keep this compatible with sh and make
-_MAKE_VERSION=20220204
+_MAKE_VERSION=20220208
diff --git a/contrib/bmake/meta.c b/contrib/bmake/meta.c
index 2d6ab3ab8a3d..8c37db62928c 100644
--- a/contrib/bmake/meta.c
+++ b/contrib/bmake/meta.c
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.196 2022/02/04 23:22:19 rillig Exp $ */
+/*      $NetBSD: meta.c,v 1.197 2022/02/08 22:36:02 sjg Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -1055,36 +1055,31 @@ append_if_new(StringList *list, const char *str)
     Lst_Append(list, bmake_strdup(str));
 }
 
+/* A "reserved" variable to store the command to be filtered */
+#define META_CMD_FILTER_VAR ".MAKE.cmd_filtered"
+
 static char *
-meta_filter_cmd(Buffer *buf, GNode *gn, char *s)
+meta_filter_cmd(GNode *gn, char *s)
 {
-    Buf_Clear(buf);
-    Buf_AddStr(buf, "${");
-    Buf_AddStr(buf, s);
-    Buf_AddStr(buf, ":L:${" MAKE_META_CMP_FILTER ":ts:}}");
-    Var_Subst(buf->data, gn, VARE_WANTRES, &s);
+    Var_Set(gn, META_CMD_FILTER_VAR, s);
+    Var_Subst("${" META_CMD_FILTER_VAR ":${" MAKE_META_CMP_FILTER ":ts:}}", gn, VARE_WANTRES, &s);
     return s;
 }
 
 static int
 meta_cmd_cmp(GNode *gn, char *a, char *b, bool filter)
 {
-    static bool once = false;
-    static Buffer buf;
     int rc;
 
     rc = strcmp(a, b);
     if (rc == 0 || !filter)
 	return rc;
-    if (!once) {
-	once = true;
-	Buf_Init(&buf);
-    }
-    a = meta_filter_cmd(&buf, gn, a);
-    b = meta_filter_cmd(&buf, gn, b);
+    a = meta_filter_cmd(gn, a);
+    b = meta_filter_cmd(gn, b);
     rc = strcmp(a, b);
     free(a);
     free(b);
+    Var_Delete(gn, META_CMD_FILTER_VAR);
     return rc;
 }
 
diff --git a/contrib/bmake/mk/ChangeLog b/contrib/bmake/mk/ChangeLog
index f196b086e7a1..f8f69630d981 100644
--- a/contrib/bmake/mk/ChangeLog
+++ b/contrib/bmake/mk/ChangeLog
@@ -1,3 +1,14 @@
+2022-02-06  Simon J Gerraty  <sjg@beast.crufty.net>
+
+	* install-mk (MK_VERSION): 20220206
+
+	* cc-wrap.mk: docuement how CCACHE etc might be set for
+	maximum flexibility
+
+2022-02-05  Simon J Gerraty  <sjg@beast.crufty.net>
+
+	* sys.vars.mk: use JOT_CMD (jot or seq) if available for M_JOT
+
 2022-02-04  Simon J Gerraty  <sjg@beast.crufty.net>
 
 	* install-mk (MK_VERSION): 20220204
diff --git a/contrib/bmake/mk/cc-wrap.mk b/contrib/bmake/mk/cc-wrap.mk
index 7b9c6e0cb8c0..1e3931d1cae5 100644
--- a/contrib/bmake/mk/cc-wrap.mk
+++ b/contrib/bmake/mk/cc-wrap.mk
@@ -1,4 +1,4 @@
-# $Id: cc-wrap.mk,v 1.4 2022/02/02 17:41:56 sjg Exp $
+# $Id: cc-wrap.mk,v 1.5 2022/02/07 19:02:55 sjg Exp $
 #
 #	@(#) Copyright (c) 2022, Simon J. Gerraty
 #
@@ -30,6 +30,11 @@ CC_WRAP_LIST := ${CC_WRAP_LIST:O:u}
 # what might we wrap them with?
 CC_WRAPPERS += ccache distcc icecc
 CC_WRAPPERS := ${CC_WRAPPERS:O:u}
+# $W can be as simple or complicated as you like (default is just $w)
+# eg.
+# CCACHE ?= ${CCACHE_ENV_VARS:@v@$v='${$v}'@} ${CCACHE_CMD} ${CCACHE_FLAGS}
+# or if you want global vars to be used modifiable after this include:
+# CCACHE ?= $${CCACHE_ENV_VARS:@v@$$v='$${$$v}'@} $${CCACHE_CMD} $${CCACHE_FLAGS}
 .for w in ${CC_WRAPPERS}
 ${w:tu} ?= $w
 .endfor
diff --git a/contrib/bmake/mk/install-mk b/contrib/bmake/mk/install-mk
index d309f116f4ff..abae12374249 100755
--- a/contrib/bmake/mk/install-mk
+++ b/contrib/bmake/mk/install-mk
@@ -55,7 +55,7 @@
 #       Simon J. Gerraty <sjg@crufty.net>
 
 # RCSid:
-#	$Id: install-mk,v 1.213 2022/02/05 01:39:12 sjg Exp $
+#	$Id: install-mk,v 1.214 2022/02/07 19:02:55 sjg Exp $
 #
 #	@(#) Copyright (c) 1994 Simon J. Gerraty
 #
@@ -70,7 +70,7 @@
 #	sjg@crufty.net
 #
 
-MK_VERSION=20220204
+MK_VERSION=20220206
 OWNER=
 GROUP=
 MODE=444
diff --git a/contrib/bmake/mk/sys.vars.mk b/contrib/bmake/mk/sys.vars.mk
index 391967b18a2b..b759cc3f5cfb 100644
--- a/contrib/bmake/mk/sys.vars.mk
+++ b/contrib/bmake/mk/sys.vars.mk
@@ -1,4 +1,4 @@
-# $Id: sys.vars.mk,v 1.7 2021/12/08 05:56:50 sjg Exp $
+# $Id: sys.vars.mk,v 1.9 2022/02/05 19:04:53 sjg Exp $
 #
 #	@(#) Copyright (c) 2003-2009, Simon J. Gerraty
 #
@@ -57,12 +57,26 @@ _type_sh = which
 M_type = @x@(${_type_sh:Utype} $$x) 2> /dev/null; echo;@:sh:[0]:N* found*:[@]:C,[()],,g
 M_whence = ${M_type}:M/*:[1]
 
-# produce similar output to jot(1)
+# produce similar output to jot(1) or seq(1)
 # eg. ${LIST:[#]:${M_JOT}}
 # would be 1 2 3 4 5 if LIST has 5 words
 # ${9:L:${M_JOT}}
 # would be 1 2 3 4 5 6 7 8 9
-M_JOT = @x@i=1;while [ $$$$i -le $$x ]; do echo $$$$i; i=$$$$((i + 1)); done;@:sh
+.if ${.MAKE.LEVEL} == 0
+.for x in jot seq
+.if empty(JOT_CMD)
+JOT_CMD := ${$x:L:${M_whence}}
+.endif
+.endfor
+.if !empty(JOT_CMD)
+.export JOT_CMD
+.endif
+.endif
+.if !empty(JOT_CMD)
+M_JOT = [1]:S,^,${JOT_CMD} ,:sh
+.else
+M_JOT = [1]:@x@i=1;while [ $$$$i -le $$x ]; do echo $$$$i; i=$$$$((i + 1)); done;@:sh
+.endif
 
 # ${LIST:${M_RANGE}} is 1 2 3 4 5 if LIST has 5 words
 .if ${MAKE_VERSION} >= 20170130
diff --git a/contrib/bmake/os.sh b/contrib/bmake/os.sh
old mode 100644
new mode 100755
diff --git a/contrib/bmake/parse.c b/contrib/bmake/parse.c
index 3faeafe1c739..9732fa396b46 100644
--- a/contrib/bmake/parse.c
+++ b/contrib/bmake/parse.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.662 2022/02/05 00:37:19 sjg Exp $	*/
+/*	$NetBSD: parse.c,v 1.663 2022/02/07 23:24:26 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -121,7 +121,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.662 2022/02/05 00:37:19 sjg Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.663 2022/02/07 23:24:26 rillig Exp $");
 
 /*
  * A file being read.
@@ -722,7 +722,6 @@ ApplyDependencySourceWait(bool isSpecial)
 		RememberLocation(gn);
 	gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
 	LinkToTargets(gn, isSpecial);
-
 }
 
 static bool
diff --git a/contrib/bmake/unit-tests/Makefile b/contrib/bmake/unit-tests/Makefile
index 2a300133eccc..eecbaa0fa33e 100644
--- a/contrib/bmake/unit-tests/Makefile
+++ b/contrib/bmake/unit-tests/Makefile
@@ -1,6 +1,6 @@
-# $Id: Makefile,v 1.171 2022/01/28 21:33:18 sjg Exp $
+# $Id: Makefile,v 1.174 2022/02/09 02:42:59 sjg Exp $
 #
-# $NetBSD: Makefile,v 1.302 2022/01/27 21:50:50 sjg Exp $
+# $NetBSD: Makefile,v 1.303 2022/02/07 22:43:50 rillig Exp $
 #
 # Unit tests for make(1)
 #
@@ -315,6 +315,7 @@ TESTS+=		suff-transform-debug
 TESTS+=		suff-transform-endless
 TESTS+=		suff-transform-expand
 TESTS+=		suff-transform-select
+TESTS+=		suff-use
 TESTS+=		sunshcmd
 TESTS+=		ternary
 TESTS+=		unexport
@@ -444,6 +445,17 @@ BROKEN_TESTS+= sh-flags
 .if ${.MAKE.OS:NDarwin} == ""
 BROKEN_TESTS+= shell-ksh
 .endif
+.if ${.MAKE.OS} == "Linux" && ${.SHELL:tA:T} != "bash"
+.if exists(/etc/os-release)
+distro!= . /etc/os-release && echo $$NAME
+.endif
+# dash fails -x output
+# .SHELL is not bash so may be dash
+# if distro is Ubuntu or we cannot tell, assume the worst
+.if ${distro:U:NUbuntu} == ""
+BROKEN_TESTS+= opt-debug-x-trace
+.endif
+.endif
 .if ${.MAKE.OS} == "SCO_SV"
 BROKEN_TESTS+= \
 	opt-debug-graph[23] \
diff --git a/contrib/bmake/unit-tests/suff-use.exp b/contrib/bmake/unit-tests/suff-use.exp
new file mode 100644
index 000000000000..4a9374d8e156
--- /dev/null
+++ b/contrib/bmake/unit-tests/suff-use.exp
@@ -0,0 +1,7 @@
+: 'Making demo.c out of nothing'
+make: don't know how to make demo.o (continuing)
+`all' not remade because of errors.
+
+Stop.
+make: stopped in unit-tests
+exit status 1
diff --git a/contrib/bmake/unit-tests/suff-use.mk b/contrib/bmake/unit-tests/suff-use.mk
new file mode 100644
index 000000000000..954c846b41fa
--- /dev/null
+++ b/contrib/bmake/unit-tests/suff-use.mk
@@ -0,0 +1,50 @@
+# $NetBSD: suff-use.mk,v 1.1 2022/02/07 22:43:50 rillig Exp $
+#
+# This test combines a .USE node with suffix rules, trying to add an
+# additional command before and after successful compilation of a .c file.
+#
+# History:
+#	bin/make-2001.11.12.21.58.18-plain
+#	| : 'Making demo.c out of nothing'
+#	| make: don't know how to make demo.o. Stop
+#	|
+#	| make: stopped in /home/rillig/proj/make-archive
+#	| exit status 2
+#	bin/make-2007.10.11.21.19.28-plain
+#
+#	bin/make-2014.08.23.15.05.40-plain
+#	| : 'Making demo.c out of nothing'
+#	| : 'Compiling demo.c to demo.o'
+#	| exit status 0
+#	bin/make-2014.09.05.06.57.20-plain
+#
+#	bin/make-2014.09.07.20.55.34-plain
+#	| : 'Making demo.c out of nothing'
+#	| make: don't know how to make demo.o. Stop
+#	|
+#	| make: stopped in /home/rillig/proj/make-archive
+#	| exit status 2
+#	...
+#
+# See also:
+#	https://gnats.netbsd.org/20993
+
+
+.SUFFIXES: .c .o
+
+all: demo.o
+
+.c.o:
+	: 'Compiling ${.IMPSRC} to ${.TARGET}'
+
+demo.c:
+	: 'Making ${.TARGET} out of nothing'
+
+using-before: .USEBEFORE
+	: 'Before making ${.TARGET} from ${.ALLSRCS}'
+
+using-after: .USE
+	: 'After making ${.TARGET} from ${.ALLSRCS}'
+
+# expect: make: don't know how to make demo.o (continuing)
+.c.o: using-before using-after
diff --git a/contrib/bmake/unit-tests/var-scope-local.mk b/contrib/bmake/unit-tests/var-scope-local.mk
index 91a5f525e855..1ff025299bf7 100644
--- a/contrib/bmake/unit-tests/var-scope-local.mk
+++ b/contrib/bmake/unit-tests/var-scope-local.mk
@@ -1,4 +1,4 @@
-# $NetBSD: var-scope-local.mk,v 1.3 2022/01/29 00:52:53 rillig Exp $
+# $NetBSD: var-scope-local.mk,v 1.4 2022/02/05 10:41:15 rillig Exp $
 #
 # Tests for target-local variables, such as ${.TARGET} or $@.  These variables
 # are relatively short-lived as they are created just before making the
@@ -198,3 +198,32 @@ a_use: .USE VAR=use
 
 all: var-scope-local-use.o
 var-scope-local-use.o: a_use
+
+
+# Since parse.c 1.656 from 2022-01-27 and before parse.c 1.662 from
+# 2022-02-05, there was an out-of-bounds read in Parse_IsVar when looking for
+# a variable assignment in a dependency line with trailing whitespace.  Lines
+# without trailing whitespace were not affected.  Global variable assignments
+# were guaranteed to have no trailing whitespace and were thus not affected.
+#
+# Try to reproduce some variants that may lead to a crash, depending on the
+# memory allocator.  To get a crash, the terminating '\0' of the line must be
+# the last byte of a memory page.  The expression '${:U}' forces this trailing
+# whitespace.
+
+# On FreeBSD x86_64, a crash could in some cases be forced using the following
+# line, which has length 47, so the terminating '\0' may end up at an address
+# of the form 0xXXXX_XXXX_XXXX_Xfff:
+Try_to_crash_FreeBSD.xxxxxxxxxxxxxxxxxx: 12345 ${:U}
+
+# The following line has length 4095, so line[4095] == '\0'.  If the line is
+# allocated on a page boundary and the following page is not mapped, this line
+# leads to a segmentation fault.
+${:U:range=511:@_@1234567@:ts.}: 12345 ${:U}
+
+# The following line has length 8191, so line[8191] == '\0'.  If the line is
+# allocated on a page boundary and the following page is not mapped, this line
+# leads to a segmentation fault.
+${:U:range=1023:@_@1234567@:ts.}: 12345 ${:U}
+
+12345:
diff --git a/usr.bin/bmake/Makefile.config b/usr.bin/bmake/Makefile.config
index ea46f5795b27..bf3e401548ad 100644
--- a/usr.bin/bmake/Makefile.config
+++ b/usr.bin/bmake/Makefile.config
@@ -7,7 +7,7 @@ SRCTOP?= ${.CURDIR:H:H}
 
 # things set by configure
 
-_MAKE_VERSION?=20220204
+_MAKE_VERSION?=20220208
 
 prefix?= /usr
 srcdir= ${SRCTOP}/contrib/bmake
diff --git a/usr.bin/bmake/unit-tests/Makefile b/usr.bin/bmake/unit-tests/Makefile
index 348fc0614c1b..4b3f49bb07b2 100644
--- a/usr.bin/bmake/unit-tests/Makefile
+++ b/usr.bin/bmake/unit-tests/Makefile
@@ -2,9 +2,9 @@
 # See contrib/bmake/bsd.after-import.mk
 #
 # $FreeBSD$
-# $Id: Makefile,v 1.171 2022/01/28 21:33:18 sjg Exp $
+# $Id: Makefile,v 1.174 2022/02/09 02:42:59 sjg Exp $
 #
-# $NetBSD: Makefile,v 1.302 2022/01/27 21:50:50 sjg Exp $
+# $NetBSD: Makefile,v 1.303 2022/02/07 22:43:50 rillig Exp $
 #
 # Unit tests for make(1)
 #
@@ -319,6 +319,7 @@ TESTS+=		suff-transform-debug
 TESTS+=		suff-transform-endless
 TESTS+=		suff-transform-expand
 TESTS+=		suff-transform-select
+TESTS+=		suff-use
 TESTS+=		sunshcmd
 TESTS+=		ternary
 TESTS+=		unexport
@@ -448,6 +449,17 @@ BROKEN_TESTS+= sh-flags
 .if ${.MAKE.OS:NDarwin} == ""
 BROKEN_TESTS+= shell-ksh
 .endif
+.if ${.MAKE.OS} == "Linux" && ${.SHELL:tA:T} != "bash"
+.if exists(/etc/os-release)
+distro!= . /etc/os-release && echo $$NAME
+.endif
+# dash fails -x output
+# .SHELL is not bash so may be dash
+# if distro is Ubuntu or we cannot tell, assume the worst
+.if ${distro:U:NUbuntu} == ""
+BROKEN_TESTS+= opt-debug-x-trace
+.endif
+.endif
 .if ${.MAKE.OS} == "SCO_SV"
 BROKEN_TESTS+= \
 	opt-debug-graph[23] \