Bug 557060: Interpose _Throw() on MSVC and centralize pseudo-throw code in mozalloc. r=ehsan sr=bsmedberg
authorChris Jones <jones.chris.g@gmail.com>
Tue, 20 Apr 2010 15:12:02 -0500
changeset 41040 f0ccd76765b96bfac70b4014d01d69621897f00b
parent 41039 c2085ab04bfe4ce581126f68ebd624f385e32832
child 41041 f7f54ede7aea67196102cfa13803f03f10a32565
push idunknown
push userunknown
push dateunknown
reviewersehsan, bsmedberg
bugs557060
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 557060: Interpose _Throw() on MSVC and centralize pseudo-throw code in mozalloc. r=ehsan sr=bsmedberg
config/config.mk
config/gcc-stl-wrapper.template.h
config/make-stl-wrappers.py
config/msvc-stl-wrapper.template.h
config/stl-headers
embedding/browser/gtk/tests/Makefile.in
js/src/config/config.mk
memory/mozalloc/Makefile.in
memory/mozalloc/mozalloc.h
memory/mozalloc/mozalloc_abort.cpp
memory/mozalloc/mozalloc_abort.h
memory/mozalloc/mozalloc_oom.cpp
memory/mozalloc/throw_gcc.h
memory/mozalloc/throw_msvc.cpp
memory/mozalloc/throw_msvc.h
profile/dirserviceprovider/standalone/Makefile.in
xpcom/base/nscore.h
xpcom/glue/Makefile.in
xpcom/glue/functexcept.h
xpcom/glue/standalone/Makefile.in
xpcom/tools/registry/Makefile.in
--- a/config/config.mk
+++ b/config/config.mk
@@ -521,17 +521,17 @@ ifeq ($(OS_ARCH),Darwin)
 # Darwin doesn't cross-compile, so just set both types of flags here.
 HOST_CMFLAGS += -fobjc-exceptions
 HOST_CMMFLAGS += -fobjc-exceptions
 OS_COMPILE_CMFLAGS += -fobjc-exceptions
 OS_COMPILE_CMMFLAGS += -fobjc-exceptions
 endif
 
 COMPILE_CFLAGS	= $(VISIBILITY_FLAGS) $(DEFINES) $(INCLUDES) $(DSO_CFLAGS) $(DSO_PIC_CFLAGS) $(CFLAGS) $(RTL_FLAGS) $(OS_COMPILE_CFLAGS)
-COMPILE_CXXFLAGS = $(VISIBILITY_FLAGS) $(STL_FLAGS) $(DEFINES) $(INCLUDES) $(DSO_CFLAGS) $(DSO_PIC_CFLAGS) $(CXXFLAGS) $(RTL_FLAGS) $(OS_COMPILE_CXXFLAGS)
+COMPILE_CXXFLAGS = $(STL_FLAGS) $(VISIBILITY_FLAGS) $(DEFINES) $(INCLUDES) $(DSO_CFLAGS) $(DSO_PIC_CFLAGS) $(CXXFLAGS) $(RTL_FLAGS) $(OS_COMPILE_CXXFLAGS)
 COMPILE_CMFLAGS = $(OS_COMPILE_CMFLAGS)
 COMPILE_CMMFLAGS = $(OS_COMPILE_CMMFLAGS)
 
 ifndef CROSS_COMPILE
 HOST_CFLAGS += $(RTL_FLAGS)
 endif
 
 #
--- a/config/gcc-stl-wrapper.template.h
+++ b/config/gcc-stl-wrapper.template.h
@@ -40,23 +40,33 @@
 
 #ifndef mozilla_${HEADER}_h
 #define mozilla_${HEADER}_h
 
 #if __EXCEPTIONS
 #  error "STL code can only be used with -fno-exceptions"
 #endif
 
+// Silence "warning: #include_next is a GCC extension"
+#pragma GCC system_header
+
+// mozalloc.h wants <new>; break the cycle by always explicitly
+// including <new> here.  NB: this is a tad sneaky.  Sez the gcc docs:
+//
+//    `#include_next' does not distinguish between <file> and "file"
+//    inclusion, nor does it check that the file you specify has the
+//    same name as the current file. It simply looks for the file
+//    named, starting with the directory in the search path after the
+//    one where the current file was found.
+#include_next <new>
+
 // See if we're in code that can use mozalloc.  NB: this duplicates
 // code in nscore.h because nscore.h pulls in prtypes.h, and chromium
 // can't build with that being included before base/basictypes.h.
 #if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
-#  include <new>              // to give mozalloc std::bad_alloc
-#  include <stdlib.h>         // to give mozalloc malloc/free decls
-#  include <string.h>
 #  include "mozilla/mozalloc.h"
 #else
 #  error "STL code can only be used with infallible ::operator new()"
 #endif
 
 #if defined(DEBUG) && !defined(_GLIBCXX_DEBUG)
 // Enable checked iterators and other goodies
 //
@@ -73,13 +83,13 @@
 
 // gcc calls a __throw_*() function from bits/functexcept.h when it
 // wants to "throw an exception".  functexcept exists nominally to
 // support -fno-exceptions, but since we'll always use the system
 // libstdc++, and it's compiled with exceptions, then in practice
 // these __throw_*() functions will always throw exceptions (shades of
 // -fshort-wchar).  We don't want that and so define our own inlined
 // __throw_*().
-#ifndef mozilla_functexcept_h
-#  include "mozilla/functexcept.h"
+#ifndef mozilla_throw_gcc_h
+#  include "mozilla/throw_gcc.h"
 #endif
 
 #endif  // if mozilla_${HEADER}_h
--- a/config/make-stl-wrappers.py
+++ b/config/make-stl-wrappers.py
@@ -57,27 +57,29 @@ def header_path(header, compiler):
 def is_comment(line):
     return re.match(r'\s*#.*', line)
 
 def main(outdir, compiler, template_file, header_list_file):
     if not os.path.isdir(outdir):
         os.mkdir(outdir)
 
     template = open(template_file, 'r').read()
-    
+    path_to_new = header_path('new', compiler)
+
     for header in open(header_list_file, 'r'):
         header = header.rstrip()
         if 0 == len(header) or is_comment(header):
             continue
 
         path = header_path(header, compiler)
         try:
             f = open(os.path.join(outdir, header), 'w')
             f.write(string.Template(template).substitute(HEADER=header,
-                                                         HEADER_PATH=path))
+                                                         HEADER_PATH=path,
+                                                         NEW_HEADER_PATH=path_to_new))
         finally:
             f.close()
 
 
 if __name__ == '__main__':
     if 5 != len(sys.argv):
         print >>sys.stderr, """Usage:
   python %s OUT_DIR ('msvc'|'gcc') TEMPLATE_FILE HEADER_LIST_FILE
--- a/config/msvc-stl-wrapper.template.h
+++ b/config/msvc-stl-wrapper.template.h
@@ -40,23 +40,32 @@
 
 #ifndef mozilla_${HEADER}_h
 #define mozilla_${HEADER}_h
 
 #if _HAS_EXCEPTIONS
 #  error "STL code can only be used with -fno-exceptions"
 #endif
 
+// Code built with !_HAS_EXCEPTIONS calls std::_Throw(), but the win2k
+// CRT doesn't export std::_Throw().  So we define it.
+#ifndef mozilla_Throw_h
+#  include "mozilla/throw_msvc.h"
+#endif
+
+// Code might include <new> before other wrapped headers, but <new>
+// includes <exception> and so we want to wrap it.  But mozalloc.h
+// wants <new> also, so we break the cycle by always explicitly
+// including <new> here.
+#include <${NEW_HEADER_PATH}>
+
 // See if we're in code that can use mozalloc.  NB: this duplicates
 // code in nscore.h because nscore.h pulls in prtypes.h, and chromium
 // can't build with that being included before base/basictypes.h.
 #if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
-#  include <new>              // to give mozalloc std::bad_alloc
-#  include <stdlib.h>         // to give mozalloc malloc/free decls
-#  include <string.h>
 #  include "mozilla/mozalloc.h"
 #else
 #  error "STL code can only be used with infallible ::operator new()"
 #endif
 
 #ifdef DEBUG
 // From
 //   http://msdn.microsoft.com/en-us/library/aa985982%28VS.80%29.aspx
--- a/config/stl-headers
+++ b/config/stl-headers
@@ -7,13 +7,22 @@
 # At build time, each header listed here is converted into a "wrapper
 # header" that is installed into dist/stl_includes.
 #
 # If you would like to request a new STL header <foo> be added, please
 # file a Core:XPCOM bug with a title like "STL: Review exception
 # safety of <foo> for gcc and MSVC".
 #
 
+new
+
 # FIXME: these headers haven't been reviewed yet, but we use them
-# unsafely in modules/libpr0n, so we might as well prevent it from
+# unsafely in Gecko, so we might as well prevent them from
 # throwing exceptions
 algorithm
+deque
+iostream
+list
+map
+memory
+stack
+string
 vector
--- a/embedding/browser/gtk/tests/Makefile.in
+++ b/embedding/browser/gtk/tests/Makefile.in
@@ -71,16 +71,17 @@ endif
 
 include $(topsrcdir)/config/config.mk
 
 include $(topsrcdir)/config/rules.mk
 
 LIBS += $(XPCOM_STANDALONE_GLUE_LDOPTS)
 
 DEFINES += -DXPCOM_GLUE
+STL_FLAGS =
 
 CXXFLAGS	+= $(MOZ_GTK2_CFLAGS)
 
 ifdef ENABLE_GNOME
 CXXFLAGS	+= `gnome-config --cflags gnomeui`
 EXTRA_LIBS	+= `gnome-config --libs gnomeui`
 endif
 
--- a/js/src/config/config.mk
+++ b/js/src/config/config.mk
@@ -521,17 +521,17 @@ ifeq ($(OS_ARCH),Darwin)
 # Darwin doesn't cross-compile, so just set both types of flags here.
 HOST_CMFLAGS += -fobjc-exceptions
 HOST_CMMFLAGS += -fobjc-exceptions
 OS_COMPILE_CMFLAGS += -fobjc-exceptions
 OS_COMPILE_CMMFLAGS += -fobjc-exceptions
 endif
 
 COMPILE_CFLAGS	= $(VISIBILITY_FLAGS) $(DEFINES) $(INCLUDES) $(DSO_CFLAGS) $(DSO_PIC_CFLAGS) $(CFLAGS) $(RTL_FLAGS) $(OS_COMPILE_CFLAGS)
-COMPILE_CXXFLAGS = $(VISIBILITY_FLAGS) $(STL_FLAGS) $(DEFINES) $(INCLUDES) $(DSO_CFLAGS) $(DSO_PIC_CFLAGS) $(CXXFLAGS) $(RTL_FLAGS) $(OS_COMPILE_CXXFLAGS)
+COMPILE_CXXFLAGS = $(STL_FLAGS) $(VISIBILITY_FLAGS) $(DEFINES) $(INCLUDES) $(DSO_CFLAGS) $(DSO_PIC_CFLAGS) $(CXXFLAGS) $(RTL_FLAGS) $(OS_COMPILE_CXXFLAGS)
 COMPILE_CMFLAGS = $(OS_COMPILE_CMFLAGS)
 COMPILE_CMMFLAGS = $(OS_COMPILE_CMMFLAGS)
 
 ifndef CROSS_COMPILE
 HOST_CFLAGS += $(RTL_FLAGS)
 endif
 
 #
--- a/memory/mozalloc/Makefile.in
+++ b/memory/mozalloc/Makefile.in
@@ -39,16 +39,20 @@
 DEPTH		= ../..
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 VISIBILITY_FLAGS=
+STL_FLAGS	=
+ifdef _MSC_VER
+STL_FLAGS	= -D_HAS_EXCEPTIONS=0
+endif
 
 MODULE		= mozalloc
 LIBRARY_NAME	= mozalloc
 FORCE_SHARED_LIB= 1
 DIST_INSTALL 	= 1
 
 ifeq (,$(filter-out WINCE,$(OS_ARCH)))
 # mozalloc gets its allocation methods from the shunt, which gets their 
@@ -57,16 +61,32 @@ OS_LIBS		=
 endif
 
 ifeq (,$(filter-out OS2,$(OS_ARCH)))
 # The strndup declaration in string.h is in an ifdef __USE_GNU section
 DEFINES		+= -D_GNU_SOURCE
 endif
 
 EXPORTS_NAMESPACES 	= mozilla
-EXPORTS_mozilla 	= mozalloc.h mozalloc_macro_wrappers.h mozalloc_oom.h
+EXPORTS_mozilla 	=			\
+	mozalloc.h		 		\
+	mozalloc_abort.h			\
+	mozalloc_macro_wrappers.h 		\
+	mozalloc_oom.h				\
+	$(NULL)
 
-CPPSRCS =					\
+CPPSRCS 		=			\
 	mozalloc.cpp				\
+	mozalloc_abort.cpp			\
 	mozalloc_oom.cpp			\
 	$(NULL)
 
+ifdef WRAP_STL_INCLUDES
+ifdef GCC_VERSION
+EXPORTS_mozilla	+= throw_gcc.h
+endif
+ifdef _MSC_VER
+EXPORTS_mozilla	+= throw_msvc.h
+CPPSRCS		+= throw_msvc.cpp
+endif
+endif  # WRAP_STL_INCLUDES
+
 include $(topsrcdir)/config/rules.mk
--- a/memory/mozalloc/mozalloc.h
+++ b/memory/mozalloc/mozalloc.h
@@ -36,28 +36,26 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef mozilla_mozalloc_h
 #define mozilla_mozalloc_h
 
-
 /*
  * https://bugzilla.mozilla.org/show_bug.cgi?id=427099
  */
 
-/* 
- * NB: this header depends on the symbols malloc(), free(), and
- * std::bad_alloc.  But because this header is used in situations
- * where malloc/free have different visibility, we rely on code
- * including this header to provide the declarations of malloc/free.
- * I.e., we don't #include <stdlib.h> or <new> on purpose.
- */
+#include <stdlib.h>
+#include <string.h>
+#if defined(__cplusplus)
+#  include <new>
+#endif
+
 
 #if defined(MOZALLOC_EXPORT)
 // do nothing: it's been defined to __declspec(dllexport) by
 // mozalloc*.cpp on platforms where that's required
 #elif defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec))
 #  define MOZALLOC_EXPORT __declspec(dllimport)
 #elif defined(HAVE_VISIBILITY_ATTRIBUTE)
 /* Make sure symbols are still exported even if we're wrapped in a
new file mode 100644
--- /dev/null
+++ b/memory/mozalloc/mozalloc_abort.cpp
@@ -0,0 +1,91 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: sw=4 ts=4 et :
+ */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Code
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Chris Jones <jones.chris.g@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <stdio.h>
+#include <stdlib.h>             // for abort()
+
+#if defined(_WIN32)
+#  include <signal.h>           // for raise
+#elif defined(XP_UNIX)
+#  include <unistd.h>           // for _exit
+#endif
+
+#if defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec))
+#  define MOZALLOC_EXPORT __declspec(dllexport)
+#endif
+
+#include "mozilla/mozalloc_abort.h"
+
+static int gDummyCounter;
+
+void
+mozalloc_abort(const char* const msg)
+{
+    fputs(msg, stderr);
+    fputs("\n", stderr);
+
+    // XXX/cjones: most of this function was copied from
+    // xpcom/base/nsDebugImpl.cpp:Abort(), except that we assume on
+    // UNIX-like platforms can directly abort() rather than need to go
+    // through PR_Abort(). we don't want this code to rely on NSPR.
+
+    // FIXME/bug 558928: improve implementation for windows/wince
+
+#if defined(_WIN32)
+#  if !defined(WINCE)
+    //This should exit us
+    raise(SIGABRT);
+#  endif
+    //If we are ignored exit this way..
+    _exit(3);
+#elif defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
+    abort();
+#else
+#  warning not attempting to abort() on this platform
+#endif
+
+    // Still haven't aborted?  Try dereferencing null.
+    // (Written this way to lessen the likelihood of it being optimized away.)
+    gDummyCounter += *((int*) 0); // TODO annotation saying we know 
+    // this is crazy
+    
+    // Still haven't aborted?  Try _exit().
+    _exit(127);
+}
new file mode 100644
--- /dev/null
+++ b/memory/mozalloc/mozalloc_abort.h
@@ -0,0 +1,64 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: sw=4 ts=4 et :
+ */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Code
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Chris Jones <jones.chris.g@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef mozilla_mozalloc_abort_h
+#define mozilla_mozalloc_abort_h
+
+#if defined(MOZALLOC_EXPORT)
+// do nothing: it's been defined to __declspec(dllexport) by
+// mozalloc*.cpp on platforms where that's required
+#elif defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec))
+#  define MOZALLOC_EXPORT __declspec(dllimport)
+#elif defined(HAVE_VISIBILITY_ATTRIBUTE)
+/* Make sure symbols are still exported even if we're wrapped in a
+ * |visibility push(hidden)| blanket. */
+#  define MOZALLOC_EXPORT __attribute__ ((visibility ("default")))
+#else
+#  define MOZALLOC_EXPORT
+#endif
+
+/**
+ * Terminate this process in such a way that breakpad is triggered, if
+ * at all possible.
+ */
+MOZALLOC_EXPORT void mozalloc_abort(const char* const msg) NS_NORETURN;
+
+
+#endif  /* ifndef mozilla_mozalloc_abort_h */
--- a/memory/mozalloc/mozalloc_oom.cpp
+++ b/memory/mozalloc/mozalloc_oom.cpp
@@ -33,57 +33,23 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-#include <stdlib.h>             // for abort()
-
-#if defined(_WIN32)
-#  include <signal.h>           // for raise
-#elif defined(XP_UNIX)
-#  include <unistd.h>           // for _exit
-#endif
-
 #if defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec))
 #  define MOZALLOC_EXPORT __declspec(dllexport)
 #endif
 
+#include "mozilla/mozalloc_abort.h"
 #include "mozilla/mozalloc_oom.h"
 
-static int gDummyCounter;
-
 void
 mozalloc_handle_oom()
 {
     // NB: this is handle_oom() stage 1, which simply aborts on OOM.
     // we might proceed to a stage 2 in which an attempt is made to
     // reclaim memory
-
-    // XXX/cjones: most of this function was copied from
-    // xpcom/base/nsDebugImpl.cpp:Abort(), except that we assume on
-    // UNIX-like platforms can directly abort() rather than need to go
-    // through PR_Abort(). we don't want this code to rely on NSPR.
-
-#if defined(_WIN32)
-#  if !defined(WINCE)
-    //This should exit us
-    raise(SIGABRT);
-#  endif
-    //If we are ignored exit this way..
-    _exit(3);
-#elif defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
-    abort();
-#else
-#  warning not attempting to abort() on this platform
-#endif
-
-    // Still haven't aborted?  Try dereferencing null.
-    // (Written this way to lessen the likelihood of it being optimized away.)
-    gDummyCounter += *((int*) 0); // TODO annotation saying we know 
-    // this is crazy
-    
-    // Still haven't aborted?  Try _exit().
-    _exit(127);
+    mozalloc_abort("out of memory");
 }
new file mode 100644
--- /dev/null
+++ b/memory/mozalloc/throw_gcc.h
@@ -0,0 +1,153 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: sw=4 ts=4 et :
+ */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Code
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Chris Jones <jones.chris.g@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef mozilla_throw_gcc_h
+#define mozilla_throw_gcc_h
+
+#include <stdio.h>              // snprintf
+
+// For gcc, we define these inline to abort so that we're absolutely
+// certain that (i) no exceptions are thrown from Gecko; (ii) these
+// errors are always terminal and caught by breakpad.
+
+#include "mozilla/mozalloc_abort.h"
+
+namespace std {
+
+// NB: user code is not supposed to touch the std:: namespace.  We're
+// doing this after careful review because we want to define our own
+// exception throwing semantics.  Don't try this at home!
+
+inline void NS_NORETURN
+__throw_bad_exception(void)
+{
+    mozalloc_abort("fatal: STL threw bad_exception");
+}
+
+inline void NS_NORETURN
+__throw_bad_alloc(void)
+{
+    mozalloc_abort("fatal: STL threw bad_alloc");
+}
+
+inline void NS_NORETURN
+__throw_bad_cast(void)
+{
+    mozalloc_abort("fatal: STL threw bad_cast");
+}
+
+inline void NS_NORETURN
+__throw_bad_typeid(void)
+{
+    mozalloc_abort("fatal: STL threw bad_typeid");
+}
+
+inline void NS_NORETURN
+__throw_logic_error(const char* msg)
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_domain_error(const char* msg)
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_invalid_argument(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_length_error(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_out_of_range(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_runtime_error(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_range_error(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_overflow_error(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_underflow_error(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_ios_failure(const char* msg) 
+{
+    mozalloc_abort(msg);
+}
+
+inline void NS_NORETURN
+__throw_system_error(int err) 
+{
+    char error[128];
+    snprintf(error, sizeof(error)-1,
+             "fatal: STL threw system_error: %s (%d)", strerror(err), err);
+    mozalloc_abort(error);
+}
+
+} // namespace std
+
+#endif  // mozilla_throw_gcc_h
new file mode 100644
--- /dev/null
+++ b/memory/mozalloc/throw_msvc.cpp
@@ -0,0 +1,63 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: sw=4 ts=4 et :
+ */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Code
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Chris Jones <jones.chris.g@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <exception>
+
+#if defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec))
+#  define MOZALLOC_EXPORT __declspec(dllexport)
+#endif
+
+#include "mozilla/mozalloc_abort.h"
+
+namespace std {
+
+// NB: user code is not supposed to touch the std:: namespace.  We're
+// doing this after careful review because we want to define our own
+// exception throwing semantics.  Don't try this at home!
+
+__declspec(dllexport) void mozilla_Throw(const exception& e);
+
+void
+mozilla_Throw(const exception& e)
+{
+    mozalloc_abort(e.what());
+}
+
+}  // namespace std
new file mode 100644
--- /dev/null
+++ b/memory/mozalloc/throw_msvc.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: sw=4 ts=4 et :
+ */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Mozilla Code
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Chris Jones <jones.chris.g@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef mozilla_throw_msvc_h
+#define mozilla_throw_msvc_h
+
+// For MSVC, we define our own _Throw because the Win2k CRT doesn't
+// export it.
+
+#ifdef _EXCEPTION_
+#  error "Unable to wrap _Throw(); CRT _Throw() already declared"
+#endif
+
+#define _Throw  mozilla_Throw
+#include <exception>
+
+#endif  // mozilla_throw_msvc_h
--- a/profile/dirserviceprovider/standalone/Makefile.in
+++ b/profile/dirserviceprovider/standalone/Makefile.in
@@ -50,13 +50,14 @@ CPPSRCS		= $(MODULES_PROFILEDIRSERVICE_S
 # we don't want the shared lib
 FORCE_STATIC_LIB = 1
 
 include $(topsrcdir)/config/rules.mk
 
 LOCAL_INCLUDES = -I$(srcdir)/../src
 
 DEFINES += -DXPCOM_GLUE
+STL_FLAGS =
 
 export:: $(MODULES_PROFILEDIRSERVICE_SRC_CSRCS)
 	$(INSTALL) $^ .
 
 GARBAGE		= $(notdir $(MODULES_PROFILEDIRSERVICE_SRC_CSRCS))
--- a/xpcom/base/nscore.h
+++ b/xpcom/base/nscore.h
@@ -42,23 +42,16 @@
  * c++ definitions needed by nscore.h
  */
 #ifndef _XPCOM_CONFIG_H_
 #include "xpcom-config.h"
 #endif
 
 /* Definitions of functions and operators that allocate memory. */
 #if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
-#  if defined(__cplusplus)
-#    include NEW_H              /* to give mozalloc std::bad_alloc */
-#  endif
-/* include these to avoid the macro wrappers causing these headers to
- * declare system functions with moz_ prefixes */
-#  include <stdlib.h>
-#  include <string.h>
 #  include "mozilla/mozalloc.h"
 #  include "mozilla/mozalloc_macro_wrappers.h"
 #endif
 
 /**
  * Incorporate the core NSPR data types which XPCOM uses.
  */
 #include "prtypes.h"
--- a/xpcom/glue/Makefile.in
+++ b/xpcom/glue/Makefile.in
@@ -129,22 +129,16 @@ EXPORTS_mozilla = \
   BlockingResourceBase.h \
   CondVar.h \
   DeadlockDetector.h \
   Monitor.h \
   Mutex.h \
   SSE.h \
   $(NULL)
 
-ifdef WRAP_STL_INCLUDES
-ifdef GCC_VERSION
-EXPORTS_mozilla += functexcept.h
-endif
-endif
-
 SDK_LIBRARY     =                        \
 		$(LIB_PREFIX)xpcomglue_s.$(LIB_SUFFIX) \
 		$(NULL)
 
 
 # we don't want the shared lib, but we want to force the creation of a static lib.
 FORCE_STATIC_LIB = 1
 
deleted file mode 100644
--- a/xpcom/glue/functexcept.h
+++ /dev/null
@@ -1,164 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
- * vim: sw=2 ts=2 et :
- */
-/* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is Mozilla Code
- *
- * The Initial Developer of the Original Code is
- * The Mozilla Foundation.
- * Portions created by the Initial Developer are Copyright (C) 2010
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *   Chris Jones <jones.chris.g@gmail.com>
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
-#ifndef mozilla_functexcept_h
-#define mozilla_functexcept_h
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-// For gcc, we define these inline to abort so that we're absolutely
-// certain that (i) no exceptions are thrown from Gecko; (ii) these
-// errors are always terminal and caught by breakpad.
-
-// NB: It would be nice to #include "nsDebug.h" and use
-// NS_RUNTIMEABORT(), but because this file is used within chromium,
-// and nsDebug pulls in nscore, and nscore pulls in prtypes, and
-// chromium can't build with prtypes being included before
-// base/basictypes, then we have to roll our own aborting impl.
-
-// Assume that if we're building with gcc, we're on a platform where
-// abort() is fatal and triggers breakpad.
-#define MOZ_FE_ABORT(_msg)                        \
-  do {                                            \
-    fputs(_msg, stderr);                          \
-    fputs("\n", stderr);                          \
-    abort();                                      \
-  } while (0)
-
-namespace std {
-
-inline void NS_NORETURN
-__throw_bad_exception(void)
-{
-  MOZ_FE_ABORT("fatal: STL threw bad_exception");
-}
-
-inline void NS_NORETURN
-__throw_bad_alloc(void)
-{
-  MOZ_FE_ABORT("fatal: STL threw bad_alloc");
-}
-
-inline void NS_NORETURN
-__throw_bad_cast(void)
-{
-  MOZ_FE_ABORT("fatal: STL threw bad_cast");
-}
-
-inline void NS_NORETURN
-__throw_bad_typeid(void)
-{
-  MOZ_FE_ABORT("fatal: STL threw bad_typeid");
-}
-
-inline void NS_NORETURN
-__throw_logic_error(const char* msg)
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_domain_error(const char* msg)
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_invalid_argument(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_length_error(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_out_of_range(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_runtime_error(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_range_error(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_overflow_error(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_underflow_error(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_ios_failure(const char* msg) 
-{
-  MOZ_FE_ABORT(msg);
-}
-
-inline void NS_NORETURN
-__throw_system_error(int err) 
-{
-  char error[128];
-  snprintf(error, sizeof(error)-1,
-           "fatal: STL threw system_error: %s (%d)", strerror(err), err);
-  MOZ_FE_ABORT(error);
-}
-
-} // namespace std
-
-#endif  // mozilla_functexcept_h
--- a/xpcom/glue/standalone/Makefile.in
+++ b/xpcom/glue/standalone/Makefile.in
@@ -100,16 +100,19 @@ FORCE_STATIC_LIB = 1
 # Force use of PIC
 FORCE_USE_PIC	= 1
 
 # Pretend we're statically linking the CRT, even though we might not be: this
 # avoids "msvcrp" and assembly dependencies from creeping into the directives
 # for this library on Windows.
 USE_STATIC_LIBS = 1
 
+# Don't use STL wrappers here (i.e. wrapped <new>); they require mozalloc
+STL_FLAGS	=
+
 GARBAGE         += $(XPCOM_GLUE_SRC_LCSRCS) $(XPCOM_GLUE_SRC_LCPPSRCS) $(wildcard *.$(OBJ_SUFFIX))
 
 SRCS_IN_OBJDIR	= 1
 
 include $(topsrcdir)/config/rules.mk
 
 ifdef _MSC_VER
 # Don't include directives about which CRT to use
--- a/xpcom/tools/registry/Makefile.in
+++ b/xpcom/tools/registry/Makefile.in
@@ -41,16 +41,17 @@ srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE		= xpcom
 CPPSRCS		= regxpcom.cpp
 
 DEFINES		+= -DXPCOM_GLUE
+STL_FLAGS	=
 
 LOCAL_INCLUDES	= \
 		-I$(srcdir)/../../build \
 		$(NULL)
 
 SIMPLE_PROGRAMS	= $(CPPSRCS:.cpp=$(BIN_SUFFIX))
 
 LIBS            = \