Skip to content

Commit b2e65a8

Browse files
committed
[fix](be) Keep legacy glibc TLS allocations on jemalloc
### What problem does this PR solve? Issue Number: N/A Related PR: N/A Problem Summary: Before glibc 2.25, the dynamic loader allocates TLS for late-loaded native libraries with __libc_memalign and releases it through public free. BE redirects public allocation functions to jemalloc but leaves this internal entry point on libc, so thread cleanup can pass a libc allocation to jemalloc and crash the BE. Alias __libc_memalign to the existing doris_memalign implementation. Add standalone native allocator tests through run-be-ut.sh --jemalloc-hook. The regular BE UT disables jemalloc and uses sanitizer interceptors, so it cannot validate this allocation/free pair. The tests cover the legacy pair in a subprocess, alignment and error semantics, cross-thread frees, and dynamic TLS cleanup from a late-loaded shared library. ### Release note Fix a BE crash when threads clean up dynamic TLS from native libraries on older glibc systems. ### Check List (For Author) - Test: Unit Test - ./run-be-ut.sh --jemalloc-hook --clean --run -j 2 - Four native tests passed on glibc 2.32; the unpatched hook triggers SIGSEGV in the legacy allocation/free subprocess. - clang-format v16, clang-tidy, build hygiene, and bash syntax checks passed. - CentOS 7 / Jindo / DLF end-to-end testing was not run. - Behavior changed: Yes - legacy glibc TLS allocations now use jemalloc consistently with their deallocation. - Does this need documentation: No
1 parent 76011df commit b2e65a8

5 files changed

Lines changed: 242 additions & 1 deletion

File tree

‎be/src/runtime/memory/jemalloc_hook.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ void* realloc(void* p, size_t size) __THROW ALIAS(doris_realloc);
7777
void* calloc(size_t n, size_t size) __THROW ALIAS(doris_calloc);
7878
void cfree(void* ptr) __THROW ALIAS(doris_cfree);
7979
void* memalign(size_t align, size_t size) __THROW ALIAS(doris_memalign);
80+
#ifdef __GLIBC__
81+
// Before glibc 2.25, dlopened modules allocate dynamic TLS with __libc_memalign
82+
// and release it through free. Route both to jemalloc so thread exit does not
83+
// pass a glibc allocation to doris_free.
84+
void* __libc_memalign(size_t align, size_t size) __THROW ALIAS(doris_memalign);
85+
#endif
8086
void* aligned_alloc(size_t align, size_t size) __THROW ALIAS(doris_aligned_alloc);
8187
void* valloc(size_t size) __THROW ALIAS(doris_valloc);
8288
void* pvalloc(size_t size) __THROW ALIAS(doris_pvalloc);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# This allocator test must not use ASAN: its interceptors would hide missing
19+
# glibc allocation entry points. Keep it separate from doris_be_test, which
20+
# run-be-ut.sh builds with USE_JEMALLOC=OFF.
21+
cmake_minimum_required(VERSION 3.20)
22+
project(doris_jemalloc_hook_test LANGUAGES CXX)
23+
24+
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
25+
message(FATAL_ERROR "jemalloc hook tests require Linux/glibc")
26+
endif()
27+
28+
set(CMAKE_CXX_STANDARD 17)
29+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
30+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
31+
get_filename_component(DORIS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ABSOLUTE)
32+
set(THIRDPARTY_DIR "$ENV{DORIS_THIRDPARTY}/installed")
33+
find_package(Threads REQUIRED)
34+
35+
foreach (LIB_NAME gtest gtest_main jemalloc_doris unwind lzma)
36+
find_library(${LIB_NAME}_LIBRARY NAMES ${LIB_NAME}
37+
PATHS "${THIRDPARTY_DIR}/lib" "${THIRDPARTY_DIR}/lib64"
38+
NO_DEFAULT_PATH REQUIRED)
39+
endforeach()
40+
41+
add_library(jemalloc_hook_test_dso SHARED jemalloc_hook_test_dso.cpp)
42+
target_compile_options(jemalloc_hook_test_dso PRIVATE -ftls-model=global-dynamic)
43+
44+
add_executable(jemalloc_hook_test
45+
jemalloc_hook_test.cpp
46+
"${DORIS_SOURCE_DIR}/be/src/runtime/memory/jemalloc_hook.cpp")
47+
target_include_directories(jemalloc_hook_test PRIVATE "${THIRDPARTY_DIR}/include")
48+
target_compile_definitions(jemalloc_hook_test PRIVATE
49+
JEMALLOC_HOOK_TEST_DSO="$<TARGET_FILE:jemalloc_hook_test_dso>")
50+
# Do not enable ENABLE_EXPORTS: verify the hook also works with the production
51+
# executable's default symbol visibility.
52+
target_link_libraries(jemalloc_hook_test PRIVATE
53+
"${gtest_main_LIBRARY}" "${gtest_LIBRARY}" "${jemalloc_doris_LIBRARY}"
54+
"${unwind_LIBRARY}" "${lzma_LIBRARY}" Threads::Threads ${CMAKE_DL_LIBS})
55+
add_dependencies(jemalloc_hook_test jemalloc_hook_test_dso)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include <dlfcn.h>
19+
#include <gtest/gtest.h>
20+
21+
#include <array>
22+
#include <cerrno>
23+
#include <cstdint>
24+
#include <cstdlib>
25+
#include <cstring>
26+
#include <limits>
27+
#include <memory>
28+
#include <thread>
29+
30+
namespace doris {
31+
32+
class JemallocHookTest : public testing::Test {
33+
protected:
34+
using MemalignFunction = void* (*)(size_t, size_t);
35+
36+
void SetUp() override {
37+
void* symbol = dlsym(RTLD_DEFAULT, "__libc_memalign");
38+
ASSERT_NE(nullptr, symbol) << dlerror();
39+
_memalign = reinterpret_cast<MemalignFunction>(symbol);
40+
// Exercise the legacy allocation/free pair even on newer glibc versions
41+
// whose loader no longer uses __libc_memalign for TLS. A subprocess turns
42+
// an allocator-mismatch crash into a test failure instead of aborting UT.
43+
ASSERT_EXIT(
44+
{
45+
void* ptr = _memalign(16, 32);
46+
const int result = (ptr == nullptr);
47+
std::free(ptr);
48+
std::_Exit(result);
49+
},
50+
testing::ExitedWithCode(0), "");
51+
}
52+
53+
MemalignFunction _memalign = nullptr;
54+
};
55+
56+
TEST_F(JemallocHookTest, AlignedAllocationsCanBeFreed) {
57+
for (size_t alignment : {1, 8, 16, 64, 4096}) {
58+
for (size_t size : {0, 1, 17, 4097}) {
59+
void* ptr = _memalign(alignment, size);
60+
ASSERT_NE(nullptr, ptr);
61+
EXPECT_EQ(0, reinterpret_cast<uintptr_t>(ptr) % alignment);
62+
std::memset(ptr, 0x5a, size);
63+
std::free(ptr);
64+
}
65+
}
66+
}
67+
68+
TEST_F(JemallocHookTest, AllocationErrorsPreserveMemalignSemantics) {
69+
errno = 0;
70+
void* ptr = _memalign(3, 16);
71+
EXPECT_EQ(nullptr, ptr);
72+
EXPECT_EQ(EINVAL, errno);
73+
std::free(ptr);
74+
75+
errno = 0;
76+
ptr = _memalign(64, std::numeric_limits<size_t>::max());
77+
EXPECT_EQ(nullptr, ptr);
78+
EXPECT_EQ(ENOMEM, errno);
79+
std::free(ptr);
80+
}
81+
82+
TEST_F(JemallocHookTest, AllocationsCanBeFreedAfterAllocatingThreadsExit) {
83+
std::array<void*, 64> pointers = {};
84+
std::array<std::thread, 64> threads;
85+
for (size_t i = 0; i < threads.size(); ++i) {
86+
threads[i] = std::thread([&, i] { pointers[i] = _memalign(64, 1031); });
87+
}
88+
for (auto& thread : threads) {
89+
thread.join();
90+
}
91+
for (void* ptr : pointers) {
92+
ASSERT_NE(nullptr, ptr);
93+
std::memset(ptr, 0x5a, 1031);
94+
std::free(ptr);
95+
}
96+
}
97+
98+
TEST_F(JemallocHookTest, LateLoadedDynamicTlsCanBeFreedAfterThreadsExit) {
99+
std::unique_ptr<void, decltype(&dlclose)> handle(
100+
dlopen(JEMALLOC_HOOK_TEST_DSO, RTLD_NOW | RTLD_LOCAL), &dlclose);
101+
ASSERT_NE(nullptr, handle) << dlerror();
102+
using TouchTlsFunction = int (*)();
103+
auto touch_tls =
104+
reinterpret_cast<TouchTlsFunction>(dlsym(handle.get(), "jemalloc_hook_test_touch_tls"));
105+
ASSERT_NE(nullptr, touch_tls) << dlerror();
106+
107+
// Exceed glibc's default stack cache to exercise TLS deallocation instead
108+
// of only keeping exited threads' stacks cached.
109+
for (int round = 0; round < 8; ++round) {
110+
std::array<int, 64> results = {};
111+
std::array<std::thread, 64> threads;
112+
for (size_t i = 0; i < threads.size(); ++i) {
113+
threads[i] = std::thread([&, i] {
114+
for (int call = 0; call < 8; ++call) {
115+
results[i] = touch_tls();
116+
}
117+
});
118+
}
119+
for (auto& thread : threads) {
120+
thread.join();
121+
}
122+
for (int result : results) {
123+
EXPECT_EQ(8, result);
124+
}
125+
}
126+
}
127+
128+
} // namespace doris
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Access TLS from a late-loaded library, as happens with JNI native libraries.
19+
extern "C" int jemalloc_hook_test_touch_tls() {
20+
static thread_local int tls_values[1024] = {};
21+
return ++tls_values[1023];
22+
}

‎run-be-ut.sh‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# Optional options:
2323
# --clean clean and build ut
2424
# --run build and run all ut
25+
# --jemalloc-hook build standalone jemalloc hook tests without sanitizers
2526
# --run --filter=xx build and run specified ut
2627
# --gdb debug with gdb, does not take effect if --run is not specified
2728
# --coverage generate coverage report, does not take effect if --gdb is specified
@@ -117,6 +118,7 @@ Usage: $0 <options>
117118
Optional options:
118119
--clean clean and build ut
119120
--run build and run all ut
121+
--jemalloc-hook build standalone Linux jemalloc hook tests without sanitizers
120122
--run --filter=xx build and run specified ut
121123
--run --gen_out generate expected check data for test
122124
--run --gen_regression_case generate regression test cases corrresponding to ut cases for ut cases that support it
@@ -145,14 +147,15 @@ Usage: $0 <options>
145147
exit 1
146148
}
147149

148-
if ! OPTS="$(getopt -n "$0" -o vhj:f: -l gen_out,gen_regression_case,coverage,benchmark,run,gdb,clean,filter: -- "$@")"; then
150+
if ! OPTS="$(getopt -n "$0" -o vhj:f: -l gen_out,gen_regression_case,coverage,benchmark,run,gdb,clean,jemalloc-hook,filter: -- "$@")"; then
149151
usage
150152
fi
151153

152154
eval set -- "${OPTS}"
153155

154156
CLEAN=0
155157
RUN=0
158+
JEMALLOC_HOOK=0
156159
GDB=0
157160
DENABLE_CLANG_COVERAGE='OFF'
158161
BUILD_AZURE='ON'
@@ -170,6 +173,10 @@ if [[ "$#" != 1 ]]; then
170173
RUN=1
171174
shift
172175
;;
176+
--jemalloc-hook)
177+
JEMALLOC_HOOK=1
178+
shift
179+
;;
173180
--gdb)
174181
GDB=1
175182
shift
@@ -209,6 +216,29 @@ if [[ -z "${PARALLEL}" ]]; then
209216
PARALLEL="$(($(nproc) / 5 + 1))"
210217
fi
211218

219+
# The regular BE UT uses ASAN and disables jemalloc. Exercise allocator
220+
# interposition in a separate native executable, before initializing BE's
221+
# unrelated submodules or configuring the sanitizer build.
222+
if [[ "${JEMALLOC_HOOK}" -eq 1 ]]; then
223+
# These modes belong to the sanitizer BE UT, not the native allocator test.
224+
if [[ "${GDB}" -eq 1 || "${DENABLE_CLANG_COVERAGE}" == 'ON' || -n "${GEN_OUT}${GEN_REGRESSION_CASE}" ]]; then
225+
echo "--jemalloc-hook supports --run, --filter, --clean and -j"
226+
exit 1
227+
fi
228+
JEMALLOC_TEST_BUILD_DIR="${DORIS_HOME}/be/ut_build_JEMALLOC"
229+
if [[ "${CLEAN}" -eq 1 ]]; then
230+
rm -rf "${JEMALLOC_TEST_BUILD_DIR}"
231+
fi
232+
"${CMAKE_CMD}" -S "${DORIS_HOME}/be/test/jemalloc_hook" \
233+
-B "${JEMALLOC_TEST_BUILD_DIR}" -G "${GENERATOR}"
234+
"${CMAKE_CMD}" --build "${JEMALLOC_TEST_BUILD_DIR}" --parallel "${PARALLEL}"
235+
if [[ "${RUN}" -eq 1 ]]; then
236+
"${JEMALLOC_TEST_BUILD_DIR}/jemalloc_hook_test" "${FILTER:---gtest_filter=*}" \
237+
"--gtest_output=xml:${JEMALLOC_TEST_BUILD_DIR}/gtest_output/jemalloc_hook_test.xml"
238+
fi
239+
exit 0
240+
fi
241+
212242
CMAKE_BUILD_TYPE="${BUILD_TYPE_UT:-ASAN}"
213243
CMAKE_BUILD_TYPE="$(echo "${CMAKE_BUILD_TYPE}" | awk '{ print(toupper($0)) }')"
214244

0 commit comments

Comments
 (0)