Skip to contentSkip to conversation

kbuild: pahole-version: avoid errors if executing fails

PATCH In review Miguel Ojeda 4 participants 7 messages Jul 28, 2024 – Sep 2, 2024

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

PATCH 1/1 kbuild: pahole-version: avoid errors if executing fails

Like patch "rust: suppress error messages from CONFIG_{RUSTC,BINDGEN}_VERSION_TEXT" [1], do not assume the file existing and being executable implies executing it will succeed. Instead, bail out if executing it fails for any reason.

For instance, pahole may be built for another architecture, may be a program we do not expect or may be completely broken:

    $ echo 'bad' > bad-pahole
    $ chmod u+x bad-pahole
    $ make PAHOLE=./bad-pahole defconfig
    ...
    ./bad-pahole: 1: bad: not found
    init/Kconfig:112: syntax error
    init/Kconfig:112: invalid statement
---
 scripts/pahole-version.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/pahole-version.sh +3 -3
index f8a32ab93ad1..a35b557f1901 100755
@@ -5,9 +5,9 @@
 #
 # Prints pahole's version in a 3-digit form, such as 119 for v1.19.
 
-if [ ! -x "$(command -v "$@")" ]; then
+if output=$("$@" --version 2>/dev/null); then
+	echo "$output" | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'
+else
 	echo 0
 	exit 1
 fi
-
-"$@" --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'

base-commit: 256abd8e550ce977b728be79a74e1729438b4948

Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 40 lines · Miguel Ojeda

Like patch "rust: suppress error messages from CONFIG_{RUSTC,BINDGEN}_VERSION_TEXT" [1], do not assume the file existing and being executable implies executing it will succeed. Instead, bail out if executing it fails for any reason.

For instance, pahole may be built for another architecture, may be a program we do not expect or may be completely broken:

    $ echo 'bad' > bad-pahole
    $ chmod u+x bad-pahole
    $ make PAHOLE=./bad-pahole defconfig
    ...
    ./bad-pahole: 1: bad: not found
    init/Kconfig:112: syntax error
    init/Kconfig:112: invalid statement
---
 scripts/pahole-version.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/pahole-version.sh +3 -3
index f8a32ab93ad1..a35b557f1901 100755
@@ -5,9 +5,9 @@
 #
 # Prints pahole's version in a 3-digit form, such as 119 for v1.19.
 
-if [ ! -x "$(command -v "$@")" ]; then
+if output=$("$@" --version 2>/dev/null); then
+	echo "$output" | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'
+else
 	echo 0
 	exit 1
 fi
-
-"$@" --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'
Quoted 4 lines

base-commit: 256abd8e550ce977b728be79a74e1729438b4948

Signature · 1 line
2.45.2

thanks, looks good to me.

#2 lore raw
In reply to Miguel Ojeda
Quoted 9 lines · Miguel Ojeda

Like patch "rust: suppress error messages from CONFIG_{RUSTC,BINDGEN}_VERSION_TEXT" [1], do not assume the file existing and being executable implies executing it will succeed. Instead, bail out if executing it fails for any reason.

For instance, pahole may be built for another architecture, may be a program we do not expect or may be completely broken:

Quoted 7 lines
    $ echo 'bad' > bad-pahole
    $ chmod u+x bad-pahole
    $ make PAHOLE=./bad-pahole defconfig
    ...
    ./bad-pahole: 1: bad: not found
    init/Kconfig:112: syntax error
    init/Kconfig:112: invalid statement

Even with this patch applied, a syntax error can happen.

$ git log --oneline -1
dd1c54d77f11 kbuild: pahole-version: avoid errors if executing fails
$ echo 'echo' > bad-pahole
$ chmod u+x bad-pahole
$ make PAHOLE=./bad-pahole defconfig
*** Default configuration is based on 'x86_64_defconfig'
init/Kconfig:114: syntax error
init/Kconfig:114: invalid statement
make[2]: *** [scripts/kconfig/Makefile:95: defconfig] Error 1
make[1]: *** [/home/masahiro/workspace/linux-kbuild/Makefile:680:
defconfig] Error 2
make: *** [Makefile:224: __sub-make] Error 2
Quoted 24 lines
---
 scripts/pahole-version.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/pahole-version.sh +3 -3
index f8a32ab93ad1..a35b557f1901 100755
@@ -5,9 +5,9 @@
 #
 # Prints pahole's version in a 3-digit form, such as 119 for v1.19.
 
-if [ ! -x "$(command -v "$@")" ]; then
+if output=$("$@" --version 2>/dev/null); then
+       echo "$output" | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'
+else
        echo 0
        exit 1
 fi
-
-"$@" --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/'
Quoted 4 lines

base-commit: 256abd8e550ce977b728be79a74e1729438b4948 --
2.45.2

--
Best Regards
Masahiro Yamada

#3 lore raw
Quoted 39 lines · Masahiro Yamada

Date: Fri, 23 Aug 2024 02:28:28 +0900
From: Masahiro Yamada <[email protected]>
To: Miguel Ojeda <[email protected]>
Cc: Alexei Starovoitov <[email protected]>, Daniel Borkmann
<[email protected]>, Andrii Nakryiko <[email protected]>, Martin KaFai Lau <[email protected]>, Eduard Zingerman <[email protected]>, Song Liu <[email protected]>, Yonghong Song <[email protected]>, John Fastabend <[email protected]>, KP Singh <[email protected]>, Stanislav Fomichev <[email protected]>, Hao Luo <[email protected]>, Jiri Olsa <[email protected]>, [email protected], Nathan Chancellor <[email protected]>, Nicolas Schier <[email protected]>, [email protected], [email protected], [email protected]
Subject: Re: [PATCH] kbuild: pahole-version: avoid errors if executing fails
X-Mailing-List: [email protected]
Message-ID: <CAK7LNARhR=GGZ2Vr-SSog1yjnjh6iT7cCEe4mpYg889GhJnO9g@mail.gmail.com>

Quoted 9 lines · Miguel Ojeda

Like patch "rust: suppress error messages from CONFIG_{RUSTC,BINDGEN}_VERSION_TEXT" [1], do not assume the file existing and being executable implies executing it will succeed. Instead, bail out if executing it fails for any reason.

For instance, pahole may be built for another architecture, may be a program we do not expect or may be completely broken:

Quoted 7 lines
    $ echo 'bad' > bad-pahole
    $ chmod u+x bad-pahole
    $ make PAHOLE=./bad-pahole defconfig
    ...
    ./bad-pahole: 1: bad: not found
    init/Kconfig:112: syntax error
    init/Kconfig:112: invalid statement

Even with this patch applied, a syntax error can happen.

Quoted 13 lines
$ git log --oneline -1
dd1c54d77f11 kbuild: pahole-version: avoid errors if executing fails
$ echo 'echo' > bad-pahole
$ chmod u+x bad-pahole
$ make PAHOLE=./bad-pahole defconfig
*** Default configuration is based on 'x86_64_defconfig'
init/Kconfig:114: syntax error
init/Kconfig:114: invalid statement
make[2]: *** [scripts/kconfig/Makefile:95: defconfig] Error 1
make[1]: *** [/home/masahiro/workspace/linux-kbuild/Makefile:680:
defconfig] Error 2
make: *** [Makefile:224: __sub-make] Error 2

Do we have to catch all possibilities? Then, what about this:

#!/bin/sh
trap "echo 0; exit 1" EXIT
set -e

output=$("$@" --version 2>/dev/null)

output=$(echo "${output}" | sed -nE 's/^v([0-9]+)\.([0-9][0-9])$/\1\2/p')
if [ -z "${output}" ]; then
echo "warning: pahole binary '$1' outputs incompatible version number, pahole will not be used." >&2 exit 1
fi

echo "${output}"
trap EXIT

Kind regards,
Nicolas

#4 lore raw
Quoted 2 lines · Nicolas Schier

Do we have to catch all possibilities? Then, what about this:

Something like that sounds good to me too -- we do something similar in rust_is_available.sh. We also have a 1 in the beginning of (most of) the sed commands there to check only the first line.

I guess it depends on whether Masahiro thinks the extra checks/complexity is worth it. Here I was aiming to catch the case he reported, i.e. non-successful programs.

Cheers,
Miguel

#5 lore raw
Quoted 5 lines · Miguel Ojeda
Quoted 2 lines · Nicolas Schier

Do we have to catch all possibilities? Then, what about this:

Quoted 7 lines

Something like that sounds good to me too -- we do something similar in rust_is_available.sh. We also have a 1 in the beginning of (most of) the sed commands there to check only the first line.

I guess it depends on whether Masahiro thinks the extra checks/complexity is worth it. Here I was aiming to catch the case he reported, i.e. non-successful programs.

My previous report was slightly different.

CONFIG_{RUSTC,BINDGEN}_VERSION_TEXT are string type symbols.
The shell command is allowed to return any string, including an empty string, as long as the value is enclosed by double quotes.

In this case, CONFIG_PAHOLE_VERSION is an int type symbol, hence the shell command must not return an empty string.

Ensuring this should be easy.
Why don't we fix it properly while we are here?

Quoted 4 lines

Cheers,
Miguel

--
Best Regards
Masahiro Yamada