This is an archive of the discontinued LLVM Phabricator instance.

[fuzzer] Don't hard-code page size in FuzzerUtil.h
ClosedPublic

Authored by Ami-zhang on Dec 22 2022, 10:20 PM.

Diff Detail

Event Timeline

tangyouling created this revision.Dec 22 2022, 10:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2022, 10:20 PM
tangyouling requested review of this revision.Dec 22 2022, 10:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2022, 10:20 PM
xen0n added a comment.EditedDec 23 2022, 12:13 AM

This change makes sense on LoongArch, and should cause no behavioral change on 4KiB-page platforms like X86, but more testing on other architectures (e.g. AArch64 systems with 16KiB or 64KiB pages) would be welcome.

I'd leave the approval for other sanitizer reviewers.

MaskRay added a subscriber: kcc.Dec 24 2022, 3:41 PM

getpagesize is not recommended. Linux can use getauxval(AT_PAGESZ).
HandleInline8bitCountersInit is called once for an executable/DSO, so a function call isn't too expensive, but better to avoid calling it 2 times for RoundUpByPage/RoundDownByPage, and possibly multiple times in HandleInline8bitCountersInit.


@kcc made https://reviews.llvm.org/rG6fd4d8ab9c6e6ddb67df5d822f855de980d1eea6 in 2019, noted down that it is "Needed for future optimization."
It seems that enabled is always true and OneFullPage is unused. Is there a pending optimization which has not landed?

getpagesize is not recommended. Linux can use getauxval(AT_PAGESZ).
HandleInline8bitCountersInit is called once for an executable/DSO, so a function call isn't too expensive, but better to avoid calling it 2 times for RoundUpByPage/RoundDownByPage, and possibly multiple times in HandleInline8bitCountersInit.

Can it be defined as follows?

#if SANITIZER_USE_GETAUXVAL
#include <sys/auxv.h>
#endif

inline size_t PageSize()
{
#if SANITIZER_USE_GETAUXVAL
  return getauxval(AT_PAGESZ);
#else
  return 4096;
#endif
}

It still needs to be like GetPageSize() in sanitizer_linux.cpp?

#if !SANITIZER_ANDROID
uptr GetPageSize() {
#if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) && \
    defined(EXEC_PAGESIZE)
  return EXEC_PAGESIZE;
#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
// Use sysctl as sysconf can trigger interceptors internally.
  int pz = 0;
  uptr pzl = sizeof(pz);
  int mib[2] = {CTL_HW, HW_PAGESIZE};
  int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
  CHECK_EQ(rv, 0);
  return (uptr)pz;
#elif SANITIZER_USE_GETAUXVAL
  return getauxval(AT_PAGESZ);
#else
  return sysconf(_SC_PAGESIZE);  // EXEC_PAGESIZE may not be trustworthy.
#endif
}
#endif // !SANITIZER_ANDROID

Modify the PageSize() implementation.

vitalybuka requested changes to this revision.Jan 18 2023, 12:35 AM
vitalybuka added inline comments.
compiler-rt/lib/fuzzer/FuzzerUtil.h
19

sanitizer_common is not used by fuzzer, so no SANITIZER_USE_GETAUXVAL

103

Code indeed does not look hot, however it would be nice to have "cached" approach
with getauxval and #include <sys/auxv.h> in cpp file

This revision now requires changes to proceed.Jan 18 2023, 12:35 AM

The patch will continue to be updated by @Ami-zhang

Ami-zhang commandeered this revision.May 25 2023, 4:14 AM
Ami-zhang added a reviewer: tangyouling.
Ami-zhang added inline comments.May 25 2023, 5:08 AM
compiler-rt/lib/fuzzer/FuzzerUtil.h
19

Ok, done. Thanks.

103

Update it and use "cached" approach with getauxval to get pagesize.
Thanks for your suggestion.

Ami-zhang updated this revision to Diff 525550.May 25 2023, 5:09 AM

Use "cached" approach with getauxval to get pagesize.

vitalybuka accepted this revision.May 25 2023, 4:25 PM
This revision is now accepted and ready to land.May 25 2023, 4:25 PM

In the previous snapshot initialization was not thread safe.
and we don't want to inline initialization all over the program
and rename is unnececary

This revision was landed with ongoing or failed builds.May 25 2023, 4:35 PM
This revision was automatically updated to reflect the committed changes.

Hi, this has broken the lldb bots on Darwin systems; there is no <sys/auxv.h> there. Can you please revert or replace it with a has_include(sys/auxv.h) etc and fall back to the old hardcoded value?

I reverted this change in cb9f2de2e80292c0445c99be754fcc78b94b0f16 to unblock the CI test bots.

Hi, this has broken the lldb bots on Darwin systems; there is no <sys/auxv.h> there. Can you please revert or replace it with a has_include(sys/auxv.h) etc and fall back to the old hardcoded value?

Yes, this broke tests on Darwin systems. I'm sorry for that.
Instead, I prefer to use the sysconf(_SC_PAGESIZE) function which is more available on most systems including Darwin systems to obtain the page size.
I have submitted the revised changes in https://reviews.llvm.org/D151530.

vitalybuka reopened this revision.May 25 2023, 7:58 PM
This revision is now accepted and ready to land.May 25 2023, 7:58 PM
Ami-zhang updated this revision to Diff 525923.May 25 2023, 8:02 PM

Use sysconf(_SC_PAGESIZE) to obtain the page size instead.

Switch to sysconf

This revision was landed with ongoing or failed builds.May 25 2023, 8:24 PM
This revision was automatically updated to reflect the committed changes.