Commit 026f58c8 authored by Nathan Bronson's avatar Nathan Bronson Committed by Facebook Github Bot

check for insane stack size in MemoryIdler

Summary:
In code that accidentally sets an impossibly huge stack size
(RLIMIT_SIZE=-1, for example), MemoryIdler can unmap pages beyond the
stack that are still in use.  This diff causes that case to assert in
debug builds and disable memory idling in production.

Reviewed By: edwinsmith

Differential Revision: D7688511

fbshipit-source-id: 7ef4261aaa3273ac7edf9edf596c2ea504e35877
parent 30aacd4b
......@@ -32,10 +32,11 @@
#include <string.h>
#include <utility>
namespace folly { namespace detail {
namespace folly {
namespace detail {
AtomicStruct<std::chrono::steady_clock::duration>
MemoryIdler::defaultIdleTimeout(std::chrono::seconds(5));
MemoryIdler::defaultIdleTimeout(std::chrono::seconds(5));
void MemoryIdler::flushLocalMallocCaches() {
if (!usingJEMalloc()) {
......@@ -77,7 +78,6 @@ void MemoryIdler::flushLocalMallocCaches() {
}
}
// Stack madvise isn't Linux or glibc specific, but the system calls
// and arithmetic (and bug compatibility) are not portable. The set of
// platforms could be increased if it was useful.
......@@ -105,7 +105,9 @@ static void fetchStackLimits() {
tls_stackSize = 1;
return;
}
SCOPE_EXIT { pthread_attr_destroy(&attr); };
SCOPE_EXIT {
pthread_attr_destroy(&attr);
};
void* addr;
size_t rawSize;
......@@ -116,6 +118,18 @@ static void fetchStackLimits() {
tls_stackSize = 1;
return;
}
if (rawSize >= 4UL * 1024 * 1024 * 1024) {
// Avoid unmapping huge swaths of memory if there is an insane stack
// size. If we went into /proc to find the actual contiguous mapped
// pages before unmapping we wouldn't care about the size at all, but
// our current strategy is to unmap even pages that are not mapped.
// We will warn (because it is a bug) but better not to crash.
FB_LOG_EVERY_MS(ERROR, 10000)
<< "pthread_attr_getstack returned insane stack size " << rawSize;
assert(false);
tls_stackSize = 1;
return;
}
assert(addr != nullptr);
assert(rawSize >= PTHREAD_STACK_MIN);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment