-
Notifications
You must be signed in to change notification settings - Fork 8.2k
llext: add tests for init arrays #77941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabiobaltieri
merged 6 commits into
zephyrproject-rtos:main
from
pillo79:pr-test-init-arrays
Sep 10, 2024
+483
−49
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7543bd
llext: add support for ELF init/fini arrays
pillo79 70678db
llext: add bringup, teardown, and bootstrap APIs
pillo79 035b4ed
doc: llext: add extension bringup and teardown documentation
pillo79 6c2d708
tests/llext: refactor: simplify test case definition
pillo79 3d4bc03
tests/llext: refactor: rename perm_setup to test_setup, add test_cleanup
pillo79 ad53931
tests/llext: add init_fini test
pillo79 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||||||
| /* | ||||||||||
| * Copyright (c) 2024 Arduino SA | ||||||||||
| * | ||||||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| #include <string.h> | ||||||||||
| #include <zephyr/llext/llext.h> | ||||||||||
| #include <zephyr/llext/loader.h> | ||||||||||
| #include <zephyr/internal/syscall_handler.h> | ||||||||||
| #include <zephyr/kernel.h> | ||||||||||
|
|
||||||||||
| #include <zephyr/logging/log.h> | ||||||||||
| LOG_MODULE_DECLARE(llext, CONFIG_LLEXT_LOG_LEVEL); | ||||||||||
|
|
||||||||||
| #include "llext_priv.h" | ||||||||||
|
|
||||||||||
| ssize_t z_impl_llext_get_fn_table(struct llext *ext, bool is_init, void *buf, size_t buf_size) | ||||||||||
| { | ||||||||||
| size_t table_size; | ||||||||||
|
|
||||||||||
| if (!ext) { | ||||||||||
| return -EINVAL; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (is_init) { | ||||||||||
| table_size = ext->mem_size[LLEXT_MEM_PREINIT] + | ||||||||||
| ext->mem_size[LLEXT_MEM_INIT]; | ||||||||||
| } else { | ||||||||||
| table_size = ext->mem_size[LLEXT_MEM_FINI]; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (buf) { | ||||||||||
| char *byte_ptr = buf; | ||||||||||
|
|
||||||||||
| if (buf_size < table_size) { | ||||||||||
| return -ENOMEM; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (is_init) { | ||||||||||
| /* setup functions from preinit_array and init_array */ | ||||||||||
| memcpy(byte_ptr, | ||||||||||
| ext->mem[LLEXT_MEM_PREINIT], ext->mem_size[LLEXT_MEM_PREINIT]); | ||||||||||
| memcpy(byte_ptr + ext->mem_size[LLEXT_MEM_PREINIT], | ||||||||||
| ext->mem[LLEXT_MEM_INIT], ext->mem_size[LLEXT_MEM_INIT]); | ||||||||||
| } else { | ||||||||||
| /* cleanup functions from fini_array */ | ||||||||||
| memcpy(byte_ptr, | ||||||||||
| ext->mem[LLEXT_MEM_FINI], ext->mem_size[LLEXT_MEM_FINI]); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* Sanity check: pointers in this table must map inside the | ||||||||||
| * text region of the extension. If this fails, something went | ||||||||||
| * wrong during the relocation process. | ||||||||||
| * Using "char *" for these simplifies pointer arithmetic. | ||||||||||
| */ | ||||||||||
| const char *text_start = ext->mem[LLEXT_MEM_TEXT]; | ||||||||||
| const char *text_end = text_start + ext->mem_size[LLEXT_MEM_TEXT]; | ||||||||||
| const char **fn_ptrs = buf; | ||||||||||
|
|
||||||||||
| for (int i = 0; i < table_size / sizeof(void *); i++) { | ||||||||||
| if (fn_ptrs[i] < text_start || fn_ptrs[i] >= text_end) { | ||||||||||
| LOG_ERR("init function %i (%p) outside text region", | ||||||||||
| i, fn_ptrs[i]); | ||||||||||
|
Comment on lines
+63
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
|
||||||||||
| return -EFAULT; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return table_size; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #ifdef CONFIG_USERSPACE | ||||||||||
|
|
||||||||||
| static int ext_is_valid(struct llext *ext, void *arg) | ||||||||||
| { | ||||||||||
| return ext == arg; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| static inline ssize_t z_vrfy_llext_get_fn_table(struct llext *ext, bool is_init, | ||||||||||
| void *buf, size_t size) | ||||||||||
| { | ||||||||||
| /* Test that ext matches a loaded extension */ | ||||||||||
| K_OOPS(llext_iterate(ext_is_valid, ext) == 0); | ||||||||||
|
|
||||||||||
| if (buf) { | ||||||||||
| /* Test that buf is a valid user-accessible pointer */ | ||||||||||
| K_OOPS(K_SYSCALL_MEMORY_WRITE(buf, size)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return z_impl_llext_get_fn_table(ext, is_init, buf, size); | ||||||||||
| } | ||||||||||
| #include <zephyr/syscalls/llext_get_fn_table_mrsh.c> | ||||||||||
|
|
||||||||||
| #endif /* CONFIG_USERSPACE */ | ||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
enum fn_table_type table_typeinstead ofbool is_initwould be more future-proof, although I don't know if there are other tables we might have to make available.