Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Windows doesn't support pthread_key_create's destr_function, and in fact | 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 | 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. | 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 | 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 | 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 | 10 // LoadLibrary, but it hooks into the "standard" set of TLS callbacks that are |
| 11 // provided for static linking. | 11 // provided for static linking. |
| 12 | 12 |
| 13 // This code is deliberately written to match the style of calls seen in | 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 | 14 // base/threading/thread_local_storage_win.cc. Please keep the two in sync if |
| 15 // coding conventions are changed. | 15 // coding conventions are changed. |
| 16 | 16 |
| 17 // WARNING: Do *NOT* try to include this in the construction of the base | 17 // WARNING: Do *NOT* try to include this in the construction of the base |
| 18 // library, even though it potentially drives code in | 18 // library, even though it potentially drives code in |
| 19 // base/threading/thread_local_storage_win.cc. If you do, some users will end | 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. | 20 // up getting duplicate definition of DllMain() in some of their later links. |
| 21 | 21 |
| 22 // Force a reference to _tls_used to make the linker create the TLS directory | 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). | 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 | 24 // Force a reference to p_thread_callback_dllmain_typical_entry to prevent whole |
| 25 // program optimization from discarding the variables. | 25 // program optimization from discarding the variables. |
| 26 | 26 |
| 27 #include <windows.h> | 27 #include <windows.h> |
| 28 | 28 |
| 29 #include "base/logging.h" | 29 #include "base/compiler_specific.h" |
| 30 | 30 |
| 31 // Indicate if another service is scanning the callbacks. When this becomes | 31 // Indicate if another service is scanning the callbacks. When this becomes |
| 32 // set to true, then DllMain() will stop supporting the callback service. This | 32 // set to true, then DllMain() will stop supporting the callback service. This |
| 33 // value is set to true the first time any of our callbacks are called, as that | 33 // value is set to true the first time any of our callbacks are called, as that |
| 34 // shows that some other service is handling callbacks. | 34 // shows that some other service is handling callbacks. |
| 35 static bool linker_notifications_are_active = false; | 35 static bool linker_notifications_are_active = false; |
| 36 | 36 |
| 37 // Indicates if we should crash in response to DLL_PROCESS_DETACH. | |
| 38 static bool crash_on_process_detach = false; | |
| 39 | |
| 37 // This will be our mostly no-op callback that we'll list. We won't | 40 // This will be our mostly no-op callback that we'll list. We won't |
| 38 // deliberately call it, and if it is called, that means we don't need to do any | 41 // deliberately call it, and if it is called, that means we don't need to do any |
| 39 // of the callbacks anymore. We expect such a call to arrive via a | 42 // of the callbacks anymore. We expect such a call to arrive via a |
| 40 // THREAD_ATTACH message, long before we'd have to perform our THREAD_DETACH | 43 // THREAD_ATTACH message, long before we'd have to perform our THREAD_DETACH |
| 41 // callbacks. | 44 // callbacks. |
| 42 static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved); | 45 static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved); |
| 43 | 46 |
| 44 #ifdef _WIN64 | 47 #ifdef _WIN64 |
| 45 | 48 |
| 46 #pragma comment(linker, "/INCLUDE:_tls_used") | 49 #pragma comment(linker, "/INCLUDE:_tls_used") |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 76 | 79 |
| 77 #pragma data_seg(push, old_seg) | 80 #pragma data_seg(push, old_seg) |
| 78 // Use a typical possible name in the .CRT$XL? list of segments. | 81 // Use a typical possible name in the .CRT$XL? list of segments. |
| 79 #pragma data_seg(".CRT$XLB") | 82 #pragma data_seg(".CRT$XLB") |
| 80 PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback; | 83 PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback; |
| 81 #pragma data_seg(pop, old_seg) | 84 #pragma data_seg(pop, old_seg) |
| 82 | 85 |
| 83 #endif // _WIN64 | 86 #endif // _WIN64 |
| 84 } // extern "C" | 87 } // extern "C" |
| 85 | 88 |
| 89 NOINLINE static void CrashOnProcessDetach() { | |
| 90 *((int*)0) = 0x356; | |
| 91 } | |
| 86 | 92 |
| 87 // Make DllMain call the listed callbacks. This way any third parties that are | 93 // Make DllMain call the listed callbacks. This way any third parties that are |
| 88 // linked in will also be called. | 94 // linked in will also be called. |
| 89 BOOL WINAPI DllMain(PVOID h, DWORD reason, PVOID reserved) { | 95 BOOL WINAPI DllMain(PVOID h, DWORD reason, PVOID reserved) { |
| 96 if (DLL_PROCESS_DETACH == reason && crash_on_process_detach) | |
| 97 CrashOnProcessDetach(); | |
| 98 | |
| 90 if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason) | 99 if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason) |
| 91 return true; // We won't service THREAD_ATTACH calls. | 100 return true; // We won't service THREAD_ATTACH calls. |
| 92 | 101 |
| 93 if (linker_notifications_are_active) | 102 if (linker_notifications_are_active) |
| 94 return true; // Some other service is doing this work. | 103 return true; // Some other service is doing this work. |
| 95 | 104 |
| 96 for (PIMAGE_TLS_CALLBACK* it = &__xl_a; it < &__xl_z; ++it) { | 105 for (PIMAGE_TLS_CALLBACK* it = &__xl_a; it < &__xl_z; ++it) { |
| 97 if (*it == NULL || *it == on_callback) | 106 if (*it == NULL || *it == on_callback) |
| 98 continue; // Don't bother to call our own callback. | 107 continue; // Don't bother to call our own callback. |
| 99 (*it)(h, reason, reserved); | 108 (*it)(h, reason, reserved); |
| 100 } | 109 } |
| 101 return true; | 110 return true; |
| 102 } | 111 } |
| 103 | 112 |
| 104 static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved) { | 113 static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved) { |
| 105 // Do nothing. We were just a place holder in the list used to test that we | 114 // Do nothing. We were just a place holder in the list used to test that we |
| 106 // call all items. | 115 // call all items. |
| 107 // If we are called, it means that some other system is scanning the callbacks | 116 // If we are called, it means that some other system is scanning the callbacks |
| 108 // and we don't need to do so in DllMain(). | 117 // and we don't need to do so in DllMain(). |
| 109 linker_notifications_are_active = true; | 118 linker_notifications_are_active = true; |
| 110 // Note: If some other routine some how plays this same game... we could both | 119 // Note: If some other routine some how plays this same game... we could both |
| 111 // decide not to do the scanning <sigh>, but this trick should suppress | 120 // decide not to do the scanning <sigh>, but this trick should suppress |
| 112 // duplicate calls on Vista, where the runtime takes care of the callbacks, | 121 // duplicate calls on Vista, where the runtime takes care of the callbacks, |
| 113 // and allow us to do the callbacks on XP, where we are currently devoid of | 122 // and allow us to do the callbacks on XP, where we are currently devoid of |
| 114 // callbacks (due to an explicit LoadLibrary call). | 123 // callbacks (due to an explicit LoadLibrary call). |
| 115 } | 124 } |
| 125 | |
| 126 namespace base { | |
| 127 namespace win { | |
| 128 | |
| 129 void DLLMain_SetCrashOnProcessDetach(bool crash) { | |
|
rvargas (doing something else)
2012/03/21 03:13:45
This is problematic because this file is not part
eroman
2012/03/21 04:14:31
Good point.
How about if I add exported methods t
rvargas (doing something else)
2012/03/21 17:46:12
Sounds good.
| |
| 130 crash_on_process_detach = crash; | |
| 131 } | |
| 132 | |
| 133 } // namespace win | |
| 134 } // namespace base | |
| OLD | NEW |