linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH rcu 1/5] tools/nolibc/stdlib: Support overflow checking for older compiler versions
  2022-06-20 23:13 [PATCH rcu 0/5] nolibc updates for v5.20 Paul E. McKenney
@ 2022-06-20 23:13 ` Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 2/5] tools/nolibc/stdio: Add format attribute to enable printf warnings Paul E. McKenney
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-06-20 23:13 UTC (permalink / raw)
  To: rcu
  Cc: linux-kernel, kernel-team, rostedt, Ammar Faizi, Willy Tarreau,
	Alviro Iskandar Setiawan, Paul E . McKenney

From: Ammar Faizi <ammarfaizi2@gnuweeb.org>

Previously, we used __builtin_mul_overflow() to check for overflow in
the multiplication operation in the calloc() function. However, older
compiler versions don't support this built-in. This patch changes the
overflow checking mechanism to make it work on any compiler version
by using a division method to check for overflow. No functional change
intended. While in there, remove the unused variable `void *orig`.

Link: https://lore.kernel.org/lkml/20220330024114.GA18892@1wt.eu
Suggested-by: Willy Tarreau <w@1wt.eu>
Cc: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Acked-by: Willy Tarreau <w@1wt.eu>
Reviewed-by: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 tools/include/nolibc/stdlib.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 8fd32eaf80370..92378c4b96605 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -128,10 +128,9 @@ void *malloc(size_t len)
 static __attribute__((unused))
 void *calloc(size_t size, size_t nmemb)
 {
-	void *orig;
-	size_t res = 0;
+	size_t x = size * nmemb;
 
-	if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) {
+	if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
 		SET_ERRNO(ENOMEM);
 		return NULL;
 	}
@@ -140,7 +139,7 @@ void *calloc(size_t size, size_t nmemb)
 	 * No need to zero the heap, the MAP_ANONYMOUS in malloc()
 	 * already does it.
 	 */
-	return malloc(res);
+	return malloc(x);
 }
 
 static __attribute__((unused))
-- 
2.31.1.189.g2e36527f23


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH rcu 0/5] nolibc updates for v5.20
@ 2022-06-20 23:13 Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 1/5] tools/nolibc/stdlib: Support overflow checking for older compiler versions Paul E. McKenney
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-06-20 23:13 UTC (permalink / raw)
  To: rcu; +Cc: linux-kernel, kernel-team, rostedt

Hello!

This series provides nolibc updates, including improved testing:

1.	Support overflow checking for older compiler versions, courtesy
	of Ammar Faizi.

2.	Add format attribute to enable printf warnings, courtesy of
	Alviro Iskandar Setiawan.

3.	fix the makefile to also work as "make -C tools ...", courtesy
	of Willy Tarreau.

4.	make the default target build the headers, courtesy of Willy
	Tarreau.

5.	add a help target to list supported targets, courtesy of Willy
	Tarreau.

						Thanx, Paul

------------------------------------------------------------------------

 b/tools/Makefile                |    3 +++
 b/tools/include/nolibc/Makefile |   18 +++++++++++++++++-
 b/tools/include/nolibc/stdio.h  |    4 ++--
 b/tools/include/nolibc/stdlib.h |    7 +++----
 tools/include/nolibc/Makefile   |   19 ++++++++++++++++++-
 5 files changed, 43 insertions(+), 8 deletions(-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH rcu 2/5] tools/nolibc/stdio: Add format attribute to enable printf warnings
  2022-06-20 23:13 [PATCH rcu 0/5] nolibc updates for v5.20 Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 1/5] tools/nolibc/stdlib: Support overflow checking for older compiler versions Paul E. McKenney
@ 2022-06-20 23:13 ` Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 3/5] tools/nolibc: fix the makefile to also work as "make -C tools ..." Paul E. McKenney
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-06-20 23:13 UTC (permalink / raw)
  To: rcu
  Cc: linux-kernel, kernel-team, rostedt, Alviro Iskandar Setiawan,
	Ammar Faizi, Willy Tarreau, Paul E . McKenney

From: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>

When we use printf and fprintf functions from the nolibc, we don't
get any warning from the compiler if we have the wrong arguments.
For example, the following calls will compile silently:
```
  printf("%s %s\n", "aaa");
  fprintf(stdout, "%s %s\n", "xxx", 1);
```
(Note the wrong arguments).

Those calls are undefined behavior. The compiler can help us warn
about the above mistakes by adding a `printf` format attribute to
those functions declaration. This patch adds it, and now it yields
these warnings for those mistakes:
```
  warning: format `%s` expects a matching `char *` argument [-Wformat=]
  warning: format `%s` expects argument of type `char *`, but argument 4 has type `int` [-Wformat=]
```

  [ ammarfaizi2: Simplify the attribute placement. ]

Signed-off-by: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 tools/include/nolibc/stdio.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 15dedf8d0902d..a3cebc4bc3ac4 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -273,7 +273,7 @@ int vfprintf(FILE *stream, const char *fmt, va_list args)
 	return written;
 }
 
-static __attribute__((unused))
+static __attribute__((unused, format(printf, 2, 3)))
 int fprintf(FILE *stream, const char *fmt, ...)
 {
 	va_list args;
@@ -285,7 +285,7 @@ int fprintf(FILE *stream, const char *fmt, ...)
 	return ret;
 }
 
-static __attribute__((unused))
+static __attribute__((unused, format(printf, 1, 2)))
 int printf(const char *fmt, ...)
 {
 	va_list args;
-- 
2.31.1.189.g2e36527f23


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH rcu 3/5] tools/nolibc: fix the makefile to also work as "make -C tools ..."
  2022-06-20 23:13 [PATCH rcu 0/5] nolibc updates for v5.20 Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 1/5] tools/nolibc/stdlib: Support overflow checking for older compiler versions Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 2/5] tools/nolibc/stdio: Add format attribute to enable printf warnings Paul E. McKenney
@ 2022-06-20 23:13 ` Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 4/5] tools/nolibc: make the default target build the headers Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 5/5] tools/nolibc: add a help target to list supported targets Paul E. McKenney
  4 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-06-20 23:13 UTC (permalink / raw)
  To: rcu; +Cc: linux-kernel, kernel-team, rostedt, Willy Tarreau, Paul E . McKenney

From: Willy Tarreau <w@1wt.eu>

As reported by Linus, the nolibc's makefile is currently broken when
invoked as per the documented method (make -C tools nolibc_<target>),
because it now relies on the ARCH and OUTPUT variables that are not
set in this case.

This patch addresses this by sourcing subarch.include, and by
presetting OUTPUT to the current directory if not set. This is
sufficient to make the commands work both as a standalone target
and as a tools/ sub-target.

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 tools/include/nolibc/Makefile | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index 7a16d917c1859..e8bac6ef36538 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -7,6 +7,22 @@ ifeq ($(srctree),)
 srctree := $(patsubst %/tools/include/,%,$(dir $(CURDIR)))
 endif
 
+# when run as make -C tools/ nolibc_<foo> the arch is not set
+ifeq ($(ARCH),)
+include $(srctree)/scripts/subarch.include
+ARCH = $(SUBARCH)
+endif
+
+# OUTPUT is only set when run from the main makefile, otherwise
+# it defaults to this nolibc directory.
+OUTPUT ?= $(CURDIR)/
+
+ifeq ($(V),1)
+Q=
+else
+Q=@
+endif
+
 nolibc_arch := $(patsubst arm64,aarch64,$(ARCH))
 arch_file := arch-$(nolibc_arch).h
 all_files := ctype.h errno.h nolibc.h signal.h std.h stdio.h stdlib.h string.h \
@@ -36,7 +52,7 @@ headers:
 
 headers_standalone: headers
 	$(Q)$(MAKE) -C $(srctree) headers
-	$(Q)$(MAKE) -C $(srctree) headers_install INSTALL_HDR_PATH=$(OUTPUT)/sysroot
+	$(Q)$(MAKE) -C $(srctree) headers_install INSTALL_HDR_PATH=$(OUTPUT)sysroot
 
 clean:
 	$(call QUIET_CLEAN, nolibc) rm -rf "$(OUTPUT)sysroot"
-- 
2.31.1.189.g2e36527f23


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH rcu 4/5] tools/nolibc: make the default target build the headers
  2022-06-20 23:13 [PATCH rcu 0/5] nolibc updates for v5.20 Paul E. McKenney
                   ` (2 preceding siblings ...)
  2022-06-20 23:13 ` [PATCH rcu 3/5] tools/nolibc: fix the makefile to also work as "make -C tools ..." Paul E. McKenney
@ 2022-06-20 23:13 ` Paul E. McKenney
  2022-06-20 23:13 ` [PATCH rcu 5/5] tools/nolibc: add a help target to list supported targets Paul E. McKenney
  4 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-06-20 23:13 UTC (permalink / raw)
  To: rcu; +Cc: linux-kernel, kernel-team, rostedt, Willy Tarreau, Paul E . McKenney

From: Willy Tarreau <w@1wt.eu>

The help in "make -C tools" enumerates nolibc as a valid target so we
must at least make it do something. Let's make it do the equivalent
of "make headers" in that it will prepare a sysroot with the arch's
headers, but will not install the kernel's headers. This is the
minimum some tools will need when built with a full-blown toolchain
anyway.

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 tools/Makefile                | 3 +++
 tools/include/nolibc/Makefile | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/Makefile b/tools/Makefile
index c074e42fd92f5..e497875fc7e3f 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -78,6 +78,9 @@ bpf/%: FORCE
 libapi: FORCE
 	$(call descend,lib/api)
 
+nolibc: FORCE
+	$(call descend,include/nolibc)
+
 nolibc_%: FORCE
 	$(call descend,include/nolibc,$(patsubst nolibc_%,%,$@))
 
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index e8bac6ef36538..9768819abd55d 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -29,7 +29,7 @@ all_files := ctype.h errno.h nolibc.h signal.h std.h stdio.h stdlib.h string.h \
              sys.h time.h types.h unistd.h
 
 # install all headers needed to support a bare-metal compiler
-all:
+all: headers
 
 # Note: when ARCH is "x86" we concatenate both x86_64 and i386
 headers:
-- 
2.31.1.189.g2e36527f23


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH rcu 5/5] tools/nolibc: add a help target to list supported targets
  2022-06-20 23:13 [PATCH rcu 0/5] nolibc updates for v5.20 Paul E. McKenney
                   ` (3 preceding siblings ...)
  2022-06-20 23:13 ` [PATCH rcu 4/5] tools/nolibc: make the default target build the headers Paul E. McKenney
@ 2022-06-20 23:13 ` Paul E. McKenney
  4 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-06-20 23:13 UTC (permalink / raw)
  To: rcu; +Cc: linux-kernel, kernel-team, rostedt, Willy Tarreau, Paul E . McKenney

From: Willy Tarreau <w@1wt.eu>

The "help" target simply presents the list of supported targets
and the current set of variables being used to build the sysroot.

Since the help in tools/ suggests to use "install", which is
supported by most tools while such a target is not really relevant
here, an "install" target was also added, redirecting to "help".

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
---
 tools/include/nolibc/Makefile | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index 9768819abd55d..cfd06764b5aee 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -31,6 +31,23 @@ all_files := ctype.h errno.h nolibc.h signal.h std.h stdio.h stdlib.h string.h \
 # install all headers needed to support a bare-metal compiler
 all: headers
 
+install: help
+
+help:
+	@echo "Supported targets under nolibc:"
+	@echo "  all                 call \"headers\""
+	@echo "  clean               clean the sysroot"
+	@echo "  headers             prepare a sysroot in tools/include/nolibc/sysroot"
+	@echo "  headers_standalone  like \"headers\", and also install kernel headers"
+	@echo "  help                this help"
+	@echo ""
+	@echo "These targets may also be called from tools as \"make nolibc_<target>\"."
+	@echo ""
+	@echo "Currently using the following variables:"
+	@echo "  ARCH    = $(ARCH)"
+	@echo "  OUTPUT  = $(OUTPUT)"
+	@echo ""
+
 # Note: when ARCH is "x86" we concatenate both x86_64 and i386
 headers:
 	$(Q)mkdir -p $(OUTPUT)sysroot
-- 
2.31.1.189.g2e36527f23


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-20 23:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-20 23:13 [PATCH rcu 0/5] nolibc updates for v5.20 Paul E. McKenney
2022-06-20 23:13 ` [PATCH rcu 1/5] tools/nolibc/stdlib: Support overflow checking for older compiler versions Paul E. McKenney
2022-06-20 23:13 ` [PATCH rcu 2/5] tools/nolibc/stdio: Add format attribute to enable printf warnings Paul E. McKenney
2022-06-20 23:13 ` [PATCH rcu 3/5] tools/nolibc: fix the makefile to also work as "make -C tools ..." Paul E. McKenney
2022-06-20 23:13 ` [PATCH rcu 4/5] tools/nolibc: make the default target build the headers Paul E. McKenney
2022-06-20 23:13 ` [PATCH rcu 5/5] tools/nolibc: add a help target to list supported targets Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).