Skip to content

runtime,os: add os.Executable() for Darwin #4878

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

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/os/executable_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build darwin

package os

// via runtime because we need argc/argv ptrs
func runtime_executable_path() string

func Executable() (string, error) {
p := runtime_executable_path()
if p != "" && p[0] == '/' {
// absolute path
return p, nil
}
cwd, err := Getwd()
if err != nil {
return "", err
}
return joinPath(cwd, p), nil
}
2 changes: 1 addition & 1 deletion src/os/executable_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux || baremetal
//go:build (!linux && !darwin) || baremetal

package os

Expand Down
35 changes: 35 additions & 0 deletions src/runtime/os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,38 @@ func call_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) int32

//export tinygo_syscall6X
func call_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) uintptr

//go:linkname os_runtime_executable_path os.runtime_executable_path
func os_runtime_executable_path() string {
argv := (*unsafe.Pointer)(unsafe.Pointer(main_argv))

// skip over argv
argv = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(argv), (uintptr(main_argc)+1)*unsafe.Sizeof(argv)))

// skip over envv
for (*argv) != nil {
argv = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(argv), unsafe.Sizeof(argv)))
}

// next string is exe path
argv = (*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(argv), unsafe.Sizeof(argv)))

cstr := unsafe.Pointer(*argv)
length := strlen(cstr)
argString := _string{
length: length,
ptr: (*byte)(cstr),
}
executablePath := *(*string)(unsafe.Pointer(&argString))
Comment on lines +250 to +254
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not executablePath := unsafe.String((*byte)(cstr), length)?

Copy link
Member

Choose a reason for hiding this comment

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

We still support Go 1.19, and unsafe.String was introduced in Go 1.20.

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't (yet) use unsafe.String() in the runtime package, although there is a comment to that effect:

// TODO: use unsafe.String instead once we require Go 1.20.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks. I did in fact look up when unsafe.String was introduced but then went ahead and assumed 1.20 was more than safe. Out of curiosity, is there any reason for keeping support for such a old version of Go?

Copy link
Member

Choose a reason for hiding this comment

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

We could indeed drop support for this older version by now. One consideration might be that Debian bookworm (current stable) uses Go 1.19, dropping support for that means people will need to upgrade the Go version on Debian.


// strip "executable_path=" prefix if available, it's added after OS X 10.11.
executablePath = stringsTrimPrefix(executablePath, "executable_path=")
return executablePath
}

func stringsTrimPrefix(s, prefix string) string {
if len(s) >= len(prefix) && s[:len(prefix)] == prefix {
return s[len(prefix):]
}
return s
}