Skip to contentSkip to conversation

arm64: Fix strrchr() matching of null terminator

PATCH In review Edgar E. Iglesias 4 participants 8 messages May 18 – May 19

lore
Share & tools
lore
#0 lore raw

cover arm64: Fix strrchr() matching of null terminator

Found a problem with strrchr("", '\0') returning NULL when searching for the terminating null byte. This fixes it.

Found while GDB-fuzzing xen/lib helpers under QEMU.

Cheers,
Edgar

Edgar E. Iglesias (1):
arm64: Fix strrchr() matching of null terminator

 xen/arch/arm/arm64/lib/strrchr.S | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 arm64: Fix strrchr() matching of null terminator

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

The ARM64 assembly version stopped as soon as it loaded the terminator and returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")
---
 xen/arch/arm/arm64/lib/strrchr.S | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/lib/strrchr.S +3 -4
index 81033c0822..31f304b183 100644
@@ -30,11 +30,10 @@ FUNC(strrchr)
 	mov	x3, #0
 	and	w1, w1, #0xff
 1:	ldrb	w2, [x0], #1
-	cbz	w2, 2f
 	cmp	w2, w1
-	b.ne	1b
+	b.ne	2f
 	sub	x3, x0, #1
-	b	1b
-2:	mov	x0, x3
+2:	cbnz	w2, 1b
+	mov	x0, x3
 	ret
 END(strrchr)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#2 lore raw
Quoted 6 lines · Edgar E. Iglesias

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

The ARM64 assembly version stopped as soon as it loaded the terminator and returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

I wonder though: Why would one pass '\0' to strrchr()? If you want to find the end of a string, more efficient (at least in the general case) options exist (strchr(), memchr(), strlen()).

Quoted 3 lines

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Nit: "..., so a pointer to the terminator ...".

Quoted 2 lines
Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")

However, the function having come from Linux, imo the patch wants to go to Linux (ideally first, but at the very least also).

Additionally, looking at strchr() - couldn't the code here be written in a similar way, allowing to get away with just a single branch? (Arm32's pair of functions is also pretty similar in this regard.)

Jan

Quoted 17 lines
xen/arch/arm/arm64/lib/strrchr.S +3 -4
@@ -30,11 +30,10 @@ FUNC(strrchr)
 	mov	x3, #0
 	and	w1, w1, #0xff
 1:	ldrb	w2, [x0], #1
-	cbz	w2, 2f
 	cmp	w2, w1
-	b.ne	1b
+	b.ne	2f
 	sub	x3, x0, #1
-	b	1b
-2:	mov	x0, x3
+2:	cbnz	w2, 1b
+	mov	x0, x3
 	ret
 END(strrchr)
#3 lore raw

Hi Edgar and Jan,

Quoted 10 lines · Jan Beulich
Quoted 5 lines · Edgar E. Iglesias

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

Quoted 1 line
Quoted 1 line

The ARM64 assembly version stopped as soon as it loaded the terminator and

returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

I wonder though: Why would one pass '\0' to strrchr()? If you want to find the end of a string, more efficient (at least in the general case) options exist (strchr(), memchr(), strlen()).

+1 I am interested to know the use-case for this change. Is this for compliance or real issue? If the latter, can we add some details.

It might also be worth to write a selftest to avoid any regression (in particular if we decide to diverge from Linux).

Quoted 7 lines
Quoted 4 lines

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Nit: "..., so a pointer to the terminator ...".

Quoted 7 lines
Quoted 2 lines
Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")

However, the function having come from Linux, imo the patch wants to go to Linux (ideally first, but at the very least also).

We are trying to keep the core implementation in lib the same as linux (see arch/arm/README.LinuxPrimitives). I would prefer if this is also first committed to Linux and then backported.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 31 lines · Julien Grall

Hi Edgar and Jan,

Quoted 4 lines · Jan Beulich
Quoted 3 lines · Edgar E. Iglesias

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

Quoted 1 line
Quoted 1 line

The ARM64 assembly version stopped as soon as it loaded the terminator

and

Quoted 6 lines
Quoted 2 lines

returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

I wonder though: Why would one pass '\0' to strrchr()? If you want to find the end of a string, more efficient (at least in the general case) options exist (strchr(), memchr(), strlen()).

+1 I am interested to know the use-case for this change. Is this for compliance or real issue? If the latter, can we add some details.

It might also be worth to write a selftest to avoid any regression (in particular if we decide to diverge from Linux).

Quoted 10 lines
Quoted 4 lines

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Nit: "..., so a pointer to the terminator ...".

Quoted 2 lines
Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")
Quoted 9 lines
Quoted 4 lines

However, the function having come from Linux, imo the patch wants to go to Linux (ideally first, but at the very least also).

We are trying to keep the core implementation in lib the same as linux (see arch/arm/README.LinuxPrimitives). I would prefer if this is also first committed to Linux and then backported.

Sounds good, thanks!

#5 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 6 lines · Edgar E. Iglesias

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

The ARM64 assembly version stopped as soon as it loaded the terminator and returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

I wonder though: Why would one pass '\0' to strrchr()? If you want to find the end of a string, more efficient (at least in the general case) options exist (strchr(), memchr(), strlen()).

Right, this came from a fuzzer checking standard strrchr() behavior, not from an explicit end-of-string use case.

I agree strlen() or strchr(s, '\0') would be better for that, but strrchr(s, '\0') is valid input, so we should handle it to avoid surprises for existing or future callers.

Quoted 6 lines
Quoted 4 lines

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Nit: "..., so a pointer to the terminator ...".

Updated, thanks.

Quoted 8 lines
Quoted 3 lines
Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")

However, the function having come from Linux, imo the patch wants to go to Linux (ideally first, but at the very least also).

Yes, sounds good.

Quoted 4 lines

Additionally, looking at strchr() - couldn't the code here be written in a similar way, allowing to get away with just a single branch? (Arm32's pair of functions is also pretty similar in this regard.)

Hm, good point. I had a closer look and came up with a couple of options that look better. This one is not quite the same shape as strchr(), but it eliminates one branch and shortens the inner loop from 5 to 4 insns...

FUNC(strrchr)

	mov	x3, #0		/* Last match, or NULL */
	sub	x0, x0, #1	/* For the pre-indexed load */
	and	w1, w1, #0xff
1:	ldrb	w2, [x0, #1]!	/* Load next byte */
	cmp	w2, w1
	csel	x3, x0, x3, eq	/* Remember match */
	cbnz	w2, 1b		/* Keep going until NUL */
	mov	x0, x3
	ret

END(strrchr)

Cheers,
Edgar

Quoted 21 lines

Jan

Quoted 17 lines
xen/arch/arm/arm64/lib/strrchr.S +3 -4
@@ -30,11 +30,10 @@ FUNC(strrchr)
 	mov	x3, #0
 	and	w1, w1, #0xff
 1:	ldrb	w2, [x0], #1
-	cbz	w2, 2f
 	cmp	w2, w1
-	b.ne	1b
+	b.ne	2f
 	sub	x3, x0, #1
-	b	1b
-2:	mov	x0, x3
+2:	cbnz	w2, 1b
+	mov	x0, x3
 	ret
 END(strrchr)
#6 lore raw
In reply to Edgar E. Iglesias
Quoted 11 lines · Edgar E. Iglesias

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

The ARM64 assembly version stopped as soon as it loaded the terminator and returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Quoted 5 lines
Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")
---
 xen/arch/arm/arm64/lib/strrchr.S | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

A couple of tangential things.

This file was inherited from Linux.  Does the same bug need fixing there?  What about the arm32 side?

Looking at your example, it surely wasn't actually as simple as strrchr("", '\0') ?  I'd expect the optimiser to be able to turn that into a constant and not call out to the library implementation.

Elsewhere, I've created xen/common/bitops.c to be CONFIG_SELF_TESTS for the bit operations including the arch-optimised variations, because they're subtle and easy to get wrong.  This looks like it's worth doing the same for the bits of libc we implement.

~Andrew

#7 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 11 lines · Edgar E. Iglesias

The generic Xen strrchr() implementation returns a pointer to the string terminator when searching for '\0', matching the standard C semantics.

The ARM64 assembly version stopped as soon as it loaded the terminator and returned the previous match pointer instead. This made strrchr("", '\0') return NULL.

Compare the loaded byte against the requested character before deciding whether to stop at the terminator, so the terminator itself can be returned when it is the requested character.

Quoted 5 lines
Fixes 42c4eb6a83 ("xen: arm64: assembly optimised mem* and str*")
---
 xen/arch/arm/arm64/lib/strrchr.S | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
Quoted 4 lines

A couple of tangential things.

This file was inherited from Linux.  Does the same bug need fixing there?  What about the arm32 side?

Yes, I'll have a look at Linux. Arm32 looks fine to me.

Quoted 5 lines

Looking at your example, it surely wasn't actually as simple as strrchr("", '\0') ?  I'd expect the optimiser to be able to turn that into a constant and not call out to the library implementation.

We ran into this while exploring a new QEMU based test framework, we had it do some fuzzing and it tripped over this.

Quoted 5 lines

Elsewhere, I've created xen/common/bitops.c to be CONFIG_SELF_TESTS for the bit operations including the arch-optimised variations, because they're subtle and easy to get wrong.  This looks like it's worth doing the same for the bits of libc we implement.

Yes, that looks like a good fit, thanks.

Cheers,
Edgar