Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: Move kunit_abort() call out of kunit_do_failed_assertion()

KUnit aborts the current thread when an assertion fails. Currently, this
is done conditionally as part of the kunit_do_failed_assertion()
function, but this hides the kunit_abort() call from the compiler
(particularly if it's in another module). This, in turn, can lead to
both suboptimal code generation (the compiler can't know if
kunit_do_failed_assertion() will return), and to static analysis tools
like smatch giving false positives.

Moving the kunit_abort() call into the macro should give the compiler
and tools a better chance at understanding what's going on. Doing so
requires exporting kunit_abort(), though it's recommended to continue to
use assertions in lieu of aborting directly.

In addition, kunit_abort() and kunit_do_failed_assertion() are renamed
to make it clear they they're intended for internal KUnit use, to:
__kunit_do_failed_assertion() and __kunit_abort()

Suggested-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Daniel Latypov <dlatypov@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

David Gow and committed by
Shuah Khan
26075518 c042030a

+16 -14
+12 -8
include/kunit/test.h
··· 482 482 */ 483 483 #define KUNIT_SUCCEED(test) do {} while (0) 484 484 485 - void kunit_do_failed_assertion(struct kunit *test, 485 + void __noreturn __kunit_abort(struct kunit *test); 486 + 487 + void __kunit_do_failed_assertion(struct kunit *test, 486 488 const struct kunit_loc *loc, 487 489 enum kunit_assert_type type, 488 490 const struct kunit_assert *assert, ··· 494 492 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ 495 493 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ 496 494 const struct assert_class __assertion = INITIALIZER; \ 497 - kunit_do_failed_assertion(test, \ 498 - &__loc, \ 499 - assert_type, \ 500 - &__assertion.assert, \ 501 - assert_format, \ 502 - fmt, \ 503 - ##__VA_ARGS__); \ 495 + __kunit_do_failed_assertion(test, \ 496 + &__loc, \ 497 + assert_type, \ 498 + &__assertion.assert, \ 499 + assert_format, \ 500 + fmt, \ 501 + ##__VA_ARGS__); \ 502 + if (assert_type == KUNIT_ASSERTION) \ 503 + __kunit_abort(test); \ 504 504 } while (0) 505 505 506 506
+4 -6
lib/kunit/test.c
··· 323 323 string_stream_destroy(stream); 324 324 } 325 325 326 - static void __noreturn kunit_abort(struct kunit *test) 326 + void __noreturn __kunit_abort(struct kunit *test) 327 327 { 328 328 kunit_try_catch_throw(&test->try_catch); /* Does not return. */ 329 329 ··· 335 335 */ 336 336 WARN_ONCE(true, "Throw could not abort from test!\n"); 337 337 } 338 + EXPORT_SYMBOL_GPL(__kunit_abort); 338 339 339 - void kunit_do_failed_assertion(struct kunit *test, 340 + void __kunit_do_failed_assertion(struct kunit *test, 340 341 const struct kunit_loc *loc, 341 342 enum kunit_assert_type type, 342 343 const struct kunit_assert *assert, ··· 354 353 kunit_fail(test, loc, type, assert, assert_format, &message); 355 354 356 355 va_end(args); 357 - 358 - if (type == KUNIT_ASSERTION) 359 - kunit_abort(test); 360 356 } 361 - EXPORT_SYMBOL_GPL(kunit_do_failed_assertion); 357 + EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion); 362 358 363 359 void kunit_init_test(struct kunit *test, const char *name, char *log) 364 360 {