Skip to contentSkip to conversation

sparc: mdesc: Fix compile error seen with gcc 11.x

PATCH In review Guenter Roeck 3 participants 3 messages Sep 14, 2021 – Sep 15, 2021 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sparc: mdesc: Fix compile error seen with gcc 11.x

sparc64 images fail to compile with gcc 11.x, reporting the following errors.

arch/sparc/kernel/mdesc.c:647:22: error:
'strcmp' reading 1 or more bytes from a region of size 0
arch/sparc/kernel/mdesc.c:692:22: error:
'strcmp' reading 1 or more bytes from a region of size 0
arch/sparc/kernel/mdesc.c:719:21:
error: 'strcmp' reading 1 or more bytes from a region of size 0

The underlying problem is that node_block() returns a pointer beyond the end of struct mdesc_hdr. gcc 11.x detects that and reports the error. Adding an additional zero-length field to struct mdesc_hdr and pointing to that field fixes the problem.

Cc Arnd Bergmann <[email protected]>
Cc David Laight <[email protected]>
Cc Anatoly Pugachev <[email protected]>
Cc Sam Ravnborg <[email protected]>

---
v2: data[0] => data[]

I am not sure if there was agreement to accept this patch or not, but I was asked to resend it with the above change, so here it is. An open question was if it is acceptable to have a structure named xxx_hdr include an element pointing to the data following that header.

If this patch is not acceptable, the patch in buildbot may be a possible alternative to consider.

    https://git.busybox.net/buildroot/commit/?id=6e1106b4a9aee25d1556310d5cd1cb6dde2e6e3f
 arch/sparc/kernel/mdesc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sparc/kernel/mdesc.c +2 -1
index 8e645ddac58e..83e1f699bc32 100644
@@ -39,6 +39,7 @@ struct mdesc_hdr {
 	u32	node_sz; /* node block size */
 	u32	name_sz; /* name block size */
 	u32	data_sz; /* data block size */
+	char	data[];
 } __attribute__((aligned(16)));
 
 struct mdesc_elem {
@@ -612,7 +613,7 @@ EXPORT_SYMBOL(mdesc_get_node_info);
 
 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
 {
-	return (struct mdesc_elem *) (mdesc + 1);
+	return (struct mdesc_elem *) (mdesc->data);
 }
 
 static void *name_block(struct mdesc_hdr *mdesc)
Signature · 1 line
2.33.0
#1 lore raw

From: Guenter Roeck

Quoted 1 line

Sent: 14 September 2021 23:47

...

Quoted 4 lines

I am not sure if there was agreement to accept this patch or not, but I was asked to resend it with the above change, so here it is. An open question was if it is acceptable to have a structure named xxx_hdr include an element pointing to the data following that header.

It may be a pragmatic solution to the problem.
But it isn't 'correct'.
OTOH I think gcc is broken.
It ought to at least give a sane method of getting the warning ignored in specific cases.

Quoted 20 lines

If this patch is not acceptable, the patch in buildbot may be a possible alternative to consider.

    https://git.busybox.net/buildroot/commit/?id=6e1106b4a9aee25d1556310d5cd1cb6dde2e6e3f
 arch/sparc/kernel/mdesc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sparc/kernel/mdesc.c +1 -0
index 8e645ddac58e..83e1f699bc32 100644
@@ -39,6 +39,7 @@ struct mdesc_hdr {
 	u32	node_sz; /* node block size */
 	u32	name_sz; /* name block size */
 	u32	data_sz; /* data block size */
+	char	data[];
 } __attribute__((aligned(16)));
 
 struct mdesc_elem {

arch/sparc/kernel/mdesc.c EXPORT_SYMBOL(mdesc_get_node_info); · line 617

Quoted 6 lines
arch/sparc/kernel/mdesc.c +1 -1
@@ -612,7 +613,7 @@ EXPORT_SYMBOL(mdesc_get_node_info);
 
 static struct mdesc_elem *node_block(struct mdesc_hdr *mdesc)
 {
-	return (struct mdesc_elem *) (mdesc + 1);
+	return (struct mdesc_elem *) (mdesc->data);
 }

In order for gcc to consider (mdesc + 1) to have size 0 I think it must have tracked the pointer from a structure that has another field (or structure end) following 'mdesc'. If that is the case then it should also know that the data[] must also be size 0.
So the warning may reappear with the next gcc version.

The busybox patch has:
+@@ -75,6 +75,7 @@ struct mdesc_handle {
+ refcount_t refcnt;
+ unsigned int handle_size;
+ struct mdesc_hdr mdesc;
++ char data[];
+ };

Which really ought to be more than enough. Although the extra space could be considered to even be outside that structure.
But the gcc folks suggested a completely brain-dead change that requires taking the offset from the outer structure.
-- return (struct mdesc_elem *) (mdesc + 1);
++ return (struct mdesc_elem *) hp + offsetof(struct mdesc_handle, data);
which is probably missing a (char *) cast.

I wonder if it might be better to 'launder' the pointer so that gcc can't track its size.
It may be that:

	return (struct mdesc_elem *)(ulong)(mdesc + 1);

is enough.
Otherwise it will need to be passed into an asm block.

But gcc is getting stupid for system programming.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

Reviewed-by on v2 1/1
#2 lore raw
In reply to Guenter Roeck
Quoted 21 lines · Guenter Roeck

sparc64 images fail to compile with gcc 11.x, reporting the following errors.

arch/sparc/kernel/mdesc.c:647:22: error:
'strcmp' reading 1 or more bytes from a region of size 0
arch/sparc/kernel/mdesc.c:692:22: error:
'strcmp' reading 1 or more bytes from a region of size 0
arch/sparc/kernel/mdesc.c:719:21:
error: 'strcmp' reading 1 or more bytes from a region of size 0

The underlying problem is that node_block() returns a pointer beyond the end of struct mdesc_hdr. gcc 11.x detects that and reports the error. Adding an additional zero-length field to struct mdesc_hdr and pointing to that field fixes the problem.

Cc Arnd Bergmann <[email protected]>
Cc David Laight <[email protected]>
Cc Anatoly Pugachev <[email protected]>
Cc Sam Ravnborg <[email protected]>
Quoted 8 lines

---
v2: data[0] => data[]

I am not sure if there was agreement to accept this patch or not, but I was asked to resend it with the above change, so here it is. An open question was if it is acceptable to have a structure named xxx_hdr include an element pointing to the data following that header.