Skip to content

environ: Move the environment functions to libc. - #20583

Merged
acassis merged 6 commits into
apache:masterfrom
casaroli:environ-libc
Oct 17, 2026
Merged

acassis merged 6 commits into
apache:masterfrom
casaroli:environ-libc

Conversation

@casaroli

@casaroli casaroli commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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.
  • openvela e60e6de (guoshichao): the environment fields move from task_group_s to task_info_s, which is in user memory. Ported: env_sanitize_secure(), added later on master, reads them there too.
  • openvela 2c27af0 (guoshichao): the environment functions move to libs/libc/environ, and ta_lock becomes recursive. Ported to master: the code around each conflict stays as on master, env_secureexec.c moves too, clearenv() keeps the lock from dc4c846, and the files include the headers that they use.
  • 0c13138 (guoshichao): environ is addressable (get_environ_ptr() returns char ***).
  • libs/libc/environ: Allocate the environment in the heap of its task. The functions now also run in kernel code, where lib_umalloc() is the user heap. A kernel build faulted at boot, when the idle task sets PATH. 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 assign environ or replace an entry in place (gnulib's putenv() does, GNU make assigns its own arrays). Without this commit, setenv() after an in-place replacement frees the program's string and free() 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, and env_release() frees the list. So a pointer that getenv() returned stays valid after setenv(). Before a change, env_reserve() copies an array that the program assigned to environ; 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

  • The environment functions are libc functions, not system calls. Programs of a kernel build must be built again.
  • environ can be assigned and its address taken in all builds.
  • A replaced or removed environment string is freed when the task exits, not at once. A program that sets one variable many times keeps the old values until it exits.

Testing

A test program that assigns environ and changes its entries, built against the export package of qemu-armv8a:knsh and run in QEMU. Before the last commit:

envtest: setenv A, B                        A=1 B=2 C=-
envtest: environ[i] replaced in place       A=replaced-in-place B=2 C=-
dump_assert_info: Assertion failed : at file: mm_heap/mm_free.c:243 task: envtest

With the series:

envtest: setenv A, B                        A=1 B=2 C=-
envtest: environ[i] replaced in place       A=replaced-in-place B=2 C=-
envtest: setenv A after the replacement     A=3 B=2 C=-
envtest: unsetenv A                         A=- B=2 C=-
envtest: environ = own array                A=- B=2 C=from-the-stack
envtest: setenv B after the assignment      A=- B=4 C=from-the-stack
envtest: old getenv("B") pointer reads      2
envtest: environ = the saved array          A=- B=2 C=-
envtest: clearenv                           A=- B=- C=-
envtest: setenv A after clearenv            A=5 B=- C=-
envtest: done

ostest on qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build): the environment checks pass, and ostest_main: Exiting with status 0. Without the heap commit, qemu-armv8a:knsh faults in umm_try_initialize() at boot.

Builds, with no warning in the environment files: qemu-armv8a:knsh (also with SCHED_USER_IDENTITY, and with make export and the apps make import), qemu-armv8a:nsh, qemu-armv7a:knsh, lm3s6965-ek:qemu-flat, lm3s6965-ek:qemu-protected, mps2-an500:nsh.

tools/checkpatch.sh -c -u -m -g passes.

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.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Oct 11, 2026
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

arduino-mega2560

  • flash: .text +20 B (+0.0%, 64,978 B / 262,144 B, total: 25% used)
  • sram: .bss +16 B (+0.6%, 2,718 B / 8,192 B, total: 33% used)

esp32-devkitc

  • ROM: .flash.rodata +8 B, .flash.text +208 B (+0.2%, 126,184 B / 4,194,272 B, total: 3% used)
  • dram0_0_seg: .dram0.bss +16 B (+0.2%, 10,012 B / 180,736 B, total: 6% used)
  • drom0_0_seg: .flash.rodata +8 B (+0.1%, 13,468 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +208 B (+0.2%, 90,380 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text +40 B (+0.1%, 67,144 B / 131,072 B, total: 51% used)

qemu-armv8a

  • Code: .rodata -16 B, .text.clearenv -4 B, .text.env_dup +44 B, .text.env_findvar +20 B, .text.env_foreach -20 B, .text.env_release -32 B, .text.env_removevar -108 B, .text.exec_module +4 B, .text.get_environ_ptr -8 B, .text.group_leave -8 B, .text.nxtask_init +4 B, .text.nxtask_setup_fork +4 B, .text.proc_read +4 B, .text.putenv -12 B, .text.setenv -300 B, .text.task_init_info +4 B, .text.task_uninit_info +12 B, .text.unsetenv +36 B (+0.1%, 348,412 B)
  • Data: .bss.g_kthread_group +24 B (+0.0%, 77,498 B)

qemu-intel64

  • Code: .text +257 B (+0.0%, 8,683,454 B)

rx65n-rsk2mb

  • ROM: .text +128 B (+0.1%, 87,412 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +176 B (+0.0%, 375,776 B)

stm32-nucleo-f103rb

  • flash: .text +12 B (+0.0%, 34,452 B / 131,072 B, total: 26% used)
  • sram: .bss +4 B (+0.2%, 2,452 B / 20,480 B, total: 12% used)
    No memory changes detected for:
  • hifive1-revb

@casaroli casaroli closed this Oct 11, 2026
@casaroli casaroli reopened this Oct 11, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@casaroli why not continue cherry-pick open-vela/nuttx@2c27af0:

commit 2c27af036bc927617fad72b85607bccad3c2564d
Author: guoshichao <[email protected]>
Date:   Mon Nov 10 17:25:55 2025 +0800

    environ: move the environ from sched/environ to libc/environ
    
    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]>

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

and this patch: xiaoxiang781216@0c13138

@github-actions github-actions Bot added Area: BINFMT and removed Area: OS Components OS Components issues labels Oct 12, 2026
Comment thread libs/libc/environ/env_putenv.c Resolved
@casaroli

Copy link
Copy Markdown
Contributor Author

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.

Comment thread libs/libc/environ/env_dup.c Resolved
Comment thread libs/libc/environ/env_sync.c Resolved
casaroli and others added 5 commits October 13, 2026 06:51
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]>
Comment thread include/nuttx/tls.h Resolved
acassis
acassis previously approved these changes Oct 13, 2026
@casaroli

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 what about this? do you think this one is acceptable? thank you.

jerpelea
jerpelea previously approved these changes Oct 15, 2026
Comment thread libs/libc/environ/env_putenv.c Resolved
Comment thread libs/libc/environ/env_release.c Resolved
Comment thread libs/libc/environ/environ.h Resolved
Comment thread libs/libc/environ/env_alloc.c Resolved
Comment thread libs/libc/environ/env_alloc.c Resolved
@casaroli
casaroli dismissed stale reviews from jerpelea and acassis via 8d364de October 17, 2026 08:47
@casaroli casaroli closed this Oct 17, 2026
@casaroli casaroli reopened this Oct 17, 2026
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]>
@casaroli
casaroli requested review from acassis and jerpelea October 17, 2026 13:56
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/39023096622

@acassis
acassis merged commit 9fa99ca into apache:master Oct 17, 2026
52 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: BINFMT Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants