|
| 1 | +// Copyright 2021-present StarRocks, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// FIXME: The case doesn't work on ASAN case since the malloc will be hooked |
| 16 | +// when doing the ASAN init. Can't make it work so just disable it for now. |
| 17 | +#ifndef ADDRESS_SANITIZER |
| 18 | + |
| 19 | +#include "service/mem_hook.h" |
| 20 | + |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace starrocks { |
| 26 | + |
| 27 | +static void try_malloc_memory(size_t size) { |
| 28 | + std::vector<int8_t> arr; |
| 29 | + arr.reserve(size); |
| 30 | + EXPECT_EQ(size, arr.capacity()); |
| 31 | +} |
| 32 | + |
| 33 | +static void try_calloc_memory(size_t n, size_t size) { |
| 34 | + void* ptr = calloc(n, size); |
| 35 | + if (ptr == nullptr) { |
| 36 | + throw std::bad_alloc(); |
| 37 | + } |
| 38 | + free(ptr); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(MemhookTest, test_malloc_mem_hook_block_without_try_catch) { |
| 42 | + set_large_memory_alloc_failure_threshold(0); |
| 43 | + int64_t blocking_size = 1024 * 1024; |
| 44 | + |
| 45 | + set_large_memory_alloc_failure_threshold(blocking_size); |
| 46 | + // successful because blocking_size <= g_large_memory_alloc_failure_threshold |
| 47 | + EXPECT_NO_THROW(try_malloc_memory(blocking_size)); |
| 48 | + |
| 49 | + set_large_memory_alloc_failure_threshold(blocking_size - 1); |
| 50 | + // fail with std::bad_alloc for the same size |
| 51 | + EXPECT_THROW(try_malloc_memory(blocking_size), std::bad_alloc); |
| 52 | + |
| 53 | + // reset to no limit |
| 54 | + set_large_memory_alloc_failure_threshold(0); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(MemhookTest, test_malloc_mem_hook_block_with_try_catch) { |
| 58 | + // IS_BAD_ALLOC_CATCHED is always `false` when builds with -DBE_TEST. |
| 59 | + // There is no easy way to simulate the mem_tracker limit check here. |
| 60 | + // Leave the test body empty as intended for now. |
| 61 | +} |
| 62 | + |
| 63 | +TEST(MemhookTest, test_calloc_mem_hook_block_without_try_catch) { |
| 64 | + int64_t blocking_size = 1024 * 1024; |
| 65 | + |
| 66 | + set_large_memory_alloc_failure_threshold(blocking_size); |
| 67 | + // successful because blocking_size <= g_large_memory_alloc_failure_threshold |
| 68 | + EXPECT_NO_THROW(try_calloc_memory(blocking_size / sizeof(int), sizeof(int))); |
| 69 | + |
| 70 | + set_large_memory_alloc_failure_threshold(blocking_size - 1); |
| 71 | + // fail with std::bad_alloc for the same size |
| 72 | + EXPECT_THROW(try_calloc_memory(blocking_size / sizeof(int), sizeof(int)), std::bad_alloc); |
| 73 | + |
| 74 | + // reset to no limit |
| 75 | + set_large_memory_alloc_failure_threshold(0); |
| 76 | +} |
| 77 | +} // namespace starrocks |
| 78 | +#endif |
0 commit comments