* [PATCH] KVM: selftests: Make guest_snprintf() a macro and use it as appropriate
@ 2026-09-30 0:13 Sean Christopherson
2026-09-30 0:22 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2026-09-30 0:13 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Sean Christopherson, kvm, linux-kernel, Gokul K
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().
Reported-by: Gokul K <gokul02k@gmail.com>
Closes: https://lore.kernel.org/all/20260918113631.634233-4-gokul02k@gmail.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
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(-)
diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index 3a432499b338..9ea5b8906dd4 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -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)));
diff --git a/tools/testing/selftests/kvm/lib/guest_sprintf.c b/tools/testing/selftests/kvm/lib/guest_sprintf.c
index 7a33965349a7..6caebb456f3d 100644
--- a/tools/testing/selftests/kvm/lib/guest_sprintf.c
+++ b/tools/testing/selftests/kvm/lib/guest_sprintf.c
@@ -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;
-}
diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c
index 029ce21f9f2f..e9df71197367 100644
--- a/tools/testing/selftests/kvm/lib/ucall_common.c
+++ b/tools/testing/selftests/kvm/lib/ucall_common.c
@@ -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
--
2.56.0.rc1.315.gc6ed9934b7-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: selftests: Make guest_snprintf() a macro and use it as appropriate
2026-09-30 0:13 [PATCH] KVM: selftests: Make guest_snprintf() a macro and use it as appropriate Sean Christopherson
@ 2026-09-30 0:22 ` sashiko-bot
2026-09-30 1:18 ` Sean Christopherson
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-09-30 0:22 UTC (permalink / raw)
To: Sean Christopherson; +Cc: kvm
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 <seanjc@google.com>
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.
Closes: https://lore.kernel.org/all/20260918113631.634233-4-gokul02k@gmail.com
> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> index a6a3e16578959..817d4d5ca7cc7 100644
> --- a/tools/testing/selftests/kvm/include/test_util.h
> +++ b/tools/testing/selftests/kvm/include/test_util.h
> @@ -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.
> + \
> + 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).
> + len = guest_vsnprintf(__buf, __n, __fmt, va); \
> + va_end(va); \
> + len; \
> +})
>
> char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260930001341.3171558-1-seanjc@google.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] KVM: selftests: Make guest_snprintf() a macro and use it as appropriate
2026-09-30 0:22 ` sashiko-bot
@ 2026-09-30 1:18 ` Sean Christopherson
0 siblings, 0 replies; 3+ messages in thread
From: Sean Christopherson @ 2026-09-30 1:18 UTC (permalink / raw)
To: sashiko-reviews; +Cc: kvm
On Wed, Sep 30, 2026, sashiko-bot@kernel.org wrote:
> Closes: https://lore.kernel.org/all/20260918113631.634233-4-gokul02k@gmail.com
>
> > diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> > index a6a3e16578959..817d4d5ca7cc7 100644
> > --- a/tools/testing/selftests/kvm/include/test_util.h
> > +++ b/tools/testing/selftests/kvm/include/test_util.h
> > @@ -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.
>
> > + \
> > + 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?
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-30 1:18 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-30 0:13 [PATCH] KVM: selftests: Make guest_snprintf() a macro and use it as appropriate Sean Christopherson
2026-09-30 0:22 ` sashiko-bot
2026-09-30 1:18 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox