KVM: selftests: Make guest_snprintf() a macro and use it as appropriate
Sean Christopherson
reviewerPATCH 1/1 KVM: selftests: Make guest_snprintf() a macro and use it as appropriate
Turn guest_snprintf() into a macro so that it can be used by variadic functions, and use it in the ucall APIs instead of open coding the VA goo.
Note, using a macro drops the __printf() validation, but that's a non-issue because in practice the caller will have its own validation, as is the case with both ucall_assert() and ucall_fmt().
--- tools/testing/selftests/kvm/include/test_util.h | 12 +++++++++++- tools/testing/selftests/kvm/lib/guest_sprintf.c | 12 ------------ tools/testing/selftests/kvm/lib/ucall_common.c | 10 ++-------- 3 files changed, 13 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +11 -1
@@ -238,7 +238,17 @@ static inline u32 atoi_non_negative(const char *name, const char *num_str) }int guest_vsnprintf (char *buf,int n,const char *fmt,va_list args); -__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...); + +#define guest_snprintf (__buf, __n, __fmt) \ +({ \ + va_list va; \ + int len; \ + \ + va_start(va, __fmt); \ + len = guest_vsnprintf(__buf, __n, __fmt, va); \ + va_end(va); \ + len; \ +})char *strdup_printf (const char *fmt, ...) __attribute__((format (printf,1 ,2 ),nonnull (1 )));
tools/testing/selftests/kvm/lib/guest_sprintf.c +0 -12
@@ -300,15 +300,3 @@ int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args) *str ='\0' ;return str - buf; } - -int guest_snprintf (char *buf,int n,const char *fmt, ...) -{ -va_list va; -int len; - -va_start (va, fmt); - len =guest_vsnprintf (buf, n, fmt, va); -va_end (va); - -return len; -}
tools/testing/selftests/kvm/lib/ucall_common.c +2 -8
@@ -83,7 +83,6 @@ void ucall_assert(u64 cmd, const char *exp, const char *file, unsigned int line, const char *fmt, ...) { struct ucall *uc; -va_list va; uc =ucall_alloc (); uc->cmd = cmd;
@@ -92,9 +91,7 @@ void ucall_assert(u64 cmd, const char *exp, const char *file,WRITE_ONCE (uc->args[GUEST_FILE ], (u64)(file));WRITE_ONCE (uc->args[GUEST_LINE ], line); -va_start (va, fmt); -guest_vsnprintf (uc->buffer,UCALL_BUFFER_LEN , fmt, va); -va_end (va); +guest_snprintf (uc->buffer,UCALL_BUFFER_LEN , fmt);ucall_arch_do_ucall ((gva_t )uc->hva);
@@ -104,14 +101,11 @@ void ucall_assert(u64 cmd, const char *exp, const char *file,void ucall_fmt (u64 cmd,const char *fmt, ...) {struct ucall *uc; -va_list va; uc =ucall_alloc (); uc->cmd = cmd; -va_start (va, fmt); -guest_vsnprintf (uc->buffer,UCALL_BUFFER_LEN , fmt, va); -va_end (va); +guest_snprintf (uc->buffer,UCALL_BUFFER_LEN , fmt);ucall_arch_do_ucall ((gva_t )uc->hva);
base-commit: a0bc8e1d7a82bb143b8f8f44ae3a01938f37c2ea
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests/kvm: guest_snprintf() macro variables shadow caller arguments - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/kvm: guest_snprintf() macro variables shadow caller arguments
- [Medium] selftests/kvm: guest_snprintf() implicitly captures caller variadic args --
commit fab1360854d5b8141536eb1f70a7ed77983d6dcd
Author: Sean Christopherson <[email protected]>
KVM: selftests: Make guest_snprintf() a macro and use it as appropriate
This commit turns guest_snprintf() into a macro to allow its use by variadic functions. It updates the ucall APIs to use this macro instead of open coding the variable argument handling.
Quoted 5 lines
tools/testing/selftests/kvm/include/test_util.h +0
-0
tools/testing/selftests/kvm/include/test_util.h static inline u32 atoi_non_negative(const char *name, const char *num_str) · line 245
Quoted 9 lines
tools/testing/selftests/kvm/include/test_util.h +5
-1@@ -238,7 +238,17 @@ static inline u32 atoi_non_negative(const char *name, const char *num_str) }int guest_vsnprintf (char *buf,int n,const char *fmt,va_list args); -__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...); + +#define guest_snprintf (__buf, __n, __fmt) \ +({ \ + va_list va; \ + int len; \
[Severity: Medium]
Does this macro shadow caller arguments? If a caller passes an argument named len to guest_snprintf(), it will be shadowed by the uninitialized local len due to C scoping rules for statement expressions. The inner len is then passed to guest_vsnprintf(), which could result in a random or uninitialized buffer size.
tools/testing/selftests/kvm/include/test_util.h static inline u32 atoi_non_negative(const char *name, const char *num_str) · line 247
Quoted 2 lines
tools/testing/selftests/kvm/include/test_util.h +2
-0+ \ +va_start (va, __fmt); \
[Severity: Medium]
Does this implicitly capture the caller's variadic arguments?
The macro internally invokes va_start(va, __fmt), which couples it to the caller's stack frame. If this macro is used in a non-variadic function, or if __fmt is not the rightmost named argument of the calling function, could va_start cause undefined behavior?
Additionally, this restricts the macro to exactly 3 arguments, preventing standard snprintf usage like guest_snprintf(buf, size, "%d", 5).
Quoted 6 lines
tools/testing/selftests/kvm/include/test_util.h +4
-0+ len =guest_vsnprintf (__buf, __n, __fmt, va); \ +va_end (va); \ + len; \ +})char *strdup_printf (const char *fmt, ...) __attribute__((format (printf,1 ,2 ),nonnull (1 )));
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
reviewerQuoted 30 lines · On Wed, Sep 30, 2026, [email protected] wrote:
Quoted 5 lines
tools/testing/selftests/kvm/include/test_util.h +0
-0tools/testing/selftests/kvm/include/test_util.h static inline u32 atoi_non_negative(const char *name, const char *num_str) · line 245
Quoted 9 lines
tools/testing/selftests/kvm/include/test_util.h +5
-1@@ -238,7 +238,17 @@ static inline u32 atoi_non_negative(const char *name, const char *num_str) }int guest_vsnprintf (char *buf,int n,const char *fmt,va_list args); -__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...); + +#define guest_snprintf (__buf, __n, __fmt) \ +({ \ + va_list va; \ + int len; \[Severity: Medium]
Does this macro shadow caller arguments? If a caller passes an argument named len toguest_snprintf(), it will be shadowed by the uninitialized local len due to C scoping rules for statement expressions. The inner len is then passed toguest_vsnprintf(), which could result in a random or uninitialized buffer size.tools/testing/selftests/kvm/include/test_util.h static inline u32 atoi_non_negative(const char *name, const char *num_str) · line 247
Quoted 2 lines
tools/testing/selftests/kvm/include/test_util.h +2
-0+ \ +va_start (va, __fmt); \[Severity: Medium]
Does this implicitly capture the caller's variadic arguments?
Quoted 4 lines
The macro internally invokes
va_start(va, __fmt), which couples it to the caller's stack frame. If this macro is used in a non-variadic function, or if__fmtis not the rightmost named argument of the calling function, couldva_startcause undefined behavior?
Ugh, and now I remember why we ended up with duplicate code. Lame. I'll drop this and grab Gokul's original patch. As much as I want to keep guest_snprintf() around "just in case", in practice I highly doubt we'll ever use it.