Skip to content

Commit

Permalink
WIP: (squash) livepatch/selftests: Add __asm__ symbol renaming examples
Browse files Browse the repository at this point in the history
Note: squash with ("livepatch/selftests: add klp-convert")

GCC can rename symbols like static data and optimized functions, adding
a suffix that includes illegal C characters.  Extend the klp-convert
examples to demonstrate how to use __asm__ renaming from C code to
create klp-relocations to such renamed symbols.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
  • Loading branch information
joe-lawrence committed Oct 27, 2021
1 parent 22638d6 commit ce26109
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/livepatch/test_klp_convert1.c
Expand Up @@ -15,6 +15,9 @@ extern char homonym_string[];
extern const char *get_homonym_string(void);
extern const char *test_klp_get_driver_name(void);

extern char klp_string_a[] __asm__("klp_string.12345");
extern char klp_string_b[] __asm__("klp_string.67890");

void print_saved_command_line(void)
{
pr_info("saved_command_line, 0: %s\n", saved_command_line);
Expand All @@ -32,13 +35,20 @@ void print_homonym_string(void)
pr_info("get_homonym_string(), 1: %s\n", get_homonym_string());
}

void print_static_strings(void)
{
pr_info("klp_string.12345 = %s\n", klp_string_a);
pr_info("klp_string.67890 = %s\n", klp_string_b);
}

/* provide a sysfs handle to invoke debug functions */
static int print_debug = 0;
static int print_debug_set(const char *val, const struct kernel_param *kp)
{
print_saved_command_line();
print_driver_name();
print_homonym_string();
print_static_strings();

return 0;
}
Expand Down Expand Up @@ -74,6 +84,7 @@ KLP_MODULE_RELOC(test_klp_convert_mod) test_klp_convert_mod_relocs_a[] = {
KLP_SYMPOS(homonym_string, 1),
KLP_SYMPOS(get_homonym_string, 1),
KLP_SYMPOS(test_klp_get_driver_name, 0),
KLP_SYMPOS(klp_string_b, 1),
};

static struct klp_func funcs[] = {
Expand Down
6 changes: 6 additions & 0 deletions lib/livepatch/test_klp_convert_mod_a.c
Expand Up @@ -20,6 +20,12 @@ __used static const char *get_homonym_string(void)
return homonym_string;
}

__used static void static_string_function(void)
{
__used static char klp_string[] __asm__("klp_string.12345") =
__FILE__ " static string";
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
MODULE_DESCRIPTION("Livepatch test: klp-convert module");
6 changes: 6 additions & 0 deletions lib/livepatch/test_klp_convert_mod_b.c
Expand Up @@ -11,3 +11,9 @@ __used static char *get_homonym_string(void)
{
return homonym_string;
}

__used static void static_string_function(void)
{
__used static char klp_string[] __asm__("klp_string.67890") =
__FILE__ " static string";
}
4 changes: 4 additions & 0 deletions tools/testing/selftests/livepatch/test-livepatch.sh
Expand Up @@ -200,6 +200,8 @@ $MOD_KLP_CONVERT1: driver_name, 0: $MOD_KLP_CONVERT_MOD
$MOD_KLP_CONVERT1: test_klp_get_driver_name(), 0: $MOD_KLP_CONVERT_MOD
$MOD_KLP_CONVERT1: homonym_string, 1: homonym string A
$MOD_KLP_CONVERT1: get_homonym_string(), 1: homonym string A
test_klp_convert1: klp_string.12345 = lib/livepatch/test_klp_convert_mod_a.c static string
test_klp_convert1: klp_string.67890 = lib/livepatch/test_klp_convert_mod_b.c static string
% echo 0 > /sys/kernel/livepatch/$MOD_KLP_CONVERT1/enabled
livepatch: '$MOD_KLP_CONVERT1': initializing unpatching transition
livepatch: '$MOD_KLP_CONVERT1': starting unpatching transition
Expand Down Expand Up @@ -265,6 +267,8 @@ $MOD_KLP_CONVERT1: driver_name, 0: $MOD_KLP_CONVERT_MOD
$MOD_KLP_CONVERT1: test_klp_get_driver_name(), 0: $MOD_KLP_CONVERT_MOD
$MOD_KLP_CONVERT1: homonym_string, 1: homonym string A
$MOD_KLP_CONVERT1: get_homonym_string(), 1: homonym string A
test_klp_convert1: klp_string.12345 = lib/livepatch/test_klp_convert_mod_a.c static string
test_klp_convert1: klp_string.67890 = lib/livepatch/test_klp_convert_mod_b.c static string
% echo 0 > /sys/kernel/livepatch/$MOD_KLP_CONVERT1/enabled
livepatch: '$MOD_KLP_CONVERT1': initializing unpatching transition
livepatch: '$MOD_KLP_CONVERT1': starting unpatching transition
Expand Down

0 comments on commit ce26109

Please sign in to comment.