Skip to contentSkip to conversation

xen: Enable -Wwrite-strings

PATCH Superseded Andrew Cooper 3 participants 25 messages Nov 20, 2023 – Nov 22, 2023 Arm Samsung Exynos

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen: Enable -Wwrite-strings

I got carried away and fixed all remaining -Wwrite-strings issues I could reproduce in local builds.

Pipeline in Gitlab:

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1079379918

Andrew Cooper (6):
x86/setup: Make the loader variable const
x86/setup: Rework cmdline_cook() to be compatible with -Wwrite-strings xen/efi: Make efi-boot.h compile with -Wwrite-strings arm/duart: make dt_uart_init() compile with -Wwrite-strings arm/platforms: Make compatbile with -Wwrite-strings
xen: Enable -Wwrite-strings

 xen/Makefile                     |  2 +-
 xen/arch/arm/efi/efi-boot.h      |  2 +-
 xen/arch/arm/platforms/brcm.c    |  2 +-
 xen/arch/arm/platforms/exynos5.c |  2 +-
 xen/arch/x86/efi/efi-boot.h      |  3 ++-
 xen/arch/x86/setup.c             | 21 +++++++++++----------
 xen/drivers/char/arm-uart.c      | 12 ++++++++----
 7 files changed, 25 insertions(+), 19 deletions(-)

base-commit: fa2da5bce90b3777aa7a323e1cf201c97b56d278

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/6 x86/setup: Make the loader variable const

It is never written to, but has a string literal assigned, and needs to be const to support -Wwrite-strings

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/arch/x86/setup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/setup.c +4 -4
index a3d3f797bb1e..c41dfdb2bdf8 100644
@@ -971,8 +971,8 @@ static struct domain *__init create_dom0(const module_t *image,
 /* SAF-1-safe */
 void __init noreturn __start_xen(unsigned long mbi_p)
 {
-    const char *memmap_type = NULL;
-    char *cmdline, *kextra, *loader;
+    const char *memmap_type = NULL, *loader;
+    char *cmdline, *kextra;
     void *bsp_stack;
     struct cpu_info *info = get_cpu_info(), *bsp_info;
     unsigned int initrdidx, num_parked = 0;
@@ -1023,8 +1023,8 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         mod = __va(mbi->mods_addr);
     }
 
-    loader = (mbi->flags & MBI_LOADERNAME)
-        ? (char *)__va(mbi->boot_loader_name) : "unknown";
+    loader = (mbi->flags & MBI_LOADERNAME) ? __va(mbi->boot_loader_name)
+                                           : "unknown";
 
     /* Parse the command-line options. */
     cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
Signature · 1 line
2.30.2
Reviewed-by on v1 1/6
#3 lore raw

Re: [PATCH 1/6] x86/setup: Make the loader variable const

In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

It is never written to, but has a string literal assigned, and needs to be const to support -Wwrite-strings

Yet maybe "written through" rather than "written to" in the description?

Jan

#4 lore raw

PATCH 2/6 x86/setup: Rework cmdline_cook() to be compatible with -Wwrite-strings

In reply to Andrew Cooper

Constify both cmdline variables in create_dom0() and __start_xen(). Initialise Xen's variable to the empty string to simplify the parsing logic.

Update cmdline_cook() to take and return const pointers, changing it to have an early exit for a NULL input (which can happen if the mbi-> pointers happen to be 0).

Note this only compiles because strstr() launders the const off the pointer when assigning to the mutable kextra, but that logic only mutates the mbi->cmdline buffer.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

I don't particularly like this, but it's the best I can come up with.

---
 xen/arch/x86/setup.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
xen/arch/x86/setup.c +9 -8
index c41dfdb2bdf8..a06a241943f6 100644
@@ -837,9 +837,10 @@ static bool __init loader_is_grub2(const char *loader_name)
     return (p != NULL) && (p[5] != '0');
 }
 
-static char * __init cmdline_cook(char *p, const char *loader_name)
+static const char *__init cmdline_cook(const char *p, const char *loader_name)
 {
-    p = p ? : "";
+    if ( !p )
+        return "";
 
     /* Strip leading whitespace. */
     while ( *p == ' ' )
@@ -885,7 +886,7 @@ static struct domain *__init create_dom0(const module_t *image,
         },
     };
     struct domain *d;
-    char *cmdline;
+    const char *cmdline;
     domid_t domid;
 
     if ( opt_dom0_pvh )
@@ -971,8 +972,8 @@ static struct domain *__init create_dom0(const module_t *image,
 /* SAF-1-safe */
 void __init noreturn __start_xen(unsigned long mbi_p)
 {
-    const char *memmap_type = NULL, *loader;
-    char *cmdline, *kextra;
+    const char *memmap_type = NULL, *loader, *cmdline = "";
+    char *kextra;
     void *bsp_stack;
     struct cpu_info *info = get_cpu_info(), *bsp_info;
     unsigned int initrdidx, num_parked = 0;
@@ -1027,9 +1028,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                                            : "unknown";
 
     /* Parse the command-line options. */
-    cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
-                           __va(mbi->cmdline) : NULL,
-                           loader);
+    if ( mbi->flags & MBI_CMDLINE )
+        cmdline = cmdline_cook(__va(mbi->cmdline), loader);
+
     if ( (kextra = strstr(cmdline, " -- ")) != NULL )
     {
         /*
Signature · 1 line
2.30.2
Reviewed-by on v1 2/6
#5 lore raw

Re: [PATCH 2/6] x86/setup: Rework cmdline_cook() to be compatible with -Wwrite-strings

Quoted 7 lines · Andrew Cooper

Constify both cmdline variables in create_dom0() and __start_xen(). Initialise Xen's variable to the empty string to simplify the parsing logic.

Update cmdline_cook() to take and return const pointers, changing it to have an early exit for a NULL input (which can happen if the mbi-> pointers happen to be 0).

Quoted 5 lines

Note this only compiles because strstr() launders the const off the pointer when assigning to the mutable kextra, but that logic only mutates the mbi->cmdline buffer.

#6 lore raw

Re: [PATCH 2/6] x86/setup: Rework cmdline_cook() to be compatible with -Wwrite-strings

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

Constify both cmdline variables in create_dom0() and __start_xen(). Initialise Xen's variable to the empty string to simplify the parsing logic.

Update cmdline_cook() to take and return const pointers, changing it to have an early exit for a NULL input (which can happen if the mbi-> pointers happen to be 0).

Note this only compiles because strstr() launders the const off the pointer when assigning to the mutable kextra, but that logic only mutates the mbi->cmdline buffer.

And a good static analyzer would spot this. At the very least I think this warrants a comment next to that code. But really I'm inclined to re-write this to eliminate the issue altogether; I'll try to remember to do so once your change has gone in.

xen/arch/x86/setup.c static bool __init loader_is_grub2(const char *loader_name) · line 843

Quoted 12 lines
xen/arch/x86/setup.c +3 -2
@@ -837,9 +837,10 @@ static bool __init loader_is_grub2(const char *loader_name)
     return (p != NULL) && (p[5] != '0');
 }
 
-static char * __init cmdline_cook(char *p, const char *loader_name)
+static const char *__init cmdline_cook(const char *p, const char *loader_name)
 {
-    p = p ? : "";
+    if ( !p )
+        return "";

This change is now needed only for create_dom0(), whereas the call site change to __start_xen() is redundant with the change here. Did you consider doing a similar transformation in create_dom0(), thus eliminating the need for this check altogether? Alternatively I'd like to ask that ...

Quoted 23 lines
xen/arch/x86/setup.c +3 -3
@@ -885,7 +886,7 @@ static struct domain *__init create_dom0(const module_t *image,
         },
     };
     struct domain *d;
-    char *cmdline;
+    const char *cmdline;
     domid_t domid;
 
     if ( opt_dom0_pvh )
@@ -971,8 +972,8 @@ static struct domain *__init create_dom0(const module_t *image,
 /* SAF-1-safe */
 void __init noreturn __start_xen(unsigned long mbi_p)
 {
-    const char *memmap_type = NULL, *loader;
-    char *cmdline, *kextra;
+    const char *memmap_type = NULL, *loader, *cmdline = "";
+    char *kextra;
     void *bsp_stack;
     struct cpu_info *info = get_cpu_info(), *bsp_info;
     unsigned int initrdidx, num_parked = 0;
@@ -1027,9 +1028,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                                            : "unknown";
 

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1036

Quoted 10 lines
xen/arch/x86/setup.c +3 -3
     /* Parse the command-line options. */
-    cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
-                           __va(mbi->cmdline) : NULL,
-                           loader);
+    if ( mbi->flags & MBI_CMDLINE )
+        cmdline = cmdline_cook(__va(mbi->cmdline), loader);
+
     if ( (kextra = strstr(cmdline, " -- ")) != NULL )
     {
         /*

... this last hunk be dropped, along with cmdline's initializer. No need for extra code churn when not gaining us anything. (Also but not only because the reformatting here is actually beneficial from a readability pov imo, the variant with applying the same transformation to create_dom0() would seem preferable to me.)

Jan

#7 lore raw

Re: [PATCH 2/6] x86/setup: Rework cmdline_cook() to be compatible with -Wwrite-strings

Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper

Constify both cmdline variables in create_dom0() and __start_xen(). Initialise Xen's variable to the empty string to simplify the parsing logic.

Update cmdline_cook() to take and return const pointers, changing it to have an early exit for a NULL input (which can happen if the mbi-> pointers happen to be 0).

Quoted 7 lines
Quoted 3 lines

Note this only compiles because strstr() launders the const off the pointer when assigning to the mutable kextra, but that logic only mutates the mbi->cmdline buffer.

And a good static analyzer would spot this. At the very least I think this warrants a comment next to that code. But really I'm inclined to re-write this to eliminate the issue altogether; I'll try to remember to do so once your change has gone in.

This string handling leaves a lot to be desired.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/setup.c +0 -0
@@ -837,9 +837,10 @@ static bool __init loader_is_grub2(const char *loader_name)
     return (p != NULL) && (p[5] != '0');
 }
 
Quoted 11 lines

xen/arch/x86/setup.c static bool __init loader_is_grub2(const char *loader_name) · line 843

Quoted 6 lines
xen/arch/x86/setup.c +3 -2
-static char * __init cmdline_cook(char *p, const char *loader_name)
+static const char *__init cmdline_cook(const char *p, const char *loader_name)
 {
-    p = p ? : "";
+    if ( !p )
+        return "";

This change is now needed only for create_dom0(), whereas the call site change to __start_xen() is redundant with the change here. Did you consider doing a similar transformation in create_dom0(), thus eliminating the need for this check altogether? Alternatively I'd like to ask that ...

It occurs to me that __va(0) != 0, so this path isn't actually taken, even when there is a bad mbi-> pointer.

But the mbi information is already processed by us earlier on boot so we have reasonable expectation that the pointer is good if MBI_CMDLINE is set.

Quoted 24 lines
Quoted 24 lines
xen/arch/x86/setup.c +3 -3
@@ -885,7 +886,7 @@ static struct domain *__init create_dom0(const module_t *image,
         },
     };
     struct domain *d;
-    char *cmdline;
+    const char *cmdline;
     domid_t domid;
 
     if ( opt_dom0_pvh )
@@ -971,8 +972,8 @@ static struct domain *__init create_dom0(const module_t *image,
 /* SAF-1-safe */
 void __init noreturn __start_xen(unsigned long mbi_p)
 {
-    const char *memmap_type = NULL, *loader;
-    char *cmdline, *kextra;
+    const char *memmap_type = NULL, *loader, *cmdline = "";
+    char *kextra;
     void *bsp_stack;
     struct cpu_info *info = get_cpu_info(), *bsp_info;
     unsigned int initrdidx, num_parked = 0;
@@ -1027,9 +1028,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                                            : "unknown";
 
Quoted 15 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1036

Quoted 10 lines
xen/arch/x86/setup.c +3 -3
     /* Parse the command-line options. */
-    cmdline = cmdline_cook((mbi->flags & MBI_CMDLINE) ?
-                           __va(mbi->cmdline) : NULL,
-                           loader);
+    if ( mbi->flags & MBI_CMDLINE )
+        cmdline = cmdline_cook(__va(mbi->cmdline), loader);
+
     if ( (kextra = strstr(cmdline, " -- ")) != NULL )
     {
         /*

... this last hunk be dropped, along with cmdline's initializer. No need for extra code churn when not gaining us anything. (Also but not only because the reformatting here is actually beneficial from a readability pov imo, the variant with applying the same transformation to create_dom0() would seem preferable to me.)

I'll see what I can do.  I definitely do prefer this form, from a clarity point of view.

~Andrew

#8 lore raw

Re: [PATCH 2/6] x86/setup: Rework cmdline_cook() to be compatible with -Wwrite-strings

Quoted 20 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/setup.c +0 -0
@@ -837,9 +837,10 @@ static bool __init loader_is_grub2(const char *loader_name)
     return (p != NULL) && (p[5] != '0');
 }
 
Quoted 11 lines

xen/arch/x86/setup.c static bool __init loader_is_grub2(const char *loader_name) · line 843

Quoted 6 lines
xen/arch/x86/setup.c +3 -2
-static char * __init cmdline_cook(char *p, const char *loader_name)
+static const char *__init cmdline_cook(const char *p, const char *loader_name)
 {
-    p = p ? : "";
+    if ( !p )
+        return "";

This change is now needed only for create_dom0(), whereas the call site change to __start_xen() is redundant with the change here. Did you consider doing a similar transformation in create_dom0(), thus eliminating the need for this check altogether? Alternatively I'd like to ask that ...

Quoted 2 lines

It occurs to me that __va(0) != 0, so this path isn't actually taken, even when there is a bad mbi-> pointer.

But it is taken when the (remaining) caller passes in NULL explicitly (from the conditional operator ahead of the function invocation). But anyway, I'll go look at v2.

Jan

#9 lore raw

PATCH 3/6 xen/efi: Make efi-boot.h compile with -Wwrite-strings

In reply to Andrew Cooper

GCC complains:

In file included from arch/arm/efi/boot.c:700:
arch/arm/efi/efi-boot.h: In function 'efi_arch_handle_cmdline':
arch/arm/efi/efi-boot.h:482:16: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

    482 |         name.s = "xen";
        |                ^

There's no easy option. .rodata is really read-only, so the fact Xen doesn't crash means these strings aren't written to.

Lie to the compiler using a union.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

I *really* don't like this, but it's the only suggestion given.

---
 xen/arch/arm/efi/efi-boot.h | 2 +-
 xen/arch/x86/efi/efi-boot.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
xen/arch/arm/efi/efi-boot.h +1 -1
index 1c3640bb65fd..c26bf18b68b9 100644
@@ -479,7 +479,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
 
     prop_len = 0;
     prop_len += snprintf(buf + prop_len,
xen/arch/x86/efi/efi-boot.h +2 -1
index eebc54180bf7..e2d256e0517b 100644
@@ -324,7 +324,8 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
+
     place_string(&mbi.cmdline, name.s);
 
     if ( mbi.cmdline )
Signature · 1 line
2.30.2
#10 lore raw

Re: [PATCH 3/6] xen/efi: Make efi-boot.h compile with -Wwrite-strings

Quoted 26 lines · Andrew Cooper

GCC complains:

In file included from arch/arm/efi/boot.c:700:
arch/arm/efi/efi-boot.h: In function 'efi_arch_handle_cmdline':
arch/arm/efi/efi-boot.h:482:16: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

    482 |         name.s = "xen";
        |                ^

There's no easy option. .rodata is really read-only, so the fact Xen doesn't crash means these strings aren't written to.

Lie to the compiler using a union.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

Quoted 1 line

I *really* don't like this, but it's the only suggestion given.

Hi Andrew, I am trying to understand what is the part you don't like. I understand that union string looks iffy due to being a union with const and non-const. Is that your concern or you also spotted additional problems with this?

If that is the only issue, maybe we can come up with a matter in-code comment or commit message. (The TODO is not immediately obvious to me what the issue to be improved is.)

Quoted 36 lines
---
 xen/arch/arm/efi/efi-boot.h | 2 +-
 xen/arch/x86/efi/efi-boot.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
xen/arch/arm/efi/efi-boot.h +1 -1
index 1c3640bb65fd..c26bf18b68b9 100644
@@ -479,7 +479,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
 
     prop_len = 0;
     prop_len += snprintf(buf + prop_len,
xen/arch/x86/efi/efi-boot.h +2 -1
index eebc54180bf7..e2d256e0517b 100644
@@ -324,7 +324,8 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
+
     place_string(&mbi.cmdline, name.s);
 
     if ( mbi.cmdline )
Signature · 1 line
2.30.2
#11 lore raw

Re: [PATCH 3/6] xen/efi: Make efi-boot.h compile with -Wwrite-strings

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

GCC complains:

In file included from arch/arm/efi/boot.c:700:
arch/arm/efi/efi-boot.h: In function 'efi_arch_handle_cmdline':
arch/arm/efi/efi-boot.h:482:16: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

    482 |         name.s = "xen";
        |                ^

There's no easy option. .rodata is really read-only, so the fact Xen doesn't crash means these strings aren't written to.

And the consuming sites confirm this being the case. Hence ...

Quoted 1 line

Lie to the compiler using a union.

... to at least slightly limit the lying, how about ...

xen/arch/arm/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 485

Quoted 11 lines
xen/arch/arm/efi/efi-boot.h +1 -1
@@ -479,7 +479,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
 
     prop_len = 0;
     prop_len += snprintf(buf + prop_len,

... you also switch to using name.cs down below here and ...

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 329

Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -324,7 +324,8 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
+
     place_string(&mbi.cmdline, name.s);

... here?

An alternative would be to introduce 'char xen[4] = "xen";' in both cases, and use them instead of plain string literals.

Jan

#12 lore raw

Re: [PATCH 3/6] xen/efi: Make efi-boot.h compile with -Wwrite-strings

Quoted 41 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

GCC complains:

In file included from arch/arm/efi/boot.c:700:
arch/arm/efi/efi-boot.h: In function 'efi_arch_handle_cmdline':
arch/arm/efi/efi-boot.h:482:16: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

    482 |         name.s = "xen";
        |                ^

There's no easy option. .rodata is really read-only, so the fact Xen doesn't crash means these strings aren't written to.

And the consuming sites confirm this being the case. Hence ...

Quoted 1 line

Lie to the compiler using a union.

... to at least slightly limit the lying, how about ...

xen/arch/arm/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 485

Quoted 11 lines
xen/arch/arm/efi/efi-boot.h +1 -1
@@ -479,7 +479,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
 
     prop_len = 0;
     prop_len += snprintf(buf + prop_len,

... you also switch to using name.cs down below here and ...

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 329

Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -324,7 +324,8 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
+
     place_string(&mbi.cmdline, name.s);

... here?

Quoted 2 lines

An alternative would be to introduce 'char xen[4] = "xen";' in both cases, and use them instead of plain string literals.

They'd have to be static, or dynamically allocated or they'll end up out of scope, wont they?

I have to admit I find this logic very hard to follow.

Furthermore, I note:

mbi.boot_loader_name = (long)"EFI";

which is exactly the kind of pointer which is liable to end up being edited via kextra in the other patch.

~Andrew

#13 lore raw

Re: [PATCH 3/6] xen/efi: Make efi-boot.h compile with -Wwrite-strings

Quoted 47 lines · Andrew Cooper
Quoted 41 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

GCC complains:

In file included from arch/arm/efi/boot.c:700:
arch/arm/efi/efi-boot.h: In function 'efi_arch_handle_cmdline':
arch/arm/efi/efi-boot.h:482:16: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

    482 |         name.s = "xen";
        |                ^

There's no easy option. .rodata is really read-only, so the fact Xen doesn't crash means these strings aren't written to.

And the consuming sites confirm this being the case. Hence ...

Quoted 1 line

Lie to the compiler using a union.

... to at least slightly limit the lying, how about ...

xen/arch/arm/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 485

Quoted 11 lines
xen/arch/arm/efi/efi-boot.h +1 -1
@@ -479,7 +479,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
 
     prop_len = 0;
     prop_len += snprintf(buf + prop_len,

... you also switch to using name.cs down below here and ...

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 329

Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -324,7 +324,8 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
+
     place_string(&mbi.cmdline, name.s);

... here?

Quoted 2 lines

An alternative would be to introduce 'char xen[4] = "xen";' in both cases, and use them instead of plain string literals.

They'd have to be static, or dynamically allocated or they'll end up out of scope, wont they?

Quoted 8 lines

I have to admit I find this logic very hard to follow.

Furthermore, I note:

mbi.boot_loader_name = (long)"EFI";

which is exactly the kind of pointer which is liable to end up being edited via kextra in the other patch.

Hang on.  place_string()'ing here is just to prepend a piece of data we go to other lengths to strip and ignore later in boot.

On x86 we can just delete it and make our lives simpler.  I hope the same is true on ARM too.

~Andrew

#14 lore raw

Re: [PATCH 3/6] xen/efi: Make efi-boot.h compile with -Wwrite-strings

Quoted 43 lines · Andrew Cooper
Quoted 42 lines · Andrew Cooper
Quoted 41 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

GCC complains:

In file included from arch/arm/efi/boot.c:700:
arch/arm/efi/efi-boot.h: In function 'efi_arch_handle_cmdline':
arch/arm/efi/efi-boot.h:482:16: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

    482 |         name.s = "xen";
        |                ^

There's no easy option. .rodata is really read-only, so the fact Xen doesn't crash means these strings aren't written to.

And the consuming sites confirm this being the case. Hence ...

Quoted 1 line

Lie to the compiler using a union.

... to at least slightly limit the lying, how about ...

xen/arch/arm/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 485

Quoted 11 lines
xen/arch/arm/efi/efi-boot.h +1 -1
@@ -479,7 +479,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
 
     prop_len = 0;
     prop_len += snprintf(buf + prop_len,

... you also switch to using name.cs down below here and ...

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_handle_cmdline(CHAR16 *image_name, · line 329

Quoted 10 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -324,7 +324,8 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
         w2s(&name);
     }
     else
-        name.s = "xen";
+        name.cs = "xen"; /* TODO, find a better way of doing this. */
+
     place_string(&mbi.cmdline, name.s);

... here?

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

An alternative would be to introduce 'char xen[4] = "xen";' in both cases, and use them instead of plain string literals.

They'd have to be static, or dynamically allocated or they'll end up out of scope, wont they?

No, place_string() copies into the target area. The input string then isn't further used.

Quoted 6 lines
Quoted 6 lines

I have to admit I find this logic very hard to follow.

Furthermore, I note:

mbi.boot_loader_name = (long)"EFI";

Quoted 8 lines
Quoted 2 lines

which is exactly the kind of pointer which is liable to end up being edited via kextra in the other patch.

Hang on.  place_string()'ing here is just to prepend a piece of data we go to other lengths to strip and ignore later in boot.

On x86 we can just delete it and make our lives simpler.  I hope the same is true on ARM too.

Well, yes, the prepending is just to allow uniform handling later on. Surely this can be done differently. Again - I'll go look at v2.

Jan

#15 lore raw

PATCH 4/6 arm/duart: make dt_uart_init() compile with -Wwrite-strings

In reply to Andrew Cooper

GCC complains:

drivers/char/arm-uart.c: In function 'dt_uart_init':
drivers/char/arm-uart.c:81:17: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

     81 |         options = "";
        |                 ^

The problem is using the options string for both splitting opt_duart, and to hold a token "" for no options.

Use two variables; one mutable one one const.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/drivers/char/arm-uart.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
xen/drivers/char/arm-uart.c +8 -4
index 8098a968c285..91f13a41368d 100644
@@ -42,7 +42,8 @@ static void __init dt_uart_init(void)
     struct dt_device_node *dev;
     int ret;
     const char *devpath = opt_dtuart;
-    char *options;
+    const char *options;
+    char *split;
 
     if ( !console_has("dtuart") )
         return; /* Not for us */
@@ -74,9 +75,12 @@ static void __init dt_uart_init(void)
         return;
     }
 
-    options = strchr(opt_dtuart, ':');
-    if ( options != NULL )
-        *(options++) = '\0';
+    split = strchr(opt_dtuart, ':');
+    if ( split )
+    {
+        split[0] = '\0';
+        options = split + 1;
+    }
     else
         options = "";
 
Signature · 1 line
2.30.2
#16 lore raw

Re: [PATCH 4/6] arm/duart: make dt_uart_init() compile with -Wwrite-strings

Quoted 11 lines · Andrew Cooper

GCC complains:

drivers/char/arm-uart.c: In function 'dt_uart_init':
drivers/char/arm-uart.c:81:17: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

     81 |         options = "";
        |                 ^

The problem is using the options string for both splitting opt_duart, and to hold a token "" for no options.

Use two variables; one mutable one one const.

This should say "and one const".  Fixed up locally.

~Andrew

Reviewed-by on v1 4/6
#17 lore raw

Re: [PATCH 4/6] arm/duart: make dt_uart_init() compile with -Wwrite-strings

In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

GCC complains:

drivers/char/arm-uart.c: In function 'dt_uart_init':
drivers/char/arm-uart.c:81:17: error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

     81 |         options = "";
        |                 ^
Quoted 6 lines

The problem is using the options string for both splitting opt_duart, and to hold a token "" for no options.

Use two variables; one mutable one one const.

Quoted 46 lines

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/drivers/char/arm-uart.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
xen/drivers/char/arm-uart.c +8 -4
index 8098a968c285..91f13a41368d 100644
@@ -42,7 +42,8 @@ static void __init dt_uart_init(void)
     struct dt_device_node *dev;
     int ret;
     const char *devpath = opt_dtuart;
-    char *options;
+    const char *options;
+    char *split;
 
     if ( !console_has("dtuart") )
         return; /* Not for us */
@@ -74,9 +75,12 @@ static void __init dt_uart_init(void)
         return;
     }
 
-    options = strchr(opt_dtuart, ':');
-    if ( options != NULL )
-        *(options++) = '\0';
+    split = strchr(opt_dtuart, ':');
+    if ( split )
+    {
+        split[0] = '\0';
+        options = split + 1;
+    }
     else
         options = "";
 
Signature · 1 line
2.30.2
#18 lore raw

PATCH 5/6 arm/platforms: Make compatbile with -Wwrite-strings

In reply to Andrew Cooper

GCC complains:

arch/arm/platforms/brcm.c: In function ‘brcm_populate_plat_regs’:
arch/arm/platforms/brcm.c:76:27: error: passing argument 1 of ‘brcm_get_dt_node’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]

     76 |     rc = brcm_get_dt_node("brcm,brcmstb-cpu-biu-ctrl", &node, &reg_base);
        |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~

and

arch/arm/platforms/exynos5.c: In function ‘exynos5_smp_init’:
arch/arm/platforms/exynos5.c:109:20: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]

    109 |         compatible = "samsung,exynos4210-sysram-ns";
        |                    ^

In both cases, just make the relevant variable const.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/arch/arm/platforms/brcm.c    | 2 +-
 xen/arch/arm/platforms/exynos5.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/arm/platforms/brcm.c +1 -1
index 951e4d6cc328..43a07ab1a69d 100644
@@ -35,7 +35,7 @@ static u32 brcm_boot_continuation_pc;
 
 static struct brcm_plat_regs regs;
 
-static __init int brcm_get_dt_node(char *compat_str,
+static __init int brcm_get_dt_node(const char *compat_str,
                                    const struct dt_device_node **dn,
                                    u32 *reg_base)
 {
xen/arch/arm/platforms/exynos5.c +1 -1
index c48093cd4fa4..f7c09520675e 100644
@@ -96,7 +96,7 @@ static int __init exynos5_smp_init(void)
 {
     struct dt_device_node *node;
     void __iomem *sysram;
-    char *compatible;
+    const char *compatible;
     paddr_t sysram_addr;
     paddr_t size;
     paddr_t sysram_offset;
Signature · 1 line
2.30.2
Reviewed-by on v1 5/6
#19 lore raw

Re: [PATCH 5/6] arm/platforms: Make compatbile with -Wwrite-strings

Quoted 9 lines · Andrew Cooper

GCC complains:

arch/arm/platforms/brcm.c: In function ‘brcm_populate_plat_regs’:
arch/arm/platforms/brcm.c:76:27: error: passing argument 1 of ‘brcm_get_dt_node’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]

     76 |     rc = brcm_get_dt_node("brcm,brcmstb-cpu-biu-ctrl", &node, &reg_base);
        |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~

and

Quoted 8 lines

arch/arm/platforms/exynos5.c: In function ‘exynos5_smp_init’:
arch/arm/platforms/exynos5.c:109:20: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]

    109 |         compatible = "samsung,exynos4210-sysram-ns";
        |                    ^

In both cases, just make the relevant variable const.

Quoted 43 lines

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/arch/arm/platforms/brcm.c    | 2 +-
 xen/arch/arm/platforms/exynos5.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/arm/platforms/brcm.c +1 -1
index 951e4d6cc328..43a07ab1a69d 100644
@@ -35,7 +35,7 @@ static u32 brcm_boot_continuation_pc;
 
 static struct brcm_plat_regs regs;
 
-static __init int brcm_get_dt_node(char *compat_str,
+static __init int brcm_get_dt_node(const char *compat_str,
                                    const struct dt_device_node **dn,
                                    u32 *reg_base)
 {
xen/arch/arm/platforms/exynos5.c +1 -1
index c48093cd4fa4..f7c09520675e 100644
@@ -96,7 +96,7 @@ static int __init exynos5_smp_init(void)
 {
     struct dt_device_node *node;
     void __iomem *sysram;
-    char *compatible;
+    const char *compatible;
     paddr_t sysram_addr;
     paddr_t size;
     paddr_t sysram_offset;
Signature · 1 line
2.30.2
#20 lore raw

PATCH 6/6 xen: Enable -Wwrite-strings

In reply to Andrew Cooper

The codebase is now -Wwrite-strings clean. Activate the option to cause string literals to have a const type, and prevent any violations of MISRA Rule 7.4 being reintroduced.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/Makefile +1 -1
index 7b869f4b3037..ca571103c868 100644
@@ -392,7 +392,7 @@ endif
 CFLAGS-$(CONFIG_CC_SPLIT_SECTIONS) += -ffunction-sections -fdata-sections
 
 CFLAGS += -nostdinc -fno-builtin -fno-common
-CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith
+CFLAGS += -Werror -Wredundant-decls -Wwrite-strings -Wno-pointer-arith
 $(call cc-option-add,CFLAGS,CC,-Wvla)
 CFLAGS += -pipe -D__XEN__ -include $(srctree)/include/xen/config.h
 CFLAGS-$(CONFIG_DEBUG_INFO) += -g
Signature · 1 line
2.30.2
#21 lore raw
Quoted 25 lines · Andrew Cooper

The codebase is now -Wwrite-strings clean. Activate the option to cause string literals to have a const type, and prevent any violations of MISRA Rule 7.4 being reintroduced.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/Makefile +0 -0
index 7b869f4b3037..ca571103c868 100644

xen/Makefile endif · line 398

Quoted 8 lines
xen/Makefile +1 -1
@@ -392,7 +392,7 @@ endif
 CFLAGS-$(CONFIG_CC_SPLIT_SECTIONS) += -ffunction-sections -fdata-sections
 
 CFLAGS += -nostdinc -fno-builtin -fno-common
-CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith
+CFLAGS += -Werror -Wredundant-decls -Wwrite-strings -Wno-pointer-arith
 $(call cc-option-add,CFLAGS,CC,-Wvla)
 CFLAGS += -pipe -D__XEN__ -include $(srctree)/include/xen/config.h
 CFLAGS-$(CONFIG_DEBUG_INFO) += -g

I meant to say.  I've checked our minimum compiler versions, and -Wwrite-strings is supported by everything.

~Andrew

Acked-by on v1 6/6
#22 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

The codebase is now -Wwrite-strings clean. Activate the option to cause string literals to have a const type, and prevent any violations of MISRA Rule 7.4 being reintroduced.

Excellent!

Quoted 32 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Roberto Bagnara <[email protected]>
CC: Nicola Vetrini <[email protected]>

---
 xen/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/Makefile +1 -1
index 7b869f4b3037..ca571103c868 100644
@@ -392,7 +392,7 @@ endif
 CFLAGS-$(CONFIG_CC_SPLIT_SECTIONS) += -ffunction-sections -fdata-sections
 
 CFLAGS += -nostdinc -fno-builtin -fno-common
-CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith
+CFLAGS += -Werror -Wredundant-decls -Wwrite-strings -Wno-pointer-arith
 $(call cc-option-add,CFLAGS,CC,-Wvla)
 CFLAGS += -pipe -D__XEN__ -include $(srctree)/include/xen/config.h
 CFLAGS-$(CONFIG_DEBUG_INFO) += -g
Signature · 1 line
2.30.2
#23 lore raw
In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper

I got carried away and fixed all remaining -Wwrite-strings issues I could reproduce in local builds.

Pipeline in Gitlab:

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1079379918

Randconfig found:

https://gitlab.com/xen-project/people/andyhhp/xen/-/jobs/5580160813

which looks to be the ACPI support.

(quoted diff) +0 -0

b/xen/arch/arm/acpi/domain_build.c
index ed824c017882..b58389ce9e9f 100644

xen/arch/arm/acpi/domain_build.c +0 -0
@@ -245,7 +245,7 @@ static int __init acpi_create_rsdp(struct domain *d,

struct membank tbl_add[])

 static void __init acpi_xsdt_modify_entry(u64 entry[], unsigned long entry_count, -                                          char *signature, u64 addr) +                                          const char *signature, u64 addr) {
     int i;
     struct acpi_table_header *table;

should fix it, and it probably wants to merge into patch 5.

~Andrew

#24 lore raw
Quoted 31 lines · Andrew Cooper
Quoted 6 lines · Andrew Cooper

I got carried away and fixed all remaining -Wwrite-strings issues I could reproduce in local builds.

Pipeline in Gitlab:

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1079379918

Randconfig found:

https://gitlab.com/xen-project/people/andyhhp/xen/-/jobs/5580160813

which looks to be the ACPI support.

xen/arch/arm/acpi/domain_build.c +0 -0
index ed824c017882..b58389ce9e9f 100644
@@ -245,7 +245,7 @@ static int __init acpi_create_rsdp(struct domain *d,
 struct membank tbl_add[])


 static void __init acpi_xsdt_modify_entry(u64 entry[], unsigned long entry_count, -                                          char *signature, u64 addr) +                                          const char *signature, u64 addr) {
     int i;
     struct acpi_table_header *table;

Quoted 1 line

should fix it, and it probably wants to merge into patch 5.