Skip to content

Commit e047c5e

Browse files
sulixshuahkh
authored andcommitted
kunit: Expose 'static stub' API to redirect functions
Add a simple way of redirecting calls to functions by including a special prologue in the "real" function which checks to see if the replacement function should be called (and, if so, calls it). To redirect calls to a function, make the first (non-declaration) line of the function: KUNIT_STATIC_STUB_REDIRECT(function_name, [function arguments]); (This will compile away to nothing if KUnit is not enabled, otherwise it will check if a redirection is active, call the replacement function, and return. This check is protected by a static branch, so has very little overhead when there are no KUnit tests running.) Calls to the real function can be redirected to a replacement using: kunit_activate_static_stub(test, real_fn, replacement_fn); The redirection will only affect calls made from within the kthread of the current test, and will be automatically disabled when the test completes. It can also be manually disabled with kunit_deactivate_static_stub(). The 'example' KUnit test suite has a more complete example. Co-developed-by: Daniel Latypov <[email protected]> Signed-off-by: Daniel Latypov <[email protected]> Signed-off-by: David Gow <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 7170b7e commit e047c5e

File tree

6 files changed

+278
-0
lines changed

6 files changed

+278
-0
lines changed

include/kunit/static_stub.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 <[email protected]>
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_TRIGGER_STATIC_STUB(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_TRIGGER_STATIC_STUB(), 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_TRIGGER_STATIC_STUB() 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(&real_fn_addr), replacement_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

include/kunit/test-bug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ DECLARE_STATIC_KEY_FALSE(kunit_running);
2020
/* Hooks table: a table of function pointers filled in when kunit loads */
2121
extern struct kunit_hooks_table {
2222
__printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...);
23+
void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr);
2324
} kunit_hooks;
2425

2526
/**

lib/kunit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ obj-$(CONFIG_KUNIT) += kunit.o
22

33
kunit-objs += test.o \
44
resource.o \
5+
static_stub.o \
56
string-stream.o \
67
assert.o \
78
try-catch.o \

lib/kunit/hooks-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
/* List of declarations. */
1818
void __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...);
19+
void *__kunit_get_static_stub_address_impl(struct kunit *test, void *real_fn_addr);
1920

2021
/* Code to set all of the function pointers. */
2122
static inline void kunit_install_hooks(void)
2223
{
2324
/* Install the KUnit hook functions. */
2425
kunit_hooks.fail_current_test = __kunit_fail_current_test_impl;
26+
kunit_hooks.get_static_stub_address = __kunit_get_static_stub_address_impl;
2527
}
2628

2729
#endif /* _KUNIT_HOOKS_IMPL_H */

lib/kunit/kunit-example-test.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <kunit/test.h>
10+
#include <kunit/static_stub.h>
1011

1112
/*
1213
* This is the most fundamental element of KUnit, the test case. A test case
@@ -130,6 +131,42 @@ static void example_all_expect_macros_test(struct kunit *test)
130131
KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
131132
}
132133

134+
/* This is a function we'll replace with static stubs. */
135+
static int add_one(int i)
136+
{
137+
/* This will trigger the stub if active. */
138+
KUNIT_STATIC_STUB_REDIRECT(add_one, i);
139+
140+
return i + 1;
141+
}
142+
143+
/* This is used as a replacement for the above function. */
144+
static int subtract_one(int i)
145+
{
146+
/* We don't need to trigger the stub from the replacement. */
147+
148+
return i - 1;
149+
}
150+
151+
/*
152+
* This test shows the use of static stubs.
153+
*/
154+
static void example_static_stub_test(struct kunit *test)
155+
{
156+
/* By default, function is not stubbed. */
157+
KUNIT_EXPECT_EQ(test, add_one(1), 2);
158+
159+
/* Replace add_one() with subtract_one(). */
160+
kunit_activate_static_stub(test, add_one, subtract_one);
161+
162+
/* add_one() is now replaced. */
163+
KUNIT_EXPECT_EQ(test, add_one(1), 0);
164+
165+
/* Return add_one() to normal. */
166+
kunit_deactivate_static_stub(test, add_one);
167+
KUNIT_EXPECT_EQ(test, add_one(1), 2);
168+
}
169+
133170
/*
134171
* Here we make a list of all the test cases we want to add to the test suite
135172
* below.
@@ -145,6 +182,7 @@ static struct kunit_case example_test_cases[] = {
145182
KUNIT_CASE(example_skip_test),
146183
KUNIT_CASE(example_mark_skipped_test),
147184
KUNIT_CASE(example_all_expect_macros_test),
185+
KUNIT_CASE(example_static_stub_test),
148186
{}
149187
};
150188

lib/kunit/static_stub.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 <[email protected]>
7+
*/
8+
9+
#include <kunit/test.h>
10+
#include <kunit/static_stub.h>
11+
#include "hooks-impl.h"
12+
13+
14+
/* Context for a static stub. This is stored in the resource data. */
15+
struct kunit_static_stub_ctx {
16+
void *real_fn_addr;
17+
void *replacement_addr;
18+
};
19+
20+
static void __kunit_static_stub_resource_free(struct kunit_resource *res)
21+
{
22+
kfree(res->data);
23+
}
24+
25+
/* Matching function for kunit_find_resource(). match_data is real_fn_addr. */
26+
static bool __kunit_static_stub_resource_match(struct kunit *test,
27+
struct kunit_resource *res,
28+
void *match_real_fn_addr)
29+
{
30+
/* This pointer is only valid if res is a static stub resource. */
31+
struct kunit_static_stub_ctx *ctx = res->data;
32+
33+
/* Make sure the resource is a static stub resource. */
34+
if (res->free != &__kunit_static_stub_resource_free)
35+
return false;
36+
37+
return ctx->real_fn_addr == match_real_fn_addr;
38+
}
39+
40+
/* Hook to return the address of the replacement function. */
41+
void *__kunit_get_static_stub_address_impl(struct kunit *test, void *real_fn_addr)
42+
{
43+
struct kunit_resource *res;
44+
struct kunit_static_stub_ctx *ctx;
45+
void *replacement_addr;
46+
47+
res = kunit_find_resource(test,
48+
__kunit_static_stub_resource_match,
49+
real_fn_addr);
50+
51+
if (!res)
52+
return NULL;
53+
54+
ctx = res->data;
55+
replacement_addr = ctx->replacement_addr;
56+
kunit_put_resource(res);
57+
return replacement_addr;
58+
}
59+
60+
void kunit_deactivate_static_stub(struct kunit *test, void *real_fn_addr)
61+
{
62+
struct kunit_resource *res;
63+
64+
KUNIT_ASSERT_PTR_NE_MSG(test, real_fn_addr, NULL,
65+
"Tried to deactivate a NULL stub.");
66+
67+
/* Look up the existing stub for this function. */
68+
res = kunit_find_resource(test,
69+
__kunit_static_stub_resource_match,
70+
real_fn_addr);
71+
72+
/* Error out if the stub doesn't exist. */
73+
KUNIT_ASSERT_PTR_NE_MSG(test, res, NULL,
74+
"Tried to deactivate a nonexistent stub.");
75+
76+
/* Free the stub. We 'put' twice, as we got a reference
77+
* from kunit_find_resource()
78+
*/
79+
kunit_remove_resource(test, res);
80+
kunit_put_resource(res);
81+
}
82+
EXPORT_SYMBOL_GPL(kunit_deactivate_static_stub);
83+
84+
/* Helper function for kunit_activate_static_stub(). The macro does
85+
* typechecking, so use it instead.
86+
*/
87+
void __kunit_activate_static_stub(struct kunit *test,
88+
void *real_fn_addr,
89+
void *replacement_addr)
90+
{
91+
struct kunit_static_stub_ctx *ctx;
92+
struct kunit_resource *res;
93+
94+
KUNIT_ASSERT_PTR_NE_MSG(test, real_fn_addr, NULL,
95+
"Tried to activate a stub for function NULL");
96+
97+
/* If the replacement address is NULL, deactivate the stub. */
98+
if (!replacement_addr) {
99+
kunit_deactivate_static_stub(test, replacement_addr);
100+
return;
101+
}
102+
103+
/* Look up any existing stubs for this function, and replace them. */
104+
res = kunit_find_resource(test,
105+
__kunit_static_stub_resource_match,
106+
real_fn_addr);
107+
if (res) {
108+
ctx = res->data;
109+
ctx->replacement_addr = replacement_addr;
110+
111+
/* We got an extra reference from find_resource(), so put it. */
112+
kunit_put_resource(res);
113+
} else {
114+
ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
115+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
116+
ctx->real_fn_addr = real_fn_addr;
117+
ctx->replacement_addr = replacement_addr;
118+
res = kunit_alloc_resource(test, NULL,
119+
&__kunit_static_stub_resource_free,
120+
GFP_KERNEL, ctx);
121+
}
122+
}
123+
EXPORT_SYMBOL_GPL(__kunit_activate_static_stub);

0 commit comments

Comments
 (0)