Files
doris/be/test/jemalloc_hook/jemalloc_hook_test_dso.cpp
T
daidai b65a564ea5 [fix](be) Route glibc allocation entry points through jemalloc (#69819)
### What problem does this PR solve?

Problem Summary:
On glibc < 2.25, dynamic TLS can be allocated by libc and freed by
jemalloc, crashing during thread cleanup. Route all seven glibc
allocation entry points through the existing jemalloc wrappers, preserve
allocator API semantics, and add native interposition tests.

### Release note

Fix a BE crash during dynamic TLS cleanup on older glibc systems and
keep glibc allocation entry points paired with jemalloc.

### Check List (For Author)

- Test
    - [ ] Regression test
    - [x] Unit Test
    - [ ] Manual test (add detailed scripts or steps below)
    - [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
        - [ ] Previous test can cover this change.
        - [ ] No code files have been changed.
        - [ ] Other reason

`./run-be-ut.sh --jemalloc-hook --run -j 2`: 12/12 passed on Linux
x86_64/glibc 2.32. Full BE, older glibc, ARM64 and customer JNI E2E were
not run.

- Behavior changed:
    - [ ] No.
- [x] Yes. Glibc allocation entry points now consistently use jemalloc,
with allocator boundary semantics preserved.

- Does this need documentation?
    - [x] No.
    - [ ] Yes.

### Check List (For Reviewer who merge this PR)

- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
2026-11-04 10:00:12 +08:00

45 lines
2.0 KiB
C++

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <cstdlib>
extern "C" void* __libc_memalign(size_t alignment, size_t size) noexcept;
extern "C" void* __libc_malloc(size_t size) noexcept;
extern "C" void __libc_free(void* ptr) noexcept;
extern "C" void* __libc_calloc(size_t count, size_t size) noexcept;
extern "C" void* __libc_realloc(void* ptr, size_t size) noexcept;
extern "C" void* __libc_valloc(size_t size) noexcept;
extern "C" void* __libc_pvalloc(size_t size) noexcept;
extern "C" void jemalloc_hook_test_allocator_symbols(void** symbols) {
symbols[0] = reinterpret_cast<void*>(&__libc_memalign);
symbols[1] = reinterpret_cast<void*>(&std::malloc);
symbols[2] = reinterpret_cast<void*>(&std::free);
symbols[3] = reinterpret_cast<void*>(&__libc_malloc);
symbols[4] = reinterpret_cast<void*>(&__libc_free);
symbols[5] = reinterpret_cast<void*>(&__libc_calloc);
symbols[6] = reinterpret_cast<void*>(&__libc_realloc);
symbols[7] = reinterpret_cast<void*>(&__libc_valloc);
symbols[8] = reinterpret_cast<void*>(&__libc_pvalloc);
}
// Access TLS from a late-loaded library, as happens with JNI native libraries.
extern "C" int jemalloc_hook_test_touch_tls() {
static thread_local int tls_values[1024] = {};
return ++tls_values[1023];
}