Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / contrib / heap_trace / tests / src / realloc_interception_test.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "common_test_environment.h"
18 #include "file_content_manipulations.h"
19
20 #include "trace.h"
21 #include "symbol_searcher.h"
22 #include "memory_pool_for_symbol_searcher_internals.h"
23
24 #include <limits>
25
26 extern std::unique_ptr<Trace> GlobalTrace;
27
28 namespace backstage
29 {
30
31 struct ReallocStub : public TestEnv
32 {
33   ReallocStub() : TestEnv("./realloc_interception_test.log") {}
34 };
35
36 TEST_F(ReallocStub, must_allocate_space_as_standard_realloc)
37 {
38   void *p = malloc(128);
39   p = realloc(p, 1024);
40
41   ASSERT_TRUE(p);
42   free(p);
43 }
44
45 TEST_F(ReallocStub, must_log_allocation_deallocation_events_if_trace_is_ready_for_it)
46 {
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);
53   GlobalTrace.reset();
54
55   ASSERT_TRUE(p3);
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");
60   free(p3);
61 }
62
63 TEST_F(ReallocStub,
64        must_not_do_the_record_about_allocation_deallocation_events_if_original_function_failed)
65 {
66   GlobalTrace.reset();
67   void *p = malloc(128);
68   GlobalTrace.reset(new Trace);
69
70   void *ptr_after_realloc = realloc(p, std::numeric_limits<size_t>::max());
71   ptr_after_realloc = realloc(p, 0);
72   GlobalTrace.reset();
73
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");
78
79   free(p);
80 }
81
82 TEST_F(ReallocStub, should_work_as_malloc_when_incoming_ptr_is_equal_to_nullptr)
83 {
84   void *p = realloc(nullptr, 1024);
85   GlobalTrace.reset();
86
87   ASSERT_TRUE(p);
88   ASSERT_STREQ(
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");
92
93   free(p);
94 }
95
96 TEST_F(
97   ReallocStub,
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)
99 {
100   void *p = realloc(nullptr, 0);
101   free(p);
102   GlobalTrace.reset();
103
104   ASSERT_TRUE(p);
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");
108 }
109
110 TEST_F(ReallocStub, should_allocate_memory_from_pool_for_symbol_searcher_internal_usage_if_need)
111 {
112   signalizeThatNextAllocationsWillBeForSymbolSearcherInternalUsage();
113   void *p = malloc(128);
114   p = realloc(p, 1024);
115   signalizeThatSymbolSearcherEndedOfWork();
116   GlobalTrace.reset();
117
118   MemoryPoolForSymbolSearcherInternals pool;
119   ASSERT_TRUE(p);
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");
124 }
125
126 } // namespace backstage