Skip to content

Commit

Permalink
tests/llext: refactor: rename perm_setup to test_setup, add test_cleanup
Browse files Browse the repository at this point in the history
This commit renames the perm_setup callback to test_setup and provides
the extension as an additional parameter. It also adds a test_cleanup
callback that is called after each test completes.

Setup and cleanup functions are now called regardless of whether
CONFIG_USERSPACE is enabled or not.

Signed-off-by: Luca Burelli <[email protected]>
  • Loading branch information
pillo79 authored and pull[bot] committed Sep 24, 2024
1 parent c3fa21e commit 8000c98
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions tests/subsys/llext/simple/src/test_llext_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ struct llext_test {
size_t buf_len;

bool kernel_only;
void (*perm_setup)(struct k_thread *llext_thread);

/*
* Optional callbacks
*/

/* Called in kernel context before each test starts */
void (*test_setup)(struct llext *ext, struct k_thread *llext_thread);

/* Called in kernel context after each test completes */
void (*test_cleanup)(struct llext *ext);
};


Expand Down Expand Up @@ -99,16 +108,20 @@ K_THREAD_STACK_DEFINE(my_thread_stack, MY_THREAD_STACK_SIZE);
EXPORT_SYMBOL(my_thread_stack);

#ifdef CONFIG_USERSPACE
/* Allow the user space test thread to access global objects */
static void threads_objects_perm_setup(struct k_thread *llext_thread)
/* Allow the test threads to access global objects.
* Note: Permissions on objects used in the test by this thread are initialized
* even in supervisor mode, so that user mode descendant threads can inherit
* these permissions.
*/
static void threads_objects_test_setup(struct llext *, struct k_thread *llext_thread)
{
k_object_access_grant(&my_sem, llext_thread);
k_object_access_grant(&my_thread, llext_thread);
k_object_access_grant(&my_thread_stack, llext_thread);
}
#else
/* No need to set up permissions for supervisor mode */
#define threads_objects_perm_setup NULL
#define threads_objects_test_setup NULL
#endif /* CONFIG_USERSPACE */

void load_call_unload(const struct llext_test *test_case)
Expand Down Expand Up @@ -161,17 +174,17 @@ void load_call_unload(const struct llext_test *test_case)

k_mem_domain_add_thread(&domain, &llext_thread);

/* Even in supervisor mode, initialize permissions on objects used in
* the test by this thread, so that user mode descendant threads can
* inherit these permissions.
*/
if (test_case->perm_setup) {
test_case->perm_setup(&llext_thread);
if (test_case->test_setup) {
test_case->test_setup(ext, &llext_thread);
}

k_thread_start(&llext_thread);
k_thread_join(&llext_thread, K_FOREVER);

if (test_case->test_cleanup) {
test_case->test_cleanup(ext);
}

/* Some extensions may wish to be tried from the context
* of a userspace thread along with the usual supervisor context
* tried above.
Expand All @@ -184,17 +197,29 @@ void load_call_unload(const struct llext_test *test_case)

k_mem_domain_add_thread(&domain, &llext_thread);

if (test_case->perm_setup) {
test_case->perm_setup(&llext_thread);
if (test_case->test_setup) {
test_case->test_setup(ext, &llext_thread);
}

k_thread_start(&llext_thread);
k_thread_join(&llext_thread, K_FOREVER);

if (test_case->test_cleanup) {
test_case->test_cleanup(ext);
}
}

#else /* CONFIG_USERSPACE */
if (test_case->test_setup) {
test_case->test_setup(ext, NULL);
}

zassert_ok(llext_call_fn(ext, "test_entry"),
"test_entry call should succeed");

if (test_case->test_cleanup) {
test_case->test_cleanup(ext);
}
#endif /* CONFIG_USERSPACE */

llext_unload(&ext);
Expand Down Expand Up @@ -257,7 +282,7 @@ static LLEXT_CONST uint8_t threads_kernel_objects_ext[] ELF_ALIGN = {
#include "threads_kernel_objects.inc"
};
LLEXT_LOAD_UNLOAD(threads_kernel_objects,
.perm_setup = threads_objects_perm_setup,
.test_setup = threads_objects_test_setup,
)
#endif

Expand Down

0 comments on commit 8000c98

Please sign in to comment.