Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5756)

Unified Diff: base/process/memory_linux.cc

Issue 55333002: Make possible to check memory allocations inside chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix component and windows build Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/process/memory_linux.cc
diff --git a/base/process/memory_linux.cc b/base/process/memory_linux.cc
index 6bed68bd8320346b1ff98a39417e604e0339b020..2ab531a1edcaf53473f0a1811d788af67ca1edc7 100644
--- a/base/process/memory_linux.cc
+++ b/base/process/memory_linux.cc
@@ -12,6 +12,10 @@
#include "base/process/internal_linux.h"
#include "base/strings/string_number_conversions.h"
+#if defined(USE_TCMALLOC)
+#include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h"
+#endif
+
namespace base {
size_t g_oom_size = 0U;
@@ -125,6 +129,47 @@ int posix_memalign(void** ptr, size_t alignment, size_t size) {
#endif // !*_SANITIZER
+#if defined(USE_TCMALLOC)
+static tc_malloc_skip_new_handler_function tc_malloc_skip_new_handler = NULL;
+
+void SetTCMallocSkipNewHandlerFunction(
+ tc_malloc_skip_new_handler_function function) {
willchan no longer on Chromium 2014/01/16 01:30:05 Please CHECK(!tc_malloc_skip_new_handler); It shou
+ tc_malloc_skip_new_handler = function;
+}
+#endif
+
+bool UncheckedMalloc(size_t size, void** result) {
willchan no longer on Chromium 2014/01/16 01:30:05 I'm confused in this function. Why don't we always
kbalazs 2014/01/16 02:24:50 In case of LIBC_GLIBC && !USE_TCMALLOC we override
willchan no longer on Chromium 2014/01/16 02:54:44 I see. That's a good point...I'm trying to think u
+#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
+ defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
+ (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC))
+ *result = malloc(size);
+#elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
+ *result = __libc_malloc(size);
+#elif defined(USE_TCMALLOC)
+ *result = tc_malloc_skip_new_handler ?
+ (*tc_malloc_skip_new_handler)(size) : malloc(size);
+#else
+#error Not implemented
willchan no longer on Chromium 2014/01/16 01:30:05 I don't believe it's possible to ever hit this #el
+#endif
+ return *result;
+}
+
+bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
+ const size_t alloc_size = num_items * size;
+
+ // Overflow check
+ if (size && ((alloc_size / size) != num_items)) {
+ result = NULL;
+ return false;
+ }
+
+ if (!UncheckedMalloc(alloc_size, result))
+ return false;
+
+ memset(result, 0, alloc_size);
+ return true;
+}
+
void EnableTerminationOnHeapCorruption() {
// On Linux, there nothing to do AFAIK.
}

Powered by Google App Engine
This is Rietveld 408576698