linux: Fix TLS error on dlopen of libcef.so (fixes #3616)

This commit is contained in:
Sergey Markelov 2024-08-05 12:21:25 -04:00 committed by Marshall Greenblatt
parent 785713c4cc
commit be4085981c
2 changed files with 38 additions and 0 deletions

View File

@ -730,5 +730,10 @@ patches = [
# hidden menu items.
# https://github.com/chromiumembedded/cef/issues/3577
'name': 'ui_menu_model_3577'
},
{
# linux: Fix cannot allocate memory in static TLS block in dlopen libcef.so
# https://github.com/chromiumembedded/cef/issues/3616
'name': 'third_party_sentencepiece_3616'
}
]

View File

@ -0,0 +1,33 @@
diff --git third_party/sentencepiece/src/src/util.cc third_party/sentencepiece/src/src/util.cc
index 538b00b..61c4e5d 100644
--- third_party/sentencepiece/src/src/util.cc
+++ third_party/sentencepiece/src/src/util.cc
@@ -16,6 +16,7 @@
#include <atomic>
#include <iostream>
+#include <memory>
namespace sentencepiece {
@@ -187,8 +190,18 @@ std::mt19937 *GetRandomGenerator() {
}
#else
std::mt19937 *GetRandomGenerator() {
- thread_local static std::mt19937 mt(GetRandomGeneratorSeed());
- return &mt;
+ // Thread-locals occupy stack space in every thread ever created by the
+ // program, even if that thread never uses the thread-local variable.
+ //
+ // https://maskray.me/blog/2021-02-14-all-about-thread-local-storage
+ //
+ // sizeof(std::mt19937) is several kilobytes, so it is safer to put that on
+ // the heap, leaving only a pointer to it in thread-local storage. This must
+ // be a unique_ptr, not a raw pointer, so that the generator is not leaked on
+ // thread exit.
+ thread_local static auto mt =
+ std::make_unique<std::mt19937>(GetRandomGeneratorSeed());
+ return mt.get();
}
#endif
} // namespace random