Skip to contentSkip to conversation

ELF: add and use SUPRESS_WARN_UNUSED_RESULT

PATCH Superseded Alexey Dobriyan 6 participants 13 messages Jun 25, 2021 – Jun 26, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ELF: add and use SUPRESS_WARN_UNUSED_RESULT

Last write to the "error" variable in load_elf_binary() is dead write.

Add and use SUPRESS_WARN_UNUSED_RESULT macro to express intent better.

Credit goes to Ed Catmur:

	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34

Macro doesn't work for WUR functions returning structures and unions, but it will work once gcc copies clang.

---

 fs/binfmt_elf.c                     |    3 ++-
 include/linux/compiler_attributes.h |    4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -1
@@ -1290,7 +1290,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		   and some applications "depend" upon this behavior.
 		   Since we do not have the power to recompile these, we
 		   emulate the SVr4 behavior. Sigh. */
-		error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
+		SUPRESS_WARN_UNUSED_RESULT
+		vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
 				MAP_FIXED | MAP_PRIVATE, 0);
 	}
 
include/linux/compiler_attributes.h +4 -0
@@ -284,6 +284,10 @@
  * clang: https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result
  */
 #define __must_check                    __attribute__((__warn_unused_result__))
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPRESS_WARN_UNUSED_RESULT	(void)!
 
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
#1 lore raw

include/linux/compiler_attributes.h line 290

Quoted 5 lines · Alexey Dobriyan
include/linux/compiler_attributes.h +4 -0
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPRESS_WARN_UNUSED_RESULT     (void)!

While it is related to the attribute, this macro is not an attribute, so please add it somewhere else.

By the way, the name has a typo.

Cheers,
Miguel

#2 lore raw
Quoted 9 lines · Miguel Ojeda

include/linux/compiler_attributes.h line 290

Quoted 5 lines · Alexey Dobriyan
include/linux/compiler_attributes.h +4 -0
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPRESS_WARN_UNUSED_RESULT     (void)!

While it is related to the attribute, this macro is not an attribute, so please add it somewhere else.

This is natural place. If you're supressing WUR, then the WUR macro itself is defined implying that the header has been included.

Quoted 1 line

By the way, the name has a typo.

Ha!

#3 lore raw
Quoted 11 lines · Alexey Dobriyan
Quoted 9 lines · Miguel Ojeda

include/linux/compiler_attributes.h line 290

Quoted 5 lines · Alexey Dobriyan
include/linux/compiler_attributes.h +4 -0
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPRESS_WARN_UNUSED_RESULT     (void)!

While it is related to the attribute, this macro is not an attribute, so please add it somewhere else.

Quoted 7 lines

This is natural place. If you're supressing WUR, then the WUR macro itself is defined implying that the header has been included.

Quoted 1 line

By the way, the name has a typo.

Ha!

as in Aha!?

s/SUPRESS/SUPPRESS/

#4 lore raw
In reply to Alexey Dobriyan
Quoted 3 lines · Alexey Dobriyan

This is natural place. If you're supressing WUR, then the WUR macro itself is defined implying that the header has been included.

I am not sure I understand -- I guess you are saying that your macro is only ever needed if __must_check is defined, which is what I meant by "related".

But this header was intentionally created to untangle others by keeping only attributes here.

Cheers,
Miguel

#5 lore raw

Re: [PATCH] ELF: add and use SUPRESS_WARN_UNUSED_RESULT\

Quoted 11 lines · Miguel Ojeda
Quoted 3 lines · Alexey Dobriyan

This is natural place. If you're supressing WUR, then the WUR macro itself is defined implying that the header has been included.

I am not sure I understand -- I guess you are saying that your macro is only ever needed if __must_check is defined, which is what I meant by "related".

But this header was intentionally created to untangle others by keeping only attributes here.

Sure. But then developer needs to include another header to use SUPRESS_WUR. As posted, attribute and its suppressor go hand to hand.

#6 lore raw

Re: [PATCH] ELF: add and use SUPRESS_WARN_UNUSED_RESULT\

Quoted 3 lines · Alexey Dobriyan

Sure. But then developer needs to include another header to use SUPRESS_WUR. As posted, attribute and its suppressor go hand to hand.

These headers are automatically included in most places.

Cheers,
Miguel

#7 lore raw

PATCH v2 1/1 ELF: add and use SUPPRESS_WARN_UNUSED_RESULT

In reply to Alexey Dobriyan

Last write to the "error" variable in load_elf_binary() is dead write.

Add and use SUPPRESS_WARN_UNUSED_RESULT macro to express intent better.

Credit goes to Ed Catmur:

	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34

Macro doesn't work for WUR functions returning structures and unions, but it will work when gcc copies clang.

---

 fs/binfmt_elf.c                     |    3 ++-
 include/linux/compiler_attributes.h |    4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -1
@@ -1290,7 +1290,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		   and some applications "depend" upon this behavior.
 		   Since we do not have the power to recompile these, we
 		   emulate the SVr4 behavior. Sigh. */
-		error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
+		SUPPRESS_WARN_UNUSED_RESULT
+		vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
 				MAP_FIXED | MAP_PRIVATE, 0);
 	}
 
include/linux/compiler_attributes.h +4 -0
@@ -284,6 +284,10 @@
  * clang: https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result
  */
 #define __must_check                    __attribute__((__warn_unused_result__))
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPPRESS_WARN_UNUSED_RESULT	(void)!
 
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
#8 lore raw

Re: [PATCH v2] ELF: add and use SUPPRESS_WARN_UNUSED_RESULT

Quoted 28 lines · On Sat, 26 Jun 2021 00:13:12 +0300 Alexey Dobriyan <[email protected]> wrote:

Last write to the "error" variable in load_elf_binary() is dead write.

Add and use SUPPRESS_WARN_UNUSED_RESULT macro to express intent better.

Credit goes to Ed Catmur:

	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34

Macro doesn't work for WUR functions returning structures and unions, but it will work when gcc copies clang.

...

fs/binfmt_elf.c +2 -1
@@ -1290,7 +1290,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		   and some applications "depend" upon this behavior.
 		   Since we do not have the power to recompile these, we
 		   emulate the SVr4 behavior. Sigh. */
-		error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
+		SUPPRESS_WARN_UNUSED_RESULT
+		vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
 				MAP_FIXED | MAP_PRIVATE, 0);
 	}
 

include/linux/compiler_attributes.h line 290

Quoted 7 lines
include/linux/compiler_attributes.h +4 -0
@@ -284,6 +284,10 @@
  * clang: https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result
  */
 #define __must_check                    __attribute__((__warn_unused_result__))
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPPRESS_WARN_UNUSED_RESULT	(void)!

That macro is rather ugly. Hopefully we won't really need it - how many such sites are there in a full kernel build anyway?

I can't imagine who added this to load_elf_binary():

	if (current->personality & MMAP_PAGE_ZERO) {
		/* Why this, you ask???  Well SVr4 maps page 0 as read-only,
		   and some applications "depend" upon this behavior.
		   Since we do not have the power to recompile these, we
		   emulate the SVr4 behavior. Sigh. */
		error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
				MAP_FIXED | MAP_PRIVATE, 0);
	}

I think it was there before most of us were born. The comment has a torvaldsy/viroey feel to it.

Do we really care about userspace which relies upon an SVR4 quirk? I guess it's too hard to prove the no case, so it stays.

But given that the loader is being asked to map this page, shouldn't we handle this error (fail the exec) if the mapping attempt failed? That seems better behavior than permitting some creaky old application to blunder into a mysterious crash?

#9 lore raw

Re: [PATCH v2] ELF: add and use SUPPRESS_WARN_UNUSED_RESULT

Quoted 12 lines · Andrew Morton

I can't imagine who added this to load_elf_binary():

if (current->personality & MMAP_PAGE_ZERO) {
/* Why this, you ask??? Well SVr4 maps page 0 as read-only,
and some applications "depend" upon this behavior. Since we do not have the power to recompile these, we emulate the SVr4 behavior. Sigh. */
error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE, 0);
}

Quoted 2 lines

I think it was there before most of us were born. The comment has a torvaldsy/viroey feel to it.

Heh.

It goes back to at least 1.1.14 (1994, I think) and originates in the ibcs code (Intel Binary Compatibility Specification 2), back in the dark ages when we thought that mattered.

Native Linux binaries were still a.out at that point. When ELF then became a native thing, we just moved (or copied) the old iBCS2 code over, and that "map zeroes at NULL" came along.

And I think it's actually Eric Youngdale who did that code. See

    https://www.linuxjournal.com/article/2809

and

    https://www.linuxjournal.com/article/1059
    https://www.linuxjournal.com/article/1060
Quoted 2 lines

Do we really care about userspace which relies upon an SVR4 quirk? I guess it's too hard to prove the no case, so it stays.

I think we can safely remove it. Doing a mmap() at address zero will not actually work anyway in any half-way modern Linux environment.

And I think the "map zeroes at NULL" wasn't even universal for SVr4. _Some_ binaries may have expected it, but I suspect it was the exception rather than the rule.

So I'd happily take a patch that just removes it. If nothing else, it would be trivial to put back if somebody screams, but I seriously doubt that is going to happen.

Linus

maintainer
#10 lore raw

Re: [PATCH v2] ELF: add and use SUPPRESS_WARN_UNUSED_RESULT

Quoted 6 lines · Linus Torvalds
Quoted 2 lines

Do we really care about userspace which relies upon an SVR4 quirk? I guess it's too hard to prove the no case, so it stays.

I think we can safely remove it. Doing a mmap() at address zero will not actually work anyway in any half-way modern Linux environment.

Quoted 7 lines

And I think the "map zeroes at NULL" wasn't even universal for SVr4. _Some_ binaries may have expected it, but I suspect it was the exception rather than the rule.

So I'd happily take a patch that just removes it. If nothing else, it would be trivial to put back if somebody screams, but I seriously doubt that is going to happen.

Wasn't there some emulator (dosemu? wine?) that relied upon that? Said that, I could be easily wrong - half-asleep right now...

#11 lore raw

Re: [PATCH v2] ELF: add and use SUPPRESS_WARN_UNUSED_RESULT

Quoted 3 lines · Al Viro

Wasn't there some emulator (dosemu? wine?) that relied upon that? Said that, I could be easily wrong - half-asleep right now...

Different issue.

dosemu used to use vm86 mode to do hardware acceleration of 16-bit emulation. And the way vm86 mode works, it all has to be mapped beginning at address 0.

So yes, dosemu used to do a mmap at address 0 too, but it didn't use this ELF mmap MMAP_PAGE_ZERO personality thing to do it, it just did its own (it also wants a lot more than one page).

Nobody uses vm86 mode any more, because hardware acceleration of 16-bit code just isn't relevant any more. Any 16-bit code you have doesn't need special hw modes to run sufficiently fast.

Plus x86-64 doesn't support vm86 mode at all (well, technically you can do it in a VM that runs a 32-bit OS if you really really want to, but see above as to why you don't really need it).

There are other things that have mmap'ed things at zero. I think all PA-RISC HP-UX binaries used to do the same thing iBCS2 did, and the compiler would actually hoist loads to before the NULL pointer test, so it was pretty much "architectural". Afaik, that's the same reason why iBCS2 did that zero mapping too, but PA-RISC just required it a lot more.

It's a horrible thing to do, and only makes debugging harder (because you won't actually get a SIGSEGV on a NULL pointer load, you'll just silently get a zero value and your buggy program will continue running).

Linus

#12 lore raw

Re: [PATCH v2] ELF: add and use SUPPRESS_WARN_UNUSED_RESULT

In reply to Andrew Morton
Quoted 16 lines · Andrew Morton
Quoted 13 lines · On Sat, 26 Jun 2021 00:13:12 +0300 Alexey Dobriyan <[email protected]> wrote:

Last write to the "error" variable in load_elf_binary() is dead write.

Add and use SUPPRESS_WARN_UNUSED_RESULT macro to express intent better.

Credit goes to Ed Catmur:

	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34

Macro doesn't work for WUR functions returning structures and unions, but it will work when gcc copies clang.

...

Quoted 8 lines

include/linux/compiler_attributes.h line 290

Quoted 5 lines

#define __must_check __attribute__((__warn_unused_result__))

include/linux/compiler_attributes.h +4 -0
+/*
+ * "(void)" is enough for clang but not for gcc.
+ */
+#define SUPPRESS_WARN_UNUSED_RESULT	(void)!

That macro is rather ugly. Hopefully we won't really need it - how many such sites are there in a full kernel build anyway?

I don't know really. And they're hard to find because "(void)" doesn't work and not everything is marked as WUR.

copy_from_user/copy_to_user are WUR but get_user/put_user aren't. Logically they should bed. And if put_user() is WUR then I know another place inside fork:

	        if (clone_flags & CLONE_PARENT_SETTID)
	                put_user(nr, args->parent_tid);