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 MallocStub : public TestEnv
33 MallocStub() : TestEnv("./malloc_interception_test.log") {}
36 TEST_F(MallocStub, must_allocate_space_as_standard_malloc)
38 void *p = malloc(128);
44 TEST_F(MallocStub, must_log_allocation_events_if_trace_is_ready_for_it)
47 void *p1 = malloc(1024);
49 GlobalTrace.reset(new Trace);
50 void *p2 = malloc(128);
51 void *p3 = malloc(64);
54 ASSERT_TRUE(p1 && p2 && p3);
55 ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
56 "On CPU - Peak heap usage: 192 B, Total allocated: 192 B, Total deallocated: 0 "
57 "B\nOn GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
63 TEST_F(MallocStub, must_not_do_the_record_about_allocation_event_if_original_function_failed)
65 void *p = malloc(std::numeric_limits<size_t>::max());
69 ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
70 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
71 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
74 TEST_F(MallocStub, should_allocate_memory_from_pool_for_symbol_searcher_internal_usage_if_need)
76 signalizeThatNextAllocationsWillBeForSymbolSearcherInternalUsage();
77 void *p = malloc(1024);
78 signalizeThatSymbolSearcherEndedOfWork();
81 MemoryPoolForSymbolSearcherInternals pool;
83 ASSERT_TRUE(pool.containsMemorySpaceStartedFromPointer(p));
84 ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
85 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
86 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
91 should_not_influence_on_trace_results_even_if_orignal_function_return_any_not_null_ptr_when_incoming_size_is_zero)
98 ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
99 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
100 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
103 } // namespace backstage