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

Make GetProgramExecutableName reliable on X86_64 XNU (macOS) assimilated binaries #1169

Open
mrdomino opened this issue May 6, 2024 · 2 comments

Comments

@mrdomino
Copy link
Collaborator

mrdomino commented May 6, 2024

Writing a somewhat long-ish description in the hopes that this might make a good starter bug for somebody who is not scared of a little assembly.

XNU doesn't do auxv the way other platforms do; instead, it has a sort of addendum to envp, i.e. more env-style key-value pairs after the sentinel NULL at the end of envp. The (generally?) first of these begins executable_path= and contains the path of the running binary.

If we are running assimilated on x86_64 XNU, that value is the only reliable way we have of locating the path of the running binary for use in GetProgramExecutableName (and hence the ZipOS /zip filesystem.)

In libc/crt/crt.S, the pseudo-auxv is currently zeroed out:

cosmopolitan/libc/crt/crt.S

Lines 104 to 111 in 06d916b

#if SupportsXnu()
// xnu doesn't have auxiliary values
testb IsXnu()
jz 1f // polyfill xnu auxv
push $0 // auxv[1][1]=0
push $0 // auxv[1][0]=0
mov %rsp,%rcx // auxv
#endif

It would be good if the executable path were stored in __program_executable_name prior to this happening, if we are running assimilated.

@mrdomino mrdomino changed the title Make GetProgramExecutableName reliable on X86_64 XNU assimilated binaries Make GetProgramExecutableName reliable on X86_64 MacOS (XNU) assimilated binaries May 6, 2024
@mrdomino mrdomino changed the title Make GetProgramExecutableName reliable on X86_64 MacOS (XNU) assimilated binaries Make GetProgramExecutableName reliable on X86_64 XNU (macOS) assimilated binaries May 6, 2024
@mrdomino
Copy link
Collaborator Author

mrdomino commented May 8, 2024

From #1168 (comment):

I guess if we are continuing down the road of using AT_FLAGS to signal that argv[0] is preserved, and if we care about XNU, then another thing that might be done is to make that snippet in crt.S not run if we notice that we've been loaded.

Strawman logic might be:

if __program_executable_name is set
  do nothing, the loader has already set auxv up for us
else
  if it is good to do so (i.e. we're assimilated, not running under an older loader)
    save executable_path= to __program_executable_name
  zero out auxv

@mrdomino
Copy link
Collaborator Author

(The above pseudocode is still basically sound even though we're probably not continuing down the road of (ab)using AT_FLAGS that way.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant