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

Support older linux systems #545

Merged
merged 4 commits into from
Jun 17, 2022
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
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ endif()

include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckIncludeFileCXX)
include(CMakeDependentOption)

option(SNMALLOC_HEADER_ONLY_LIBRARY "Use snmalloc has a header-only library" OFF)
Expand Down Expand Up @@ -105,6 +106,14 @@ int main() {
return res;
}
" SNMALLOC_PLATFORM_HAS_GETENTROPY)

# check if linux/random.h is available
# older libcs might not have sys/random.h
# but some might provide the necessary flags via linux/random.h
# the __has_include macro isn't working properly on all platforms for that header
# this is why we check its existence here
CHECK_INCLUDE_FILE_CXX(linux/random.h SNMALLOC_HAS_LINUX_RANDOM_H)

# Provide as function so other projects can reuse
# FIXME: This modifies some variables that may or may not be the ones that
# provide flags and so is broken by design. It should be removed once Verona
Expand Down Expand Up @@ -203,6 +212,7 @@ add_as_define(SNMALLOC_QEMU_WORKAROUND)
add_as_define(SNMALLOC_TRACING)
add_as_define(SNMALLOC_CI_BUILD)
add_as_define(SNMALLOC_PLATFORM_HAS_GETENTROPY)
add_as_define(SNMALLOC_HAS_LINUX_RANDOM_H)
if (SNMALLOC_NO_REALLOCARRAY)
add_as_define(SNMALLOC_NO_REALLOCARRAY)
endif()
Expand Down
20 changes: 19 additions & 1 deletion src/snmalloc/pal/pal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
# include <sys/mman.h>
# include <sys/prctl.h>
# include <syscall.h>
// __has_include does not reliably determine if we actually have linux/random.h
// available
# if defined(SNMALLOC_HAS_LINUX_RANDOM_H)
Copy link
Contributor

Choose a reason for hiding this comment

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

What's going on with __has_include?

It might also be worth a comment here to point out that below we check for SYS_getrandom, which won't be defined if this file isn't included?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I honestly cannot tell you what is going on with __has_include other than with gcc 9.3 is wasn't picking up the following:

#if __has_include(<linux/random.h>)
#   include <linux/random.h>
#endif

I honestly have no idea why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SYS_getrandom seems to come from <syscall.h>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But overall if both sys/random.h and linux/random.h aren't available, the build will fail.

# include <linux/random.h>
# endif

extern "C" int puts(const char* str);

Expand Down Expand Up @@ -35,6 +40,19 @@ namespace snmalloc
*/
static constexpr int default_mmap_flags = MAP_NORESERVE;

/**
* MADV_FREE is only available since Linux 4.5.
*
* Fallback to MADV_DONTNEED on older kernels
*/
static constexpr int madvise_free_flags =
# ifdef SNMALLOC_HAS_LINUX_RANDOM_H
MADV_FREE
# else
MADV_DONTNEED
# endif
;

static void* reserve(size_t size) noexcept
{
void* p = PALPOSIX<PALLinux>::reserve(size);
Expand Down Expand Up @@ -108,7 +126,7 @@ namespace snmalloc
memset(p, 0x5a, size);

madvise(p, size, MADV_DONTDUMP);
madvise(p, size, MADV_FREE);
madvise(p, size, madvise_free_flags);

if constexpr (PalEnforceAccess)
{
Expand Down