Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/sys/unix: access to ELF auxiliary vector #67839

Closed
ghost opened this issue Jun 5, 2024 · 23 comments
Closed

x/sys/unix: access to ELF auxiliary vector #67839

ghost opened this issue Jun 5, 2024 · 23 comments

Comments

@ghost
Copy link

ghost commented Jun 5, 2024

Proposal Details

like os.Args and os.Environ, I was wondering why there is neither an API to get ELF auxiliary vector nor Aux type. C does provide them in Elf.h

@ghost ghost added the Proposal label Jun 5, 2024
@seankhliao seankhliao changed the title os: ELF auxiliary vector proposal: os: access to ELF auxiliary vector Jun 5, 2024
@gopherbot gopherbot added this to the Proposal milestone Jun 5, 2024
@ianlancetaylor
Copy link
Member

If we do this I think it should be in the golang.org/x/sys/unix package. The os package is mainly aimed at OS-independent functionality, but auxv is specific to ELF-based systems.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Jun 5, 2024
@ghost
Copy link
Author

ghost commented Jun 5, 2024

so, in the current state of art, is there no way to get the full aux vector?

@ianlancetaylor
Copy link
Member

As far as I know there is not.

@adonovan
Copy link
Member

adonovan commented Jun 5, 2024

so, in the current state of art, is there no way to get the full aux vector?

Not from Go. In C you can usually derive it from the address of the environ symbol, but it may be non-portable.

@seankhliao
Copy link
Member

I see runtime has an internal sysauxv which x/sys/cpu hooks into, falling back to /proc/self/auxv. Is the proc file sufficient?

@ghost
Copy link
Author

ghost commented Jun 16, 2024

/proc/self/auxv

could you provide a minimal example on how to retrieve auxv values?

@florianl
Copy link
Contributor

florianl commented Jun 21, 2024

#68089 advocated to expose runtime.getAuxv() and make it a public API. Exposing the auxiliary vector via os was not considered as a viable option, as os should be more of a higher level layer with cross OS compatability.
But adding a public API, like runtime.Getauxv(), would also introduce some kind of OS specifics into runtime.

An alternative, also mentioned in #67839 (comment) and go.dev/cl/458256, could be an exposure of the auxiliary vector via golang.org/x/sys. golang.org/x/sys is OS specific and therefore a perfect fit. The unknown for me in this approach is how golang.org/x/sys can get access to something, that only runtime knows about, without techniques like //go:linkname.

@ianlancetaylor
Copy link
Member

golang.org/x/sys/cpu already reaches into the runtime to fetch the auxiliary vector, using go:linkname. Adding an exported function in golang.org/x/sys/unix isn't going to make matters better or worse.

Reaching in from packages in x/sys is better than reaching in from arbitrary external packages, because we can change the x/sys packages ourselves, and then drop the support in runtime when we no longer support older versions of the x/sys packages.

@florianl
Copy link
Contributor

Thanks for the clarification. I really wasn't sure about x/sys, as I considered x/sys as an external package, "just" in a more controlled manner.

If it helps, I can open a CL against x/sys for GetAuxv() []uintptr using go:linkname.

@ianlancetaylor
Copy link
Member

Thanks, may as well wait for the proposal process.

@lmb
Copy link
Contributor

lmb commented Jun 25, 2024

Is the proc file sufficient?

/proc is tricky because it has additional permission checks and interacts with capabilities. See https://dxuuu.xyz/filecaps.html. Using auxv from the runtime really is a lot less error prone.

@florianl
Copy link
Contributor

Is there something I can help with to get feedback on this proposal or continue with the proposal process?

@rsc
Copy link
Contributor

rsc commented Dec 4, 2024

We should drop the "Get" but func Auxv() []uintptr in x/sys/unix seems fine?

@rsc
Copy link
Contributor

rsc commented Dec 4, 2024

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals Dec 4, 2024
@aclements
Copy link
Member

We should drop the "Get" but func Auxv() []uintptr in x/sys/unix seems fine?

Auxv is a sequence of key/value pairs, so I'd lean toward a slightly more structured API: func Auxv() [][2]uintptr

@aclements
Copy link
Member

There should probably be an error result as well, since there are corner cases where we just can't get the auxv (e.g., in c-shared mode on Android where /proc/self/auxv is unreadable).

@aclements aclements changed the title proposal: os: access to ELF auxiliary vector proposal: x/sys/unix: access to ELF auxiliary vector Jan 8, 2025
@aclements
Copy link
Member

Have all remaining concerns about this proposal been addressed?

The proposal is to add the following to golang.org/x/sys/unix:

// Auxv returns the ELF auxiliary vector as a sequence of key/value pairs.
// The returned slice is always a fresh copy, owned by the caller.
// It returns an error on non-ELF platforms, of if the auxiliary vector cannot be accessed,
// which happens in some locked-down environments and build modes.
func Auxv() ([][2]uintptr, error)

@aclements aclements moved this from Active to Likely Accept in Proposals Jan 16, 2025
@aclements
Copy link
Member

Based on the discussion above, this proposal seems like a likely accept.

The proposal is to add the following to golang.org/x/sys/unix:

// Auxv returns the ELF auxiliary vector as a sequence of key/value pairs.
// The returned slice is always a fresh copy, owned by the caller.
// It returns an error on non-ELF platforms, of if the auxiliary vector cannot be accessed,
// which happens in some locked-down environments and build modes.
func Auxv() ([][2]uintptr, error)

@aclements aclements moved this from Likely Accept to Accepted in Proposals Jan 23, 2025
@aclements
Copy link
Member

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— aclements for the proposal review group

The proposal is to add the following to golang.org/x/sys/unix:

// Auxv returns the ELF auxiliary vector as a sequence of key/value pairs.
// The returned slice is always a fresh copy, owned by the caller.
// It returns an error on non-ELF platforms, of if the auxiliary vector cannot be accessed,
// which happens in some locked-down environments and build modes.
func Auxv() ([][2]uintptr, error)

@aclements aclements changed the title proposal: x/sys/unix: access to ELF auxiliary vector x/sys/unix: access to ELF auxiliary vector Jan 23, 2025
@aclements aclements modified the milestones: Proposal, Backlog Jan 23, 2025
@seehuhn
Copy link

seehuhn commented Jan 23, 2025

of if the auxiliary vector cannot be accessed

s/of if/or if/

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/644295 mentions this issue: unix: add Auxv()

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/646535 mentions this issue: runtime: adjust comments for auxv getAuxv

gopherbot pushed a commit that referenced this issue Feb 5, 2025
github.com/cilium/ebpf no longer accesses getAuxv using linkname but now
uses the golang.org/x/sys/unix.Auxv wrapper introduced in
go.dev/cl/644295.

Also adjust the list of users to include x/sys/unix.

Updates #67839
Updates #67401

Change-Id: Ieee266360b22cc0bc4be8f740e0302afd7dbd14f
Reviewed-on: https://go-review.googlesource.com/c/go/+/646535
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Tobias Klauser <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Accepted
Development

No branches or pull requests

9 participants