Skip to content

[darwin][process]: fix errno handling and library lifetime on darwin - #2119

Merged
shirou merged 4 commits into
masterfrom
fix/darwin-errno-and-libcache
Jul 26, 2026
Merged

shirou merged 4 commits into
masterfrom
fix/darwin-errno-and-libcache

Conversation

@shirou

@shirou shirou commented Jul 19, 2026

Copy link
Copy Markdown
Owner

This is a follow-up PR on #2117.

Read errno through __error() resolved before the libc call, so that the dlsym and allocation performed by symbol resolution cannot overwrite it first. Apply the same to CwdWithContext, which additionally read errno without checking for failure and could surface a stale EPERM.

Share dlopen handles process-wide instead of opening and closing them per call, completing for libSystem what #1832 did for IOKit and CoreFoundation.

Stop populating ReadBytes/WriteBytes on darwin, which #2117 filled with the disk figures. They mean all I/O including cache on other platforms, so reusing the disk value there would silently change what the field means depending on the platform.

Read errno through __error() resolved before the libc call, so that the
dlsym and allocation performed by symbol resolution cannot overwrite it
first. Apply the same to CwdWithContext, which additionally read errno
without checking for failure and could surface a stale EPERM.

Share dlopen handles process-wide instead of opening and closing them per
call, completing for libSystem what #1832 did for IOKit and CoreFoundation.

Stop populating ReadBytes/WriteBytes on darwin, which #2117 filled with the
disk figures. They mean all I/O including cache on other platforms, so
reusing the disk value there would silently change what the field means
depending on the platform.

Copilot AI left a comment

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.

Pull request overview

This PR improves Darwin process handling by making errno retrieval reliable around libc calls, sharing purego dlopen handles for the lifetime of the process to avoid dlclose-related crashes, and aligning IOCounters semantics with other platforms by not mislabeling disk-only bytes as generic I/O bytes.

Changes:

  • Fix errno handling on Darwin by resolving __error() (errno location) before the libc call and reading it only after failure, while holding runtime.LockOSThread().
  • Make Darwin dynamic library handles shared process-wide (no dlclose), with a thread-safe cached symbol-resolution map.
  • Stop populating ReadBytes/WriteBytes on Darwin IOCounters (leave them at 0) and update docs/tests accordingly.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
process/process.go Updates IOCounters field docs to clarify Darwin semantics vs platforms that expose all-I/O byte counts.
process/process_darwin.go Fixes errno read timing/thread affinity for IOCountersWithContext and CwdWithContext, and stops setting generic Read/Write bytes on Darwin.
process/process_darwin_test.go Strengthens struct layout validation and updates IOCounters tests to reflect disk-only semantics on Darwin.
internal/common/common_darwin.go Introduces process-wide dylib handle caching (no dlclose) and thread-safe symbol caching; adds ErrnoLocation() helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

purego maps uintptr to uintptr_t, a plain integer: it neither keeps the
pointee alive nor makes it escape. A Go local therefore stays on the
goroutine stack, and its address goes stale as soon as the stack grows,
leaving the kernel to write into the old stack while the caller reads
zeroes.

This was latent until the shared library handle removed the per-call
Dlopen, whose deep call chain had been growing the stack before the
address was taken and hiding the bug. TestMemoryInfo and TestExe caught
it; TestNumThread had been asserting only n >= 0 and passing on zero.

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

internal/common/common_darwin.go:132

  • CFNumberCreate was updated to accept unsafe.Pointer to avoid passing stack addresses as uintptr into purego (which can go stale on stack growth). CFNumberGetValue (and CFNumberGetValueFunc) still take valuePtr as uintptr even though it is an output buffer written by CoreFoundation, so it can suffer the same stale-address/zeroed-results issue (e.g., callers pass pointers into local structs). Consider switching CFNumberGetValue to unsafe.Pointer and updating call sites accordingly.
func (c *CoreFoundationLib) CFNumberCreate(allocator uintptr, theType int64, valuePtr unsafe.Pointer) unsafe.Pointer {
	fn := getFunc[CFNumberCreateFunc](c.library, "CFNumberCreate")
	return fn(allocator, theType, valuePtr)
}

Comment thread process/process_darwin_test.go Resolved
shirou added 2 commits July 25, 2026 22:11
Finish what ccd5ddb ("[darwin]: pass kernel buffers as unsafe.Pointer on
darwin") started. Two arguments that point at Go memory were still
declared uintptr: valuePtr on CFNumberGetValue, which CoreFoundation
unboxes the number into, and inputStruct/outputStruct on
IOConnectCallStructMethod, which IOKit reads the SMC request from and
writes the reply into.

Both were latent rather than broken. fillStat takes the address of a stat
whose pointer it returns, and callSMC allocates its structs with new, so
escape analysis put all three on the heap and the addresses happened to
stay valid. Neither call site guarantees that, and the argument type is
what should.

purego draws the line exactly here: reflect.Ptr, reflect.UnsafePointer
and reflect.Slice all reach the C side through reflect.Value.Pointer()
and stay reachable for the duration of the call, while reflect.Uintptr is
passed as a bare integer with no keep-alive at all.

Drop CStr.Addr, which has no callers left and whose own doc comment
warned against the only thing it could be used for. Move the explanation
of the rule above the first type block that depends on it, and state the
converse as well, so the handles that are correctly uintptr -- CF and
IOKit object references, mach ports, Dlsym'd data symbols, mach vm
addresses -- are not converted by mistake later.
Darwin caps PIDs at PID_MAX, which is 99999 (bsd/sys/proc_internal.h), so
99999 is itself assignable and a busy or long-running machine can hand it
out. The test would then look up a live process and quietly stop
exercising the ESRCH path it exists for. 999999 can never be assigned.

TestIOCounters_NonExistent used 999999 until 2114d0d changed it;
TestNumFDs_NonExistent has been on 99999 since it was added in aa5d1fd.
@shirou
shirou merged commit ae7d91a into master Jul 26, 2026
54 checks passed
@shirou
shirou deleted the fix/darwin-errno-and-libcache branch July 26, 2026 00:44
mergify Bot added a commit to ArcadeData/arcadedb that referenced this pull request Aug 5, 2026
…p ci]

Bumps the go-modules group in /e2e-go with 2 updates: [github.com/moby/go-archive](https://github.com/moby/go-archive) and [github.com/shirou/gopsutil/v4](https://github.com/shirou/gopsutil).
Updates `github.com/moby/go-archive` from 0.2.1 to 0.3.2
Release notes

*Sourced from [github.com/moby/go-archive's releases](https://github.com/moby/go-archive/releases).*

> v0.3.2
> ------
>
> What's Changed
> --------------
>
> Fix a regression introduced in v0.3.0 that caused archive extraction to fail when paths traversed absolute symlinks inside the destination root, such as `var/run -> /run`. Absolute symlink targets are now resolved relative to the extraction root while relative symlink escapes remain rejected. [moby/go-archive#93](https://redirect.github.com/moby/go-archive/pull/93)
>
> **Full Changelog**: <moby/go-archive@v0.3.1...v0.3.2>
>
> v0.3.1
> ------
>
> Fixes
> -----
>
> This patch release fixes a regression introduced in v0.2.1 where archive extraction could fail when an archive omitted explicit entries for parent directories. For example, extracting `etc/dnf/` without a preceding `etc/` entry could return `mkdirat etc/dnf: no such file or directory`.
>
> This prevented affected images from being extracted. Archive extraction now creates implied parent directories for both file and directory entries.
>
> What's Changed
> --------------
>
> * archive: create implied parents for directory entries [moby/go-archive#92](https://redirect.github.com/moby/go-archive/pull/92)
> * archive: Tarballer.Go: suppress io.ErrClosedPipe logs on close [moby/go-archive#94](https://redirect.github.com/moby/go-archive/pull/94)
>
> **Full Changelog**: <moby/go-archive@v0.3.0...v0.3.1>
>
> v0.3.0
> ------
>
> Security
> --------
>
> This release fixes **CVE-2026-17106** / **[GHSA-hfg8-hc9c-6c3h](https://github.com/moby/go-archive/security/advisories/GHSA-hfg8-hc9c-6c3h)**, where a crafted tar archive could use links to cause extraction operations to create or overwrite files outside the intended destination directory.
>
> The issue affected `Unpack`, `UnpackLayer`, `Untar`, `UntarUncompressed`, and the `ApplyLayer` helpers. Users should upgrade and avoid extracting untrusted archives with earlier versions.
>
> What's Changed
> --------------
>
> * archive: harden tar extraction against path traversal [moby/go-archive#45](https://redirect.github.com/moby/go-archive/pull/45)
> * archive: do not follow reparse points in chtimes [moby/go-archive#90](https://redirect.github.com/moby/go-archive/pull/90)
> * archive: fix creation time updates on Windows [moby/go-archive#79](https://redirect.github.com/moby/go-archive/pull/79)
> * archive: minor cleanups and godoc touch-up [moby/go-archive#87](https://redirect.github.com/moby/go-archive/pull/87)
> * archive: RebaseArchiveEntries: fix archive path rebasing [moby/go-archive#43](https://redirect.github.com/moby/go-archive/pull/43)
>
> Test and CI changes
> -------------------
>
> * ci: enable dependabot for actions [moby/go-archive#81](https://redirect.github.com/moby/go-archive/pull/81)
> * archive: make breakoutErr unwrap its cause [moby/go-archive#91](https://redirect.github.com/moby/go-archive/pull/91)
> * archive: use filepath for filesystem paths in tests [moby/go-archive#80](https://redirect.github.com/moby/go-archive/pull/80)
> * archive: use filepath for filesystem paths in tests [moby/go-archive#80](https://redirect.github.com/moby/go-archive/pull/80)
>
> **Full Changelog**: <moby/go-archive@v0.2.1...v0.3.0>


Commits

* [`9e6d2c7`](moby/go-archive@9e6d2c7) Merge pull request [#93](https://redirect.github.com/moby/go-archive/issues/93) from thaJeztah/fix\_absolute\_symlinks
* [`4f6cd58`](moby/go-archive@4f6cd58) archive: resolve hardlinks through absolute symlinks
* [`e564ecc`](moby/go-archive@e564ecc) archive: resolve absolute symlinks within extraction root
* [`5bb8a45`](moby/go-archive@5bb8a45) Merge pull request [#94](https://redirect.github.com/moby/go-archive/issues/94) from thaJeztah/denoise
* [`1bec7ec`](moby/go-archive@1bec7ec) archive: Tarballer.Go: suppress io.ErrClosedPipe logs on close
* [`279fa6d`](moby/go-archive@279fa6d) Merge pull request [#92](https://redirect.github.com/moby/go-archive/issues/92) from thaJeztah/fix\_implied\_directories
* [`517985a`](moby/go-archive@517985a) archive: create implied parents for directory entries
* [`1c23372`](moby/go-archive@1c23372) Merge pull request [#43](https://redirect.github.com/moby/go-archive/issues/43) from thaJeztah/fix\_rebase\_from\_root
* [`8829a25`](moby/go-archive@8829a25) RebaseArchiveEntries: fix archive path rebasing
* [`c583b20`](moby/go-archive@c583b20) Merge pull request [#90](https://redirect.github.com/moby/go-archive/issues/90) from thaJeztah/chtimes\_nofollow
* Additional commits viewable in [compare view](moby/go-archive@v0.2.1...v0.3.2)
  
Updates `github.com/shirou/gopsutil/v4` from 4.26.6 to 4.26.7
Release notes

*Sourced from [github.com/shirou/gopsutil/v4's releases](https://github.com/shirou/gopsutil/releases).*

> v4.26.7
> -------
>
> What's Changed
> --------------
>
> ### cpu
>
> * fix: harden parsers against malformed/truncated input by [`@​shirou`](https://github.com/shirou) in [shirou/gopsutil#2109](https://redirect.github.com/shirou/gopsutil/pull/2109)
> * [cpu][windows]: compute cpu-total times from integer ticks by [`@​skartikey`](https://github.com/skartikey) in [shirou/gopsutil#2111](https://redirect.github.com/shirou/gopsutil/pull/2111)
> * [darwin][process]: fix errno handling and library lifetime on darwin by [`@​shirou`](https://github.com/shirou) in [shirou/gopsutil#2119](https://redirect.github.com/shirou/gopsutil/pull/2119)
> * [cpu][windows]: compute total counters from individual stats to handle processor groups correctly by [`@​srebhan`](https://github.com/srebhan) in [shirou/gopsutil#2125](https://redirect.github.com/shirou/gopsutil/pull/2125)
> * [cpu][windows]: harden the cpu-total computation added in [#2125](https://redirect.github.com/shirou/gopsutil/issues/2125) by [`@​shirou`](https://github.com/shirou) in [shirou/gopsutil#2128](https://redirect.github.com/shirou/gopsutil/pull/2128)
>
> ### net
>
> * fix(net): pad GetExtendedTcpTable buffer to prevent GC thrashing on Windows by [`@​HarshalPatel1972`](https://github.com/HarshalPatel1972) in [shirou/gopsutil#2108](https://redirect.github.com/shirou/gopsutil/pull/2108)
>
> ### process
>
> * process: implement Darwin IOCounters via proc\_pid\_rusage by [`@​DavRack`](https://github.com/DavRack) in [shirou/gopsutil#2117](https://redirect.github.com/shirou/gopsutil/pull/2117)
>
> ### other
>
> * feat: add psutil comparison tests for cpu, mem and load by [`@​shirou`](https://github.com/shirou) in [shirou/gopsutil#2114](https://redirect.github.com/shirou/gopsutil/pull/2114)
>
> New Contributors
> ----------------
>
> * [`@​DavRack`](https://github.com/DavRack) made their first contribution in [shirou/gopsutil#2117](https://redirect.github.com/shirou/gopsutil/pull/2117)
> * [`@​srebhan`](https://github.com/srebhan) made their first contribution in [shirou/gopsutil#2125](https://redirect.github.com/shirou/gopsutil/pull/2125)
>
> **Full Changelog**: <shirou/gopsutil@v4.26.6...v4.26.7>


Commits

* [`52a24c8`](shirou/gopsutil@52a24c8) Merge pull request [#2128](https://redirect.github.com/shirou/gopsutil/issues/2128) from shirou/feat/follow-up-2125
* [`268a953`](shirou/gopsutil@268a953) [cpu][windows]: harden the cpu-total computation added in [#2125](https://redirect.github.com/shirou/gopsutil/issues/2125)
* [`1e34da6`](shirou/gopsutil@1e34da6) Merge pull request [#2125](https://redirect.github.com/shirou/gopsutil/issues/2125) from srebhan/fix\_cpu\_windows\_total
* [`61f8802`](shirou/gopsutil@61f8802) Merge pull request [#2122](https://redirect.github.com/shirou/gopsutil/issues/2122) from shirou/dependabot/github\_actions/actions/checko...
* [`7fb4dcf`](shirou/gopsutil@7fb4dcf) Merge pull request [#2123](https://redirect.github.com/shirou/gopsutil/issues/2123) from shirou/dependabot/github\_actions/actions/setup-...
* [`ae7d91a`](shirou/gopsutil@ae7d91a) Merge pull request [#2119](https://redirect.github.com/shirou/gopsutil/issues/2119) from shirou/fix/darwin-errno-and-libcache
* [`49052a1`](shirou/gopsutil@49052a1) [darwin][process]: use a PID above PID\_MAX in the not-running tests
* [`991b238`](shirou/gopsutil@991b238) [darwin]: pass the remaining Go pointers as unsafe.Pointer on darwin
* [`b9930e2`](shirou/gopsutil@b9930e2) Merge pull request [#2124](https://redirect.github.com/shirou/gopsutil/issues/2124) from shirou/dependabot/github\_actions/actions/labele...
* [`38a01b4`](shirou/gopsutil@38a01b4) [cpu][windows]: compute total counters from individual stats to handle proces...
* Additional commits viewable in [compare view](shirou/gopsutil@v4.26.6...v4.26.7)
  
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore  major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself)
- `@dependabot ignore  minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself)
- `@dependabot ignore ` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore ` will remove all of the ignore conditions of the specified dependency
- `@dependabot unignore  ` will remove the ignore condition of the specified dependency and ignore conditions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants