Skip to content

Commit

Permalink
Bug 557060: Interpose _Throw() on MSVC and centralize pseudo-throw co…
Browse files Browse the repository at this point in the history
…de in mozalloc. r=ehsan sr=bsmedberg
  • Loading branch information
joneschrisg committed Apr 20, 2010
1 parent 34d6cce commit ce99d82
Show file tree
Hide file tree
Showing 20 changed files with 380 additions and 112 deletions.
2 changes: 1 addition & 1 deletion config/config.mk
Expand Up @@ -526,7 +526,7 @@ 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)

Expand Down
20 changes: 15 additions & 5 deletions config/gcc-stl-wrapper.template.h
Expand Up @@ -45,13 +45,23 @@
# 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()"
Expand All @@ -78,8 +88,8 @@
// 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
6 changes: 4 additions & 2 deletions config/make-stl-wrappers.py
Expand Up @@ -62,7 +62,8 @@ def main(outdir, compiler, template_file, header_list_file):
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):
Expand All @@ -72,7 +73,8 @@ def main(outdir, compiler, template_file, header_list_file):
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()

Expand Down
15 changes: 12 additions & 3 deletions config/msvc-stl-wrapper.template.h
Expand Up @@ -45,13 +45,22 @@
# 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()"
Expand Down
11 changes: 10 additions & 1 deletion config/stl-headers
Expand Up @@ -12,8 +12,17 @@
# 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
1 change: 1 addition & 0 deletions embedding/browser/gtk/tests/Makefile.in
Expand Up @@ -76,6 +76,7 @@ include $(topsrcdir)/config/rules.mk
LIBS += $(XPCOM_STANDALONE_GLUE_LDOPTS)

DEFINES += -DXPCOM_GLUE
STL_FLAGS =

CXXFLAGS += $(MOZ_GTK2_CFLAGS)

Expand Down
2 changes: 1 addition & 1 deletion js/src/config/config.mk
Expand Up @@ -526,7 +526,7 @@ 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)

Expand Down
24 changes: 22 additions & 2 deletions memory/mozalloc/Makefile.in
Expand Up @@ -44,6 +44,10 @@ 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
Expand All @@ -62,11 +66,27 @@ 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
14 changes: 6 additions & 8 deletions memory/mozalloc/mozalloc.h
Expand Up @@ -41,18 +41,16 @@
#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
Expand Down
91 changes: 91 additions & 0 deletions 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);
}
64 changes: 64 additions & 0 deletions 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 */

0 comments on commit ce99d82

Please sign in to comment.