linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kunit: take `kunit_assert` as `const`
@ 2022-05-02  9:36 Miguel Ojeda
  2022-05-02 19:43 ` Daniel Latypov
  2022-05-07  8:16 ` Kees Cook
  0 siblings, 2 replies; 7+ messages in thread
From: Miguel Ojeda @ 2022-05-02  9:36 UTC (permalink / raw)
  To: Brendan Higgins; +Cc: linux-kselftest, kunit-dev, linux-kernel

The `kunit_do_failed_assertion` function passes its
`struct kunit_assert` argument to `kunit_fail`. This one,
in turn, calls its `format` field passing the assert again
as a `const` pointer.

Therefore, the whole chain may be made `const`.

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 include/kunit/test.h | 2 +-
 lib/kunit/test.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index 00b9ff7783ab..2eff4f1beb42 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -774,7 +774,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
 void kunit_do_failed_assertion(struct kunit *test,
 			       const struct kunit_loc *loc,
 			       enum kunit_assert_type type,
-			       struct kunit_assert *assert,
+			       const struct kunit_assert *assert,
 			       const char *fmt, ...);
 
 #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 3bca3bf5c15b..b84aed09a009 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -241,7 +241,7 @@ static void kunit_print_string_stream(struct kunit *test,
 }
 
 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
-		       enum kunit_assert_type type, struct kunit_assert *assert,
+		       enum kunit_assert_type type, const struct kunit_assert *assert,
 		       const struct va_format *message)
 {
 	struct string_stream *stream;
@@ -281,7 +281,7 @@ static void __noreturn kunit_abort(struct kunit *test)
 void kunit_do_failed_assertion(struct kunit *test,
 			       const struct kunit_loc *loc,
 			       enum kunit_assert_type type,
-			       struct kunit_assert *assert,
+			       const struct kunit_assert *assert,
 			       const char *fmt, ...)
 {
 	va_list args;
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] kunit: take `kunit_assert` as `const`
  2022-05-02  9:36 [PATCH] kunit: take `kunit_assert` as `const` Miguel Ojeda
@ 2022-05-02 19:43 ` Daniel Latypov
  2022-05-04  8:09   ` Miguel Ojeda
  2022-05-07  8:16 ` Kees Cook
  1 sibling, 1 reply; 7+ messages in thread
From: Daniel Latypov @ 2022-05-02 19:43 UTC (permalink / raw)
  To: Miguel Ojeda; +Cc: Brendan Higgins, linux-kselftest, kunit-dev, linux-kernel

On Mon, May 2, 2022 at 4:36 AM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> The `kunit_do_failed_assertion` function passes its
> `struct kunit_assert` argument to `kunit_fail`. This one,
> in turn, calls its `format` field passing the assert again
> as a `const` pointer.
>
> Therefore, the whole chain may be made `const`.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Daniel Latypov <dlatypov@google.com>

Thanks for this, the code definitely should have been this way from the start.

I had wanted to make this change but mistakenly thought the format
func took it via non-const for some reason.
I must have misread it once and got it into my head that we were
leaving the door open for mutable child structs (which sounds like a
bad idea).

> ---
>  include/kunit/test.h | 2 +-
>  lib/kunit/test.c     | 4 ++--
>  2 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 00b9ff7783ab..2eff4f1beb42 100644
> --- a/include/kunit/test.h
> +++ b/include/kunit/test.h
> @@ -774,7 +774,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
>  void kunit_do_failed_assertion(struct kunit *test,
>                                const struct kunit_loc *loc,
>                                enum kunit_assert_type type,
> -                              struct kunit_assert *assert,
> +                              const struct kunit_assert *assert,
>                                const char *fmt, ...);
>
>  #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index 3bca3bf5c15b..b84aed09a009 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -241,7 +241,7 @@ static void kunit_print_string_stream(struct kunit *test,
>  }
>
>  static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
> -                      enum kunit_assert_type type, struct kunit_assert *assert,
> +                      enum kunit_assert_type type, const struct kunit_assert *assert,
>                        const struct va_format *message)
>  {
>         struct string_stream *stream;
> @@ -281,7 +281,7 @@ static void __noreturn kunit_abort(struct kunit *test)
>  void kunit_do_failed_assertion(struct kunit *test,
>                                const struct kunit_loc *loc,
>                                enum kunit_assert_type type,
> -                              struct kunit_assert *assert,
> +                              const struct kunit_assert *assert,
>                                const char *fmt, ...)
>  {
>         va_list args;
> --
> 2.35.3
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220502093625.GA23225%40kernel.org.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] kunit: take `kunit_assert` as `const`
  2022-05-02 19:43 ` Daniel Latypov
@ 2022-05-04  8:09   ` Miguel Ojeda
  2022-05-04 14:04     ` David Gow
  0 siblings, 1 reply; 7+ messages in thread
From: Miguel Ojeda @ 2022-05-04  8:09 UTC (permalink / raw)
  To: Daniel Latypov
  Cc: Miguel Ojeda, Brendan Higgins,
	open list:KERNEL SELFTEST FRAMEWORK, kunit-dev, linux-kernel

Hi Daniel,

On Mon, May 2, 2022 at 9:44 PM Daniel Latypov <dlatypov@google.com> wrote:
>
> Reviewed-by: Daniel Latypov <dlatypov@google.com>
>
> Thanks for this, the code definitely should have been this way from the start.
>
> I had wanted to make this change but mistakenly thought the format
> func took it via non-const for some reason.
> I must have misread it once and got it into my head that we were
> leaving the door open for mutable child structs (which sounds like a
> bad idea).

Thanks for reviewing it so quickly! Yeah, I was unsure too if there
was an external reason such as some future plan to use the mutability
as you mention or maybe some out-of-tree user was relying on it
already.

But I thought it would be best to make it stricter until it is
actually needed (if ever); or if there is an actual user for
mutability, it should be documented/noted in-tree.

It also simplifies a tiny bit a Rust-side call to
`kunit_do_failed_assertion` that I am using within generated Rust
documentation tests.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] kunit: take `kunit_assert` as `const`
  2022-05-04  8:09   ` Miguel Ojeda
@ 2022-05-04 14:04     ` David Gow
  2022-05-04 19:26       ` Miguel Ojeda
  0 siblings, 1 reply; 7+ messages in thread
From: David Gow @ 2022-05-04 14:04 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Daniel Latypov, Miguel Ojeda, Brendan Higgins,
	open list:KERNEL SELFTEST FRAMEWORK, KUnit Development,
	linux-kernel

On Wed, May 4, 2022 at 4:09 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> Hi Daniel,
>
> On Mon, May 2, 2022 at 9:44 PM Daniel Latypov <dlatypov@google.com> wrote:
> >
> > Reviewed-by: Daniel Latypov <dlatypov@google.com>
> >
> > Thanks for this, the code definitely should have been this way from the start.
> >
> > I had wanted to make this change but mistakenly thought the format
> > func took it via non-const for some reason.
> > I must have misread it once and got it into my head that we were
> > leaving the door open for mutable child structs (which sounds like a
> > bad idea).
>
> Thanks for reviewing it so quickly! Yeah, I was unsure too if there
> was an external reason such as some future plan to use the mutability
> as you mention or maybe some out-of-tree user was relying on it
> already.
>
> But I thought it would be best to make it stricter until it is
> actually needed (if ever); or if there is an actual user for
> mutability, it should be documented/noted in-tree.

I definitely agree here -- I can't recall any particular plan that
would require this to be non-const, and we can always change it back
if we really need to.

> It also simplifies a tiny bit a Rust-side call to
> `kunit_do_failed_assertion` that I am using within generated Rust
> documentation tests.

Very exciting! I assume that's the PR here:
https://github.com/Rust-for-Linux/linux/pull/757

Cheers,
-- David

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] kunit: take `kunit_assert` as `const`
  2022-05-04 14:04     ` David Gow
@ 2022-05-04 19:26       ` Miguel Ojeda
  2022-05-04 23:47         ` David Gow
  0 siblings, 1 reply; 7+ messages in thread
From: Miguel Ojeda @ 2022-05-04 19:26 UTC (permalink / raw)
  To: David Gow
  Cc: Daniel Latypov, Miguel Ojeda, Brendan Higgins,
	open list:KERNEL SELFTEST FRAMEWORK, KUnit Development,
	linux-kernel

Hi David,

On Wed, May 4, 2022 at 4:05 PM David Gow <davidgow@google.com> wrote:
>
> I definitely agree here -- I can't recall any particular plan that
> would require this to be non-const, and we can always change it back
> if we really need to.

That is good to know, thanks! Out-of-tree users can always be a surprise... :)

> Very exciting! I assume that's the PR here:
> https://github.com/Rust-for-Linux/linux/pull/757

Indeed! I hope you like it -- we are taking the documentation tests in
Rust (which are a very lightweight way of writing examples which
double as tests) and generating KUnit test cases on the fly. For the
moment it is just for the `kernel` crate, but the idea is to
generalize it for modules etc.

By the way, since you saw the PR... do you know if KUnit relies (or
will rely) on "stack-dumping" functions like `longjmp`?

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] kunit: take `kunit_assert` as `const`
  2022-05-04 19:26       ` Miguel Ojeda
@ 2022-05-04 23:47         ` David Gow
  0 siblings, 0 replies; 7+ messages in thread
From: David Gow @ 2022-05-04 23:47 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Daniel Latypov, Miguel Ojeda, Brendan Higgins,
	open list:KERNEL SELFTEST FRAMEWORK, KUnit Development,
	linux-kernel

On Thu, May 5, 2022 at 3:26 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> Hi David,
>
> On Wed, May 4, 2022 at 4:05 PM David Gow <davidgow@google.com> wrote:
> >
> > I definitely agree here -- I can't recall any particular plan that
> > would require this to be non-const, and we can always change it back
> > if we really need to.
>
> That is good to know, thanks! Out-of-tree users can always be a surprise... :)
>
> > Very exciting! I assume that's the PR here:
> > https://github.com/Rust-for-Linux/linux/pull/757
>
> Indeed! I hope you like it -- we are taking the documentation tests in
> Rust (which are a very lightweight way of writing examples which
> double as tests) and generating KUnit test cases on the fly. For the
> moment it is just for the `kernel` crate, but the idea is to
> generalize it for modules etc.
>
> By the way, since you saw the PR... do you know if KUnit relies (or
> will rely) on "stack-dumping" functions like `longjmp`?

I don't think so -- though there's no fundamental individual tests
couldn't use them if it made sense for them.

KUnit spins off a new kthread per test, and uses
kthread_complete_and_exit() to unwind when an assertion fails. See
lib/kunit/try-catch.c for the actual implementation. The only really
dodgy bit is the test timeout support, which attempts to stop a thread
with kthread_stop(), and IIRC has some problems.

Hope that helps!
-- David

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] kunit: take `kunit_assert` as `const`
  2022-05-02  9:36 [PATCH] kunit: take `kunit_assert` as `const` Miguel Ojeda
  2022-05-02 19:43 ` Daniel Latypov
@ 2022-05-07  8:16 ` Kees Cook
  1 sibling, 0 replies; 7+ messages in thread
From: Kees Cook @ 2022-05-07  8:16 UTC (permalink / raw)
  To: Miguel Ojeda; +Cc: Brendan Higgins, linux-kselftest, kunit-dev, linux-kernel

On Mon, May 02, 2022 at 11:36:25AM +0200, Miguel Ojeda wrote:
> The `kunit_do_failed_assertion` function passes its
> `struct kunit_assert` argument to `kunit_fail`. This one,
> in turn, calls its `format` field passing the assert again
> as a `const` pointer.
> 
> Therefore, the whole chain may be made `const`.
> 
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-05-07  8:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-02  9:36 [PATCH] kunit: take `kunit_assert` as `const` Miguel Ojeda
2022-05-02 19:43 ` Daniel Latypov
2022-05-04  8:09   ` Miguel Ojeda
2022-05-04 14:04     ` David Gow
2022-05-04 19:26       ` Miguel Ojeda
2022-05-04 23:47         ` David Gow
2022-05-07  8:16 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).