On Mon, Apr 24, 2023, Aaron Lewis wrote: > diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h > index a6e9f215ce70..45cb0dd41412 100644 > --- a/tools/testing/selftests/kvm/include/test_util.h > +++ b/tools/testing/selftests/kvm/include/test_util.h > @@ -186,4 +186,7 @@ static inline uint32_t atoi_non_negative(const char *name, const char *num_str) > return num; > } > > +int kvm_vsnprintf(char *buf, int n, const char *fmt, va_list args); > +int kvm_snprintf(char *buf, int n, const char *fmt, ...); > + > #endif /* SELFTEST_KVM_TEST_UTIL_H */ > diff --git a/tools/testing/selftests/kvm/lib/kvm_sprintf.c b/tools/testing/selftests/kvm/lib/kvm_sprintf.c > new file mode 100644 > index 000000000000..db369e00a6fc > --- /dev/null > +++ b/tools/testing/selftests/kvm/lib/kvm_sprintf.c > @@ -0,0 +1,313 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +#include "test_util.h" > + > +#define ASSIGN_AND_INC_SAFE(str, end, v) \ > +({ \ > + if (str < end) \ This should assert instead of hiding the error and forcing the caller to detect the problem, which may or may not actually happen. The easiest thing is to just thie guest only, e.g. name the helpers guest_*snprintf() and then do #define APPEND_BUFFER_SAFE(str, end, v) \ do { \ GUEST_ASSERT(str < end); \ *str++ = (v); \ } while (0) I doubt there will be a use case for using the custom snprintf in a helper that is common to guest and host, and if someone does need/want that functionality, they can add a global flag to track whether or not selftests is running guest or host code. And I would deliberately use GUEST_ASSERT() instead of a GUEST_UCALL_FAILED. KVM carves out enough space for KVM_MAX_VCPUS concurrent/nested ucalls, so for the vast majority of tests using GUEST_ASSERT() will succeed and be useful. And if some test exhausts the ucall pool, it'll end up with GUEST_UCALL_FAILED anyways.