1 // SPDX-License-Identifier: GPL-2.0 1 2 /* 3 * KUnit function redirection (static stubbing 4 * 5 * Copyright (C) 2022, Google LLC. 6 * Author: David Gow <davidgow@google.com> 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 i 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( 21 { 22 kfree(res->data); 23 } 24 25 /* Matching function for kunit_find_resource() 26 static bool __kunit_static_stub_resource_match 27 28 29 { 30 /* This pointer is only valid if res i 31 struct kunit_static_stub_ctx *ctx = re 32 33 /* Make sure the resource is a static 34 if (res->free != &__kunit_static_stub_ 35 return false; 36 37 return ctx->real_fn_addr == match_real 38 } 39 40 /* Hook to return the address of the replaceme 41 void *__kunit_get_static_stub_address_impl(str 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_stat 49 real_fn_addr 50 51 if (!res) 52 return NULL; 53 54 ctx = res->data; 55 replacement_addr = ctx->replacement_ad 56 kunit_put_resource(res); 57 return replacement_addr; 58 } 59 60 void kunit_deactivate_static_stub(struct kunit 61 { 62 struct kunit_resource *res; 63 64 KUNIT_ASSERT_PTR_NE_MSG(test, real_fn_ 65 "Tried to deac 66 67 /* Look up the existing stub for this 68 res = kunit_find_resource(test, 69 __kunit_stat 70 real_fn_addr 71 72 /* Error out if the stub doesn't exist 73 KUNIT_ASSERT_PTR_NE_MSG(test, res, NUL 74 "Tried to deac 75 76 /* Free the stub. We 'put' twice, as w 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_s 85 * typechecking, so use it instead. 86 */ 87 void __kunit_activate_static_stub(struct kunit 88 void *real_f 89 void *replac 90 { 91 struct kunit_static_stub_ctx *ctx; 92 struct kunit_resource *res; 93 94 KUNIT_ASSERT_PTR_NE_MSG(test, real_fn_ 95 "Tried to acti 96 97 /* If the replacement address is NULL, 98 if (!replacement_addr) { 99 kunit_deactivate_static_stub(t 100 return; 101 } 102 103 /* Look up any existing stubs for this 104 res = kunit_find_resource(test, 105 __kunit_stat 106 real_fn_addr 107 if (res) { 108 ctx = res->data; 109 ctx->replacement_addr = replac 110 111 /* We got an extra reference f 112 kunit_put_resource(res); 113 } else { 114 ctx = kmalloc(sizeof(*ctx), GF 115 KUNIT_ASSERT_NOT_ERR_OR_NULL(t 116 ctx->real_fn_addr = real_fn_ad 117 ctx->replacement_addr = replac 118 res = kunit_alloc_resource(tes 119 &__kunit_ 120 GFP_KERNE 121 } 122 } 123 EXPORT_SYMBOL_GPL(__kunit_activate_static_stub 124
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.