Skip to content

[libc][WIP] try to make sysconf work #74166

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

Closed
wants to merge 2 commits into from

Conversation

SchrodingerZhu
Copy link
Contributor

No description provided.

@SchrodingerZhu SchrodingerZhu changed the title Libc/expose auxv [libc][WIP] try make sysconf work Dec 2, 2023
@SchrodingerZhu SchrodingerZhu changed the title [libc][WIP] try make sysconf work [libc][WIP] try to make sysconf work Dec 2, 2023
@MaskRay
Copy link
Member

MaskRay commented Dec 2, 2023

As said on Discord

I have read rtld part of glibc, musl, and FreeBSD, but I am not familiar with llvmlibc. From a quick glance, startup/linux does not contain any relocation resolver code, so it is more like crt part for a -static executable, not for -static-pie or dynamically linked executables. The code doesn't handle features needed by ifunc.
I think startup/linux properly needs a complete rewrite, since different architectures share lots of code. The current structure would encourage a lot of duplication.

I understand the value of incremental updates, but the loader probably needs a rewrite to be mostly arch-neutral to avoid (a) wasted engineering efforts on duplicating code for aarch64/x86_64/riscv (b) wasted engineering efforts on whether a -static feature would work for -static-pie and dynamically linked executables.

Before the rewrite, we could add incremental features, but if people get used to the behavior, it would probably be harder to rewrite the loader.

@SchrodingerZhu
Copy link
Contributor Author

SchrodingerZhu commented Dec 4, 2023

Hi,
This PR itself does not mean to be merged but provides a way in which we can get the correct aux vector and corresponding page sizes. The previous implementation in sysconf simply uses EXEC_PAGESIZE which is almost incorrect on architectures other than x86-64.

The major change involves the implementation of getauxval (see also https://lwn.net/Articles/928305/):

  1. it first checks if theapp global variable is initialized, if so, it uses the auxv_ptr inside it directly.
  2. otherwise, since we need to consider the usage in overlay mode where other unit tests can be dependent on getauxval, we fall back into two possible approaches:
    1. use prctl together with PR_GET_AUXV to get a copy of the aux array in the userspace. This is a rather new API from linux kernel -- it is only available for 6.0+, but it can handles the situations where /proc is not mounted.
    2. read the data from /proc/self/auxv

I do not feel like caching the value in getauxval in overlay mode, as such caching may require pthread_once. Say, a user links the overlay build of libc with glibc, where the user may use LLVM's getauxval in their ifunc resolvers. These resolver routines can be executed at a very early stage, even before pthread runtime is properly initialized. Then, serious problems will happen.

If this proposal works, I would split the PR into several parts:

  1. implement prctl
  2. change app_h to an object library. Or change app to an inline variable as in the PR? (Optionally, I can also extract the app initialization part from the startup library. AFAIK, this part is not platform-dependent and should not replicate inside startup. @MaskRay)
  3. expose auxv_ptr from app.
  4. implement getauxval and update sysconf.

@nickdesaulniers
Copy link
Member

Honestly, I think we should back out (revert):

  1. 54878b8
  2. 985c0d1
  3. 418a3a4

Then go about:

  1. implement prcntl
  2. implement getauxval
  3. implement sysconf
  4. implement mincore

as 4 distinct commits/PRs. We should not have landed 418a3a4 using EXEC_PAGESIZE; it was a mistake of mine to have approved #73704

@SchrodingerZhu
Copy link
Contributor Author

SchrodingerZhu commented Dec 4, 2023

@nickdesaulniers I have no objections. Though I was confused by the wrong sysconf, but I was also wrong to use EXEC_PAGESIZE in subsequent patches. Those series looks like an ugly series of amendments to make things just work. So sorry about that. 😭

@nickdesaulniers
Copy link
Member

It's ok, we'll get it cleaned up. Does that plan I outlined above seem agreeable?

Do you @SchrodingerZhu want to send a PR with the reverts or shall I?

@nickdesaulniers
Copy link
Member

Do you @SchrodingerZhu want to send a PR with the reverts or shall I?

ah #74355

@SchrodingerZhu
Copy link
Contributor Author

It's ok, we'll get it cleaned up. Does that plan I outlined above seem agreeable?

I think so. 👌🏻

@SchrodingerZhu SchrodingerZhu deleted the libc/expose-auxv branch January 24, 2024 15:11
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

Successfully merging this pull request may close these issues.

3 participants