genode/repos/base/src/lib/cxx/exception.cc

121 lines
3.8 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Support for exceptions libsupc++
* \author Norman Feske
* \author Sebastian Sumpf
* \date 2006-07-21
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
/* libsupc++ includes */
#include <cxxabi.h>
#include <exception>
2019-09-16 13:06:23 +02:00
#include <typeinfo>
/* Genode includes */
#include <base/log.h>
#include <util/string.h>
/* base-internal includes */
#include <base/internal/globals.h>
2011-12-22 16:19:25 +01:00
extern "C" char __eh_frame_start__[]; /* from linker script */
extern "C" void __register_frame (const void *begin); /* from libgcc_eh */
extern "C" char *__cxa_demangle(const char *mangled_name,
char *output_buffer,
size_t *length,
int *status);
extern "C" void free(void *ptr);
2011-12-22 16:19:25 +01:00
/*
* This symbol is overwritten by Genode's dynamic linker (ld.lib.so).
* After setup, the symbol will point to the actual implementation of
* 'dl_iterate_phdr', which is located within the linker. 'dl_iterate_phdr'
* iterates through all (linker loaded) binaries and shared libraries. This
* function has to be implemented in order to support C++ exceptions within
* shared libraries.
* Return values of dl_iterate_phdr (gcc 4.2.4):
* < 0 = error
* 0 = continue program header iteration
* > 0 = stop iteration (no errors occured)
*
* See also: man dl_iterate_phdr
*/
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
extern "C" int dl_iterate_phdr(int (*callback) (void *info, unsigned long size,
void *data), void *data) __attribute__((weak));
extern "C" int dl_iterate_phdr(int (*) (void *, unsigned long, void *), void *) {
2011-12-22 16:19:25 +01:00
return -1; }
/*
* Terminate handler
*/
void terminate_handler()
{
std::type_info *t = __cxxabiv1::__cxa_current_exception_type();
if (!t)
return;
char *demangled_name = __cxa_demangle(t->name(), nullptr, nullptr, nullptr);
if (demangled_name) {
Genode::error("Uncaught exception of type "
"'", Genode::Cstring(demangled_name), "'");
free(demangled_name);
} else {
Genode::error("Uncaught exception of type '", t->name(), "' "
"(use 'c++filt -t' to demangle)");
}
}
/**
* Init program headers of the dynamic linker
*
* The weak function is used for statically linked binaries. The
* dynamic linker provides an implementation that loads the program
* headers of the linker. This must be done before the first exception
* is thrown.
*/
void Genode::init_ldso_phdr(Env &) __attribute__((weak));
void Genode::init_ldso_phdr(Env &) { }
/*
* Initialization
*/
void Genode::init_exception_handling(Env &env)
{
init_ldso_phdr(env);
init_cxx_heap(env);
__register_frame(__eh_frame_start__);
std::set_terminate(terminate_handler);
/*
* Trigger first exception. This step has two purposes.
* First, it enables us to detect problems related to exception handling as
* early as possible. If there are problems with the C++ support library,
* it is much easier to debug them at this early stage. Otherwise problems
* with half-working exception handling cause subtle failures that are hard
* to interpret.
*
* Second, the C++ support library allocates data structures lazily on the
* first occurrence of an exception. In some corner cases, this allocation
* consumes several KB of stack. This is usually not a problem when the
* first exception is triggered from the main thread but it becomes an
* issue when the first exception is thrown from the stack of a thread with
* a specially tailored (and otherwise sufficient) stack size. By throwing
* an exception here, we mitigate this issue by eagerly performing those
* allocations.
*/
try { throw 1; } catch (...) { }
}