59fdeedc9b1e45cc94a9d32954ca1aa8f5df5cd0
[platform/core/ml/nnfw.git] / runtime / contrib / heap_trace / tests / src / symbol_searcher_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 #include "test_sample1.h"
20 #include "test_sample2.h"
21 #include "test_sample4.h"
22
23 #include "symbol_searcher.h"
24 #include "trace.h"
25
26 #include <dlfcn.h>
27 #include <linux/limits.h>
28 #include <unistd.h>
29
30 #include <experimental/filesystem>
31
32 namespace fs = std::experimental::filesystem;
33
34 extern std::unique_ptr<Trace> GlobalTrace;
35
36 fs::path exePath()
37 {
38   char result[PATH_MAX] = {0};
39   ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
40   return fs::path(result).parent_path();
41 }
42
43 namespace backstage
44 {
45
46 struct SymbolSearcher : TestEnv
47 {
48   SymbolSearcher() : TestEnv("./symbol_searcher_test.log") {}
49 };
50
51 TEST_F(SymbolSearcher, should_find_symbol_in_linked_library)
52 {
53   ASSERT_TRUE((void *)funcDefinedOnlyInTestSample4 == findSymbol("funcDefinedOnlyInTestSample4"));
54 }
55
56 TEST_F(SymbolSearcher, should_find_symbol_in_library_which_have_been_loaded_in_runtime)
57 {
58   fs::path pathToTestLib = exePath() / "libtest_sample2.so";
59   void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);
60
61   ASSERT_TRUE(handle);
62   ASSERT_TRUE(dlsym(handle, "funcDefinedOnlyInTestSample2") ==
63               findSymbol("funcDefinedOnlyInTestSample2"));
64   dlclose(handle);
65 }
66
67 TEST_F(SymbolSearcher,
68        should_ignore_symbols_found_in_current_translation_unit_if_there_is_another_alternative)
69 {
70   fs::path pathToTestSample2 = exePath() / "libtest_sample2.so";
71   void *test_sample2_handle = dlopen(pathToTestSample2.c_str(), RTLD_NOW);
72   void *func_addr_in_test_sample2 =
73       dlsym(test_sample2_handle, "funcWhichCallFuncDefinedInTestSample3");
74
75   ASSERT_TRUE(test_sample2_handle);
76   ASSERT_TRUE((void *)funcDefinedInTestSample3_ButWrappedInTestSample1 !=
77               reinterpret_cast<void *(*)()>(func_addr_in_test_sample2)());
78
79   dlclose(test_sample2_handle);
80 }
81
82 TEST_F(SymbolSearcher, should_give_an_opportunity_do_not_log_its_internal_allocations)
83 {
84   GlobalTrace.reset();
85   fs::path pathToTestLib = exePath() / "libtest_sample2.so";
86   void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);
87
88   GlobalTrace.reset(new Trace);
89   void *symbolAddress = findSymbol("funcDefinedOnlyInTestSample2");
90   GlobalTrace.reset();
91
92   ASSERT_STREQ(getContentOfFile("./symbol_searcher_test.log").c_str(),
93                "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
94                "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
95
96   dlclose(handle);
97 }
98
99 } // namespace backstage