Repository navigation
environ: Move the environment functions to libc. - #20583
Conversation
|
|
@casaroli why not continue cherry-pick open-vela/nuttx@2c27af0: |
|
and this patch: xiaoxiang781216@0c13138 |
ac19f92 to
281c4f1
Compare
281c4f1 to
19c5bc6
Compare
|
Done: the PR is now e60e6de + 2c27af0 + 0c13138 (authorship kept, ported to master: the conflicts keep the master code and change only the environ calls). Two commits are added because the series alone fails here: (1) In a kernel build the idle task's setenv("PATH") at boot used lib_umalloc(), the user heap, and faulted in umm_try_initialize(). The environment is now allocated in the heap of its task_info. (2) A program that replaces environ[i] in place (gnulib putenv) or assigns environ made setenv() free a string libc did not allocate (mm_free.c assert; test output in the description). libc now frees only the strings it allocated. ostest passes on knsh and nsh. |
Indent the case labels of two switch statements two columns inside the braces, as nxstyle checks. Whitespace only. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
Since tg_envp is placed in task_info_s, both tg_envp itself and the memory it points to (group_malloc) are userspace memory. There's no need to switch to kernel mode to access tg_env, which can improve access speed. Signed-off-by: guoshichao <[email protected]> (cherry picked from commit e60e6de in https://github.com/open-vela/nuttx) Port to Apache master: env_sanitize_secure() (env_secureexec.c, added later on master) also reads the environment from the task info. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
change the environ implementation from sched/environ to libc/environ, thus we can access the environ related API can vai libc directl, which could improve the access speed in kernel build. And in order to support this modification, we change the struct task_info_s's ta_lock from nxmutex_t to nxrmutex_t, this is because the env_*-related APIs require the ta_lock to be reentrant. If recursive locks are not supported, it may lead to deadlocks in certain scenarios. For example, during initialization on the qemu-miwear platform, it is necessary to call getenv while already holding the ta_lock. To support such cases, we need to change the ta_lock from nxmutex_t to nxrmutex_t. Signed-off-by: guoshichao <[email protected]> (cherry picked from commit 2c27af0 in https://github.com/open-vela/nuttx) Port to Apache master: keep the master code around each conflict and change only the environ calls; move env_secureexec.c (env_sanitize_secure() takes the task info, declared in nuttx/environ.h); keep the clearenv() lock from dc4c846; task_uninit_info() releases the environment and stays where group_release() calls it; include the headers that openvela gets indirectly (nuttx/lib/lib.h, stdio.h, string.h). Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
this modification will enable us can access environ in the following format: ``` vputenv(&environ, "TZ=LOC-3"); ``` Signed-off-by: guoshichao <[email protected]> (cherry picked from commit 0c13138 in https://github.com/xiaoxiang781216/nuttx) Port to Apache master: keep master's named parameters and the secure_getenv() declaration in stdlib.h. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
The environment functions now run in kernel code as well as in user code. In kernel code lib_umalloc() is kumm_malloc(), the heap of the current user process. A kernel build then faulted at boot: the idle task sets PATH before any user process exists. The environment of a kernel thread is in the kernel heap, with its task information. Allocate and free each environment in the heap that holds its task information: the kernel heap for a kernel thread, the user heap for a user task. User code still uses lib_umalloc(). putenv() allocates and frees its copy of the string the same way. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
19c5bc6 to
a4a831d
Compare
|
@xiaoxiang781216 what about this? do you think this one is acceptable? thank you. |
environ is an lvalue, so a program can assign it, as POSIX allows, and change its entries. Both happen: gnulib's putenv() replaces an entry in place, and GNU make installs its own arrays. libc then freed a string that the program put there, or an array on the program's stack. For example, setenv() after an in-place replacement made free() assert. libc now frees no environment string before the task exits. Each block that env_alloc() gives for the environment goes on a list of the task, and env_release() frees the list. glibc also keeps these strings, so a pointer that getenv() returned stays valid after setenv() or unsetenv(). env_alloc.c chooses the heap of the task and keeps the block header private. Before a change, env_reserve() makes environ an array that libc allocated. If the program assigned its own array, libc copies it and does not change it. The array of libc that it replaced stays until exit, because the program can assign it again. env_dup() allocates the array and all of the strings in two blocks. putenv() passes the name part of its string to env_setvar(), a setenv() that takes the length of the name, so it needs no copy. The counts of the environment are size_t. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
8d364de to
733b579
Compare
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
Summary
An alternative to #20570 for the environment in a kernel build, built from the openvela commits, as requested in review.
fs/procfs/fs_procfsproc.c: Indent the switch statements.Whitespace only, so that the file passes nxstyle.task_group_stotask_info_s, which is in user memory. Ported:env_sanitize_secure(), added later on master, reads them there too.libs/libc/environ, andta_lockbecomes recursive. Ported to master: the code around each conflict stays as on master,env_secureexec.cmoves too,clearenv()keeps the lock from dc4c846, and the files include the headers that they use.environis addressable (get_environ_ptr()returnschar ***).libs/libc/environ: Allocate the environment in the heap of its task.The functions now also run in kernel code, wherelib_umalloc()is the user heap. A kernel build faulted at boot, when the idle task setsPATH. Each environment is now allocated in the heap that holds its task information.libs/libc/environ: Keep the strings that libc allocated until exit.A program can assignenvironor replace an entry in place (gnulib'sputenv()does, GNU make assigns its own arrays). Without this commit,setenv()after an in-place replacement frees the program's string andfree()asserts (test below). libc now frees no environment string before the task exits, as glibc: each block that it allocates for the environment goes on a list of the task, andenv_release()frees the list. So a pointer thatgetenv()returned stays valid aftersetenv(). Before a change,env_reserve()copies an array that the program assigned toenviron; the array of libc that it replaced stays until exit, because the program can assign it again.env_dup()allocates the array and all of the strings in two blocks.Impact
environcan be assigned and its address taken in all builds.Testing
A test program that assigns
environand changes its entries, built against the export package ofqemu-armv8a:knshand run in QEMU. Before the last commit:With the series:
ostestonqemu-armv8a:knsh(kernel build) andqemu-armv8a:nsh(flat build): the environment checks pass, andostest_main: Exiting with status 0. Without the heap commit,qemu-armv8a:knshfaults inumm_try_initialize()at boot.Builds, with no warning in the environment files:
qemu-armv8a:knsh(also withSCHED_USER_IDENTITY, and withmake exportand the appsmake import),qemu-armv8a:nsh,qemu-armv7a:knsh,lm3s6965-ek:qemu-flat,lm3s6965-ek:qemu-protected,mps2-an500:nsh.tools/checkpatch.sh -c -u -m -gpasses.depends-on: apache/nuttx-apps/pull/3857
For CI only: since apache/nuttx-apps#3848, apps master does not build
esp32s3-devkit:mbedtls, and apache/nuttx-apps#3857 fixes it. This PR does not need it otherwise.