File: test-symtab.cc

package info (click to toggle)
libabigail 2.9-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,021,756 kB
  • sloc: xml: 572,663; cpp: 110,945; sh: 11,868; ansic: 4,329; makefile: 3,486; python: 1,684; ada: 62
file content (447 lines) | stat: -rw-r--r-- 16,186 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- Mode: C++ -*-
//
// Copyright (C) 2020-2025 Google, Inc.
//
// Author: Matthias Maennich

/// @file
///
/// This program tests symtab invariants through abg-corpus.

#include <iostream>
#include <limits>
#include <string>
#include <vector>

#include "abg-corpus.h"
#include "abg-dwarf-reader.h"
#include "abg-fwd.h"
#include "abg-ir.h"
#include "abg-tools-utils.h"
#include "lib/catch.hpp"
#include "test-utils.h"

using namespace abigail;

using ir::environment;
using ir::environment_sptr;
using suppr::suppressions_type;

static const std::string test_data_dir =
    std::string(abigail::tests::get_src_dir()) + "/tests/data/test-symtab/";

// There are 4 undefined global variables per binary, at least in this
// testsuite. For instance, here are the undefined global variables
// what we see in the
// "tests/data/test-symtab/basic/single_variable.so" binary:
// __cxa_finalize, _ITM_registerTMCloneTable,
// _ITM_deregisterTMCloneTable, __gmon_start__.
static const int NB_UNDEFINED_VARS_PER_BINARY = 4;

fe_iface::status
read_corpus(const std::string&		    path,
	    corpus_sptr&		    result,
	    const std::vector<std::string>& whitelist_paths =
		std::vector<std::string>())
{
  const std::string& absolute_path = test_data_dir + path;

  environment env;
  const std::vector<string> debug_info_root_paths;
  abigail::elf_based_reader_sptr rdr =
    dwarf::create_reader(absolute_path, debug_info_root_paths,
			 env, /* load_all_type = */ true,
			 /* linux_kernel_mode = */ true);

  if (!whitelist_paths.empty())
    {
      const suppressions_type& wl_suppr =
	tools_utils::gen_suppr_spec_from_kernel_abi_whitelists(
	  whitelist_paths);
      REQUIRE_FALSE(wl_suppr.empty());
      rdr->add_suppressions(wl_suppr);
    }

  fe_iface::status status = fe_iface::STATUS_UNKNOWN;
  result = rdr->read_corpus(status);

  REQUIRE(status != fe_iface::STATUS_UNKNOWN);
  return status;
}

TEST_CASE("Symtab::Empty", "[symtab, basic]")
{
  const std::string	     binary = "basic/empty.so";
  corpus_sptr		     corpus_ptr;
  read_corpus(binary, corpus_ptr);
  REQUIRE(corpus_ptr->get_fun_symbol_map().empty());
  REQUIRE(corpus_ptr->get_var_symbol_map().empty());
}

TEST_CASE("Symtab::NoDebugInfo", "[symtab, basic]")
{
  const std::string	     binary = "basic/no_debug_info.so";
  corpus_sptr		     corpus_ptr;
  const fe_iface::status status = read_corpus(binary, corpus_ptr);
  REQUIRE(corpus_ptr);

  REQUIRE(status
	  == (fe_iface::STATUS_OK
	      | fe_iface::STATUS_DEBUG_INFO_NOT_FOUND));
}

// this value indicates in the following helper method, that we do not want to
// assert for this particular value. In other words, N is a placeholder for an
// arbitrary value.
#define N std::numeric_limits<size_t>::max()

corpus_sptr
assert_symbol_count(const std::string& path,
		    size_t	       function_symbols = 0,
		    size_t	       variable_symbols = 0,
		    size_t	       undefined_function_symbols = 0,
		    size_t	       undefined_variable_symbols = 0,
		    const std::vector<std::string>& whitelist_paths =
			std::vector<std::string>())
{
  corpus_sptr		     corpus_ptr;
  const fe_iface::status status =
    read_corpus(path, corpus_ptr, whitelist_paths);
  REQUIRE(corpus_ptr);

  REQUIRE((status & fe_iface::STATUS_OK));
  const corpus& corpus = *corpus_ptr;

  size_t total_symbols = 0;

  if (function_symbols != N)
    {
      CHECK(corpus.get_sorted_fun_symbols().size() == function_symbols);
      CHECK(corpus.get_fun_symbol_map().size() == function_symbols);
      total_symbols += function_symbols;
    }
  if (variable_symbols != N)
    {
      CHECK(corpus.get_sorted_var_symbols().size() == variable_symbols);
      CHECK(corpus.get_var_symbol_map().size() == variable_symbols);
      total_symbols += variable_symbols;
    }
  if (undefined_variable_symbols != N)
    {
      CHECK(corpus.get_sorted_undefined_fun_symbols().size()
	    == undefined_function_symbols);
      CHECK(corpus.get_undefined_fun_symbol_map().size()
	    == undefined_function_symbols);
      total_symbols += undefined_function_symbols;
    }
  if (undefined_function_symbols != N)
    {
      CHECK(corpus.get_sorted_undefined_var_symbols().size()
	    == undefined_variable_symbols);
      CHECK(corpus.get_undefined_var_symbol_map().size()
	    == undefined_variable_symbols);
      total_symbols += undefined_variable_symbols;
    }

  // assert the corpus reports being empty consistently with the symbol count
  CHECK(corpus.is_empty() == (total_symbols == 0));

  return corpus_ptr;
}

TEST_CASE("Symtab::SimpleSymtabs", "[symtab, basic]")
{
  GIVEN("a binary with no exported symbols")
  {
    // TODO: should pass, but does currently not as empty tables are treated
    //       like the error case, but this is an edge case anyway.
    // assert_symbol_count("empty.so");
  }

  GIVEN("a binary with a single exported function")
  {
    const std::string	   binary = "basic/single_function.so";
    const corpus_sptr&	   corpus =
      assert_symbol_count(binary, 1, 0, 0, NB_UNDEFINED_VARS_PER_BINARY);
    const elf_symbol_sptr& symbol =
	corpus->lookup_function_symbol("exported_function");
    REQUIRE(symbol);
    CHECK(!corpus->lookup_variable_symbol("exported_function"));
    CHECK(symbol == corpus->lookup_function_symbol(*symbol));
    CHECK(symbol != corpus->lookup_variable_symbol(*symbol));
  }

  GIVEN("a binary with a single exported variable")
  {
    const std::string	   binary = "basic/single_variable.so";
    const corpus_sptr&	   corpus =
      assert_symbol_count(binary, 0, 1, 0, NB_UNDEFINED_VARS_PER_BINARY);
    const elf_symbol_sptr& symbol =
	corpus->lookup_variable_symbol("exported_variable");
    REQUIRE(symbol);
    CHECK(!corpus->lookup_function_symbol("exported_variable"));
    CHECK(symbol == corpus->lookup_variable_symbol(*symbol));
    CHECK(symbol != corpus->lookup_function_symbol(*symbol));
  }

  GIVEN("a binary with one function and one variable exported")
  {
    const std::string  binary = "basic/one_function_one_variable.so";
    const corpus_sptr& corpus =
      assert_symbol_count(binary, 1, 1, 0, NB_UNDEFINED_VARS_PER_BINARY);
    CHECK(corpus->lookup_function_symbol("exported_function"));
    CHECK(!corpus->lookup_variable_symbol("exported_function"));
    CHECK(corpus->lookup_variable_symbol("exported_variable"));
    CHECK(!corpus->lookup_function_symbol("exported_variable"));
  }

  GIVEN("a binary with a single undefined function")
  {
    const std::string  binary = "basic/single_undefined_function.so";
    const corpus_sptr corpus =
      assert_symbol_count(binary, 0, 0, 1, NB_UNDEFINED_VARS_PER_BINARY);
  }

  GIVEN("a binary with a single undefined variable")
  {
    const std::string  binary = "basic/single_undefined_variable.so";
    const corpus_sptr corpus =
      assert_symbol_count(binary, 0, 0, 0, NB_UNDEFINED_VARS_PER_BINARY + 1);
  }

  GIVEN("a binary with one function and one variable undefined")
  {
    const std::string  binary = "basic/one_function_one_variable_undefined.so";
    const corpus_sptr corpus =
      assert_symbol_count(binary, 0, 0, 1, NB_UNDEFINED_VARS_PER_BINARY + 1);
  }
}

TEST_CASE("Symtab::SymtabWithWhitelist", "[symtab, whitelist]")
{
  GIVEN("a binary with one function and one variable exported")
  {
    const std::string binary = "basic/one_function_one_variable.so";

    GIVEN("we read the binary without any whitelists")
    {
      const corpus_sptr& corpus =
	assert_symbol_count(binary, 1, 1, 0, NB_UNDEFINED_VARS_PER_BINARY);
      CHECK(corpus->lookup_function_symbol("exported_function"));
      CHECK(!corpus->lookup_variable_symbol("exported_function"));
      CHECK(corpus->lookup_variable_symbol("exported_variable"));
      CHECK(!corpus->lookup_function_symbol("exported_variable"));
    }

    GIVEN("we read the binary with all symbols on the whitelists")
    {
      std::vector<std::string> whitelists;
      whitelists.push_back(test_data_dir
			   + "basic/one_function_one_variable_all.whitelist");
      const corpus_sptr& corpus =
	assert_symbol_count(binary, 1, 1, 0,
			    NB_UNDEFINED_VARS_PER_BINARY,
			    whitelists);
      CHECK(corpus->lookup_function_symbol("exported_function"));
      CHECK(!corpus->lookup_variable_symbol("exported_function"));
      CHECK(corpus->lookup_variable_symbol("exported_variable"));
      CHECK(!corpus->lookup_function_symbol("exported_variable"));
    }

    GIVEN("we read the binary with only irrelevant symbols whitelisted")
    {
      std::vector<std::string> whitelists;
      whitelists.push_back(
	test_data_dir
	+ "basic/one_function_one_variable_irrelevant.whitelist");

      corpus_sptr		 corpus_ptr;
      read_corpus(binary, corpus_ptr, whitelists);
      REQUIRE(corpus_ptr->get_fun_symbol_map().empty());
      REQUIRE(corpus_ptr->get_var_symbol_map().empty());
    }

    GIVEN("we read the binary with only the function whitelisted")
    {
      std::vector<std::string> whitelists;
      whitelists.push_back(
	test_data_dir + "basic/one_function_one_variable_function.whitelist");
      const corpus_sptr& corpus =
	assert_symbol_count(binary, 1, 0, 0,
			    NB_UNDEFINED_VARS_PER_BINARY,
			    whitelists);
      CHECK(corpus->lookup_function_symbol("exported_function"));
      CHECK(!corpus->lookup_variable_symbol("exported_function"));
      CHECK(!corpus->lookup_variable_symbol("exported_variable"));
      CHECK(!corpus->lookup_function_symbol("exported_variable"));
    }

    GIVEN("we read the binary with only the variable whitelisted")
    {
      std::vector<std::string> whitelists;
      whitelists.push_back(
	test_data_dir + "basic/one_function_one_variable_variable.whitelist");
      const corpus_sptr& corpus =
	assert_symbol_count(binary, 0, 1, 0,
			    NB_UNDEFINED_VARS_PER_BINARY,
			    whitelists);
      CHECK(!corpus->lookup_function_symbol("exported_function"));
      CHECK(!corpus->lookup_variable_symbol("exported_function"));
      CHECK(corpus->lookup_variable_symbol("exported_variable"));
      CHECK(!corpus->lookup_function_symbol("exported_variable"));
    }
  }
}

TEST_CASE("Symtab::AliasedFunctionSymbols", "[symtab, functions, aliases]")
{
  const std::string  binary = "basic/aliases.so";
  const corpus_sptr& corpus =
    assert_symbol_count(binary, 5, 5, 0,
			NB_UNDEFINED_VARS_PER_BINARY);

  // The main symbol is not necessarily the one that is aliased to in the
  // code So, this can't be decided by just looking at ELF. Hence acquire the
  // main symbol.
  const elf_symbol_sptr& main_symbol =
    corpus->lookup_function_symbol("exported_function")->get_main_symbol();
  REQUIRE(main_symbol);

  // But since we know that 'exported_function' is the main symbol and this
  // can be discovered from DWARF
  CHECK(corpus->lookup_function_symbol("exported_function")->is_main_symbol());

  CHECK(corpus->lookup_function_symbol("exported_function")
	  ->get_number_of_aliases() == 4);

  CHECK(main_symbol->has_aliases());
  CHECK(main_symbol->get_number_of_aliases() == 4);
  CHECK(main_symbol->get_main_symbol() == main_symbol);
}

TEST_CASE("Symtab::AliasedVariableSymbols", "[symtab, variables, aliases]")
{
  const std::string  binary = "basic/aliases.so";
  const corpus_sptr& corpus =
    assert_symbol_count(binary, 5, 5, 0, NB_UNDEFINED_VARS_PER_BINARY);
  // The main symbol is not necessarily the one that is aliased to in the
  // code So, this can't be decided by just looking at ELF. Hence acquire the
  // main symbol.
  const elf_symbol_sptr& main_symbol =
    corpus->lookup_variable_symbol("exported_variable")->get_main_symbol();
  REQUIRE(main_symbol);

  // But since we know that 'exported_function' is the main symbol and this
  // can be discovered from DWARF
  CHECK(corpus->lookup_variable_symbol("exported_variable")->is_main_symbol());

  CHECK(corpus->lookup_variable_symbol("exported_variable")
	  ->get_number_of_aliases() == 4);

  CHECK(main_symbol->has_aliases());
  CHECK(main_symbol->get_number_of_aliases() == 4);
  CHECK(main_symbol->get_main_symbol() == main_symbol);
}

static const char* kernel_versions[] = { "4.14", "4.19", "5.4", "5.6" };
static const size_t nr_kernel_versions =
    sizeof(kernel_versions) / sizeof(kernel_versions[0]);

TEST_CASE("Symtab::SimpleKernelSymtabs", "[symtab, basic, kernel, ksymtab]")
{
  for (size_t i = 0; i < nr_kernel_versions; ++i)
    {
      const std::string base_path =
	  "kernel-" + std::string(kernel_versions[i]) + "/";

      GIVEN("The binaries in " + base_path)
      {

	GIVEN("a kernel module with no exported symbols")
	{
	  // TODO: should pass, but does currently not as empty tables are
	  // treated
	  //       like the error case, but this is an edge case anyway.
	  // assert_symbol_count(base_path + "empty.so");
	}

	GIVEN("a kernel module with a single exported function")
	{
	  const std::string	 binary = base_path + "single_function.ko";
	  const corpus_sptr&	 corpus = assert_symbol_count(binary, 1, 0);
	  const elf_symbol_sptr& symbol =
	      corpus->lookup_function_symbol("exported_function");
	  REQUIRE(symbol);
	  CHECK(!corpus->lookup_variable_symbol("exported_function"));
	  CHECK(symbol == corpus->lookup_function_symbol(*symbol));
	  CHECK(symbol != corpus->lookup_variable_symbol(*symbol));
	}

	GIVEN("a kernel module with a single GPL exported function")
	{
	  const std::string	 binary = base_path + "single_function_gpl.ko";
	  const corpus_sptr&	 corpus = assert_symbol_count(binary, 1, 0);
	  const elf_symbol_sptr& symbol =
	      corpus->lookup_function_symbol("exported_function_gpl");
	  REQUIRE(symbol);
	  CHECK(!corpus->lookup_variable_symbol("exported_function_gpl"));
	  CHECK(symbol == corpus->lookup_function_symbol(*symbol));
	  CHECK(symbol != corpus->lookup_variable_symbol(*symbol));
	}

	GIVEN("a binary with a single exported variable")
	{
	  const std::string	 binary = base_path + "single_variable.ko";
	  const corpus_sptr&	 corpus = assert_symbol_count(binary, 0, 1);
	  const elf_symbol_sptr& symbol =
	      corpus->lookup_variable_symbol("exported_variable");
	  REQUIRE(symbol);
	  CHECK(!corpus->lookup_function_symbol("exported_variable"));
	  CHECK(symbol == corpus->lookup_variable_symbol(*symbol));
	  CHECK(symbol != corpus->lookup_function_symbol(*symbol));
	}

	GIVEN("a binary with a single GPL exported variable")
	{
	  const std::string	 binary = base_path + "single_variable_gpl.ko";
	  const corpus_sptr&	 corpus = assert_symbol_count(binary, 0, 1);
	  const elf_symbol_sptr& symbol =
	      corpus->lookup_variable_symbol("exported_variable_gpl");
	  REQUIRE(symbol);
	  CHECK(!corpus->lookup_function_symbol("exported_variable_gpl"));
	  CHECK(symbol == corpus->lookup_variable_symbol(*symbol));
	  CHECK(symbol != corpus->lookup_function_symbol(*symbol));
	}

	GIVEN("a binary with one function and one variable (GPL) exported")
	{
	  const std::string  binary = base_path + "one_of_each.ko";
	  const corpus_sptr& corpus = assert_symbol_count(binary, 2, 2);
	  CHECK(corpus->lookup_function_symbol("exported_function"));
	  CHECK(!corpus->lookup_variable_symbol("exported_function"));
	  CHECK(corpus->lookup_function_symbol("exported_function_gpl"));
	  CHECK(!corpus->lookup_variable_symbol("exported_function_gpl"));
	  CHECK(corpus->lookup_variable_symbol("exported_variable"));
	  CHECK(!corpus->lookup_function_symbol("exported_variable"));
	  CHECK(corpus->lookup_variable_symbol("exported_variable_gpl"));
	  CHECK(!corpus->lookup_function_symbol("exported_variable_gpl"));
	}
      }
    }
}

TEST_CASE("Symtab::KernelSymtabsWithCRC", "[symtab, crc, kernel, ksymtab]")
{
  const std::string base_path = "kernel-modversions/";

  GIVEN("a binary with one function and one variable (GPL) exported")
  {
    const std::string  binary = base_path + "one_of_each.ko";
    const corpus_sptr& corpus = assert_symbol_count(binary, 2, 2);
    CHECK(corpus->lookup_function_symbol("exported_function")->get_crc());
    CHECK(corpus->lookup_function_symbol("exported_function_gpl")->get_crc());
    CHECK(corpus->lookup_variable_symbol("exported_variable")->get_crc());
    CHECK(corpus->lookup_variable_symbol("exported_variable_gpl")->get_crc());
  }
}