Bug 1492121 - Make memory allocations compatible between mozglue and non-mozglue calls - r? draft
authorGerald Squelart <gsquelart@mozilla.com>
Mon, 15 Apr 2019 13:38:46 +1000
changeset 2012814 7359a1284b290cd0b8a0f2860c43fd5b078886fc
parent 2012813 e568a6a8f36ec8acd49c7f9a252d3cfb6566a890
child 2012815 3671f44f9f6960b41a24868071604b9194936c30
push id364696
push usergsquelart@mozilla.com
push dateTue, 21 May 2019 01:31:17 +0000
treeherdertry@4357b9137320 [default view] [failures only]
bugs1492121
milestone69.0a1
Bug 1492121 - Make memory allocations compatible between mozglue and non-mozglue calls - r? By default, mozglue code will use OS-provided allocators, but this can lead to issues if/when objects are allocated one way but destroyed another way (e.g., because they were transferred to/from outside code.) To fix this, a new header core/CppMemory.h must be #included by every compilation unit, so that all local built objects will use Mozilla allocators. MozProfiler should now be usable; tests and more improvements to follow.
mozglue/mozprofiler/core/CppMemory.h
mozglue/mozprofiler/core/EHABIStackWalk.cpp
mozglue/mozprofiler/core/PageInformation.cpp
mozglue/mozprofiler/core/ProfileBuffer.cpp
mozglue/mozprofiler/core/ProfileBufferEntry.cpp
mozglue/mozprofiler/core/ProfileJSONWriter.cpp
mozglue/mozprofiler/core/ProfiledThreadData.cpp
mozglue/mozprofiler/core/ProfilerBacktrace.cpp
mozglue/mozprofiler/core/ProfilerMarkerPayload.cpp
mozglue/mozprofiler/core/ProfilingCategory.cpp
mozglue/mozprofiler/core/ProfilingStack.cpp
mozglue/mozprofiler/core/RegisteredThread.cpp
mozglue/mozprofiler/core/VTuneProfiler.cpp
mozglue/mozprofiler/core/platform.cpp
mozglue/mozprofiler/core/shared-libraries-linux.cc
mozglue/mozprofiler/core/shared-libraries-macos.cc
mozglue/mozprofiler/core/shared-libraries-win32.cc
mozglue/mozprofiler/lul/AutoObjectMapper.cpp
mozglue/mozprofiler/lul/LulCommon.cpp
mozglue/mozprofiler/lul/LulDwarf.cpp
mozglue/mozprofiler/lul/LulDwarfSummariser.cpp
mozglue/mozprofiler/lul/LulElf.cpp
mozglue/mozprofiler/lul/LulMain.cpp
mozglue/mozprofiler/lul/platform-linux-lul.cpp
tools/profiler/core/platform.cpp
new file mode 100644
--- /dev/null
+++ b/mozglue/mozprofiler/core/CppMemory.h
@@ -0,0 +1,26 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * Include this at the beginning of all cpp's in mozglue/mozprofiler!
+ */
+
+#ifndef CppMemory_h__
+#define CppMemory_h__
+
+#ifdef MOZ_MEMORY
+#  define MOZ_MEMORY_IMPL
+#  include "mozmemory_wrap.h"
+#  define MALLOC_FUNCS MALLOC_FUNCS_MALLOC
+// See mozmemory_wrap.h for more details. This file is part of libmozglue, so
+// it needs to use _impl suffixes.
+#  define MALLOC_DECL(name, return_type, ...) \
+    MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
+#  include "malloc_decls.h"
+#  include "mozilla/mozalloc.h"
+#endif
+
+#endif  // CppMemory_h__
--- a/mozglue/mozprofiler/core/EHABIStackWalk.cpp
+++ b/mozglue/mozprofiler/core/EHABIStackWalk.cpp
@@ -17,16 +17,18 @@
  * possible places where an async signal could occur (e.g., in a
  * prologue or epilogue), this bounds-checks all stack accesses.
  *
  * This file uses "struct" for structures in the exception tables and
  * "class" otherwise.  We should avoid violating the C++11
  * standard-layout rules in the former.
  */
 
+#include "CppMemory.h"
+
 #include "EHABIStackWalk.h"
 
 #include "MozSharedLibraries.h"
 #include "platform.h"
 
 #include "mozilla/Atomics.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/DebugOnly.h"
--- a/mozglue/mozprofiler/core/PageInformation.cpp
+++ b/mozglue/mozprofiler/core/PageInformation.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "PageInformation.h"
 
 #include "MozProfileJSONWriter.h"
 
 PageInformation::PageInformation(const std::string& aDocShellId,
                                  uint32_t aDocShellHistoryId,
                                  const std::string& aUrl, bool aIsSubFrame)
     : mDocShellId(aDocShellId),
--- a/mozglue/mozprofiler/core/ProfileBuffer.cpp
+++ b/mozglue/mozprofiler/core/ProfileBuffer.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "ProfileBuffer.h"
 
 #include "ProfilerMarker.h"
 
 #include "mozilla/MathAlgorithms.h"
 
 using namespace mozilla;
 
--- a/mozglue/mozprofiler/core/ProfileBufferEntry.cpp
+++ b/mozglue/mozprofiler/core/ProfileBufferEntry.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "ProfileBufferEntry.h"
 
 #include "platform.h"
 #include "ProfileBuffer.h"
 
 #include "mozilla/Logging.h"
 #include "mozilla/Sprintf.h"
 #include "mozilla/StackWalk.h"
--- a/mozglue/mozprofiler/core/ProfileJSONWriter.cpp
+++ b/mozglue/mozprofiler/core/ProfileJSONWriter.cpp
@@ -1,13 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "MozProfileJSONWriter.h"
 
 #include "mozilla/HashFunctions.h"
 
 void ChunkedJSONWriteFunc::Write(const char* aStr) {
   MOZ_ASSERT(mChunkPtr >= mChunkList.back().get() && mChunkPtr <= mChunkEnd);
   MOZ_ASSERT(mChunkEnd >= mChunkList.back().get() + mChunkLengths.back());
   MOZ_ASSERT(*mChunkPtr == '\0');
--- a/mozglue/mozprofiler/core/ProfiledThreadData.cpp
+++ b/mozglue/mozprofiler/core/ProfiledThreadData.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "ProfiledThreadData.h"
 
 #include "ProfileBuffer.h"
 #include "MozProfileJSONWriter.h"
 
 #if defined(GP_OS_darwin)
 #  include <pthread.h>
 #endif
--- a/mozglue/mozprofiler/core/ProfilerBacktrace.cpp
+++ b/mozglue/mozprofiler/core/ProfilerBacktrace.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "ProfilerBacktrace.h"
 
 #include "ProfileBuffer.h"
 #include "ProfiledThreadData.h"
 #include "MozProfileJSONWriter.h"
 #include "ThreadInfo.h"
 
 ProfilerBacktrace::ProfilerBacktrace(const char* aName, int aThreadId,
--- a/mozglue/mozprofiler/core/ProfilerMarkerPayload.cpp
+++ b/mozglue/mozprofiler/core/ProfilerMarkerPayload.cpp
@@ -1,13 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "MozProfilerMarkerPayload.h"
 
 #include "MozProfiler.h"
 #include "ProfileBufferEntry.h"
 #include "MozProfileJSONWriter.h"
 #include "ProfilerBacktrace.h"
 
 #include "mozilla/Maybe.h"
--- a/mozglue/mozprofiler/core/ProfilingCategory.cpp
+++ b/mozglue/mozprofiler/core/ProfilingCategory.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * vim: set ts=8 sts=2 et sw=2 tw=80:
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "MozProfilingCategory.h"
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Assertions.h"
 
 namespace JS {
 
 // clang-format off
--- a/mozglue/mozprofiler/core/ProfilingStack.cpp
+++ b/mozglue/mozprofiler/core/ProfilingStack.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * vim: set ts=8 sts=2 et sw=2 tw=80:
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "MozProfilingStack.h"
 
 #include "mozilla/IntegerRange.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/UniquePtrExtensions.h"
 
 #include <algorithm>
 
--- a/mozglue/mozprofiler/core/RegisteredThread.cpp
+++ b/mozglue/mozprofiler/core/RegisteredThread.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "RegisteredThread.h"
 
 RegisteredThread::RegisteredThread(ThreadInfo* aInfo, void* aStackTop)
     : mRacyRegisteredThread(aInfo->ThreadId()),
       mPlatformData(AllocPlatformData(aInfo->ThreadId())),
       mStackTop(aStackTop),
       mThreadInfo(aInfo) {
   // We don't have to guess on mac
--- a/mozglue/mozprofiler/core/VTuneProfiler.cpp
+++ b/mozglue/mozprofiler/core/VTuneProfiler.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #ifdef XP_WIN
 #  undef UNICODE
 #  undef _UNICODE
 #endif
 
 #include "VTuneProfiler.h"
 
 #include <memory>
--- a/mozglue/mozprofiler/core/platform.cpp
+++ b/mozglue/mozprofiler/core/platform.cpp
@@ -21,16 +21,18 @@
 //   register values.
 //
 // - A "backtrace" sample is the simplest kind. It is done in response to an
 //   API call (profiler_suspend_and_sample_thread()). It involves getting a
 //   stack trace via a ProfilerStackCollector; it does not write to a
 //   ProfileBuffer. The sampling is done from off-thread, and so uses
 //   SuspendAndSampleAndResumeThread() to get the register values.
 
+#include "CppMemory.h"
+
 #include "platform.h"
 
 #include "MozProfiler.h"
 #include "PageInformation.h"
 #include "ProfiledThreadData.h"
 #include "ProfilerBacktrace.h"
 #include "ProfileBuffer.h"
 #include "MozProfilerMarkerPayload.h"
--- a/mozglue/mozprofiler/core/shared-libraries-linux.cc
+++ b/mozglue/mozprofiler/core/shared-libraries-linux.cc
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "MozSharedLibraries.h"
 
 #define PATH_MAX_TOSTRING(x) #x
 #define PATH_MAX_STRING(x) PATH_MAX_TOSTRING(x)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
--- a/mozglue/mozprofiler/core/shared-libraries-macos.cc
+++ b/mozglue/mozprofiler/core/shared-libraries-macos.cc
@@ -1,13 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "MozSharedLibraries.h"
 
 #include "mozilla/Unused.h"
 #include <AvailabilityMacros.h>
 
 #include <dlfcn.h>
 #include <mach-o/arch.h>
 #include <mach-o/dyld_images.h>
--- a/mozglue/mozprofiler/core/shared-libraries-win32.cc
+++ b/mozglue/mozprofiler/core/shared-libraries-win32.cc
@@ -1,13 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include <windows.h>
 #include <dbghelp.h>
 #include <sstream>
 #include <psapi.h>
 
 #include "MozSharedLibraries.h"
 
 #include "mozilla/UniquePtr.h"
--- a/mozglue/mozprofiler/lul/AutoObjectMapper.cpp
+++ b/mozglue/mozprofiler/lul/AutoObjectMapper.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include <sys/mman.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Sprintf.h"
--- a/mozglue/mozprofiler/lul/LulCommon.cpp
+++ b/mozglue/mozprofiler/lul/LulCommon.cpp
@@ -32,16 +32,18 @@
 
 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
 
 // This file is derived from the following files in
 // toolkit/crashreporter/google-breakpad:
 //   src/common/module.cc
 //   src/common/unique_string.cc
 
+#include "CppMemory.h"
+
 // There's no internal-only interface for LulCommon.  Hence include
 // the external interface directly.
 #include "LulCommonExt.h"
 
 #include <stdlib.h>
 #include <string.h>
 
 #include <string>
--- a/mozglue/mozprofiler/lul/LulDwarf.cpp
+++ b/mozglue/mozprofiler/lul/LulDwarf.cpp
@@ -36,16 +36,18 @@
 // and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details.
 
 // This file is derived from the following files in
 // toolkit/crashreporter/google-breakpad:
 //   src/common/dwarf/bytereader.cc
 //   src/common/dwarf/dwarf2reader.cc
 //   src/common/dwarf_cfi_to_module.cc
 
+#include "CppMemory.h"
+
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 #include <map>
 #include <stack>
 #include <string>
--- a/mozglue/mozprofiler/lul/LulDwarfSummariser.cpp
+++ b/mozglue/mozprofiler/lul/LulDwarfSummariser.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "LulDwarfSummariser.h"
 
 #include "LulDwarfExt.h"
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Sprintf.h"
 
 // Set this to 1 for verbose logging
--- a/mozglue/mozprofiler/lul/LulElf.cpp
+++ b/mozglue/mozprofiler/lul/LulElf.cpp
@@ -40,16 +40,18 @@
 // it out as a Breakpad symbol file.
 
 // This file is derived from the following files in
 // toolkit/crashreporter/google-breakpad:
 //   src/common/linux/dump_symbols.cc
 //   src/common/linux/elfutils.cc
 //   src/common/linux/file_id.cc
 
+#include "CppMemory.h"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <libgen.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <unistd.h>
--- a/mozglue/mozprofiler/lul/LulMain.cpp
+++ b/mozglue/mozprofiler/lul/LulMain.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include "LulMain.h"
 
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>  // write(), only for testing LUL
 
 #include <algorithm>  // std::sort
--- a/mozglue/mozprofiler/lul/platform-linux-lul.cpp
+++ b/mozglue/mozprofiler/lul/platform-linux-lul.cpp
@@ -1,13 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "CppMemory.h"
+
 #include <stdio.h>
 #include <signal.h>
 #include <string.h>
 #include <stdlib.h>
 #include <time.h>
 
 #include "platform.h"
 #include "PlatformMacros.h"
--- a/tools/profiler/core/platform.cpp
+++ b/tools/profiler/core/platform.cpp
@@ -209,19 +209,20 @@ static uint32_t AvailableFeatures() {
   ProfilerFeature::ClearStackWalk(features);
 #endif
 #if !defined(MOZ_TASK_TRACER)
   ProfilerFeature::ClearTaskTracer(features);
 #endif
 #if !(defined(MOZ_REPLACE_MALLOC) && defined(MOZ_PROFILER_MEMORY))
   ProfilerFeature::ClearMemory(features);
 #endif
-  if (!JS::TraceLoggerSupported()) {
-    ProfilerFeature::ClearJSTracer(features);
-  }
+  // FIXME
+  // if (!JS::TraceLoggerSupported()) {
+  //   ProfilerFeature::ClearJSTracer(features);
+  // }
 
   return features;
 }
 
 // Default features common to all contexts (even if not available).
 static uint32_t DefaultFeatures() {
   return ProfilerFeature::Java | ProfilerFeature::JS | ProfilerFeature::Leaf |
          ProfilerFeature::StackWalk | ProfilerFeature::Threads |