This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Align the end of PT_GNU_RELRO to max-page-size instead of common-page-size
AbandonedPublic

Authored by MaskRay on May 11 2022, 1:16 PM.

Details

Summary

We picked common-page-size to match GNU ld. Recently, the resolution to GNU ld
https://sourceware.org/bugzilla/show_bug.cgi?id=28824 (milestone: 2.39) switched
to max-page-size so that the last page can be protected by RELRO in case the
system page size is larger than common-page-size.

Thanks to our two RW PT_LOAD scheme (D58892), switching to max-page-size does
not change file size (while GNU ld's scheme may increase file size).

Diff Detail

Event Timeline

MaskRay created this revision.May 11 2022, 1:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 1:16 PM
MaskRay requested review of this revision.May 11 2022, 1:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 1:16 PM
peter.smith accepted this revision.May 12 2022, 12:43 AM

LGTM thanks for making the change. It was useful to see the write up in the binutils PR as common page versus max page size isn't documented very well.

This revision is now accepted and ready to land.May 12 2022, 12:43 AM
MaskRay updated this revision to Diff 428871.May 12 2022, 12:51 AM
MaskRay edited the summary of this revision. (Show Details)

add release note

I am seeing a failure during a two stage build of LLVM on an AArch64 host that I bisected to this change:

[3118/3661] ASTNodeAPI.json

FAILED: tools/clang/lib/Tooling/ASTNodeAPI.json …
…/bin/clang-ast-dump: error while loading shared libraries: cannot apply additional memory protection after relocation: Cannot allocate memory

What information would be helpful for investigating what is going on?

I somehow forgot that we can not simply round up the end of PT_GNU_RELRO to max-page-size.

I think this is a limitation of our 2 RW PT_LOAD scheme and we may have to expect that if system page size is larger than common-page-size, the last partial page may not be protected by RELRO.
See also https://reviews.freebsd.org/D31498 that system page size smaller than common-page-size may have problems as well.

MaskRay reopened this revision.May 12 2022, 9:08 PM
This revision is now accepted and ready to land.May 12 2022, 9:08 PM
MaskRay abandoned this revision.May 12 2022, 9:08 PM

This is superseded by https://github.com/llvm/llvm-project/pull/66042 . I've mentioned there why runtime_page_size < common-page-size may lead to glibc rtld error while loading shared libraries: cannot apply additional memory protection after relocation: Cannot allocate memory.
If we simply change common-page-size to max-page-size on the p->p_memsz line, this condition would be runtime_page_size < max-page-size .
When max-page-size is 64KiB (ARM/AArch64/PowerPC) and runtime page-size is typically smaller (4KiB), error while loading shared libraries: cannot apply additional memory protection after relocation: Cannot allocate memory will almost assuredly occur.