Skip to content

Commit

Permalink
tests: userspace: check TLS location
Browse files Browse the repository at this point in the history
Ensure that the TLS region is within the stack object.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
  • Loading branch information
Andrew Boie committed May 27, 2020
1 parent a575496 commit 2a27669
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion tests/kernel/mem_protect/userspace/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,43 @@ void test_object_recycle(void)
zassert_true(perms_count == 1, "invalid number of thread permissions");
}

#define TLS_SIZE 4096
struct k_thread tls_thread;
K_THREAD_STACK_DEFINE(tls_stack, TLS_SIZE);

void tls_entry(void *p1, void *p2, void *p3)
{
printk("tls_entry\n");
}

void test_tls_pointer(void)
{
k_thread_create(&tls_thread, tls_stack, TLS_SIZE, tls_entry,
NULL, NULL, NULL, 1, K_USER, K_FOREVER);

printk("tls pointer for thread %p: %p\n",
&tls_thread, (void *)tls_thread.userspace_local_data);

printk("stack buffer reported bounds: [%p, %p)\n",
(void *)tls_thread.stack_info.start,
(void *)(tls_thread.stack_info.start +
tls_thread.stack_info.size));

printk("stack object bounds: [%p, %p)\n",
tls_stack, tls_stack + sizeof(tls_stack));

uintptr_t tls_start = (uintptr_t)tls_thread.userspace_local_data;
uintptr_t tls_end = tls_start +
sizeof(struct _thread_userspace_local_data);

if ((tls_start < (uintptr_t)tls_stack) ||
(tls_end > (uintptr_t)tls_stack + sizeof(tls_stack))) {
printk("tls area out of bounds\n");
ztest_test_fail();
}
}


void test_main(void)
{
struct k_mem_partition *parts[] = {&part0, &part1,
Expand Down Expand Up @@ -1184,7 +1221,8 @@ void test_main(void)
ztest_unit_test(test_stack_buffer),
ztest_user_unit_test(test_unimplemented_syscall),
ztest_user_unit_test(test_bad_syscall),
ztest_unit_test(test_object_recycle)
ztest_unit_test(test_object_recycle),
ztest_unit_test(test_tls_pointer)
);
ztest_run_test_suite(userspace);
}

0 comments on commit 2a27669

Please sign in to comment.