Skip to content

arch/sim: Handle Darwin clock IDs in clock_gettime()#18962

Open
toku-mac wants to merge 1 commit into
apache:masterfrom
toku-mac:clock_gettime_macos
Open

arch/sim: Handle Darwin clock IDs in clock_gettime()#18962
toku-mac wants to merge 1 commit into
apache:masterfrom
toku-mac:clock_gettime_macos

Conversation

@toku-mac
Copy link
Copy Markdown
Contributor

Summary

This PR fixes clock_gettime() compatibility for the macOS simulator when
Darwin-targeted code is linked into NuttX.

The fix adds a sim/macOS-specific clock_gettime() override that translates
Darwin monotonic clock IDs to NuttX CLOCK_MONOTONIC before calling
nxclock_gettime().

  • Make the common clock_gettime() weak.
  • Add arch/sim/src/sim/macos/sim_clock_gettime.c.
  • Translate Darwin monotonic clock IDs.
  • Add the new source to both CMake and make builds.
  • Add the make object to REQUIREDOBJS so the override is included in the final simulator link.

Impact

Running hello_rust_cargo with nsh on macOS should no longer cause a panic.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): OS (macOS 26.5), CPU(Apple M1), compiler(Apple clang version 21.0.0)
  • Target(s): arch(sim)
  • Ensure your PATH environment variable is properly configured to allow execution of: menuconfig, olddefconfig, savedefconfig, and setconfig.
  • Use the Rust toolchain version prior to nightly-2026-04-29 to avoid errors related to lib/rustlib/src/rust/library/std/src/sys/net/connection/socket/unix.rs.
  • This PR is a successor of PR sim/posix: Add APPLE to a build target for sim's configuration using CMake. #18883.
  • To build using make, you need apps PR #3501 .

Configuration and Build:

rm -rf build-debug
make distclean
cmake -S . -B build-debug -DBOARD_CONFIG=sim:nsh -GNinja

printf "CONFIG_SYSTEM_TIME64=y
CONFIG_FS_LARGEFILE=y
CONFIG_TLS_NELEM=16
CONFIG_DEV_URANDOM=y
CONFIG_EXAMPLES_HELLO_RUST_CARGO=y
CONFIG_EXAMPLES_HELLO_RUST_CARGO_STACKSIZE=8192
" >> build-debug/.config

cmake --build build-debug -t olddefconfig
cmake --build build-debug

Testing logs before change:

NuttShell (NSH) NuttX-12.13.0
nsh> hello_rust_cargo
{"name":"John","age":30}
{"name":"Jane","age":25}
Deserialized: Alice is 28 years old
Pretty JSON:
{
  "name": "Alice",
  "age": 28
}

thread '<unnamed>' (1797510) panicked at /Users/toku/.rustup/toolchains/nightly-2026-04-29-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/sys/pal/unix/time.rs:107:68:
called `Result::unwrap()` on an `Err` value: Os { code: 0, kind: Uncategorized, message: "Unknown error 0" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Testing logs after change:

NuttShell (NSH) NuttX-12.13.0
nsh> hello_rust_cargo
{"name":"John","age":30}
{"name":"Jane","age":25}
Deserialized: Alice is 28 years old
Pretty JSON:
{
  "name": "Alice",
  "age": 28
}
Hello world from tokio!

PR verification Self-Check

  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).
  • My PR is ready for review and can be safely merged into a codebase.

@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels May 26, 2026
Add a sim/macOS-specific clock_gettime() override that translates Darwin
monotonic clock IDs to NuttX CLOCK_MONOTONIC before delegating to
nxclock_gettime().  Make the common clock_gettime() weak so the simulator
override can replace it.

This fixes a Rust tokio panic on macOS when running hello_rust_cargo.

Signed-off-by: Shoji Tokunaga <toku@mac.com>
@toku-mac toku-mac force-pushed the clock_gettime_macos branch from 195a3bc to c49d064 Compare May 26, 2026 07:41
@yamt
Copy link
Copy Markdown
Contributor

yamt commented May 26, 2026

when Darwin-targeted code is linked into NuttX.

is it expected to work at all?

****************************************************************************/

int clock_gettime(clockid_t clock_id, FAR struct timespec *tp)
int weak_function clock_gettime(clockid_t clock_id, FAR struct timespec *tp)
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.

it's wrong to expose the host clock_gettime to nuttx. We should use the same approach as Linux, which do the conversion in arch/sim/posix. what's problem do you hit on macOS?

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.

@yamt @xiaoxiang781216

is it expected to work at all?

it's wrong to expose the host clock_gettime to nuttx. We should use the same approach as Linux, which do the conversion in arch/sim/posix. what's problem do you hit on macOS?

I'll reconsider the implementation method.

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.

@yamt @xiaoxiang781216

  • Add ARCH_HAVE_CLOCKID_TRANSLATE to Kconfig.
  • Reference this in sched/clock/clock_gettime.c and include/nuttx/arch.h.
  • Add arch/sim/src/sim/posix/sim_clockid.c.

What do you think about this approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator 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.

3 participants