2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "common_test_environment.h"
18 #include "file_content_manipulations.h"
21 #include "symbol_searcher.h"
22 #include "memory_pool_for_symbol_searcher_internals.h"
26 extern std::unique_ptr<Trace> GlobalTrace;
31 struct ReallocStub : public TestEnv
33 ReallocStub() : TestEnv("./realloc_interception_test.log") {}
36 TEST_F(ReallocStub, must_allocate_space_as_standard_realloc)
38 void *p = malloc(128);
45 TEST_F(ReallocStub, must_log_allocation_deallocation_events_if_trace_is_ready_for_it)
47 std::array<char, 1024> reference_data;
48 reference_data.fill('a');
49 void *p1 = malloc(1024);
50 memcpy(p1, reference_data.data(), reference_data.size());
51 void *p2 = realloc(p1, 64);
52 void *p3 = realloc(p2, 128);
56 ASSERT_TRUE(memcmp(p3, reference_data.data(), 64) == 0);
57 ASSERT_STREQ(getContentOfFile("./realloc_interception_test.log").c_str(),
58 "On CPU - Peak heap usage: 1024 B, Total allocated: 1216 B, Total deallocated: 1088 "
59 "B\nOn GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
64 must_not_do_the_record_about_allocation_deallocation_events_if_original_function_failed)
67 void *p = malloc(128);
68 GlobalTrace.reset(new Trace);
70 void *ptr_after_realloc = realloc(p, std::numeric_limits<size_t>::max());
71 ptr_after_realloc = realloc(p, 0);
74 ASSERT_FALSE(ptr_after_realloc);
75 ASSERT_STREQ(getContentOfFile("./realloc_interception_test.log").c_str(),
76 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
77 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
82 TEST_F(ReallocStub, should_work_as_malloc_when_incoming_ptr_is_equal_to_nullptr)
84 void *p = realloc(nullptr, 1024);
89 getContentOfFile("./realloc_interception_test.log").c_str(),
90 "On CPU - Peak heap usage: 1024 B, Total allocated: 1024 B, Total deallocated: 0 B\nOn "
91 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
98 should_not_influence_on_trace_results_even_if_orignal_function_return_any_not_null_ptr_when_incoming_size_is_zero_and_ptr_is_null)
100 void *p = realloc(nullptr, 0);
105 ASSERT_STREQ(getContentOfFile("./realloc_interception_test.log").c_str(),
106 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
107 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
110 TEST_F(ReallocStub, should_allocate_memory_from_pool_for_symbol_searcher_internal_usage_if_need)
112 signalizeThatNextAllocationsWillBeForSymbolSearcherInternalUsage();
113 void *p = malloc(128);
114 p = realloc(p, 1024);
115 signalizeThatSymbolSearcherEndedOfWork();
118 MemoryPoolForSymbolSearcherInternals pool;
120 ASSERT_TRUE(pool.containsMemorySpaceStartedFromPointer(p));
121 ASSERT_STREQ(getContentOfFile("./realloc_interception_test.log").c_str(),
122 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
123 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
126 } // namespace backstage