1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Windows doesn't support pthread_key_create's destr_function, and in fact
6// it's a bit tricky to get code to run when a thread exits.  This is
7// cargo-cult magic from http://www.codeproject.com/threads/tls.asp.
8// We are trying to be compatible with both a LoadLibrary style invocation, as
9// well as static linking. This code only needs to be included if we use
10// LoadLibrary, but it hooks into the "standard" set of TLS callbacks that are
11// provided for static linking.
12
13// This code is deliberately written to match the style of calls seen in
14// base/threading/thread_local_storage_win.cc.  Please keep the two in sync if
15// coding conventions are changed.
16
17// WARNING: Do *NOT* try to include this in the construction of the base
18// library, even though it potentially drives code in
19// base/threading/thread_local_storage_win.cc.  If you do, some users will end
20// up getting duplicate definition of DllMain() in some of their later links.
21
22// Force a reference to _tls_used to make the linker create the TLS directory
23// if it's not already there (that is, even if __declspec(thread) is not used).
24// Force a reference to p_thread_callback_dllmain_typical_entry to prevent whole
25// program optimization from discarding the variables.
26
27#include <windows.h>
28
29#include "base/compiler_specific.h"
30#include "base/win/win_util.h"
31
32// Indicate if another service is scanning the callbacks.  When this becomes
33// set to true, then DllMain() will stop supporting the callback service. This
34// value is set to true the first time any of our callbacks are called, as that
35// shows that some other service is handling callbacks.
36static bool linker_notifications_are_active = false;
37
38// This will be our mostly no-op callback that we'll list.  We won't
39// deliberately call it, and if it is called, that means we don't need to do any
40// of the callbacks anymore.  We expect such a call to arrive via a
41// THREAD_ATTACH message, long before we'd have to perform our THREAD_DETACH
42// callbacks.
43static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved);
44
45#ifdef _WIN64
46
47#pragma comment(linker, "/INCLUDE:_tls_used")
48#pragma comment(linker, "/INCLUDE:p_thread_callback_dllmain_typical_entry")
49
50#else  // _WIN64
51
52#pragma comment(linker, "/INCLUDE:__tls_used")
53#pragma comment(linker, "/INCLUDE:_p_thread_callback_dllmain_typical_entry")
54
55#endif  // _WIN64
56
57// Explicitly depend on VC\crt\src\tlssup.c variables
58// to bracket the list of TLS callbacks.
59extern "C" PIMAGE_TLS_CALLBACK __xl_a, __xl_z;
60
61// extern "C" suppresses C++ name mangling so we know the symbol names for the
62// linker /INCLUDE:symbol pragmas above.
63extern "C" {
64#ifdef _WIN64
65
66// .CRT section is merged with .rdata on x64 so it must be constant data.
67#pragma data_seg(push, old_seg)
68// Use a typical possible name in the .CRT$XL? list of segments.
69#pragma const_seg(".CRT$XLB")
70// When defining a const variable, it must have external linkage to be sure the
71// linker doesn't discard it.
72extern const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry;
73const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback;
74#pragma data_seg(pop, old_seg)
75
76#else  // _WIN64
77
78#pragma data_seg(push, old_seg)
79// Use a typical possible name in the .CRT$XL? list of segments.
80#pragma data_seg(".CRT$XLB")
81PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback;
82#pragma data_seg(pop, old_seg)
83
84#endif  // _WIN64
85}  // extern "C"
86
87// Custom crash code to get a unique entry in crash reports.
88NOINLINE static void CrashOnProcessDetach() {
89  *static_cast<volatile int*>(0) = 0x356;
90}
91
92// Make DllMain call the listed callbacks.  This way any third parties that are
93// linked in will also be called.
94BOOL WINAPI DllMain(PVOID h, DWORD reason, PVOID reserved) {
95  if (DLL_PROCESS_DETACH == reason && base::win::ShouldCrashOnProcessDetach())
96    CrashOnProcessDetach();
97
98  if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason)
99    return true;  // We won't service THREAD_ATTACH calls.
100
101  if (linker_notifications_are_active)
102    return true;  // Some other service is doing this work.
103
104  for (PIMAGE_TLS_CALLBACK* it = &__xl_a; it < &__xl_z; ++it) {
105    if (*it == NULL || *it == on_callback)
106      continue;  // Don't bother to call our own callback.
107    (*it)(h, reason, reserved);
108  }
109  return true;
110}
111
112static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved) {
113  // Do nothing.  We were just a place holder in the list used to test that we
114  // call all items.
115  // If we are called, it means that some other system is scanning the callbacks
116  // and we don't need to do so in DllMain().
117  linker_notifications_are_active = true;
118  // Note: If some other routine some how plays this same game... we could both
119  // decide not to do the scanning <sigh>, but this trick should suppress
120  // duplicate calls on Vista, where the runtime takes care of the callbacks,
121  // and allow us to do the callbacks on XP, where we are currently devoid of
122  // callbacks (due to an explicit LoadLibrary call).
123}
124