this post was submitted on 11 Oct 2023
8 points (78.6% liked)

Programming

17313 readers
148 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

I'm trying to build iwlwifi module manually and for my needs.

https://git.kernel.org/pub/scm/linux/kernel/git/iwlwifi/iwlwifi-fixes.git/tree/net/wireless/

When I run Makefile as make, I get:

subcmd-util.h: In function ‘xrealloc’:
subcmd-util.h:58:31: error: pointer ‘ptr’ may be used after ‘realloc’ [-Werror=use-after-free]
   58 |                         ret = realloc(ptr, 1);
      |                               ^~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to ‘realloc’ here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
subcmd-util.h:56:23: error: pointer ‘ptr’ may be used after ‘realloc’ [-Werror=use-after-free]
   56 |                 ret = realloc(ptr, size);
      |                       ^~~~~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to ‘realloc’ here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[4]: *** [/data/iwlwifi-fixes/tools/build/Makefile.build:97: /data/iwlwifi-fixes/tools/objtool/help.o] Error 1
make[3]: *** [Makefile:59: /data/iwlwifi-fixes/tools/objtool/libsubcmd-in.o] Error 2
make[2]: *** [Makefile:63: /data/iwlwifi-fixes/tools/objtool/libsubcmd.a] Error 2
make[1]: *** [Makefile:69: objtool] Error 2
make: *** [Makefile:1349: tools/objtool] Error 2

Why is it? How to fix it?

top 14 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 1 year ago (1 children)

This would normally be a compiler warning, but someone has enabled the -Werror compiler option (probably in the makefile) which causes the compiler to treat all warnings as errors. You can just remove any -Werror flags from the makefile and it should compile properly.

[–] nothingness 0 points 1 year ago
[–] cbarrick 3 points 1 year ago* (last edited 1 year ago) (3 children)

Why is it?

The code hitting that error is here:

https://git.kernel.org/pub/scm/linux/kernel/git/iwlwifi/iwlwifi-fixes.git/tree/tools/lib/subcmd/subcmd-util.h

It looks fine to me.

What you are seeing is a warning that your compiler may have found a use-after-free bug, but I think this is a false positive. Your build is configured to turn this warning into a hard error.

How to fix it?

I think it will be difficult to know how to fix this without knowing more about your build setup. Are you passing any custom CFLAGS? What compiler and version are you using?

Also, here is someone asking about the same issue (in the same code) on Stack Exchange using GGC 12.1:

https://unix.stackexchange.com/questions/709671/linux-kernel-5-15-54-compilation-errors-with-gcc-12-1

This was the top result when Googling linux "-Werror=use-after-free".

I believe you can disable this warning in this file by adding a pragma after the includes (line 8):

#pragma GCC diagnostic ignored "-Wuse-after-free"

See https://stackoverflow.com/questions/925179/selectively-remove-a-warning-message-using-gcc

Edit: If you don't want to change the code, try disabling the use-after-free warning from the make call:

make CFLAGS="-Wno-use-after-free"
[–] [email protected] 1 points 1 year ago* (last edited 1 year ago) (1 children)

No objections to your answer to the OP's question, but as a curiosity, I'm trying to figure out what the original xrealloc() function is trying to do.

So far as I can tell, it tries a normal realloc() with the requested size, but if that fails, tries again with size=1. But strangely, it that fails, tries using the requested size a second time. And if that still fails, tries once more with size=1.

The POSIX man page isn't giving me any hints as to why size=1 might be special, or if this is some sort of Linux-specific behavior or workaround. I wondered if you might have some insight why this function is the way it is.

Note: I'm on mobile, so haven't checked the Git Blame history yet.

[–] cbarrick 1 points 1 year ago* (last edited 1 year ago) (1 children)

So realloc(ptr, 1) only happens when !ret && !size i.e. the call failed and size == 0.

Presumably this is to support a size of zero even when the underlying realloc does not.

The code is duplicated to try the realloc twice before failing.

I'm not sure what the use case of zero size is though.

[–] [email protected] 1 points 1 year ago

Thanks for the explanation! I figured there was something odd, in combination with me reading the code too quickly.

[–] nothingness 1 points 1 year ago* (last edited 1 year ago) (1 children)
  1. make CFLAGS="-Wno-use-after-free" ---> didn't work, same errors

  2. there's no "use-after-free" flag in any Makefile of the repo, no string "use-after-free" either, only the comments

  3. (line 8): #pragma GCC diagnostic ignored "-Wuse-after-free" ---> line 8 of what file? Makefile?

Any idea?

[–] cbarrick 1 points 1 year ago* (last edited 1 year ago)
  1. Bummer that the CFLAGS trick didn't work.

  2. use-after-free is a default warning. The makefile has -Werror to turn warnings into errors.

  3. The C file that I linked to (subcmd-util.h)

[–] nothingness 0 points 1 year ago (1 children)

I think it will be difficult to know how to fix this without knowing more about your build setup. Are you passing any custom CFLAGS? What compiler and version are you using?

No.

gcc --version
gcc (GCC) 13.2.1 20230801

The goal - simply compile it for now.

[–] cbarrick 1 points 1 year ago (1 children)

If you don't want to change the code, try disabling the use-after-free warning from the make call:

make CFLAGS="-Wno-use-after-free"
[–] nothingness 1 points 1 year ago

didn't help -- same errors

[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

don't listen to people and silence the warning. just add "ptr = NULL" after the realloc() and make sure "ret" is used after that and not ptr. realloc() can return values != ptr and the compiler warns you.

Also you can report this as it's a bug that slipped through testing.

[–] AlmightySnoo 1 points 1 year ago* (last edited 1 year ago)

~~What revision are you using? Because I don't see the ret = realloc(ptr, 1); line in https://github.com/torvalds/linux/blob/master/tools/lib/subcmd/subcmd-util.h .~~ (EDIT: my bad, it's in the subcmd-util.h file linked in the other comment)

Also, make sure you have the latest version of GCC, because I remember it yielding many false-positives when I was implementing some reference counting (which obviously is prone to false positives in static analysis) and those warnings disappeared after updating the compiler.

EDIT: from the looks of it GCC's "static analysis" basically assumed the worst case (ie that the first realloc is freeing memory and the second one is allocating new memory elsewhere and is copying from the old pointer, thus accessing it and hence the use-after-free warning). You can probably just remove the warning as the others have suggested, or you could try to downgrade GCC to version 11 as many of the search results regarding those use-after-free warnings involve later versions.

[–] fireflash38 1 points 1 year ago

Try building a tagged release, rather than the tip of the branch.