Skip to contentSkip to conversation

efi: const correct EFI functions

RFC PATCH Superseded Trammell Hudson 2 participants 7 messages Sep 14, 2020 – Sep 15, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 efi: const correct EFI functions

By defining IN as const, the EFI handler functions become almost const-correct and allow most of the rest of the EFI boot code to use constant strings.

There are a few places in the code that casts away the const that should be reconsidered. The config parser code modifies the config file in place, which would not work if it were in a read-only segment.

---
 xen/arch/arm/efi/efi-boot.h |  8 ++--
 xen/arch/x86/efi/efi-boot.h | 26 +++++------
 xen/common/efi/boot.c       | 92 +++++++++++++++++++------------------
 xen/include/efi/efidef.h    |  2 +-
 4 files changed, 66 insertions(+), 62 deletions(-)
xen/arch/arm/efi/efi-boot.h +4 -4
index 6527cb0bdf..13666bc065 100644
@@ -418,9 +418,9 @@ static void __init efi_arch_memory_setup(void)
 {
 }
 
-static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
-                                           CHAR16 *cmdline_options,
-                                           char *cfgfile_options)
+static void __init efi_arch_handle_cmdline(const CHAR16 *image_name,
+                                           const CHAR16 *cmdline_options,
+                                           const char *cfgfile_options)
 {
     union string name;
     char *buf;
@@ -482,7 +482,7 @@ static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
 }
 
 static void __init efi_arch_handle_module(struct file *file, const CHAR16 *name,
-                                          char *options)
+                                          const char *options)
 {
     int node;
     int chosen;
xen/arch/x86/efi/efi-boot.h +13 -13
index 7188c9a551..ce5577012b 100644
@@ -280,10 +280,10 @@ static void __init efi_arch_cfg_file_late(EFI_FILE_HANDLE dir_handle, char *sect
 {
     union string name;
 
-    name.s = get_value(&cfg, section, "ucode");
-    if ( !name.s )
-        name.s = get_value(&cfg, "global", "ucode");
-    if ( name.s )
+    name.cs = get_value(&cfg, section, "ucode");
+    if ( !name.cs )
+        name.cs = get_value(&cfg, "global", "ucode");
+    if ( name.cs )
     {
         microcode_set_module(mbi.mods_count);
         split_string(name.s);
@@ -292,29 +292,29 @@ static void __init efi_arch_cfg_file_late(EFI_FILE_HANDLE dir_handle, char *sect
     }
 }
 
-static void __init efi_arch_handle_cmdline(CHAR16 *image_name,
-                                           CHAR16 *cmdline_options,
-                                           char *cfgfile_options)
+static void __init efi_arch_handle_cmdline(const CHAR16 *image_name,
+                                           const CHAR16 *cmdline_options,
+                                           const char *cfgfile_options)
 {
     union string name;
 
     if ( cmdline_options )
     {
-        name.w = cmdline_options;
+        name.cw = cmdline_options;
         w2s(&name);
-        place_string(&mbi.cmdline, name.s);
+        place_string(&mbi.cmdline, name.cs);
     }
     if ( cfgfile_options )
         place_string(&mbi.cmdline, cfgfile_options);
     /* Insert image name last, as it gets prefixed to the other options. */
     if ( image_name )
     {
-        name.w = image_name;
+        name.cw = image_name;
         w2s(&name);
     }
     else
-        name.s = "xen";
-    place_string(&mbi.cmdline, name.s);
+        name.cs = "xen";
+    place_string(&mbi.cmdline, name.cs);
 
     if ( mbi.cmdline )
         mbi.flags |= MBI_CMDLINE;
@@ -636,7 +636,7 @@ static void __init efi_arch_memory_setup(void)
 }
 
 static void __init efi_arch_handle_module(struct file *file, const CHAR16 *name,
-                                          char *options)
+                                          const char *options)
 {
     union string local_name;
     void *ptr;
xen/common/efi/boot.c +48 -44

243 lines not on this page · raw message

xen/include/efi/efidef.h +1 -1
index 86a7e111bf..1d1c779d66 100644
@@ -50,7 +50,7 @@ typedef VOID            *EFI_EVENT;
 //
 
 #ifndef IN
-    #define IN
+    #define IN const
     #define OUT
     #define OPTIONAL
 #endif
Signature · 1 line
2.25.1
#1 lore raw
Quoted 3 lines · Trammell Hudson

By defining IN as const, the EFI handler functions become almost const-correct and allow most of the rest of the EFI boot code to use constant strings.

How does this work with combined "IN OUT"? I'm afraid there is a reason why things aren't done the way you suggest.

xen/include/efi/efidef.h typedef VOID *EFI_EVENT; · line 56

Quoted 11 lines
xen/include/efi/efidef.h +1 -1
@@ -50,7 +50,7 @@ typedef VOID            *EFI_EVENT;
 //
 
 #ifndef IN
-    #define IN
+    #define IN const
     #define OUT
     #define OPTIONAL
 #endif

I think the #ifdef here is precisely so you can override the three identifiers without having to touch this (imported) header. We try to keep imported headers as little modified as possible.

Jan

#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Trammell Hudson

By defining IN as const, the EFI handler functions become almost const-correct and allow most of the rest of the EFI boot code to use constant strings.

How does this work with combined "IN OUT"? I'm afraid there is a reason why things aren't done the way you suggest.

WTF FFS UEFI. They really do continue to find new ways to disappoint me.

So I see three options:

Option 1 is to retroactively modify the C spec to allow us to have a "nonconst" that will override any prior "const" modifiers (last one wins, like Duck Season/Rabbit Season).

Option 2 would be to modify the imported header to change the 30 uses of "IN OUT" to "INOUT" and define that to be an empty string.

Option 3 would be to write wrappers for the few functions that are used in the EFI boot path that cast-away the constness of their arguments (while also silently cursing the UEFI forum for not writing const-correct code).

--
Trammell

#3 lore raw
Quoted 9 lines · Trammell Hudson
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Trammell Hudson

By defining IN as const, the EFI handler functions become almost const-correct and allow most of the rest of the EFI boot code to use constant strings.

How does this work with combined "IN OUT"? I'm afraid there is a reason why things aren't done the way you suggest.

Quoted 7 lines

WTF FFS UEFI. They really do continue to find new ways to disappoint me.

So I see three options:

Option 1 is to retroactively modify the C spec to allow us to have a "nonconst" that will override any prior "const" modifiers (last one wins, like Duck Season/Rabbit Season).

LoL

Quoted 3 lines

Option 2 would be to modify the imported header to change the 30 uses of "IN OUT" to "INOUT" and define that to be an empty string.

This is something that one might hope even the gnu-efi folks could be persuaded to do.

Quoted 4 lines

Option 3 would be to write wrappers for the few functions that are used in the EFI boot path that cast-away the constness of their arguments (while also silently cursing the UEFI forum for not writing const-correct code).

This would be kind of a last resort fallback (except for the cursing, which of course we can do at any time).

Jan

#4 lore raw
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Trammell Hudson

Option 3 would be to write wrappers for the few functions that are used in the EFI boot path that cast-away the constness of their arguments (while also silently cursing the UEFI forum for not writing const-correct code).

This would be kind of a last resort fallback (except for the cursing, which of course we can do at any time).

Since you didn't like the time travel option, I checked to see which functions would need to be wrapped. It is a surprisingly small number:

#define PrintStr(s) StdOut->OutputString(StdOut, (CHAR16 *)(s)) #define PrintErr(s) StdErr->OutputString(StdErr, (CHAR16 *)(s)) #define efi_file_open(file,handle,name,mode,attr) \
(file)->Open(file, handle, (CHAR16 *)(name), mode, attr)
#define shim_verify(shim, ptr, len) \
(shim)->Verify((void *)(ptr), len)

--
Trammell

#5 lore raw
Quoted 14 lines · Trammell Hudson
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Trammell Hudson

Option 3 would be to write wrappers for the few functions that are used in the EFI boot path that cast-away the constness of their arguments (while also silently cursing the UEFI forum for not writing const-correct code).

This would be kind of a last resort fallback (except for the cursing, which of course we can do at any time).

Since you didn't like the time travel option, I checked to see which functions would need to be wrapped. It is a surprisingly small number:

Quoted 6 lines

#define PrintStr(s) StdOut->OutputString(StdOut, (CHAR16 *)(s)) #define PrintErr(s) StdErr->OutputString(StdErr, (CHAR16 *)(s)) #define efi_file_open(file,handle,name,mode,attr) \
(file)->Open(file, handle, (CHAR16 *)(name), mode, attr)
#define shim_verify(shim, ptr, len) \
(shim)->Verify((void *)(ptr), len)

That's surprisingly few. What about e.g. HandleProtocol() and LocateHandle()? GUIDs shouldn't really be non-const either.

Jan

#6 lore raw
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Trammell Hudson

[...] I checked to see
which functions would need to be wrapped. It is a surprisingly small number:
#define PrintStr(s) StdOut->OutputString(StdOut, (CHAR16 *)(s)) #define PrintErr(s) StdErr->OutputString(StdErr, (CHAR16 *)(s)) #define efi_file_open(file,handle,name,mode,attr) \
(file)->Open(file, handle, (CHAR16 *)(name), mode, attr) #define shim_verify(shim, ptr, len) \
(shim)->Verify((void *)(ptr), len)

Quoted 2 lines

That's surprisingly few. What about e.g. HandleProtocol() and LocateHandle()? GUIDs shouldn't really be non-const either.

Good point -- I did not track down all uses of efi_bs and every GUID. I'll send a RFC patch v2 in a minute with all of the GUIDs converted to __initconst.

--
Trammell