selftests/livepatch: more verification in test-klp-shadow-vars

This change makes the test feel more familiar with narrowing to a
typical usage by operating on a number of identical structure instances
and populating the same two new shadow variables symmetrically while
keeping the same testing and verification criteria for the extra
variables.

Signed-off-by: Yannick Cote <ycote@redhat.com>
Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Acked-by: Miroslav Benes <mbenes@suse.cz>
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20200603182058.109470-4-ycote@redhat.com
This commit is contained in:
Yannick Cote 2020-06-03 14:20:57 -04:00 committed by Petr Mladek
parent 6a26a9df16
commit 76efe6da89
2 changed files with 139 additions and 126 deletions

View File

@ -128,6 +128,11 @@ static int shadow_ctor(void *obj, void *shadow_data, void *ctor_data)
return 0; return 0;
} }
/*
* With more than one item to free in the list, order is not determined and
* shadow_dtor will not be passed to shadow_free_all() which would make the
* test fail. (see pass 6)
*/
static void shadow_dtor(void *obj, void *shadow_data) static void shadow_dtor(void *obj, void *shadow_data)
{ {
int **sv = shadow_data; int **sv = shadow_data;
@ -136,6 +141,9 @@ static void shadow_dtor(void *obj, void *shadow_data)
__func__, ptr_id(obj), ptr_id(sv)); __func__, ptr_id(obj), ptr_id(sv));
} }
/* number of objects we simulate that need shadow vars */
#define NUM_OBJS 3
/* dynamically created obj fields have the following shadow var id values */ /* dynamically created obj fields have the following shadow var id values */
#define SV_ID1 0x1234 #define SV_ID1 0x1234
#define SV_ID2 0x1235 #define SV_ID2 0x1235
@ -157,122 +165,106 @@ struct test_object {
static int test_klp_shadow_vars_init(void) static int test_klp_shadow_vars_init(void)
{ {
struct test_object obj1, obj2, obj3; struct test_object objs[NUM_OBJS];
char nfield1, nfield2, *pnfield1, *pnfield2, **sv1, **sv2; char nfields1[NUM_OBJS], *pnfields1[NUM_OBJS], **sv1[NUM_OBJS];
int nfield3, nfield4, *pnfield3, *pnfield4, **sv3, **sv4; char *pndup[NUM_OBJS];
int nfields2[NUM_OBJS], *pnfields2[NUM_OBJS], **sv2[NUM_OBJS];
void **sv; void **sv;
int i;
pnfield1 = &nfield1;
pnfield2 = &nfield2;
pnfield3 = &nfield3;
pnfield4 = &nfield4;
ptr_id(NULL); ptr_id(NULL);
ptr_id(pnfield1);
ptr_id(pnfield2);
ptr_id(pnfield3);
ptr_id(pnfield4);
/* /*
* With an empty shadow variable hash table, expect not to find * With an empty shadow variable hash table, expect not to find
* any matches. * any matches.
*/ */
sv = shadow_get(&obj1, SV_ID1); sv = shadow_get(&objs[0], SV_ID1);
if (!sv) if (!sv)
pr_info(" got expected NULL result\n"); pr_info(" got expected NULL result\n");
/* /* pass 1: init & alloc a char+int pair of svars for each objs */
* Allocate a few shadow variables with different <obj> and <id>. for (i = 0; i < NUM_OBJS; i++) {
*/ pnfields1[i] = &nfields1[i];
sv1 = shadow_alloc(&obj1, SV_ID1, sizeof(pnfield1), GFP_KERNEL, shadow_ctor, &pnfield1); ptr_id(pnfields1[i]);
if (!sv1)
return -ENOMEM;
sv2 = shadow_alloc(&obj2, SV_ID1, sizeof(pnfield2), GFP_KERNEL, shadow_ctor, &pnfield2); if (i % 2) {
if (!sv2) sv1[i] = shadow_alloc(&objs[i], SV_ID1,
return -ENOMEM; sizeof(pnfields1[i]), GFP_KERNEL,
shadow_ctor, &pnfields1[i]);
} else {
sv1[i] = shadow_get_or_alloc(&objs[i], SV_ID1,
sizeof(pnfields1[i]), GFP_KERNEL,
shadow_ctor, &pnfields1[i]);
}
if (!sv1[i])
return -ENOMEM;
sv3 = shadow_alloc(&obj1, SV_ID2, sizeof(pnfield3), GFP_KERNEL, shadow_ctor, &pnfield3); pnfields2[i] = &nfields2[i];
if (!sv3) ptr_id(pnfields2[i]);
return -ENOMEM; sv2[i] = shadow_alloc(&objs[i], SV_ID2, sizeof(pnfields2[i]),
GFP_KERNEL, shadow_ctor, &pnfields2[i]);
if (!sv2[i])
return -ENOMEM;
}
/* /* pass 2: verify we find allocated svars and where they point to */
* Verify we can find our new shadow variables and that they point for (i = 0; i < NUM_OBJS; i++) {
* to expected data. /* check the "char" svar for all objects */
*/ sv = shadow_get(&objs[i], SV_ID1);
sv = shadow_get(&obj1, SV_ID1); if (!sv)
if (!sv) return -EINVAL;
return -EINVAL; if ((char **)sv == sv1[i] && *sv1[i] == pnfields1[i])
if ((char **)sv == sv1 && *sv1 == pnfield1) pr_info(" got expected PTR%d -> PTR%d result\n",
pr_info(" got expected PTR%d -> PTR%d result\n", ptr_id(sv1[i]), ptr_id(*sv1[i]));
ptr_id(sv1), ptr_id(*sv1));
sv = shadow_get(&obj2, SV_ID1); /* check the "int" svar for all objects */
if (!sv) sv = shadow_get(&objs[i], SV_ID2);
return -EINVAL; if (!sv)
if ((char **)sv == sv2 && *sv2 == pnfield2) return -EINVAL;
pr_info(" got expected PTR%d -> PTR%d result\n", if ((int **)sv == sv2[i] && *sv2[i] == pnfields2[i])
ptr_id(sv2), ptr_id(*sv2)); pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv2[i]), ptr_id(*sv2[i]));
}
sv = shadow_get(&obj1, SV_ID2); /* pass 3: verify that 'get_or_alloc' returns already allocated svars */
if (!sv) for (i = 0; i < NUM_OBJS; i++) {
return -EINVAL; pndup[i] = &nfields1[i];
if ((int **)sv == sv3 && *sv3 == pnfield3) ptr_id(pndup[i]);
pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv3), ptr_id(*sv3));
/* sv = shadow_get_or_alloc(&objs[i], SV_ID1, sizeof(pndup[i]),
* Allocate or get a few more, this time with the same <obj>, <id>. GFP_KERNEL, shadow_ctor, &pndup[i]);
* The second invocation should return the same shadow var. if (!sv)
*/ return -EINVAL;
sv4 = shadow_get_or_alloc(&obj3, SV_ID1, sizeof(pnfield4), GFP_KERNEL, shadow_ctor, &pnfield4); if ((char **)sv == sv1[i] && *sv1[i] == pnfields1[i])
if (!sv4) pr_info(" got expected PTR%d -> PTR%d result\n",
return -ENOMEM; ptr_id(sv1[i]), ptr_id(*sv1[i]));
}
sv = shadow_get_or_alloc(&obj3, SV_ID1, sizeof(pnfield4), GFP_KERNEL, shadow_ctor, &pnfield4); /* pass 4: free <objs[*], SV_ID1> pairs of svars, verify removal */
if (!sv) for (i = 0; i < NUM_OBJS; i++) {
return -EINVAL; shadow_free(&objs[i], SV_ID1, shadow_dtor); /* 'char' pairs */
if ((int **)sv == sv4 && *sv4 == pnfield4) sv = shadow_get(&objs[i], SV_ID1);
pr_info(" got expected PTR%d -> PTR%d result\n", if (!sv)
ptr_id(sv4), ptr_id(*sv4)); pr_info(" got expected NULL result\n");
}
/* /* pass 5: check we still find <objs[*], SV_ID2> svar pairs */
* Free the <obj=*, id> shadow variables and check that we can no for (i = 0; i < NUM_OBJS; i++) {
* longer find them. sv = shadow_get(&objs[i], SV_ID2); /* 'int' pairs */
*/ if (!sv)
shadow_free(&obj1, SV_ID1, shadow_dtor); /* sv1 */ return -EINVAL;
sv = shadow_get(&obj1, SV_ID1); if ((int **)sv == sv2[i] && *sv2[i] == pnfields2[i])
if (!sv) pr_info(" got expected PTR%d -> PTR%d result\n",
pr_info(" got expected NULL result\n"); ptr_id(sv2[i]), ptr_id(*sv2[i]));
}
shadow_free(&obj2, SV_ID1, shadow_dtor); /* sv2 */
sv = shadow_get(&obj2, SV_ID1);
if (!sv)
pr_info(" got expected NULL result\n");
shadow_free(&obj3, SV_ID1, shadow_dtor); /* sv4 */
sv = shadow_get(&obj3, SV_ID1);
if (!sv)
pr_info(" got expected NULL result\n");
/*
* We should still find an <id+1> variable.
*/
sv = shadow_get(&obj1, SV_ID2);
if (!sv)
return -EINVAL;
if ((int **)sv == sv3 && *sv3 == pnfield3)
pr_info(" got expected PTR%d -> PTR%d result\n",
ptr_id(sv3), ptr_id(*sv3));
/*
* Free all the <id+1> variables, too.
*/
shadow_free_all(SV_ID2, shadow_dtor); /* sv3 */
sv = shadow_get(&obj1, SV_ID1);
if (!sv)
pr_info(" shadow_get() got expected NULL result\n");
/* pass 6: free all the <objs[*], SV_ID2> svar pairs too. */
shadow_free_all(SV_ID2, NULL); /* 'int' pairs */
for (i = 0; i < NUM_OBJS; i++) {
sv = shadow_get(&objs[i], SV_ID2);
if (!sv)
pr_info(" got expected NULL result\n");
}
free_ptr_list(); free_ptr_list();

View File

@ -19,42 +19,63 @@ load_mod $MOD_TEST
unload_mod $MOD_TEST unload_mod $MOD_TEST
check_result "% modprobe $MOD_TEST check_result "% modprobe $MOD_TEST
$MOD_TEST: klp_shadow_get(obj=PTR5, id=0x1234) = PTR0 $MOD_TEST: klp_shadow_get(obj=PTR1, id=0x1234) = PTR0
$MOD_TEST: got expected NULL result $MOD_TEST: got expected NULL result
$MOD_TEST: shadow_ctor: PTR6 -> PTR1 $MOD_TEST: shadow_ctor: PTR3 -> PTR2
$MOD_TEST: klp_shadow_alloc(obj=PTR5, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR7, ctor_data=PTR1 = PTR6 $MOD_TEST: klp_shadow_get_or_alloc(obj=PTR1, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR2 = PTR3
$MOD_TEST: shadow_ctor: PTR8 -> PTR2 $MOD_TEST: shadow_ctor: PTR6 -> PTR5
$MOD_TEST: klp_shadow_alloc(obj=PTR9, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR7, ctor_data=PTR2 = PTR8 $MOD_TEST: klp_shadow_alloc(obj=PTR1, id=0x1235, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR5 = PTR6
$MOD_TEST: shadow_ctor: PTR10 -> PTR3 $MOD_TEST: shadow_ctor: PTR8 -> PTR7
$MOD_TEST: klp_shadow_alloc(obj=PTR5, id=0x1235, size=8, gfp_flags=GFP_KERNEL), ctor=PTR7, ctor_data=PTR3 = PTR10 $MOD_TEST: klp_shadow_alloc(obj=PTR9, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR7 = PTR8
$MOD_TEST: klp_shadow_get(obj=PTR5, id=0x1234) = PTR6 $MOD_TEST: shadow_ctor: PTR11 -> PTR10
$MOD_TEST: got expected PTR6 -> PTR1 result $MOD_TEST: klp_shadow_alloc(obj=PTR9, id=0x1235, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR10 = PTR11
$MOD_TEST: shadow_ctor: PTR13 -> PTR12
$MOD_TEST: klp_shadow_get_or_alloc(obj=PTR14, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR12 = PTR13
$MOD_TEST: shadow_ctor: PTR16 -> PTR15
$MOD_TEST: klp_shadow_alloc(obj=PTR14, id=0x1235, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR15 = PTR16
$MOD_TEST: klp_shadow_get(obj=PTR1, id=0x1234) = PTR3
$MOD_TEST: got expected PTR3 -> PTR2 result
$MOD_TEST: klp_shadow_get(obj=PTR1, id=0x1235) = PTR6
$MOD_TEST: got expected PTR6 -> PTR5 result
$MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1234) = PTR8 $MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1234) = PTR8
$MOD_TEST: got expected PTR8 -> PTR2 result $MOD_TEST: got expected PTR8 -> PTR7 result
$MOD_TEST: klp_shadow_get(obj=PTR5, id=0x1235) = PTR10 $MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1235) = PTR11
$MOD_TEST: got expected PTR10 -> PTR3 result $MOD_TEST: got expected PTR11 -> PTR10 result
$MOD_TEST: shadow_ctor: PTR11 -> PTR4 $MOD_TEST: klp_shadow_get(obj=PTR14, id=0x1234) = PTR13
$MOD_TEST: klp_shadow_get_or_alloc(obj=PTR12, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR7, ctor_data=PTR4 = PTR11 $MOD_TEST: got expected PTR13 -> PTR12 result
$MOD_TEST: klp_shadow_get_or_alloc(obj=PTR12, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR7, ctor_data=PTR4 = PTR11 $MOD_TEST: klp_shadow_get(obj=PTR14, id=0x1235) = PTR16
$MOD_TEST: got expected PTR11 -> PTR4 result $MOD_TEST: got expected PTR16 -> PTR15 result
$MOD_TEST: shadow_dtor(obj=PTR5, shadow_data=PTR6) $MOD_TEST: klp_shadow_get_or_alloc(obj=PTR1, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR2 = PTR3
$MOD_TEST: klp_shadow_free(obj=PTR5, id=0x1234, dtor=PTR13) $MOD_TEST: got expected PTR3 -> PTR2 result
$MOD_TEST: klp_shadow_get(obj=PTR5, id=0x1234) = PTR0 $MOD_TEST: klp_shadow_get_or_alloc(obj=PTR9, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR7 = PTR8
$MOD_TEST: got expected PTR8 -> PTR7 result
$MOD_TEST: klp_shadow_get_or_alloc(obj=PTR14, id=0x1234, size=8, gfp_flags=GFP_KERNEL), ctor=PTR4, ctor_data=PTR12 = PTR13
$MOD_TEST: got expected PTR13 -> PTR12 result
$MOD_TEST: shadow_dtor(obj=PTR1, shadow_data=PTR3)
$MOD_TEST: klp_shadow_free(obj=PTR1, id=0x1234, dtor=PTR17)
$MOD_TEST: klp_shadow_get(obj=PTR1, id=0x1234) = PTR0
$MOD_TEST: got expected NULL result $MOD_TEST: got expected NULL result
$MOD_TEST: shadow_dtor(obj=PTR9, shadow_data=PTR8) $MOD_TEST: shadow_dtor(obj=PTR9, shadow_data=PTR8)
$MOD_TEST: klp_shadow_free(obj=PTR9, id=0x1234, dtor=PTR13) $MOD_TEST: klp_shadow_free(obj=PTR9, id=0x1234, dtor=PTR17)
$MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1234) = PTR0 $MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1234) = PTR0
$MOD_TEST: got expected NULL result $MOD_TEST: got expected NULL result
$MOD_TEST: shadow_dtor(obj=PTR12, shadow_data=PTR11) $MOD_TEST: shadow_dtor(obj=PTR14, shadow_data=PTR13)
$MOD_TEST: klp_shadow_free(obj=PTR12, id=0x1234, dtor=PTR13) $MOD_TEST: klp_shadow_free(obj=PTR14, id=0x1234, dtor=PTR17)
$MOD_TEST: klp_shadow_get(obj=PTR12, id=0x1234) = PTR0 $MOD_TEST: klp_shadow_get(obj=PTR14, id=0x1234) = PTR0
$MOD_TEST: got expected NULL result $MOD_TEST: got expected NULL result
$MOD_TEST: klp_shadow_get(obj=PTR5, id=0x1235) = PTR10 $MOD_TEST: klp_shadow_get(obj=PTR1, id=0x1235) = PTR6
$MOD_TEST: got expected PTR10 -> PTR3 result $MOD_TEST: got expected PTR6 -> PTR5 result
$MOD_TEST: shadow_dtor(obj=PTR5, shadow_data=PTR10) $MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1235) = PTR11
$MOD_TEST: klp_shadow_free_all(id=0x1235, dtor=PTR13) $MOD_TEST: got expected PTR11 -> PTR10 result
$MOD_TEST: klp_shadow_get(obj=PTR5, id=0x1234) = PTR0 $MOD_TEST: klp_shadow_get(obj=PTR14, id=0x1235) = PTR16
$MOD_TEST: shadow_get() got expected NULL result $MOD_TEST: got expected PTR16 -> PTR15 result
% rmmod test_klp_shadow_vars" $MOD_TEST: klp_shadow_free_all(id=0x1235, dtor=PTR0)
$MOD_TEST: klp_shadow_get(obj=PTR1, id=0x1235) = PTR0
$MOD_TEST: got expected NULL result
$MOD_TEST: klp_shadow_get(obj=PTR9, id=0x1235) = PTR0
$MOD_TEST: got expected NULL result
$MOD_TEST: klp_shadow_get(obj=PTR14, id=0x1235) = PTR0
$MOD_TEST: got expected NULL result
% rmmod $MOD_TEST"
exit 0 exit 0