summaryrefslogtreecommitdiffstats
path: root/include/kunit/test.h
diff options
context:
space:
mode:
authorDaniel Latypov <dlatypov@google.com>2022-01-13 08:59:30 -0800
committerShuah Khan <skhan@linuxfoundation.org>2022-01-25 12:49:59 -0700
commit21957f90b28f6bc118c055e3e564d45f6e4df45d (patch)
tree0e952d2553288471e36211108252893bc92d4e35 /include/kunit/test.h
parentdd640d70874bd27fb081d444252677766321c32f (diff)
downloadlinux-stable-21957f90b28f6bc118c055e3e564d45f6e4df45d.tar.gz
linux-stable-21957f90b28f6bc118c055e3e564d45f6e4df45d.tar.bz2
linux-stable-21957f90b28f6bc118c055e3e564d45f6e4df45d.zip
kunit: split out part of kunit_assert into a static const
This is per Linus's suggestion in [1]. The issue there is that every KUNIT_EXPECT/KUNIT_ASSERT puts a kunit_assert object onto the stack. Normally we rely on compilers to elide this, but when that doesn't work out, this blows up the stack usage of kunit test functions. We can move some data off the stack by making it static. This change introduces a new `struct kunit_loc` to hold the file and line number and then just passing assert_type (EXPECT or ASSERT) as an argument. In [1], it was suggested to also move out the format string as well, but users could theoretically craft a format string at runtime, so we can't. This change leaves a copy of `assert_type` in kunit_assert for now because cleaning up all the macros to not pass it around is a bit more involved. Here's an example of the expanded code for KUNIT_FAIL(): if (__builtin_expect(!!(!(false)), 0)) { static const struct kunit_loc loc = { .file = ... }; struct kunit_fail_assert __assertion = { .assert = { .type ... }; kunit_do_failed_assertion(test, &loc, KUNIT_EXPECTATION, &__assertion.assert, ...); }; [1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ Signed-off-by: Daniel Latypov <dlatypov@google.com> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Reviewed-by: David Gow <davidgow@google.com> Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Diffstat (limited to 'include/kunit/test.h')
-rw-r--r--include/kunit/test.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/include/kunit/test.h b/include/kunit/test.h
index 25ea3bce6663..7b752175e614 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -772,13 +772,18 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
#define KUNIT_SUCCEED(test) do {} while (0)
void kunit_do_failed_assertion(struct kunit *test,
+ const struct kunit_loc *loc,
+ enum kunit_assert_type type,
struct kunit_assert *assert,
const char *fmt, ...);
-#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
+#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
if (unlikely(!(pass))) { \
+ static const struct kunit_loc loc = KUNIT_CURRENT_LOC; \
struct assert_class __assertion = INITIALIZER; \
kunit_do_failed_assertion(test, \
+ &loc, \
+ assert_type, \
&__assertion.assert, \
fmt, \
##__VA_ARGS__); \
@@ -788,6 +793,7 @@ void kunit_do_failed_assertion(struct kunit *test,
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
KUNIT_ASSERTION(test, \
+ assert_type, \
false, \
kunit_fail_assert, \
KUNIT_INIT_FAIL_ASSERT_STRUCT(assert_type), \
@@ -818,6 +824,7 @@ void kunit_do_failed_assertion(struct kunit *test,
fmt, \
...) \
KUNIT_ASSERTION(test, \
+ assert_type, \
!!(condition) == !!expected_true, \
kunit_unary_assert, \
KUNIT_INIT_UNARY_ASSERT_STRUCT(assert_type, \
@@ -876,6 +883,7 @@ do { \
typeof(right) __right = (right); \
\
KUNIT_ASSERTION(test, \
+ assert_type, \
__left op __right, \
assert_class, \
ASSERT_CLASS_INIT(assert_type, \
@@ -1230,6 +1238,7 @@ do { \
const char *__right = (right); \
\
KUNIT_ASSERTION(test, \
+ assert_type, \
strcmp(__left, __right) op 0, \
kunit_binary_str_assert, \
KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(assert_type, \
@@ -1289,6 +1298,7 @@ do { \
typeof(ptr) __ptr = (ptr); \
\
KUNIT_ASSERTION(test, \
+ assert_type, \
!IS_ERR_OR_NULL(__ptr), \
kunit_ptr_not_err_assert, \
KUNIT_INIT_PTR_NOT_ERR_STRUCT(assert_type, \