Skip to contentSkip to conversation

contrib/elf2dmp: prevent uninitialized warning

PATCH In review [email protected] 2 participants 5 messages Feb 7, 2020 – Mar 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 contrib/elf2dmp: prevent uninitialized warning

From: Chen Qun <[email protected]>

Fix compilation warnings:
contrib/elf2dmp/main.c:66:17: warning: ‘KdpDataBlockEncoded’ may be used
uninitialized in this function [-Wmaybe-uninitialized]

         block = __builtin_bswap64(block ^ kdbe) ^ kwa;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contrib/elf2dmp/main.c:78:24: note: ‘KdpDataBlockEncoded’ was declared here
     uint64_t kwn, kwa, KdpDataBlockEncoded;
                        ^~~~~~~~~~~~~~~~~~~
---
 contrib/elf2dmp/main.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
contrib/elf2dmp/main.c +12 -13
index 9a2dbc2902..203b9e6d04 100644
@@ -76,6 +76,7 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb,
     DBGKD_DEBUG_DATA_HEADER64 kdbg_hdr;
     bool decode = false;
     uint64_t kwn, kwa, KdpDataBlockEncoded;
+    uint64_t KiWaitNever, KiWaitAlways;
 
     if (va_space_rw(vs,
                 KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64, Header),
@@ -84,21 +85,19 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb,
         return NULL;
     }
 
-    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
-        uint64_t KiWaitNever, KiWaitAlways;
-
-        decode = true;
+    if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
+            !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
+            !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
+        return NULL;
+    }
 
-        if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
-                !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
-                !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
-            return NULL;
-        }
+    if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
+            va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) {
+        return NULL;
+    }
 
-        if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
-                va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) {
-            return NULL;
-        }
+    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
+        decode = true;
 
         printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn);
         printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa);
Signature · 1 line
2.23.0
#1 lore raw
Quoted 56 lines · On Fri, 7 Feb 2020 12:16:01 +0800 <[email protected]> wrote:

From: Chen Qun <[email protected]>

Fix compilation warnings:
contrib/elf2dmp/main.c:66:17: warning: ‘KdpDataBlockEncoded’ may be used uninitialized in this function [-Wmaybe-uninitialized]

         block = __builtin_bswap64(block ^ kdbe) ^ kwa;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contrib/elf2dmp/main.c:78:24: note: ‘KdpDataBlockEncoded’ was
declared here uint64_t kwn, kwa, KdpDataBlockEncoded;
                        ^~~~~~~~~~~~~~~~~~~
---
 contrib/elf2dmp/main.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
contrib/elf2dmp/main.c +1 -0
index 9a2dbc2902..203b9e6d04 100644
@@ -76,6 +76,7 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t
 KernBase, struct pdb_reader *pdb, DBGKD_DEBUG_DATA_HEADER64 kdbg_hdr;
     bool decode = false;
     uint64_t kwn, kwa, KdpDataBlockEncoded;
+    uint64_t KiWaitNever, KiWaitAlways;
 
     if (va_space_rw(vs,
                 KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64,

Header), @@ -84,21 +85,19 @@ static KDDEBUGGER_DATA64

*get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, return NULL;
     }
contrib/elf2dmp/main.c +9 -11
-    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
-        uint64_t KiWaitNever, KiWaitAlways;
-
-        decode = true;
+    if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
+            !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
+            !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
+        return NULL;
+    }
 
-        if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
-                !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
-                !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
-            return NULL;
-        }
+    if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
+            va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) {
+        return NULL;
+    }
 
-        if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
-                va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0))

{

contrib/elf2dmp/main.c static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, · line 103

Quoted 7 lines
contrib/elf2dmp/main.c +2 -2
-            return NULL;
-        }
+    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
+        decode = true;
 
         printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn);
         printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa);

Hi!

I suppose the problem is in your compiler, because kdbg_decode() is only used when KdpDataBlockEncoded is already initialized by SYM_RESOLVE().

Signature · 1 line
Viktor Prutyanov
#2 lore raw
Quoted 73 lines

-----Original Message-----
From: Viktor Prutyanov [mailto:[email protected]]
Sent: Friday, March 6, 2020 2:59 AM
To: Chenqun (kuhn) <[email protected]>
Cc: [email protected]; [email protected]; Zhanghailiang <[email protected]>; [email protected]
Subject: Re: [PATCH] contrib/elf2dmp: prevent uninitialized warning

Quoted 22 lines · On Fri, 7 Feb 2020 12:16:01 +0800 <[email protected]> wrote:

From: Chen Qun <[email protected]>

Fix compilation warnings:
contrib/elf2dmp/main.c:66:17: warning: ‘KdpDataBlockEncoded’ may be used uninitialized in this function [-Wmaybe-uninitialized]

         block = __builtin_bswap64(block ^ kdbe) ^ kwa;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contrib/elf2dmp/main.c:78:24: note: ‘KdpDataBlockEncoded’ was declared
here uint64_t kwn, kwa, KdpDataBlockEncoded;
                        ^~~~~~~~~~~~~~~~~~~
---
 contrib/elf2dmp/main.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
contrib/elf2dmp/main.c index renamed from contrib/elf2dmp/main.c +0 -0

9a2dbc2902..203b9e6d04 100644

contrib/elf2dmp/main.c +0 -0
@@ -76,6 +76,7 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t
Quoted 1 line

KernBase, struct pdb_reader *pdb, DBGKD_DEBUG_DATA_HEADER64

kdbg_hdr;

Quoted 38 lines
     bool decode = false;
     uint64_t kwn, kwa, KdpDataBlockEncoded;

+ uint64_t KiWaitNever, KiWaitAlways;

if (va_space_rw(vs,
KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64,
Header), @@ -84,21 +85,19 @@ static KDDEBUGGER_DATA64

*get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, return NULL;
     }
contrib/elf2dmp/main.c +9 -11
-    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
-        uint64_t KiWaitNever, KiWaitAlways;
-
-        decode = true;
+    if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
+            !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
+            !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
+        return NULL;
+    }
 
-        if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
-                !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
-                !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
-            return NULL;
-        }
+    if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
+            va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) {
+        return NULL;
+    }
 
-        if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
-                va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0))

{

contrib/elf2dmp/main.c +2 -2
-            return NULL;
-        }
+    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
+        decode = true;
Quoted 8 lines
Quoted 2 lines
         printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn);
         printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa);

Hi!

I suppose the problem is in your compiler, because kdbg_decode() is only used when KdpDataBlockEncoded is already initialized by SYM_RESOLVE().

Hi Viktor,

I know it's actually initialized when 'decode = true;', otherwise ' return kdbg;' no need to initialize.

But usually the compiler cannot understand it, because it seems that the initialization is only in the if() statement.
If we put the initialization outside the if() statement, it might looks better without affecting the functionality ?

Thanks.

Quoted 2 lines

--
Viktor Prutyanov

#3 lore raw
Quoted 83 lines · On Fri, 6 Mar 2020 02:18:07 +0000 "Chenqun (kuhn)" <[email protected]>…
Quoted 74 lines

-----Original Message-----
From: Viktor Prutyanov [mailto:[email protected]]
Sent: Friday, March 6, 2020 2:59 AM
To: Chenqun (kuhn) <[email protected]>
Cc: [email protected]; [email protected]; Zhanghailiang <[email protected]>; [email protected]
Subject: Re: [PATCH] contrib/elf2dmp: prevent uninitialized warning

Quoted 22 lines · On Fri, 7 Feb 2020 12:16:01 +0800 <[email protected]> wrote:

From: Chen Qun <[email protected]>

Fix compilation warnings:
contrib/elf2dmp/main.c:66:17: warning: ‘KdpDataBlockEncoded’ may be used uninitialized in this function [-Wmaybe-uninitialized]

         block = __builtin_bswap64(block ^ kdbe) ^ kwa;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contrib/elf2dmp/main.c:78:24: note: ‘KdpDataBlockEncoded’ was
declared here uint64_t kwn, kwa, KdpDataBlockEncoded;
                        ^~~~~~~~~~~~~~~~~~~
---
 contrib/elf2dmp/main.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
contrib/elf2dmp/main.c index renamed from contrib/elf2dmp/main.c +0 -0

9a2dbc2902..203b9e6d04 100644

contrib/elf2dmp/main.c +0 -0
@@ -76,6 +76,7 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t
Quoted 1 line

KernBase, struct pdb_reader *pdb, DBGKD_DEBUG_DATA_HEADER64

kdbg_hdr;

Quoted 39 lines
     bool decode = false;
     uint64_t kwn, kwa, KdpDataBlockEncoded;

+ uint64_t KiWaitNever, KiWaitAlways;

if (va_space_rw(vs,
KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64,
Header), @@ -84,21 +85,19 @@ static KDDEBUGGER_DATA64

*get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, return NULL;
     }
contrib/elf2dmp/main.c +5 -7
-    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
-        uint64_t KiWaitNever, KiWaitAlways;
-
-        decode = true;
+    if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
+            !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
+            !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
+        return NULL;
+    }
 
-        if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
-                !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
-                !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded))

{

contrib/elf2dmp/main.c +4 -4
-            return NULL;
-        }
+    if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
+            va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) {
+        return NULL;
+    }
 
-        if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
-                va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa),

0)) {

contrib/elf2dmp/main.c +2 -2
-            return NULL;
-        }
+    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
+        decode = true;
Quoted 9 lines
Quoted 2 lines
         printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn);
         printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa);  

Hi!

I suppose the problem is in your compiler, because kdbg_decode() is only used when KdpDataBlockEncoded is already initialized by SYM_RESOLVE().

Quoted 6 lines

Hi Viktor,

I know it's actually initialized when 'decode = true;',
otherwise ' return kdbg;' no need to initialize.
But usually the compiler cannot understand it, because it seems
that the initialization is only in the if() statement.

As for me, my GCC 9.2.1 doesn't show any warning while building elf2dmp.

Quoted 2 lines

If we put the initialization outside the if() statement, it might looks better without affecting the functionality ?

For now, your original patch affects the functionality. The utility tries to resolve symbols as little as possible during conversion, because we don't know exactly how Windows kernel works. This is the reason why KDBG header should be checked before resolving 3 symbols.

Quoted 4 lines

Thanks.

Quoted 2 lines

--
Viktor Prutyanov

Signature · 1 line
Viktor Prutyanov
#4 lore raw
Quoted 95 lines

-----Original Message-----
From: Viktor Prutyanov [mailto:[email protected]]
Sent: Friday, March 6, 2020 7:48 PM
To: Chenqun (kuhn) <[email protected]>
Cc: [email protected]; [email protected]; qemu- [email protected]; Zhanghailiang <[email protected]>
Subject: Re: [PATCH] contrib/elf2dmp: prevent uninitialized warning

Quoted 83 lines · On Fri, 6 Mar 2020 02:18:07 +0000 "Chenqun (kuhn)" <[email protected]>…
Quoted 83 lines

-----Original Message-----
From: Viktor Prutyanov [mailto:[email protected]]
Sent: Friday, March 6, 2020 2:59 AM
To: Chenqun (kuhn) <[email protected]>
Cc: [email protected]; [email protected]; Zhanghailiang <[email protected]>; [email protected]
Subject: Re: [PATCH] contrib/elf2dmp: prevent uninitialized warning

Quoted 22 lines · On Fri, 7 Feb 2020 12:16:01 +0800 <[email protected]> wrote:

From: Chen Qun <[email protected]>

Fix compilation warnings:
contrib/elf2dmp/main.c:66:17: warning: ‘KdpDataBlockEncoded’ may be used uninitialized in this function [-Wmaybe-uninitialized]

         block = __builtin_bswap64(block ^ kdbe) ^ kwa;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
contrib/elf2dmp/main.c:78:24: note: ‘KdpDataBlockEncoded’ was
declared here uint64_t kwn, kwa, KdpDataBlockEncoded;
                        ^~~~~~~~~~~~~~~~~~~
---
 contrib/elf2dmp/main.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
contrib/elf2dmp/main.c index renamed from contrib/elf2dmp/main.c +0 -0

9a2dbc2902..203b9e6d04 100644

contrib/elf2dmp/main.c +0 -0
@@ -76,6 +76,7 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t
Quoted 1 line

KernBase, struct pdb_reader *pdb, DBGKD_DEBUG_DATA_HEADER64

kdbg_hdr;

Quoted 34 lines
     bool decode = false;
     uint64_t kwn, kwa, KdpDataBlockEncoded;

+ uint64_t KiWaitNever, KiWaitAlways;

if (va_space_rw(vs,
KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64,
Header), @@ -84,21 +85,19 @@ static KDDEBUGGER_DATA64

*get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, return NULL;
     }
contrib/elf2dmp/main.c +5 -7
-    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
-        uint64_t KiWaitNever, KiWaitAlways;
-
-        decode = true;
+    if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
+            !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
+            !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) {
+        return NULL;
+    }
 
-        if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) ||
-                !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) ||
-                !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded))

{

contrib/elf2dmp/main.c +4 -4
-            return NULL;
-        }
+    if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
+            va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) {
+        return NULL;
+    }
 
-        if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) ||
-                va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa),

0)) {

contrib/elf2dmp/main.c static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, · line 103

Quoted 7 lines
contrib/elf2dmp/main.c +2 -2
-            return NULL;
-        }
+    if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) {
+        decode = true;
 
         printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn);
         printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa);

Hi!

I suppose the problem is in your compiler, because kdbg_decode() is only used when KdpDataBlockEncoded is already initialized by SYM_RESOLVE().

Quoted 9 lines
Quoted 6 lines

Hi Viktor,

I know it's actually initialized when 'decode = true;',
otherwise ' return kdbg;' no need to initialize.
But usually the compiler cannot understand it, because it seems
that the initialization is only in the if() statement.

As for me, my GCC 9.2.1 doesn't show any warning while building elf2dmp.

Maybe you are right, my GCC version lower( 7.3.0).

Quoted 9 lines
Quoted 3 lines

If we put the initialization outside the if() statement, it might looks better without affecting the functionality ?

For now, your original patch affects the functionality. The utility tries to resolve symbols as little as possible during conversion, because we don't know exactly how Windows kernel works. This is the reason why KDBG header should be checked before resolving 3 symbols.

OK , let's drop this patch.

Thanks.

Quoted 9 lines
Quoted 4 lines

Thanks.

Quoted 2 lines

--
Viktor Prutyanov

--
Viktor Prutyanov