From 614f17b534eb38be6f097237db090a1d65174bbd Mon Sep 17 00:00:00 2001 From: Nathan Bronson Date: Fri, 16 Oct 2015 14:27:11 -0700 Subject: [PATCH] fix initializer order bug in MemoryIdler Summary: Make sure page size has been fetched from the operating system before it is used in asserts. Reviewed By: meyering Differential Revision: D2551368 fb-gh-sync-id: d3735571e2a45f613bbbd7e0f158a755d36b376c --- folly/detail/MemoryIdler.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/folly/detail/MemoryIdler.cpp b/folly/detail/MemoryIdler.cpp index f7af9ee8..7dd54c8a 100644 --- a/folly/detail/MemoryIdler.cpp +++ b/folly/detail/MemoryIdler.cpp @@ -94,10 +94,14 @@ void MemoryIdler::flushLocalMallocCaches() { // platforms could be increased if it was useful. #if (FOLLY_X64 || FOLLY_PPC64 ) && defined(_GNU_SOURCE) && defined(__linux__) -static const size_t s_pageSize = sysconf(_SC_PAGESIZE); static FOLLY_TLS uintptr_t tls_stackLimit; static FOLLY_TLS size_t tls_stackSize; +static size_t pageSize() { + static const size_t s_pageSize = sysconf(_SC_PAGESIZE); + return s_pageSize; +} + static void fetchStackLimits() { pthread_attr_t attr; pthread_getattr_np(pthread_self(), &attr); @@ -128,7 +132,7 @@ static void fetchStackLimits() { tls_stackLimit = uintptr_t(addr) + guardSize; tls_stackSize = rawSize - guardSize; - assert((tls_stackLimit & (s_pageSize - 1)) == 0); + assert((tls_stackLimit & (pageSize() - 1)) == 0); } FOLLY_NOINLINE static uintptr_t getStackPtr() { @@ -150,14 +154,14 @@ void MemoryIdler::unmapUnusedStack(size_t retain) { assert(sp >= tls_stackLimit); assert(sp - tls_stackLimit < tls_stackSize); - auto end = (sp - retain) & ~(s_pageSize - 1); + auto end = (sp - retain) & ~(pageSize() - 1); if (end <= tls_stackLimit) { // no pages are eligible for unmapping return; } size_t len = end - tls_stackLimit; - assert((len & (s_pageSize - 1)) == 0); + assert((len & (pageSize() - 1)) == 0); if (madvise((void*)tls_stackLimit, len, MADV_DONTNEED) != 0) { // It is likely that the stack vma hasn't been fully grown. In this // case madvise will apply dontneed to the present vmas, then return -- 2.34.1