~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/kunit/static_stub.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 /*
  3  * KUnit function redirection (static stubbing) API.
  4  *
  5  * Copyright (C) 2022, Google LLC.
  6  * Author: David Gow <davidgow@google.com>
  7  */
  8 #ifndef _KUNIT_STATIC_STUB_H
  9 #define _KUNIT_STATIC_STUB_H
 10 
 11 #if !IS_ENABLED(CONFIG_KUNIT)
 12 
 13 /* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */
 14 #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0)
 15 
 16 #else
 17 
 18 #include <kunit/test.h>
 19 #include <kunit/test-bug.h>
 20 
 21 #include <linux/compiler.h> /* for {un,}likely() */
 22 #include <linux/sched.h> /* for task_struct */
 23 
 24 
 25 /**
 26  * KUNIT_STATIC_STUB_REDIRECT() - call a replacement 'static stub' if one exists
 27  * @real_fn_name: The name of this function (as an identifier, not a string)
 28  * @args: All of the arguments passed to this function
 29  *
 30  * This is a function prologue which is used to allow calls to the current
 31  * function to be redirected by a KUnit test. KUnit tests can call
 32  * kunit_activate_static_stub() to pass a replacement function in. The
 33  * replacement function will be called by KUNIT_STATIC_STUB_REDIRECT(), which
 34  * will then return from the function. If the caller is not in a KUnit context,
 35  * the function will continue execution as normal.
 36  *
 37  * Example:
 38  *
 39  * .. code-block:: c
 40  *
 41  *      int real_func(int n)
 42  *      {
 43  *              KUNIT_STATIC_STUB_REDIRECT(real_func, n);
 44  *              return 0;
 45  *      }
 46  *
 47  *      int replacement_func(int n)
 48  *      {
 49  *              return 42;
 50  *      }
 51  *
 52  *      void example_test(struct kunit *test)
 53  *      {
 54  *              kunit_activate_static_stub(test, real_func, replacement_func);
 55  *              KUNIT_EXPECT_EQ(test, real_func(1), 42);
 56  *      }
 57  *
 58  */
 59 #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...)               \
 60 do {                                                                    \
 61         typeof(&real_fn_name) replacement;                              \
 62         struct kunit *current_test = kunit_get_current_test();          \
 63                                                                         \
 64         if (likely(!current_test))                                      \
 65                 break;                                                  \
 66                                                                         \
 67         replacement = kunit_hooks.get_static_stub_address(current_test, \
 68                                                         &real_fn_name); \
 69                                                                         \
 70         if (unlikely(replacement))                                      \
 71                 return replacement(args);                               \
 72 } while (0)
 73 
 74 /* Helper function for kunit_activate_static_stub(). The macro does
 75  * typechecking, so use it instead.
 76  */
 77 void __kunit_activate_static_stub(struct kunit *test,
 78                                   void *real_fn_addr,
 79                                   void *replacement_addr);
 80 
 81 /**
 82  * kunit_activate_static_stub() - replace a function using static stubs.
 83  * @test: A pointer to the 'struct kunit' test context for the current test.
 84  * @real_fn_addr: The address of the function to replace.
 85  * @replacement_addr: The address of the function to replace it with.
 86  *
 87  * When activated, calls to real_fn_addr from within this test (even if called
 88  * indirectly) will instead call replacement_addr. The function pointed to by
 89  * real_fn_addr must begin with the static stub prologue in
 90  * KUNIT_STATIC_STUB_REDIRECT() for this to work. real_fn_addr and
 91  * replacement_addr must have the same type.
 92  *
 93  * The redirection can be disabled again with kunit_deactivate_static_stub().
 94  */
 95 #define kunit_activate_static_stub(test, real_fn_addr, replacement_addr) do {   \
 96         typecheck_fn(typeof(&replacement_addr), real_fn_addr);                  \
 97         __kunit_activate_static_stub(test, real_fn_addr, replacement_addr);     \
 98 } while (0)
 99 
100 
101 /**
102  * kunit_deactivate_static_stub() - disable a function redirection
103  * @test: A pointer to the 'struct kunit' test context for the current test.
104  * @real_fn_addr: The address of the function to no-longer redirect
105  *
106  * Deactivates a redirection configured with kunit_activate_static_stub(). After
107  * this function returns, calls to real_fn_addr() will execute the original
108  * real_fn, not any previously-configured replacement.
109  */
110 void kunit_deactivate_static_stub(struct kunit *test, void *real_fn_addr);
111 
112 #endif
113 #endif
114 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php