xen: Fixes to check-endbr.sh
v2 cover xen: Fixes to check-endbr.sh
I committed the two trivial patches, which leaves the main MAWK fix from Anthony, and the fix for the portability issue spotted by Jan.
Andrew Cooper (1):
xen: Fix latent check-endbr.sh bug with 32bit build environments
Anthony PERARD (1):
xen: Fix check-endbr.sh with mawk
xen/tools/check-endbr.sh | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/2 xen: Fix check-endbr.sh with mawk
From: Anthony PERARD <[email protected]>
check-endbr.sh works with gawk, but fails with mawk. The produced $ALL file is smaller as it is missing 0x$vma_lo on every line. With mawk, int(0x2A) just produces 0, instead of the expected value.
The use of hexadecimal-constant in awk is an optional part of the posix spec, and mawk doesn't seems to implemented.
There is a way to convert an hexadecimal to a number be putting it in a string, and awk as I understand is supposed to use strtod() to convert the string to a number when needed. The expression 'int("0x15") + 21' would produce the expected value in mawk but now gawk won't convert the string to a number unless we use the option "--non-decimal-data".
So let's convert the hexadecimal number before using it in the awk script. The shell as no issue with dealing with hexadecimal-constant so we'll simply use the expression "$(( 0x15 ))" to convert the value before using it in awk.
Note: This does introduce a latent portability bug, which fixed in a separate
change to avoid mixing complexity/explanations.
Resolves: xen-project/xen#26
---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Luca Fancellu <[email protected]>
CC: Mathieu Tarral <[email protected]>
CC: Bertrand Marquis <[email protected]>
v2:
* Add comment explaining what's going on.
* Tweak commit message. Add a note about the portibility issue.
--- xen/tools/check-endbr.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
xen/tools/check-endbr.sh +6 -1
@@ -64,6 +64,11 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # split the VMA in half so AWK's numeric addition is only working on 32 bit # numbers, which don't lose precision. # +# 4) MAWK doesn't support plain hex constants (an optional part of the POSIX +# spec), and GAWK and MAWK can't agree on how to work with hex constants in +# a string. Use the shell to convert $vma_lo to decimal before passing to +# AWK. +# eval $(${OBJDUMP} -j .text $1 -h | $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}')
@@ -79,7 +84,7 @@ then else grep -aob -e "$(printf '\363\17\36\372')" -e "$(printf '\363\17\36\373')" \ -e "$(printf '\146\17\37\1')" $TEXT_BIN -fi | $AWK -F':' '{printf "%s%x\n", "'$vma_hi'", int(0x'$vma_lo') + $1}' > $ALL +fi | $AWK -F':' '{printf "%s%x\n", "'$vma_hi'", int('$((0x$vma_lo))') + $1}' > $ALL # Wait for $VALID to become complete wait
Signature · 1 line
2.11.0
PATCH v2 2/2 xen: Fix latent check-endbr.sh bug with 32bit build environments
In reply to Andrew CooperWhile Xen's current VMA means it works, the mawk fix (i.e. using $((0xN)) in the shell) isn't portable in 32bit shells. See the code comment for the fix.
The fix found a second latent bug. Recombining $vma_hi/lo should have used printf "%s%08x" and only worked previously because $vma_lo had bits set in it's top nibble. Combining with the main fix, %08x becomes %07x.
---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Luca Fancellu <[email protected]>
CC: Mathieu Tarral <[email protected]>
CC: Bertrand Marquis <[email protected]>
v2:
* New
--- xen/tools/check-endbr.sh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
xen/tools/check-endbr.sh +21 -4
@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # the lower bits, rounding integers to the nearest 4k. # # Instead, use the fact that Xen's .text is within a 1G aligned region, and -# split the VMA in half so AWK's numeric addition is only working on 32 bit -# numbers, which don't lose precision. +# split the VMA so AWK's numeric addition is only working on <32 bit +# numbers, which don't lose precision. (See point 5) # # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX # spec), and GAWK and MAWK can't agree on how to work with hex constants in # a string. Use the shell to convert $vma_lo to decimal before passing to # AWK. # +# 5) Point 4 isn't fully portable. POSIX only requires that $((0xN)) be +# evaluated as long, which in 32bit shells turns negative if bit 31 of the +# VMA is set. AWK then interprets this negative number as a double before +# adding the offsets from the binary grep. +# +# Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split. +# +# The consequence of this is that for all offsets, $vma_lo + offset needs +# to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined +# with the 9 nibbles of $vma_hi. This is fine; .text is at the start of a +# 1G aligned region, and Xen is far far smaller than 256M, but leave safety +# check nevertheless. +# eval $(${OBJDUMP} -j .text $1 -h | - $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}') + $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), substr($4, 10, 16)}') ${OBJCOPY} -j .text $1 -O binary $TEXT_BIN +bin_sz=$(stat -c '%s' $TEXT_BIN) +[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] && + { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; } + # instruction: hex: oct: # endbr64 f3 0f 1e fa 363 017 036 372 # endbr32 f3 0f 1e fb 363 017 036 373
@@ -84,7 +101,7 @@ then else grep -aob -e "$(printf '\363\17\36\372')" -e "$(printf '\363\17\36\373')" \ -e "$(printf '\146\17\37\1')" $TEXT_BIN -fi | $AWK -F':' '{printf "%s%x\n", "'$vma_hi'", int('$((0x$vma_lo))') + $1}' > $ALL +fi | $AWK -F':' '{printf "%s%07x\n", "'$vma_hi'", int('$((0x$vma_lo))') + $1}' > $ALL # Wait for $VALID to become complete wait
Signature · 1 line
2.11.0
Re: [PATCH v2 2/2] xen: Fix latent check-endbr.sh bug with 32bit build environments
Quoted 10 lines · Andrew Cooper
While Xen's current VMA means it works, the mawk fix (i.e. using $((0xN)) in the shell) isn't portable in 32bit shells. See the code comment for the fix.
The fix found a second latent bug. Recombining $vma_hi/lo should have used printf "%s%08x" and only worked previously because $vma_lo had bits set in it's top nibble. Combining with the main fix, %08x becomes %07x.
Fixes $XXX patch 1Reported-by Jan Beulich <[email protected]>Signed-off-by Andrew Cooper <[email protected]>
with, I guess, ...
Quoted 28 lines
xen/tools/check-endbr.sh +14
-2@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # the lower bits, rounding integers to the nearest 4k. # # Instead, use the fact that Xen's .text is within a 1G aligned region, and -# split the VMA in half so AWK's numeric addition is only working on 32 bit -# numbers, which don't lose precision. +# split the VMA so AWK's numeric addition is only working on <32 bit +# numbers, which don't lose precision. (See point 5) # # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX # spec), and GAWK and MAWK can't agree on how to work with hex constants in # a string. Use the shell to convert $vma_lo to decimal before passing to # AWK. # +# 5) Point 4 isn't fully portable. POSIX only requires that $((0xN)) be +# evaluated as long, which in 32bit shells turns negative if bit 31 of the +# VMA is set. AWK then interprets this negative number as a double before +# adding the offsets from the binary grep. +# +# Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split. +# +# The consequence of this is that for all offsets, $vma_lo + offset needs +# to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined +# with the 9 nibbles of $vma_hi. This is fine; .text is at the start of a +# 1G aligned region, and Xen is far far smaller than 256M, but leave safety +# check nevertheless.
xen/tools/check-endbr.sh ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & · line 92
Quoted 10 lines
xen/tools/check-endbr.sh +5
-1+# eval $(${OBJDUMP} -j .text $1 -h | - $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}') + $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), substr($4, 10, 16)}') ${OBJCOPY} -j .text $1 -O binary $TEXT_BIN +bin_sz=$(stat -c '%s' $TEXT_BIN) +[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] && + { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; }
... s/can/cannot/ ?
Jan
Re: [PATCH v2 2/2] xen: Fix latent check-endbr.sh bug with 32bit build environments
Quoted 4 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper
While Xen's current VMA means it works, the mawk fix (i.e. using $((0xN)) in the shell) isn't portable in 32bit shells. See the code comment for the fix.
Quoted 8 lines
Quoted 7 lines
The fix found a second latent bug. Recombining $vma_hi/lo should have used printf "%s%08x" and only worked previously because $vma_lo had bits set in it's top nibble. Combining with the main fix, %08x becomes %07x.
Fixes $XXX patch 1Reported-by Jan Beulich <[email protected]>Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Thanks, but...
Quoted 35 lines
with, I guess, ...
Quoted 33 lines
xen/tools/check-endbr.sh +16
-3@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # the lower bits, rounding integers to the nearest 4k. # # Instead, use the fact that Xen's .text is within a 1G aligned region, and -# split the VMA in half so AWK's numeric addition is only working on 32 bit -# numbers, which don't lose precision. +# split the VMA so AWK's numeric addition is only working on <32 bit +# numbers, which don't lose precision. (See point 5) # # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX # spec), and GAWK and MAWK can't agree on how to work with hex constants in # a string. Use the shell to convert $vma_lo to decimal before passing to # AWK. # +# 5) Point 4 isn't fully portable. POSIX only requires that $((0xN)) be +# evaluated as long, which in 32bit shells turns negative if bit 31 of the +# VMA is set. AWK then interprets this negative number as a double before +# adding the offsets from the binary grep. +# +# Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split. +# +# The consequence of this is that for all offsets, $vma_lo + offset needs +# to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined +# with the 9 nibbles of $vma_hi. This is fine; .text is at the start of a +# 1G aligned region, and Xen is far far smaller than 256M, but leave safety +# check nevertheless. +# eval $(${OBJDUMP} -j .text $1 -h | - $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}') + $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), substr($4, 10, 16)}')
Quoted 6 lines
xen/tools/check-endbr.sh ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & · line 92
Quoted 5 lines
xen/tools/check-endbr.sh +3
-0${OBJCOPY} -j .text $1 -O binary $TEXT_BIN +bin_sz=$(stat -c '%s' $TEXT_BIN) +[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] && + { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; }... s/can/cannot/ ?
Why? "Can" is correct here. If the offsets can't exceed 256M, then everything is good.
~Andrew
Re: [PATCH v2 2/2] xen: Fix latent check-endbr.sh bug with 32bit build environments
Quoted 37 lines · Andrew Cooper
Quoted 36 lines · Jan Beulich
Quoted 35 lines · Andrew Cooper
xen/tools/check-endbr.sh +16
-3@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # the lower bits, rounding integers to the nearest 4k. # # Instead, use the fact that Xen's .text is within a 1G aligned region, and -# split the VMA in half so AWK's numeric addition is only working on 32 bit -# numbers, which don't lose precision. +# split the VMA so AWK's numeric addition is only working on <32 bit +# numbers, which don't lose precision. (See point 5) # # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX # spec), and GAWK and MAWK can't agree on how to work with hex constants in # a string. Use the shell to convert $vma_lo to decimal before passing to # AWK. # +# 5) Point 4 isn't fully portable. POSIX only requires that $((0xN)) be +# evaluated as long, which in 32bit shells turns negative if bit 31 of the +# VMA is set. AWK then interprets this negative number as a double before +# adding the offsets from the binary grep. +# +# Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split. +# +# The consequence of this is that for all offsets, $vma_lo + offset needs +# to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined +# with the 9 nibbles of $vma_hi. This is fine; .text is at the start of a +# 1G aligned region, and Xen is far far smaller than 256M, but leave safety +# check nevertheless. +# eval $(${OBJDUMP} -j .text $1 -h | - $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}') + $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), substr($4, 10, 16)}') ${OBJCOPY} -j .text $1 -O binary $TEXT_BIN
Quoted 7 lines
Quoted 4 lines
xen/tools/check-endbr.sh ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & · line 92
Quoted 3 lines
xen/tools/check-endbr.sh +3
-0+bin_sz=$(stat -c '%s' $TEXT_BIN) +[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] && + { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; }... s/can/cannot/ ?
Why? "Can" is correct here. If the offsets can't exceed 256M, then everything is good.
Hmm, the wording then indeed is ambiguous. I read "can" as "are allowed to", when we mean "aren't allowed to". Maybe ".text is 256M or more in size"? If you mention "offsets", then I think the check should be based on actually observing an offset which is too large (which .text size alone doesn't guarantee will happen).
Jan
Re: [PATCH v2 2/2] xen: Fix latent check-endbr.sh bug with 32bit build environments
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Andrew Cooper
Quoted 36 lines · Jan Beulich
Quoted 35 lines · Andrew Cooper
xen/tools/check-endbr.sh +16
-3@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # the lower bits, rounding integers to the nearest 4k. # # Instead, use the fact that Xen's .text is within a 1G aligned region, and -# split the VMA in half so AWK's numeric addition is only working on 32 bit -# numbers, which don't lose precision. +# split the VMA so AWK's numeric addition is only working on <32 bit +# numbers, which don't lose precision. (See point 5) # # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX # spec), and GAWK and MAWK can't agree on how to work with hex constants in # a string. Use the shell to convert $vma_lo to decimal before passing to # AWK. # +# 5) Point 4 isn't fully portable. POSIX only requires that $((0xN)) be +# evaluated as long, which in 32bit shells turns negative if bit 31 of the +# VMA is set. AWK then interprets this negative number as a double before +# adding the offsets from the binary grep. +# +# Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split. +# +# The consequence of this is that for all offsets, $vma_lo + offset needs +# to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined +# with the 9 nibbles of $vma_hi. This is fine; .text is at the start of a +# 1G aligned region, and Xen is far far smaller than 256M, but leave safety +# check nevertheless. +# eval $(${OBJDUMP} -j .text $1 -h | - $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}') + $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), substr($4, 10, 16)}') ${OBJCOPY} -j .text $1 -O binary $TEXT_BIN
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
xen/tools/check-endbr.sh ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & · line 92
Quoted 3 lines
xen/tools/check-endbr.sh +3
-0+bin_sz=$(stat -c '%s' $TEXT_BIN) +[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] && + { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; }... s/can/cannot/ ?
Why? "Can" is correct here. If the offsets can't exceed 256M, then everything is good.
Hmm, the wording then indeed is ambiguous.
I see your point. In this case it's meant as "are able to", but this is still clearer than using "can't" because at least the text matches the check which triggered it.
Quoted 5 lines
I read "can" as "are allowed
to", when we mean "aren't allowed to". Maybe ".text is 256M or more in size"? If you mention "offsets", then I think the check should be based on actually observing an offset which is too large (which .text size alone doesn't guarantee will happen).
It's not just .text on its own because the VMA of offset by 2M, hence the subtraction of $vma_lo in the main calculation.
There's no point searching for offsets. There will be one near the end, so all searching for an offset would do is complicate the critical loop.
How about ".text offsets must not exceed 256M" ?
That should be unambiguous.
~Andrew
Re: [PATCH v2 2/2] xen: Fix latent check-endbr.sh bug with 32bit build environments
Quoted 60 lines · Andrew Cooper
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Andrew Cooper
Quoted 36 lines · Jan Beulich
Quoted 35 lines · Andrew Cooper
xen/tools/check-endbr.sh +16
-3@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & # the lower bits, rounding integers to the nearest 4k. # # Instead, use the fact that Xen's .text is within a 1G aligned region, and -# split the VMA in half so AWK's numeric addition is only working on 32 bit -# numbers, which don't lose precision. +# split the VMA so AWK's numeric addition is only working on <32 bit +# numbers, which don't lose precision. (See point 5) # # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX # spec), and GAWK and MAWK can't agree on how to work with hex constants in # a string. Use the shell to convert $vma_lo to decimal before passing to # AWK. # +# 5) Point 4 isn't fully portable. POSIX only requires that $((0xN)) be +# evaluated as long, which in 32bit shells turns negative if bit 31 of the +# VMA is set. AWK then interprets this negative number as a double before +# adding the offsets from the binary grep. +# +# Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split. +# +# The consequence of this is that for all offsets, $vma_lo + offset needs +# to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined +# with the 9 nibbles of $vma_hi. This is fine; .text is at the start of a +# 1G aligned region, and Xen is far far smaller than 256M, but leave safety +# check nevertheless. +# eval $(${OBJDUMP} -j .text $1 -h | - $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), substr($4, 9, 16)}') + $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), substr($4, 10, 16)}') ${OBJCOPY} -j .text $1 -O binary $TEXT_BINQuoted 7 lines
Quoted 6 lines
Quoted 4 lines
xen/tools/check-endbr.sh ${OBJDUMP} -j .text $1 -d -w | grep ' endbr64 *$' | cut -f 1 -d ':' > $VALID & · line 92
Quoted 3 lines
xen/tools/check-endbr.sh +3
-0+bin_sz=$(stat -c '%s' $TEXT_BIN) +[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] && + { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; }... s/can/cannot/ ?
Why? "Can" is correct here. If the offsets can't exceed 256M, then everything is good.
Hmm, the wording then indeed is ambiguous.
I see your point. In this case it's meant as "are able to", but this is still clearer than using "can't" because at least the text matches the check which triggered it.
Quoted 5 lines
I read "can" as "are allowed
to", when we mean "aren't allowed to". Maybe ".text is 256M or more in size"? If you mention "offsets", then I think the check should be based on actually observing an offset which is too large (which .text size alone doesn't guarantee will happen).It's not just .text on its own because the VMA of offset by 2M, hence the subtraction of $vma_lo in the main calculation.
Quoted 6 lines
There's no point searching for offsets. There will be one near the end, so all searching for an offset would do is complicate the critical loop.
How about ".text offsets must not exceed 256M" ?
That should be unambiguous.
Yes, that reads fine. Thanks.
Jan