summaryrefslogtreecommitdiff
path: root/runtime/contrib/heap_trace/tests/src/symbol_searcher_test.cc
blob: 9ed93311946b4f49f97a48de13f6aa6911b8071a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common_test_environment.h"
#include "file_content_manipulations.h"
#include "test_sample1.h"
#include "test_sample2.h"
#include "test_sample4.h"

#include "symbol_searcher.h"
#include "trace.h"

#include <dlfcn.h>
#include <linux/limits.h>
#include <unistd.h>

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

extern std::unique_ptr<Trace> GlobalTrace;

fs::path exePath()
{
  char result[PATH_MAX] = {0};
  ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
  return fs::path(result).parent_path();
}

namespace backstage
{

struct SymbolSearcher : TestEnv
{
  SymbolSearcher() : TestEnv("./symbol_searcher_test.log") {}
};

TEST_F(SymbolSearcher, should_find_symbol_in_linked_library)
{
  ASSERT_TRUE((void *)funcDefinedOnlyInTestSample4 == findSymbol("funcDefinedOnlyInTestSample4"));
}

TEST_F(SymbolSearcher, should_find_symbol_in_library_which_have_been_loaded_in_runtime)
{
  fs::path pathToTestLib = exePath() / "libtest_sample2.so";
  void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);

  ASSERT_TRUE(handle);
  ASSERT_TRUE(dlsym(handle, "funcDefinedOnlyInTestSample2") ==
              findSymbol("funcDefinedOnlyInTestSample2"));
  dlclose(handle);
}

TEST_F(SymbolSearcher,
       should_ignore_symbols_found_in_current_translation_unit_if_there_is_another_alternative)
{
  fs::path pathToTestSample2 = exePath() / "libtest_sample2.so";
  void *test_sample2_handle = dlopen(pathToTestSample2.c_str(), RTLD_NOW);
  void *func_addr_in_test_sample2 =
    dlsym(test_sample2_handle, "funcWhichCallFuncDefinedInTestSample3");

  ASSERT_TRUE(test_sample2_handle);
  ASSERT_TRUE((void *)funcDefinedInTestSample3_ButWrappedInTestSample1 !=
              reinterpret_cast<void *(*)()>(func_addr_in_test_sample2)());

  dlclose(test_sample2_handle);
}

TEST_F(SymbolSearcher, should_give_an_opportunity_do_not_log_its_internal_allocations)
{
  GlobalTrace.reset();
  fs::path pathToTestLib = exePath() / "libtest_sample2.so";
  void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);

  GlobalTrace.reset(new Trace);
  void *symbolAddress = findSymbol("funcDefinedOnlyInTestSample2");
  GlobalTrace.reset();

  ASSERT_STREQ(getContentOfFile("./symbol_searcher_test.log").c_str(),
               "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
               "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");

  dlclose(handle);
}

} // namespace backstage