[PATCH v2] hw/i386/amd_iommu: Fix maybe-uninitialized error with GCC 12

Paolo Bonzini posted 1 patch 2 years, 1 month ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20220321143346.156481-1-pbonzini@redhat.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Eduardo Habkost <eduardo@habkost.net>
hw/i386/amd_iommu.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
[PATCH v2] hw/i386/amd_iommu: Fix maybe-uninitialized error with GCC 12
Posted by Paolo Bonzini 2 years, 1 month ago
Be more explicit that the loop must roll at least once.  Avoids the
following warning:

  FAILED: libqemu-x86_64-softmmu.fa.p/hw_i386_amd_iommu.c.o
  In function 'pte_get_page_mask',
      inlined from 'amdvi_page_walk' at hw/i386/amd_iommu.c:945:25,
      inlined from 'amdvi_do_translate' at hw/i386/amd_iommu.c:989:5,
      inlined from 'amdvi_translate' at hw/i386/amd_iommu.c:1038:5:
  hw/i386/amd_iommu.c:877:38: error: 'oldlevel' may be used uninitialized [-Werror=maybe-uninitialized]
    877 |     return ~((1UL << ((oldlevel * 9) + 3)) - 1);
        |                      ~~~~~~~~~~~~~~~~^~~~
  hw/i386/amd_iommu.c: In function 'amdvi_translate':
  hw/i386/amd_iommu.c:906:41: note: 'oldlevel' was declared here
    906 |     unsigned level, present, pte_perms, oldlevel;
        |                                         ^~~~~~~~
  cc1: all warnings being treated as errors

Having:

  $ gcc --version
  gcc (Debian 12-20220313-1) 12.0.1 20220314 (experimental)

Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/i386/amd_iommu.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index 4d13d8e697..6986ad3b87 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -913,7 +913,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
         }
 
         /* we are at the leaf page table or page table encodes a huge page */
-        while (level > 0) {
+        do {
             pte_perms = amdvi_get_perms(pte);
             present = pte & 1;
             if (!present || perms != (perms & pte_perms)) {
@@ -932,10 +932,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
             }
             oldlevel = level;
             level = get_pte_translation_mode(pte);
-            if (level == 0x7) {
-                break;
-            }
-        }
+        } while (level > 0 && level < 7);
 
         if (level == 0x7) {
             page_mask = pte_override_page_mask(pte);
-- 
2.35.1


Re: [PATCH v2] hw/i386/amd_iommu: Fix maybe-uninitialized error with GCC 12
Posted by Philippe Mathieu-Daudé 2 years, 1 month ago
On 21/3/22 15:33, Paolo Bonzini wrote:
> Be more explicit that the loop must roll at least once.  Avoids the
> following warning:
> 
>    FAILED: libqemu-x86_64-softmmu.fa.p/hw_i386_amd_iommu.c.o
>    In function 'pte_get_page_mask',
>        inlined from 'amdvi_page_walk' at hw/i386/amd_iommu.c:945:25,
>        inlined from 'amdvi_do_translate' at hw/i386/amd_iommu.c:989:5,
>        inlined from 'amdvi_translate' at hw/i386/amd_iommu.c:1038:5:
>    hw/i386/amd_iommu.c:877:38: error: 'oldlevel' may be used uninitialized [-Werror=maybe-uninitialized]
>      877 |     return ~((1UL << ((oldlevel * 9) + 3)) - 1);
>          |                      ~~~~~~~~~~~~~~~~^~~~
>    hw/i386/amd_iommu.c: In function 'amdvi_translate':
>    hw/i386/amd_iommu.c:906:41: note: 'oldlevel' was declared here
>      906 |     unsigned level, present, pte_perms, oldlevel;
>          |                                         ^~~~~~~~
>    cc1: all warnings being treated as errors
> 
> Having:
> 
>    $ gcc --version
>    gcc (Debian 12-20220313-1) 12.0.1 20220314 (experimental)
> 
> Reported-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   hw/i386/amd_iommu.c | 7 ++-----
>   1 file changed, 2 insertions(+), 5 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Thanks!