stack_consumption.cc
Go to the documentation of this file.
00001 //
00002 // Copyright 2018 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 
00016 #include "absl/debugging/internal/stack_consumption.h"
00017 
00018 #ifdef ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION
00019 
00020 #include <signal.h>
00021 #include <sys/mman.h>
00022 #include <unistd.h>
00023 
00024 #include <string.h>
00025 
00026 #include "absl/base/attributes.h"
00027 #include "absl/base/internal/raw_logging.h"
00028 
00029 namespace absl {
00030 namespace debugging_internal {
00031 namespace {
00032 
00033 // This code requires that we know the direction in which the stack
00034 // grows. It is commonly believed that this can be detected by putting
00035 // a variable on the stack and then passing its address to a function
00036 // that compares the address of this variable to the address of a
00037 // variable on the function's own stack. However, this is unspecified
00038 // behavior in C++: If two pointers p and q of the same type point to
00039 // different objects that are not members of the same object or
00040 // elements of the same array or to different functions, or if only
00041 // one of them is null, the results of p<q, p>q, p<=q, and p>=q are
00042 // unspecified. Therefore, instead we hardcode the direction of the
00043 // stack on platforms we know about.
00044 #if defined(__i386__) || defined(__x86_64__) || defined(__ppc__)
00045 constexpr bool kStackGrowsDown = true;
00046 #else
00047 #error Need to define kStackGrowsDown
00048 #endif
00049 
00050 // To measure the stack footprint of some code, we create a signal handler
00051 // (for SIGUSR2 say) that exercises this code on an alternate stack. This
00052 // alternate stack is initialized to some known pattern (0x55, 0x55, 0x55,
00053 // ...). We then self-send this signal, and after the signal handler returns,
00054 // look at the alternate stack buffer to see what portion has been touched.
00055 //
00056 // This trick gives us the the stack footprint of the signal handler.  But the
00057 // signal handler, even before the code for it is exercised, consumes some
00058 // stack already. We however only want the stack usage of the code inside the
00059 // signal handler. To measure this accurately, we install two signal handlers:
00060 // one that does nothing and just returns, and the user-provided signal
00061 // handler. The difference between the stack consumption of these two signals
00062 // handlers should give us the stack foorprint of interest.
00063 
00064 void EmptySignalHandler(int) {}
00065 
00066 // This is arbitrary value, and could be increase further, at the cost of
00067 // memset()ting it all to known sentinel value.
00068 constexpr int kAlternateStackSize = 64 << 10;  // 64KiB
00069 
00070 constexpr int kSafetyMargin = 32;
00071 constexpr char kAlternateStackFillValue = 0x55;
00072 
00073 // These helper functions look at the alternate stack buffer, and figure
00074 // out what portion of this buffer has been touched - this is the stack
00075 // consumption of the signal handler running on this alternate stack.
00076 // This function will return -1 if the alternate stack buffer has not been
00077 // touched. It will abort the program if the buffer has overflowed or is about
00078 // to overflow.
00079 int GetStackConsumption(const void* const altstack) {
00080   const char* begin;
00081   int increment;
00082   if (kStackGrowsDown) {
00083     begin = reinterpret_cast<const char*>(altstack);
00084     increment = 1;
00085   } else {
00086     begin = reinterpret_cast<const char*>(altstack) + kAlternateStackSize - 1;
00087     increment = -1;
00088   }
00089 
00090   for (int usage_count = kAlternateStackSize; usage_count > 0; --usage_count) {
00091     if (*begin != kAlternateStackFillValue) {
00092       ABSL_RAW_CHECK(usage_count <= kAlternateStackSize - kSafetyMargin,
00093                      "Buffer has overflowed or is about to overflow");
00094       return usage_count;
00095     }
00096     begin += increment;
00097   }
00098 
00099   ABSL_RAW_LOG(FATAL, "Unreachable code");
00100   return -1;
00101 }
00102 
00103 }  // namespace
00104 
00105 int GetSignalHandlerStackConsumption(void (*signal_handler)(int)) {
00106   // The alt-signal-stack cannot be heap allocated because there is a
00107   // bug in glibc-2.2 where some signal handler setup code looks at the
00108   // current stack pointer to figure out what thread is currently running.
00109   // Therefore, the alternate stack must be allocated from the main stack
00110   // itself.
00111   void* altstack = mmap(nullptr, kAlternateStackSize, PROT_READ | PROT_WRITE,
00112                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
00113   ABSL_RAW_CHECK(altstack != MAP_FAILED, "mmap() failed");
00114 
00115   // Set up the alt-signal-stack (and save the older one).
00116   stack_t sigstk;
00117   memset(&sigstk, 0, sizeof(sigstk));
00118   stack_t old_sigstk;
00119   sigstk.ss_sp = altstack;
00120   sigstk.ss_size = kAlternateStackSize;
00121   sigstk.ss_flags = 0;
00122   ABSL_RAW_CHECK(sigaltstack(&sigstk, &old_sigstk) == 0,
00123                  "sigaltstack() failed");
00124 
00125   // Set up SIGUSR1 and SIGUSR2 signal handlers (and save the older ones).
00126   struct sigaction sa;
00127   memset(&sa, 0, sizeof(sa));
00128   struct sigaction old_sa1, old_sa2;
00129   sigemptyset(&sa.sa_mask);
00130   sa.sa_flags = SA_ONSTACK;
00131 
00132   // SIGUSR1 maps to EmptySignalHandler.
00133   sa.sa_handler = EmptySignalHandler;
00134   ABSL_RAW_CHECK(sigaction(SIGUSR1, &sa, &old_sa1) == 0, "sigaction() failed");
00135 
00136   // SIGUSR2 maps to signal_handler.
00137   sa.sa_handler = signal_handler;
00138   ABSL_RAW_CHECK(sigaction(SIGUSR2, &sa, &old_sa2) == 0, "sigaction() failed");
00139 
00140   // Send SIGUSR1 signal and measure the stack consumption of the empty
00141   // signal handler.
00142   // The first signal might use more stack space. Run once and ignore the
00143   // results to get that out of the way.
00144   ABSL_RAW_CHECK(kill(getpid(), SIGUSR1) == 0, "kill() failed");
00145 
00146   memset(altstack, kAlternateStackFillValue, kAlternateStackSize);
00147   ABSL_RAW_CHECK(kill(getpid(), SIGUSR1) == 0, "kill() failed");
00148   int base_stack_consumption = GetStackConsumption(altstack);
00149 
00150   // Send SIGUSR2 signal and measure the stack consumption of signal_handler.
00151   ABSL_RAW_CHECK(kill(getpid(), SIGUSR2) == 0, "kill() failed");
00152   int signal_handler_stack_consumption = GetStackConsumption(altstack);
00153 
00154   // Now restore the old alt-signal-stack and signal handlers.
00155   ABSL_RAW_CHECK(sigaltstack(&old_sigstk, nullptr) == 0,
00156                  "sigaltstack() failed");
00157   ABSL_RAW_CHECK(sigaction(SIGUSR1, &old_sa1, nullptr) == 0,
00158                  "sigaction() failed");
00159   ABSL_RAW_CHECK(sigaction(SIGUSR2, &old_sa2, nullptr) == 0,
00160                  "sigaction() failed");
00161 
00162   ABSL_RAW_CHECK(munmap(altstack, kAlternateStackSize) == 0, "munmap() failed");
00163   if (signal_handler_stack_consumption != -1 && base_stack_consumption != -1) {
00164     return signal_handler_stack_consumption - base_stack_consumption;
00165   }
00166   return -1;
00167 }
00168 
00169 }  // namespace debugging_internal
00170 }  // namespace absl
00171 
00172 #endif  // ABSL_INTERNAL_HAVE_DEBUGGING_STACK_CONSUMPTION


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15