Skip to contentSkip to conversation

util/keyval: fix msan findings

PATCH In review Patrick Venture 4 participants 5 messages Feb 28, 2025 – Mar 3, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/keyval: fix msan findings

From: Peter Foley <[email protected]>

e.g.
qemu: Uninitialized value was created by an allocation of 'key_in_cur.i' in the stack frame
qemu: #0 0xaaaac49f489c in keyval_parse_one third_party/qemu/util/keyval.c:190:5

---
 util/keyval.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/keyval.c +1 -1
index a70629a481..f33c64079d 100644
@@ -187,7 +187,7 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
 {
     const char *key, *key_end, *val_end, *s, *end;
     size_t len;
-    char key_in_cur[128];
+    char key_in_cur[128] = {};
     QDict *cur;
     int ret;
     QObject *next;
Signature · 1 line
2.48.1.711.g2feabab25a-goog
#1 lore raw
Quoted 8 lines · Patrick Venture

From: Peter Foley <[email protected]>

e.g.
qemu: Uninitialized value was created by an allocation of 'key_in_cur.i' in the stack frame
qemu: #0 0xaaaac49f489c in keyval_parse_one third_party/qemu/util/keyval.c:190:5

This is not a fix, since there's no bug to fix. It's just the tool complaining about something it can't reason on.

Paolo

Quoted 17 lines
---
  util/keyval.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
util/keyval.c +1 -1
index a70629a481..f33c64079d 100644
@@ -187,7 +187,7 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
  {
      const char *key, *key_end, *val_end, *s, *end;
      size_t len;
-    char key_in_cur[128];
+    char key_in_cur[128] = {};
      QDict *cur;
      int ret;
      QObject *next;
#2 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · Patrick Venture

From: Peter Foley <[email protected]>
e.g.
qemu: Uninitialized value was created by an allocation of 'key_in_cur.i' in the stack frame
qemu: #0 0xaaaac49f489c in keyval_parse_one third_party/qemu/util/keyval.c:190:5

This is not a fix, since there's no bug to fix. It's just the tool complaining about something it can't reason on.

Paolo

The code is designed to read @keyval_in_cur only in non-first iterations of the loop. The previous iteration assigned to it then.

The two lines you quoted don't make sense to me. Is this the full report you got? If not, show us the full report, please. Ideally with a reproducer.

Quoted 5 lines
Quoted 5 lines
---
 util/keyval.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/keyval.c +0 -0
index a70629a481..f33c64079d 100644
Quoted 8 lines

util/keyval.c static const char *keyval_parse_one(QDict *qdict, const char *params, · line 190

Quoted 8 lines
util/keyval.c +1 -1
@@ -187,7 +187,7 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
 {
     const char *key, *key_end, *val_end, *s, *end;
     size_t len;
-    char key_in_cur[128];
+    char key_in_cur[128] = {};

Suspect overkill. Would "" do?

Quoted 3 lines
Quoted 3 lines
util/keyval.c +0 -0
     QDict *cur;
     int ret;
     QObject *next;
#3 lore raw
Quoted 15 lines · Markus Armbruster
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Patrick Venture

From: Peter Foley <[email protected]>
e.g.
qemu: Uninitialized value was created by an allocation of

'key_in_cur.i' in the stack frame

Quoted 1 line
Quoted 1 line

qemu: #0 0xaaaac49f489c in keyval_parse_one

third_party/qemu/util/keyval.c:190:5

Quoted 4 lines
Quoted 2 lines

This is not a fix, since there's no bug to fix. It's just the tool

complaining about something it can't reason on.

Quoted 1 line
Quoted 9 lines
Quoted 1 line

Paolo

The code is designed to read @keyval_in_cur only in non-first iterations of the loop. The previous iteration assigned to it then.

The two lines you quoted don't make sense to me. Is this the full report you got? If not, show us the full report, please. Ideally with a reproducer.

The full output looks like:

Uninitialized bytes in strlen at offset 0 inside [0xffffd1958110, 5) ==9780==WARNING: MemorySanitizer: use-of-uninitialized-value

    #0 0xaaaac1c4b170 in tdb_hash third_party/qemu/qobject/qdict.c:46:31
    #1 0xaaaac1c4b4a4 in qdict_get third_party/qemu/qobject/qdict.c:164:36
    #2 0xaaaac1c78468 in keyval_parse_put third_party/qemu/util/keyval.c:152:11
    #3 0xaaaac1c77740 in keyval_parse_one third_party/qemu/util/keyval.c:295:10
    #4 0xaaaac1c77740 in keyval_parse_into third_party/qemu/util/keyval.c:530:13
    #5 0xaaaaba2f9524 in qemu_init third_party/qemu/system/vl.c:3322:21
    #6 0xaaaab9641c2c in main third_party/qemu/system/main.c:54:5
    #7 0xffffba320000 in __libc_start_main
(/usr/grte/v5/lib64/libc.so.6+0x61000) (BuildId:
613d20d3b812b4c87fe9ebf8c4caae83)
    #8 0xaaaab934bd10 in _start

/usr/grte/v5/debug-src/src/csu/../sysdeps/aarch64/start.S:92

Uninitialized value was created by an allocation of 'key_in_cur.i'
in the stack frame

    #0 0xaaaac1c7708c in keyval_parse_one third_party/qemu/util/keyval.c:190:5
    #1 0xaaaac1c7708c in keyval_parse_into third_party/qemu/util/keyval.c:530:13

SUMMARY: MemorySanitizer: use-of-uninitialized-value third_party/qemu/qobject/qdict.c:46:31 in tdb_hash Exiting

I don't have an easily shareable reproducer, but it's probably possible to whip one up.

Quoted 10 lines
Quoted 9 lines

util/keyval.c static const char *keyval_parse_one(QDict *qdict,

Quoted 9 lines
---
 util/keyval.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/keyval.c +0 -0
index a70629a481..f33c64079d 100644
@@ -187,7 +187,7 @@ static const char *keyval_parse_one(QDict *qdict,

const char *params,

Quoted 8 lines
Quoted 5 lines

util/keyval.c static const char *keyval_parse_one(QDict *qdict, const char *params, · line 190

Quoted 5 lines
util/keyval.c +1 -1
 {
     const char *key, *key_end, *val_end, *s, *end;
     size_t len;
-    char key_in_cur[128];
+    char key_in_cur[128] = {};

Suspect overkill. Would "" do?

It appears to resolve the complaint, yes.

#4 lore raw
Quoted 8 lines · Peter Foley

The full output looks like:

Uninitialized bytes in strlen at offset 0 inside [0xffffd1958110, 5) ==9780==WARNING: MemorySanitizer: use-of-uninitialized-value

     #0 0xaaaac1c4b170 in tdb_hash third_party/qemu/qobject/qdict.c:46:31
     #1 0xaaaac1c4b4a4 in qdict_get third_party/qemu/qobject/qdict.c:164:36
     #2 0xaaaac1c78468 in keyval_parse_put third_party/qemu/util/keyval.c:152:11
     #3 0xaaaac1c77740 in keyval_parse_one third_party/qemu/util/keyval.c:295:10

In order to get to

if (!keyval_parse_put(cur, key_in_cur, qstring_from_gstring(val),
key, key_end, errp)) {

you must have gone through the only exit path of the earlier "for(;;)" loop:

        memcpy(key_in_cur, s, len);
        key_in_cur[len] = 0;
        s += len;

        if (*s != '.') {
            break;
        }

meaning that key_in_cur is NULL-terminated and initialized---unless s is also uninitialized, but then adding an initializer would not do anything.

Paolo

Quoted 36 lines
     #4 0xaaaac1c77740 in keyval_parse_into third_party/qemu/util/keyval.c:530:13
     #5 0xaaaaba2f9524 in qemu_init third_party/qemu/system/vl.c:3322:21
     #6 0xaaaab9641c2c in main third_party/qemu/system/main.c:54:5
     #7 0xffffba320000 in __libc_start_main (/usr/grte/v5/lib64/libc.so.6+0x61000) (BuildId: 613d20d3b812b4c87fe9ebf8c4caae83)
     #8 0xaaaab934bd10 in _start /usr/grte/v5/debug-src/src/csu/../sysdeps/aarch64/start.S:92
   Uninitialized value was created by an allocation of 'key_in_cur.i' in the stack frame
     #0 0xaaaac1c7708c in keyval_parse_one third_party/qemu/util/keyval.c:190:5
     #1 0xaaaac1c7708c in keyval_parse_into third_party/qemu/util/keyval.c:530:13

SUMMARY: MemorySanitizer: use-of-uninitialized-value third_party/qemu/qobject/qdict.c:46:31 in tdb_hash Exiting

I don't have an easily shareable reproducer, but it's probably possible to whip one up.

     >> ---
     >>  util/keyval.c | 2 +-
     >>  1 file changed, 1 insertion(+), 1 deletion(-)
     >> diff --git a/util/keyval.c b/util/keyval.c
     >> index a70629a481..f33c64079d 100644
     >> --- a/util/keyval.c
     >> +++ b/util/keyval.c
     >> @@ -187,7 +187,7 @@ static const char *keyval_parse_one(QDict
    *qdict, const char *params,
     >>  {
     >>      const char *key, *key_end, *val_end, *s, *end;
     >>      size_t len;
     >> -    char key_in_cur[128];
     >> +    char key_in_cur[128] = {};

    Suspect overkill.  Would "" do?

It appears to resolve the complaint, yes.