Skip to content

sched/environ: Make environ an assignable variable. - #20570

Closed
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:sched-environ-assignable
Closed

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:sched-environ-assignable

Conversation

@casaroli

@casaroli casaroli commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Summary

POSIX specifies extern char **environ;, and programs assign it, for example environ = some_array; before they run another program. NuttX defines environ as a macro for get_environ_ptr(), so those programs do not compile: error: lvalue required as left operand of assignment.

Commit 1, kernel builds: in the user space of a kernel build every process has its own copy of the C library data, so environ is now a real variable.

  • libc defines it. crt0 sets it to the environment and registers its address in the new ar_environ field of the process's reserved data area.
  • The kernel keeps owning the environment. getenv(), setenv(), unsetenv(), clearenv() and get_environ_ptr() first take over an array that the program assigned to environ (env_sync_in()), and copy it before the old environment is freed, because its strings often point into the old one. After a change they point environ at the new environment (env_sync_out()). exec and posix_spawn see the program's array through the same path.
  • A program built before this change is not affected: its reserved area holds NULL.

Commit 2, flat and protected builds: all tasks share the C library data, so environ cannot be one variable. The new option CONFIG_LIBC_ENVIRON_ASSIGNABLE (default n) makes environ name a per-task-group location: #define environ (*get_environ_location()), the new ta_environ field of the group's task_info_s. That is an lvalue, so environ = array; compiles. The kernel uses the same env_sync_in() and env_sync_out().

Impact

Kernel builds: environ is a variable in user space; the kernel code above runs on each environment call. Flat and protected builds: no change unless CONFIG_LIBC_ENVIRON_ASSIGNABLE=y.

Size (text + data), flat builds with the option off: no change on lm3s6965-ek:qemu-flat (262000) and mps2-an500:nsh (365031). With the option on: +360 bytes on mps2-an500:nsh, +188 bytes on lm3s6965-ek:qemu-protected.

Not supported: changing one entry of the kernel's array in place (environ[i] = "X=1"). With the option, a program's own extern char **environ; compiles with a -Wstrict-prototypes warning, as for errno.

Testing

QEMU 11. Test programs built outside the tree against the export package for knsh, and built into the image in place of examples/hello for nsh.

qemu-armv8a:knsh: a program assigns environ, calls setenv(), runs itself as a child with posix_spawnp(..., environ), then calls clearenv().

Before:

envassign.c:24:11: error: lvalue required as left operand of assignment

After:

envassign: getenv(FOO) = bar
envassign: [FOO=bar]
envassign: [PATH=/system/bin]
envassign: [X=1]
envassign: child FOO=bar X=1
envassign: after clearenv environ 0

qemu-armv8a:nsh (flat) with CONFIG_LIBC_ENVIRON_ASSIGNABLE=y: the same steps, with a child task from task_create(). Without the option the program does not compile (lvalue error), as before.

flatenv: getenv(FOO) = bar
flatenv: [FOO=bar]
flatenv: [PATH=/bin]
flatenv: [X=1]
flatenv: child FOO=bar X=1
flatenv: child [FOO=bar]
flatenv: child [PATH=/bin]
flatenv: child [X=1]
flatenv: after clearenv environ 0 getenv(FOO) (null)

Kernel builds with -Wno-cpp -Werror, each followed by make export and the apps make import: qemu-armv8a:knsh, imx93-evk:knsh, qemu-armv7a:knsh, sabre-6quad:netknsh, sama5d4-ek:knsh, rv-virt:knsh, rv-virt:knsh64, rv-virt:ksmp64, rv-virt:knsh_paging, rv-virt:knetnsh64, rv-virt:kfb64, icicle:knsh, canmv230:knsh, esp32s3-devkit:kernel_n8r2.

Flat and protected builds with -Wno-cpp -Werror, option off and on: lm3s6965-ek:qemu-flat (on: overflows flash by 24 bytes; the board has no room left), mps2-an500:nsh, lm3s6965-ek:qemu-protected.

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

POSIX specifies "extern char **environ;", and programs assign it, for
example "environ = some_array;" before they run another program.
NuttX defines environ as a macro for get_environ_ptr(), so those
programs do not compile.

In a flat or protected build all tasks share the C library data, so the
macro stays.  In the user space of a kernel build every process has its
own copy, and environ is now a real variable:

- libc defines it, and crt0 sets it to the environment and registers
  its address in the new ar_environ field of the process's reserved
  data area.

- The kernel keeps owning the environment.  getenv(), setenv(),
  unsetenv(), clearenv() and get_environ_ptr() first take over an array
  that the program assigned to environ (env_sync_in()), copying it
  before the old environment is freed because its strings often point
  into the old one.  After a change they point environ at the new
  environment (env_sync_out()).  exec and posix_spawn see the program's
  array through the same path.

A program that did not register environ (built before this change) is
not affected: its reserved area holds NULL.  Changing one entry of the
kernel's array in place (environ[i] = "X=1") is still not supported.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
…lds.

In flat and protected builds environ is a macro for get_environ_ptr(),
a function call, so programs that assign environ do not compile there.

All tasks of those builds share the C library data, so environ cannot be
a single variable.  With the new option CONFIG_LIBC_ENVIRON_ASSIGNABLE
(default n) it names a per-task-group location instead:
"#define environ (*get_environ_location())", which returns the address
of the new ta_environ field of the group's task_info_s.  That is an
lvalue, so "environ = array;" compiles.  As with errno, a program's own
"extern char **environ;" compiles with a -Wstrict-prototypes warning.

The kernel uses the same env_sync_in() and env_sync_out() as in kernel
builds; only the location differs.  The kernel can reach any group's
task_info_s in these builds, so env_dup() also publishes the environment
of a new group.

The option adds about 330 bytes (mps2-an500:nsh).  Without it nothing
changes.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
Comment thread sched/environ/env_sync.c
*
****************************************************************************/

static FAR char ***env_user_environ(FAR struct task_group_s *group)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a better fix which moves all environ staff from kernel to libc, and will upstream the related change by tomorrow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will close this PR when your change lands; please link it here. I can test it with our program that assigns environ, calls setenv(), passes environ to posix_spawn() and calls clearenv().

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

esp32-devkitc

  • ROM: .flash.text +8 B (+0.0%, 125,096 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +8 B (+0.0%, 89,300 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +16 B (+0.0%, 84,252 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .rodata +8 B, .text.gmtime_r +12 B, .text.init_builtin_run +60 B, .text.work_qqueue +24 B (+0.0%, 347,480 B)

qemu-intel64

  • Code: .text +84 B (+0.0%, 8,682,514 B)

rx65n-rsk2mb

  • ROM: .rodata +4 B (+0.0%, 87,044 B / 2,097,152 B, total: 4% used)

s698pm-dkit

@casaroli
casaroli marked this pull request as draft October 11, 2026 05:56
@casaroli casaroli closed this Oct 11, 2026
@casaroli casaroli reopened this Oct 11, 2026
@casaroli

Copy link
Copy Markdown
Contributor Author

#20583

@casaroli casaroli closed this Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants