test_loader_resolve_symbol.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <test.h>
23 
24 typedef int (*func_t)(int);
25 
26 void
27 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_invalid_loader()
28 {
29  func_t func;
30 
31  auto ret = bfelf_loader_resolve_symbol(nullptr, "foo", reinterpret_cast<void **>(&func));
33 }
34 
35 void
36 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_invalid_name()
37 {
38  func_t func;
39 
40  bfelf_loader_t loader;
41  memset(&loader, 0, sizeof(loader));
42 
43  auto ret = bfelf_loader_resolve_symbol(&loader, nullptr, reinterpret_cast<void **>(&func));
45 }
46 
47 void
48 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_invalid_addr()
49 {
50  bfelf_loader_t loader;
51  memset(&loader, 0, sizeof(loader));
52 
53  auto ret = bfelf_loader_resolve_symbol(&loader, "foo", nullptr);
55 }
56 
57 void
58 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_no_files_added()
59 {
60  auto ret = 0LL;
61 
62  bfelf_loader_t loader;
63  memset(&loader, 0, sizeof(loader));
64 
65  ret = bfelf_loader_relocate(&loader);
66  this->expect_true(ret == BFELF_SUCCESS);
67 
68  func_t func;
69 
70  ret = bfelf_loader_resolve_symbol(&loader, "foo", reinterpret_cast<void **>(&func));
72 }
73 
74 void
75 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_uninitialized_files()
76 {
77  auto ret = 0LL;
78  bfelf_file_t dummy_misc_ef;
79  bfelf_file_t dummy_code_ef;
80 
81  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
82  this->expect_true(ret == BFELF_SUCCESS);
83  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
84  this->expect_true(ret == BFELF_SUCCESS);
85 
86  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
87  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
88 
89  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
90  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
91 
92  bfelf_loader_t loader;
93  memset(&loader, 0, sizeof(loader));
94 
95  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
96  this->expect_true(ret == BFELF_SUCCESS);
97  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
98  this->expect_true(ret == BFELF_SUCCESS);
99 
100  memset(&dummy_misc_ef, 0, sizeof(dummy_misc_ef));
101  memset(&dummy_code_ef, 0, sizeof(dummy_code_ef));
102 
103  ret = bfelf_loader_relocate(&loader);
104  this->expect_true(ret == BFELF_SUCCESS);
105 
106  func_t func;
107 
108  ret = bfelf_loader_resolve_symbol(&loader, "foo", reinterpret_cast<void **>(&func));
110 }
111 
112 void
113 bfelf_loader_ut::test_bfelf_loader_resolve_no_such_symbol()
114 {
115  auto ret = 0LL;
116  bfelf_file_t dummy_misc_ef;
117  bfelf_file_t dummy_code_ef;
118 
119  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
120  this->expect_true(ret == BFELF_SUCCESS);
121  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
122  this->expect_true(ret == BFELF_SUCCESS);
123 
124  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
125  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
126 
127  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
128  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
129 
130  bfelf_loader_t loader;
131  memset(&loader, 0, sizeof(loader));
132 
133  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
134  this->expect_true(ret == BFELF_SUCCESS);
135  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
136  this->expect_true(ret == BFELF_SUCCESS);
137 
138  ret = bfelf_loader_relocate(&loader);
139  this->expect_true(ret == BFELF_SUCCESS);
140 
141  func_t func;
142 
143  ret = bfelf_loader_resolve_symbol(&loader, "invalid_sym", reinterpret_cast<void **>(&func));
145 }
146 
147 void
148 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_success()
149 {
150  auto ret = 0LL;
151  bfelf_file_t dummy_misc_ef;
152  bfelf_file_t dummy_code_ef;
153 
154  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
155  this->expect_true(ret == BFELF_SUCCESS);
156  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
157  this->expect_true(ret == BFELF_SUCCESS);
158 
159  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
160  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
161 
162  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
163  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
164 
165  bfelf_loader_t loader;
166  memset(&loader, 0, sizeof(loader));
167 
168  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
169  this->expect_true(ret == BFELF_SUCCESS);
170  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
171  this->expect_true(ret == BFELF_SUCCESS);
172 
173  ret = bfelf_loader_relocate(&loader);
174  this->expect_true(ret == BFELF_SUCCESS);
175 
176  func_t func;
177 
178  ret = bfelf_loader_resolve_symbol(&loader, "foo", reinterpret_cast<void **>(&func));
179  this->expect_true(ret == BFELF_SUCCESS);
180 }
181 
182 void
183 bfelf_loader_ut::test_bfelf_loader_resolve_no_such_symbol_no_hash()
184 {
185  auto ret = 0LL;
186  bfelf_file_t dummy_misc_ef;
187  bfelf_file_t dummy_code_ef;
188 
189  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
190  this->expect_true(ret == BFELF_SUCCESS);
191  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
192  this->expect_true(ret == BFELF_SUCCESS);
193 
194  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
195  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
196 
197  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
198  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
199 
200  bfelf_loader_t loader;
201  memset(&loader, 0, sizeof(loader));
202 
203  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
204  this->expect_true(ret == BFELF_SUCCESS);
205  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
206  this->expect_true(ret == BFELF_SUCCESS);
207 
208  dummy_misc_ef.hash = nullptr;
209  dummy_code_ef.hash = nullptr;
210 
211  ret = bfelf_loader_relocate(&loader);
212  this->expect_true(ret == BFELF_SUCCESS);
213 
214  func_t func;
215 
216  ret = bfelf_loader_resolve_symbol(&loader, "invalid_sym", reinterpret_cast<void **>(&func));
218 }
219 
220 void
221 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_success_no_hash()
222 {
223  auto ret = 0LL;
224  bfelf_file_t dummy_misc_ef;
225  bfelf_file_t dummy_code_ef;
226 
227  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
228  this->expect_true(ret == BFELF_SUCCESS);
229  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
230  this->expect_true(ret == BFELF_SUCCESS);
231 
232  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
233  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
234 
235  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
236  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
237 
238  bfelf_loader_t loader;
239  memset(&loader, 0, sizeof(loader));
240 
241  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
242  this->expect_true(ret == BFELF_SUCCESS);
243  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
244  this->expect_true(ret == BFELF_SUCCESS);
245 
246  dummy_misc_ef.hash = nullptr;
247  dummy_code_ef.hash = nullptr;
248 
249  ret = bfelf_loader_relocate(&loader);
250  this->expect_true(ret == BFELF_SUCCESS);
251 
252  func_t func;
253 
254  ret = bfelf_loader_resolve_symbol(&loader, "foo", reinterpret_cast<void **>(&func));
255  this->expect_true(ret == BFELF_SUCCESS);
256 }
257 
258 int dummy_foo(int val)
259 { return val; }
260 
262 { (void)info; return -1; }
263 
265 { (void)info; return -1; }
266 
267 void
268 bfelf_loader_ut::test_bfelf_loader_resolve_symbol_real_test()
269 {
270  auto ret = 0LL;
271  bfelf_file_t dummy_misc_ef;
272  bfelf_file_t dummy_code_ef;
273 
274  this->expect_true(dummy_foo(-1) == -1);
275  this->expect_true(dummy_init(nullptr) == -1);
276  this->expect_true(dummy_fini(nullptr) == -1);
277 
278  ret = bfelf_file_init(m_dummy_misc.get(), m_dummy_misc_length, &dummy_misc_ef);
279  this->expect_true(ret == BFELF_SUCCESS);
280  ret = bfelf_file_init(m_dummy_code.get(), m_dummy_code_length, &dummy_code_ef);
281  this->expect_true(ret == BFELF_SUCCESS);
282 
283  auto &&dummy_misc_pair = get_elf_exec(&dummy_misc_ef);
284  auto &&dummy_code_pair = get_elf_exec(&dummy_code_ef);
285 
286  m_dummy_misc_exec = std::move(std::get<0>(dummy_misc_pair));
287  m_dummy_code_exec = std::move(std::get<0>(dummy_code_pair));
288 
289  bfelf_loader_t loader;
290  memset(&loader, 0, sizeof(loader));
291 
292  ret = bfelf_loader_add(&loader, &dummy_misc_ef, m_dummy_misc_exec.get(), m_dummy_misc_exec.get());
293  this->expect_true(ret == BFELF_SUCCESS);
294  ret = bfelf_loader_add(&loader, &dummy_code_ef, m_dummy_code_exec.get(), m_dummy_code_exec.get());
295  this->expect_true(ret == BFELF_SUCCESS);
296 
297  ret = bfelf_loader_relocate(&loader);
298  this->expect_true(ret == BFELF_SUCCESS);
299 
300  {
301  section_info_t info = {};
303 
304  ret = bfelf_file_get_section_info(&dummy_misc_ef, &info);
305  this->expect_true(ret == BFELF_SUCCESS);
306 
307  ret = bfelf_loader_resolve_symbol(&loader, "local_init", reinterpret_cast<void **>(&local_init));
308  this->expect_true(ret == BFELF_SUCCESS);
309 
310  local_init(&info);
311 
312  ret = bfelf_file_get_section_info(&dummy_code_ef, &info);
313  this->expect_true(ret == BFELF_SUCCESS);
314 
315  ret = bfelf_loader_resolve_symbol(&loader, "local_init", reinterpret_cast<void **>(&local_init));
316  this->expect_true(ret == BFELF_SUCCESS);
317 
318  local_init(&info);
319  }
320 
321  {
322  func_t func = dummy_foo;
323 
324  ret = bfelf_loader_resolve_symbol(&loader, "foo", reinterpret_cast<void **>(&func));
325  this->expect_true(ret == BFELF_SUCCESS);
326 
327  this->expect_true(func(5) == 1005);
328  }
329 
330  {
331  section_info_t info = {};
333 
334  ret = bfelf_file_get_section_info(&dummy_misc_ef, &info);
335  this->expect_true(ret == BFELF_SUCCESS);
336 
337  ret = bfelf_loader_resolve_symbol(&loader, "local_fini", reinterpret_cast<void **>(&local_fini));
338  this->expect_true(ret == BFELF_SUCCESS);
339 
340  local_fini(&info);
341 
342  ret = bfelf_file_get_section_info(&dummy_code_ef, &info);
343  this->expect_true(ret == BFELF_SUCCESS);
344 
345  ret = bfelf_loader_resolve_symbol(&loader, "local_fini", reinterpret_cast<void **>(&local_fini));
346  this->expect_true(ret == BFELF_SUCCESS);
347 
348  local_fini(&info);
349  }
350 }
int64_t(* local_init_t)(struct section_info_t *info)
Definition: crt.h:51
void * memset(void *block, int c, size_t size)
#define BFELF_ERROR_INVALID_ARG
Definition: error_codes.h:81
#define BFELF_ERROR_NO_SUCH_SYMBOL
Definition: error_codes.h:89
int64_t dummy_fini(section_info_t *info)
int(* func_t)(int)
int64_t local_init(struct section_info_t *info)
Definition: dummy_misc.cpp:90
int64_t(* local_fini_t)(struct section_info_t *info)
Definition: crt.h:67
int64_t dummy_init(section_info_t *info)
int dummy_foo(int val)
#define BFELF_SUCCESS
Definition: error_codes.h:80
#define expect_true(a)
int64_t local_fini(struct section_info_t *info)
Definition: dummy_misc.cpp:97