|
| 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 |
0 commit comments