folly/.../ExceptionTracerLib.cpp: provide less DOF fodder (avoid heap use-after-free)
[folly.git] / folly / experimental / exception_tracer / ExceptionTracerLib.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/experimental/exception_tracer/ExceptionTracerLib.h>
18
19 #include <dlfcn.h>
20
21 #include <vector>
22
23 #include <folly/Indestructible.h>
24 #include <folly/Portability.h>
25 #include <folly/SharedMutex.h>
26 #include <folly/Synchronized.h>
27
28 namespace __cxxabiv1 {
29
30 extern "C" {
31 void __cxa_throw(
32     void* thrownException,
33     std::type_info* type,
34     void (*destructor)(void*)) __attribute__((__noreturn__));
35 void* __cxa_begin_catch(void* excObj) throw();
36 void __cxa_rethrow(void) __attribute__((__noreturn__));
37 void __cxa_rethrow(void);
38 void __cxa_end_catch(void);
39 }
40
41 } // namespace __cxxabiv1
42
43 using namespace folly::exception_tracer;
44
45 namespace {
46
47 template <typename Function>
48 class CallbackHolder {
49  public:
50   void registerCallback(Function f) {
51     SYNCHRONIZED(callbacks_) { callbacks_.push_back(std::move(f)); }
52   }
53
54   // always inline to enforce kInternalFramesNumber
55   template <typename... Args>
56   FOLLY_ALWAYS_INLINE void invoke(Args... args) {
57     SYNCHRONIZED_CONST(callbacks_) {
58       for (auto& cb : callbacks_) {
59         cb(args...);
60       }
61     }
62   }
63
64  private:
65   folly::Synchronized<std::vector<Function>> callbacks_;
66 };
67
68 } // namespace
69
70 namespace folly {
71 namespace exception_tracer {
72
73 #define DECLARE_CALLBACK(NAME)                                   \
74   CallbackHolder<NAME##Type>& get##NAME##Callbacks() {           \
75     static Indestructible<CallbackHolder<NAME##Type>> Callbacks; \
76     return *Callbacks;                                           \
77   }                                                              \
78   void register##NAME##Callback(NAME##Type callback) {           \
79     get##NAME##Callbacks().registerCallback(callback);           \
80   }
81
82 DECLARE_CALLBACK(CxaThrow);
83 DECLARE_CALLBACK(CxaBeginCatch);
84 DECLARE_CALLBACK(CxaRethrow);
85 DECLARE_CALLBACK(CxaEndCatch);
86 DECLARE_CALLBACK(RethrowException);
87
88 } // exception_tracer
89 } // folly
90
91 namespace __cxxabiv1 {
92
93 void __cxa_throw(void* thrownException,
94                  std::type_info* type,
95                  void (*destructor)(void*)) {
96   static auto orig_cxa_throw =
97       reinterpret_cast<decltype(&__cxa_throw)>(dlsym(RTLD_NEXT, "__cxa_throw"));
98   getCxaThrowCallbacks().invoke(thrownException, type, destructor);
99   orig_cxa_throw(thrownException, type, destructor);
100   __builtin_unreachable(); // orig_cxa_throw never returns
101 }
102
103 void __cxa_rethrow() {
104   // __cxa_rethrow leaves the current exception on the caught stack,
105   // and __cxa_begin_catch recognizes that case.  We could do the same, but
106   // we'll implement something simpler (and slower): we pop the exception from
107   // the caught stack, and push it back onto the active stack; this way, our
108   // implementation of __cxa_begin_catch doesn't have to do anything special.
109   static auto orig_cxa_rethrow = reinterpret_cast<decltype(&__cxa_rethrow)>(
110       dlsym(RTLD_NEXT, "__cxa_rethrow"));
111   getCxaRethrowCallbacks().invoke();
112   orig_cxa_rethrow();
113   __builtin_unreachable(); // orig_cxa_rethrow never returns
114 }
115
116 void* __cxa_begin_catch(void* excObj) throw() {
117   // excObj is a pointer to the unwindHeader in __cxa_exception
118   static auto orig_cxa_begin_catch =
119       reinterpret_cast<decltype(&__cxa_begin_catch)>(
120           dlsym(RTLD_NEXT, "__cxa_begin_catch"));
121   getCxaBeginCatchCallbacks().invoke(excObj);
122   return orig_cxa_begin_catch(excObj);
123 }
124
125 void __cxa_end_catch() {
126   static auto orig_cxa_end_catch = reinterpret_cast<decltype(&__cxa_end_catch)>(
127       dlsym(RTLD_NEXT, "__cxa_end_catch"));
128   getCxaEndCatchCallbacks().invoke();
129   orig_cxa_end_catch();
130 }
131
132 } // namespace __cxxabiv1
133
134 namespace std {
135
136 void rethrow_exception(std::exception_ptr ep) {
137   // Mangled name for std::rethrow_exception
138   // TODO(tudorb): Dicey, as it relies on the fact that std::exception_ptr
139   // is typedef'ed to a type in namespace __exception_ptr
140   static auto orig_rethrow_exception =
141       reinterpret_cast<decltype(&rethrow_exception)>(
142           dlsym(RTLD_NEXT,
143                 "_ZSt17rethrow_exceptionNSt15__exception_ptr13exception_ptrE"));
144   getRethrowExceptionCallbacks().invoke(ep);
145   orig_rethrow_exception(ep);
146   __builtin_unreachable(); // orig_rethrow_exception never returns
147 }
148
149 } // namespace std