Dpkg_Shlibs.t 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #!/usr/bin/perl
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. use strict;
  16. use warnings;
  17. use Test::More tests => 118;
  18. use Cwd;
  19. use IO::String;
  20. use_ok('Dpkg::Shlibs');
  21. my $tmp;
  22. my @tmp;
  23. my %tmp;
  24. my @save_paths = Dpkg::Shlibs::get_library_paths();
  25. Dpkg::Shlibs::reset_library_paths();
  26. my $srcdir = $ENV{srcdir} || '.';
  27. my $datadir = $srcdir . '/t/Dpkg_Shlibs';
  28. # We want relative paths inside the ld.so.conf fragments to work, and $srcdir
  29. # is usually a relative path, so let's temporarily switch directory.
  30. # XXX: An alternative would be to make parse_ldso_conf relative path aware.
  31. my $cwd = cwd();
  32. chdir($srcdir);
  33. Dpkg::Shlibs::parse_ldso_conf('t/Dpkg_Shlibs/ld.so.conf');
  34. chdir($cwd);
  35. use Data::Dumper;
  36. my @librarypaths = Dpkg::Shlibs::get_library_paths();
  37. is_deeply([qw(/nonexistant32 /nonexistant/lib64
  38. /usr/local/lib /nonexistant/lib128 )],
  39. \@librarypaths, 'parsed library paths');
  40. use_ok('Dpkg::Shlibs::Objdump');
  41. my $obj = Dpkg::Shlibs::Objdump::Object->new;
  42. open my $objdump, '<', "$datadir/objdump.dbd-pg"
  43. or die "$datadir/objdump.dbd-pg: $!";
  44. $obj->parse_objdump_output($objdump);
  45. close $objdump;
  46. ok(!$obj->is_public_library(), 'Pg.so is not a public library');
  47. ok(!$obj->is_executable(), 'Pg.so is not an executable');
  48. open $objdump, '<', "$datadir/objdump.ls"
  49. or die "$datadir/objdump.ls: $!";
  50. $obj->reset();
  51. $obj->parse_objdump_output($objdump);
  52. close $objdump;
  53. ok(!$obj->is_public_library(), 'ls is not a public library');
  54. ok($obj->is_executable(), 'ls is an executable');
  55. my $sym = $obj->get_symbol('optarg@GLIBC_2.0');
  56. ok($sym, 'optarg@GLIBC_2.0 exists');
  57. ok(!$sym->{defined}, 'R_*_COPY relocations are taken into account');
  58. open $objdump, '<', "$datadir/objdump.space"
  59. or die "$datadir/objdump.space: $!";
  60. $obj->reset();
  61. $obj->parse_objdump_output($objdump);
  62. close $objdump;
  63. # Non-regression test for #506139
  64. $sym = $obj->get_symbol('singlespace');
  65. ok($sym, 'version less symbol separated by a single space are correctly parsed');
  66. open $objdump, '<', "$datadir/objdump.libc6-2.6"
  67. or die "$datadir/objdump.libc6-2.6: $!";
  68. $obj->reset();
  69. $obj->parse_objdump_output($objdump);
  70. close $objdump;
  71. ok($obj->is_public_library(), 'libc6 is a public library');
  72. ok(!$obj->is_executable(), 'libc6 is not an executable');
  73. is($obj->{SONAME}, 'libc.so.6', 'SONAME');
  74. is($obj->{HASH}, '0x13d99c', 'HASH');
  75. is($obj->{GNU_HASH}, '0x194', 'GNU_HASH');
  76. is($obj->{format}, 'elf32-i386', 'format');
  77. is_deeply($obj->{flags}, { DYNAMIC => 1, HAS_SYMS => 1, D_PAGED => 1 }, 'flags');
  78. is_deeply($obj->{NEEDED}, [ 'ld-linux.so.2' ], 'NEEDED');
  79. is_deeply([ $obj->get_needed_libraries ], [ 'ld-linux.so.2' ], 'NEEDED');
  80. $sym = $obj->get_symbol('_sys_nerr@GLIBC_2.3');
  81. is_deeply( $sym, { name => '_sys_nerr', version => 'GLIBC_2.3',
  82. soname => 'libc.so.6', objid => 'libc.so.6',
  83. section => '.rodata', dynamic => 1,
  84. debug => '', type => 'O', weak => '',
  85. local => '', global => 1, visibility => '',
  86. hidden => 1, defined => 1 }, 'Symbol' );
  87. $sym = $obj->get_symbol('_IO_stdin_used');
  88. is_deeply( $sym, { name => '_IO_stdin_used', version => '',
  89. soname => 'libc.so.6', objid => 'libc.so.6',
  90. section => '*UND*', dynamic => 1,
  91. debug => '', type => ' ', weak => 1,
  92. local => '', global => '', visibility => '',
  93. hidden => '', defined => '' }, 'Symbol 2' );
  94. my @syms = $obj->get_exported_dynamic_symbols;
  95. is( scalar @syms, 2231, 'defined && dynamic' );
  96. @syms = $obj->get_undefined_dynamic_symbols;
  97. is( scalar @syms, 9, 'undefined && dynamic' );
  98. my $obj_old = Dpkg::Shlibs::Objdump::Object->new;
  99. open $objdump, '<', "$datadir/objdump.libc6-2.3"
  100. or die "$datadir/objdump.libc6-2.3: $!";
  101. $obj_old->parse_objdump_output($objdump);
  102. close $objdump;
  103. use_ok('Dpkg::Shlibs::SymbolFile');
  104. use_ok('Dpkg::Shlibs::Symbol');
  105. my $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbol_file.tmp");
  106. my $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbol_file.tmp");
  107. my $sym_file_old = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbol_file.tmp");
  108. $sym_file->merge_symbols($obj_old, '2.3.6.ds1-13');
  109. $sym_file_old->merge_symbols($obj_old, '2.3.6.ds1-13');
  110. ok( $sym_file->has_object('libc.so.6'), 'SONAME in sym file' );
  111. $sym_file->merge_symbols($obj, '2.6-1');
  112. ok( $sym_file->get_new_symbols($sym_file_old), 'has new symbols' );
  113. ok( $sym_file_old->get_lost_symbols($sym_file), 'has lost symbols' );
  114. is( $sym_file_old->lookup_symbol('__bss_start@Base', ['libc.so.6']),
  115. undef, 'internal symbols are blacklisted' );
  116. %tmp = $sym_file->lookup_symbol('_errno@GLIBC_2.0', ['libc.so.6'], 1);
  117. isa_ok($tmp{symbol}, 'Dpkg::Shlibs::Symbol');
  118. is_deeply(\%tmp, { symbol => Dpkg::Shlibs::Symbol->new(symbol => '_errno@GLIBC_2.0',
  119. minver => '2.3.6.ds1-13', dep_id => 0,
  120. deprecated => '2.6-1'), soname => 'libc.so.6' },
  121. 'deprecated symbol');
  122. # Wildcard test
  123. my $pat = $sym_file_old->create_symbol('*@GLIBC_PRIVATE 2.3.6.wildcard');
  124. $sym_file_old->add_symbol($pat, 'libc.so.6');
  125. $sym_file_old->merge_symbols($obj, '2.6-1');
  126. $sym = $sym_file_old->lookup_symbol('__nss_services_lookup@GLIBC_PRIVATE', 'libc.so.6');
  127. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__nss_services_lookup@GLIBC_PRIVATE',
  128. minver => '2.3.6.wildcard', dep_id => 0, deprecated => 0,
  129. tags => { symver => undef, optional => undef },
  130. tagorder => [ 'symver', 'optional' ],
  131. matching_pattern => $pat ), 'wildcarded symbol');
  132. # Save -> Load test
  133. use File::Temp;
  134. use File::Basename qw(basename);
  135. sub save_load_test {
  136. my ($symfile, $comment, @opts) = @_;
  137. my $save_file = File::Temp->new();
  138. $symfile->save($save_file->filename, @opts);
  139. my $dup = Dpkg::Shlibs::SymbolFile->new(file => $save_file->filename);
  140. # Force sync of non-stored attributes
  141. $dup->{file} = $symfile->{file};
  142. $dup->{arch} = $symfile->{arch};
  143. is_deeply($dup, $symfile, $comment);
  144. if (-f $symfile->{file}) {
  145. is( system(sprintf("diff -u '%s' '%s' >&2", $symfile->{file}, $save_file->filename)), 0,
  146. basename($symfile->{file}) . ' dumped identical');
  147. }
  148. }
  149. save_load_test( $sym_file, 'save -> load' );
  150. # Test ignoring blacklisted symbols
  151. open $objdump, '<', "$datadir/objdump.blacklisted"
  152. or die "objdump.blacklisted: $!";
  153. $obj->reset();
  154. $obj->parse_objdump_output($objdump);
  155. close $objdump;
  156. # Do not ignore any blacklist
  157. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.blacklist-filter");
  158. $sym_file->merge_symbols($obj, '100.MISSING');
  159. $sym = $sym_file->lookup_symbol('symbol@Base', ['libblacklisted.so.0']);
  160. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol@Base',
  161. minver => '1.0', dep_id => 0, deprecated => 0),
  162. 'symbol unaffected w/o ignoring blacklists');
  163. $sym = $sym_file->lookup_symbol('.gomp_critical_user_foo@Base', ['libblacklisted.so.0']);
  164. is($sym, undef, 'gomp symbol omitted while filtering on blacklists');
  165. $sym = $sym_file->lookup_symbol('__aeabi_lcmp@GCC_3.0', ['libblacklisted.so.0']);
  166. is($sym, undef, 'known aeabi symbol omitted while filtering on blacklists');
  167. $sym = $sym_file->lookup_symbol('__aeabi_unknown@GCC_4.0', ['libblacklisted.so.0']);
  168. is($sym, undef, 'unknown aeabi symbol omitted while filtering on blacklists');
  169. # Ignore blacklists using the ignore-blacklists symbol tag
  170. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.blacklist-ignore");
  171. $sym_file->merge_symbols($obj, '100.MISSING');
  172. $sym = $sym_file->lookup_symbol('symbol@Base', ['libblacklisted.so.0']);
  173. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol@Base',
  174. minver => '1.0', dep_id => 0, deprecated => 0),
  175. 'symbol unaffected while ignoring blacklists via symbol tag');
  176. $sym = $sym_file->lookup_symbol('.gomp_critical_user_foo@Base', ['libblacklisted.so.0']);
  177. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '.gomp_critical_user_foo@Base',
  178. minver => '2.0', dep_id => 0, deprecated => 0,
  179. tags => { 'ignore-blacklist' => undef },
  180. tagorder => [ 'ignore-blacklist' ]),
  181. 'blacklisted gomp symbol included via symbol tag');
  182. $sym = $sym_file->lookup_symbol('__aeabi_lcmp@GCC_3.0', ['libblacklisted.so.0']);
  183. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_lcmp@GCC_3.0',
  184. minver => '3.0', dep_id => 0, deprecated => 0,
  185. tags => { 'ignore-blacklist' => undef },
  186. tagorder => [ 'ignore-blacklist' ]),
  187. 'blacklisted known aeabi symbol included via symbol tag');
  188. $sym = $sym_file->lookup_symbol('__aeabi_unknown@GCC_4.0', ['libblacklisted.so.0']);
  189. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_unknown@GCC_4.0',
  190. minver => '4.0', dep_id => 0, deprecated => 0,
  191. tags => { 'ignore-blacklist' => undef },
  192. tagorder => [ 'ignore-blacklist' ]),
  193. 'blacklisted unknown aeabi symbol omitted via symbol tag');
  194. # Ignore blacklists using the Ignore-Blacklist-Groups field
  195. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.blacklist-groups");
  196. $sym_file->merge_symbols($obj, '100.MISSING');
  197. $sym = $sym_file->lookup_symbol('symbol@Base', ['libblacklisted.so.0']);
  198. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol@Base',
  199. minver => '1.0', dep_id => 0, deprecated => 0),
  200. 'symbol unaffected w/o ignoring blacklists');
  201. $sym = $sym_file->lookup_symbol('.gomp_critical_user_foo@Base', ['libblacklisted.so.0']);
  202. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '.gomp_critical_user_foo@Base',
  203. minver => '2.0', dep_id => 0, deprecated => 0),
  204. 'blacklisted gomp symbol included via library field');
  205. $sym = $sym_file->lookup_symbol('__aeabi_lcmp@GCC_3.0', ['libblacklisted.so.0']);
  206. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_lcmp@GCC_3.0',
  207. minver => '3.0', dep_id => 0, deprecated => 0),
  208. 'blacklisted known aeabi symbol included via library field');
  209. $sym = $sym_file->lookup_symbol('__aeabi_unknown@GCC_4.0', ['libblacklisted.so.0']);
  210. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => '__aeabi_unknown@GCC_4.0',
  211. minver => '4.0', dep_id => 0, deprecated => 0),
  212. 'blacklisted unknown aeabi symbol included via library field');
  213. # Test include mechanism of SymbolFile
  214. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.include-1");
  215. $sym = $sym_file->lookup_symbol('symbol_before@Base', ['libfake.so.1']);
  216. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol_before@Base',
  217. minver => '0.9', dep_id => 0, deprecated => 0),
  218. 'symbol before include not lost');
  219. $sym = $sym_file->lookup_symbol('symbol_after@Base', ['libfake.so.1']);
  220. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol_after@Base',
  221. minver => '1.1', dep_id => 0, deprecated => 0),
  222. 'symbol after include not lost');
  223. $sym = $sym_file->lookup_symbol('symbol1_fake1@Base', ['libfake.so.1']);
  224. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol1_fake1@Base',
  225. minver => '1.0', dep_id => 0, deprecated => 0),
  226. 'overrides order with #include');
  227. $sym = $sym_file->lookup_symbol('symbol3_fake1@Base', ['libfake.so.1']);
  228. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol3_fake1@Base',
  229. minver => '0', dep_id => 0, deprecated => 0),
  230. 'overrides order with #include');
  231. is($sym_file->get_smallest_version('libfake.so.1'), '0',
  232. 'get_smallest_version with null version');
  233. $sym = $sym_file->lookup_symbol('symbol_in_libdivert@Base', ['libdivert.so.1']);
  234. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol_in_libdivert@Base',
  235. minver => '1.0~beta1', dep_id => 0, deprecated => 0),
  236. '#include can change current object');
  237. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.include-2");
  238. $sym = $sym_file->lookup_symbol('symbol1_fake2@Base', ['libfake.so.1']);
  239. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol1_fake2@Base',
  240. minver => '1.0', dep_id => 1, deprecated => 0),
  241. 'overrides order with circular #include');
  242. is($sym_file->get_smallest_version('libfake.so.1'), '1.0',
  243. 'get_smallest_version');
  244. # Check dump output
  245. my $io = IO::String->new();
  246. $sym_file->output($io, package => 'libfake1');
  247. is(${$io->string_ref()},
  248. 'libfake.so.1 libfake1 #MINVER#
  249. | libvirtualfake
  250. * Build-Depends-Package: libfake-dev
  251. symbol1_fake2@Base 1.0 1
  252. symbol2_fake2@Base 1.0
  253. symbol3_fake2@Base 1.1
  254. ', "Dump of $datadir/symbols.include-2");
  255. # Check parsing of objdump output on ia64 (local symbols
  256. # without versions and with visibility attribute)
  257. $obj = Dpkg::Shlibs::Objdump::Object->new;
  258. open $objdump, '<', "$datadir/objdump.glib-ia64"
  259. or die "$datadir/objdump.glib-ia64: $!";
  260. $obj->parse_objdump_output($objdump);
  261. close $objdump;
  262. $sym = $obj->get_symbol('IA__g_free');
  263. is_deeply( $sym, { name => 'IA__g_free', version => '',
  264. soname => 'libglib-2.0.so.0', objid => 'libglib-2.0.so.0',
  265. section => '.text', dynamic => 1,
  266. debug => '', type => 'F', weak => '',
  267. local => 1, global => '', visibility => 'hidden',
  268. hidden => '', defined => 1 },
  269. 'symbol with visibility without version' );
  270. ####### Test symbol tagging support ######
  271. # Parsing/dumping
  272. # Template mode
  273. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'amd64');
  274. save_load_test($sym_file, 'template save -> load', template_mode => 1);
  275. # Dumping in non-template mode (amd64) (test for arch tags)
  276. $io = IO::String->new();
  277. $sym_file->output($io);
  278. is(${$io->string_ref()},
  279. 'libbasictags.so.1 libbasictags1 #MINVER#
  280. | libbasictags1 (>= 1.1)
  281. symbol11_optional@Base 1.1 1
  282. symbol21_amd64@Base 2.1
  283. symbol31_randomtag@Base 3.1
  284. symbol51_untagged@Base 5.1
  285. ', 'template vs. non-template on amd64');
  286. # Dumping in non-template mode (i386) (test for arch tags)
  287. $io = IO::String->new();
  288. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'i386');
  289. $sym_file_dup = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/basictags.symbols", arch => 'i386');
  290. $sym_file->output($io);
  291. is(${$io->string_ref()},
  292. 'libbasictags.so.1 libbasictags1 #MINVER#
  293. | libbasictags1 (>= 1.1)
  294. symbol11_optional@Base 1.1 1
  295. symbol22_i386@Base 2.2
  296. symbol31_randomtag@Base 3.1
  297. symbol41_i386_and_optional@Base 4.1
  298. symbol51_untagged@Base 5.1
  299. ', 'template vs. non-template on i386');
  300. ok (defined $sym_file->{objects}{'libbasictags.so.1'}{syms}{'symbol21_amd64@Base'},
  301. 'syms keys are symbol names without quotes');
  302. # Preload objdumps
  303. my $tags_obj_i386 = Dpkg::Shlibs::Objdump::Object->new();
  304. open $objdump, '<', "$datadir/objdump.basictags-i386"
  305. or die "$datadir/objdump.basictags-i386: $!";
  306. $tags_obj_i386->parse_objdump_output($objdump);
  307. close $objdump;
  308. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  309. is_deeply($sym_file, $sym_file_dup, 'is objdump.basictags-i386 and basictags.symbols in sync');
  310. my $tags_obj_amd64 = Dpkg::Shlibs::Objdump::Object->new();
  311. open $objdump, '<', "$datadir/objdump.basictags-amd64"
  312. or die "$datadir/objdump.basictags-amd64: $!";
  313. $tags_obj_amd64->parse_objdump_output($objdump);
  314. close $objdump;
  315. # Merge/get_{new,lost} tests for optional tag:
  316. # - disappeared
  317. my $symbol11 = $tags_obj_i386->get_symbol('symbol11_optional@Base');
  318. delete $tags_obj_i386->{dynsyms}{'symbol11_optional@Base'};
  319. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  320. $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libbasictags.so.1'], 1);
  321. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol11_optional@Base',
  322. symbol_templ => 'symbol11_optional@Base',
  323. minver => '1.1', dep_id => 1, deprecated => '100.MISSING',
  324. tags => { optional => undef }, tagorder => [ 'optional' ]),
  325. 'disappered optional symbol gets deprecated');
  326. $sym_file->merge_symbols($tags_obj_i386, '101.MISSING');
  327. $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libbasictags.so.1'], 1);
  328. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol11_optional@Base',
  329. symbol_templ => 'symbol11_optional@Base',
  330. minver => '1.1', dep_id => 1, deprecated => '101.MISSING',
  331. tags => { optional => undef }, tagorder => [ 'optional' ]),
  332. 'deprecated text of MISSING optional symbol gets rebumped each merge');
  333. is( scalar($sym_file->get_lost_symbols($sym_file_dup)), 0, 'missing optional symbol is not LOST');
  334. # - reappeared (undeprecate, minver should be 1.1, not 100.MISSED)
  335. $tags_obj_i386->add_dynamic_symbol($symbol11);
  336. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  337. $sym = $sym_file->lookup_symbol('symbol11_optional@Base', ['libbasictags.so.1']);
  338. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol11_optional@Base',
  339. symbol_templ => 'symbol11_optional@Base',
  340. minver => '1.1', dep_id => 1, deprecated => 0,
  341. tags => { optional => undef }, tagorder => [ 'optional' ]),
  342. 'reappered optional symbol gets undeprecated + minver');
  343. is( scalar($sym_file->get_lost_symbols($sym_file_dup) +
  344. $sym_file->get_new_symbols($sym_file_dup)), 0, 'reappeared optional symbol: neither NEW nor LOST');
  345. # Merge/get_{new,lost} tests for arch tag:
  346. # - arch specific appears on wrong arch: 'arch' tag should be removed
  347. my $symbol21 = $tags_obj_amd64->get_symbol('symbol21_amd64@Base');
  348. $tags_obj_i386->add_dynamic_symbol($symbol21);
  349. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  350. $sym = $sym_file->lookup_symbol('symbol21_amd64@Base', ['libbasictags.so.1']);
  351. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol21_amd64@Base',
  352. symbol_templ => 'symbol21_amd64@Base', symbol_quoted => "'",
  353. minver => '2.1', dep_id => 0, deprecated => 0),
  354. 'symbol appears on foreign arch, arch tag should be removed');
  355. @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_new_symbols($sym_file_dup);
  356. is_deeply( \@tmp, [ 'symbol21_amd64@Base' ], 'symbol from foreign arch is NEW');
  357. is( $sym->get_symbolspec(1), ' symbol21_amd64@Base 2.1', 'no tags => no quotes in the symbol name' );
  358. # - arch specific symbol disappears
  359. delete $tags_obj_i386->{dynsyms}{'symbol22_i386@Base'};
  360. delete $tags_obj_i386->{dynsyms}{'symbol41_i386_and_optional@Base'};
  361. $sym_file->merge_symbols($tags_obj_i386, '100.MISSING');
  362. $sym = $sym_file->lookup_symbol('symbol22_i386@Base', ['libbasictags.so.1'], 1);
  363. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol22_i386@Base',
  364. symbol_templ => 'symbol22_i386@Base',
  365. minver => '2.2', dep_id => 0, deprecated => '100.MISSING',
  366. tags => { arch => '!amd64 !ia64 !alpha' },
  367. tagorder => [ 'arch' ]),
  368. 'disappeared arch specific symbol gets deprecated');
  369. $sym = $sym_file->lookup_symbol('symbol41_i386_and_optional@Base', ['libbasictags.so.1'], 1);
  370. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol41_i386_and_optional@Base',
  371. symbol_templ => 'symbol41_i386_and_optional@Base',
  372. symbol_quoted => '"',
  373. minver => '4.1', dep_id => 0, deprecated => '100.MISSING',
  374. tags => { arch => 'i386', optional => 'reason' },
  375. tagorder => [ 'arch', 'optional' ]),
  376. 'disappeared optional arch specific symbol gets deprecated');
  377. @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
  378. is_deeply( \@tmp, [ 'symbol22_i386@Base' ], "missing arch specific is LOST, but optional arch specific isn't");
  379. # Tests for tagged #includes
  380. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/symbols.include-3", arch => 'i386');
  381. $sym = $sym_file->lookup_symbol('symbol2_fake1@Base', ['libbasictags.so.2']);
  382. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol2_fake1@Base',
  383. minver => '1.0',
  384. tags => { optional => undef, 'random tag' => 'random value' },
  385. tagorder => [ 'optional', 'random tag' ] ),
  386. 'symbols from #included file inherits tags');
  387. $sym = $sym_file->lookup_symbol('symbol41_i386_and_optional@Base', ['libbasictags.so.1']);
  388. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol41_i386_and_optional@Base',
  389. symbol_templ => 'symbol41_i386_and_optional@Base',
  390. symbol_quoted => '"',
  391. minver => '4.1',
  392. tags => { optional => 'reason', t => 'v', arch => 'i386' },
  393. tagorder => [ 'optional', 't', 'arch' ]),
  394. 'symbols in #included file can override tag values');
  395. $sym = $sym_file->lookup_symbol('symbol51_untagged@Base', ['libbasictags.so.1']);
  396. is_deeply($sym, Dpkg::Shlibs::Symbol->new(symbol => 'symbol51_untagged@Base',
  397. minver => '5.1',
  398. tags => { optional => 'from parent', t => 'v' },
  399. tagorder => [ 'optional', 't' ]),
  400. 'symbols are properly cloned when #including');
  401. # Test Symbol::clone()
  402. $sym = Dpkg::Shlibs::Symbol->new(symbol => 'foobar', testfield => 1, teststruct => { foo => 1 });
  403. $tmp = $sym->clone();
  404. $tmp->{teststruct}{foo} = 3;
  405. $tmp->{testfield} = 3;
  406. is ( $sym->{teststruct}{foo}, 1, 'original field "foo" not changed' );
  407. is ( $sym->{testfield}, 1, 'original field "testfield" not changed' );
  408. ############ Test symbol patterns ###########
  409. sub load_patterns_obj {
  410. $obj = Dpkg::Shlibs::Objdump::Object->new();
  411. open $objdump, '<', "$datadir/objdump.patterns"
  412. or die "$datadir/objdump.patterns: $!";
  413. $obj->parse_objdump_output($objdump);
  414. close $objdump;
  415. return $obj;
  416. }
  417. sub load_patterns_symbols {
  418. $sym_file = Dpkg::Shlibs::SymbolFile->new(file => "$datadir/patterns.symbols");
  419. return $sym_file;
  420. }
  421. load_patterns_obj();
  422. $sym_file_dup = load_patterns_symbols();
  423. load_patterns_symbols();
  424. save_load_test($sym_file, 'save -> load test of patterns template', template_mode => 1);
  425. isnt( $sym_file->get_patterns('libpatterns.so.1') , 0,
  426. 'patterns.symbols has patterns');
  427. $sym_file->merge_symbols($obj, '100.MISSING');
  428. @tmp = map { $_->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
  429. is_deeply(\@tmp, [], 'no LOST symbols if all patterns matched.');
  430. @tmp = map { $_->get_symbolname() } $sym_file->get_new_symbols($sym_file_dup);
  431. is_deeply(\@tmp, [], 'no NEW symbols if all patterns matched.');
  432. # Pattern resolution order: aliases (c++, symver), generic
  433. $sym = $sym_file->lookup_symbol('SYMVER_1@SYMVER_1', 'libpatterns.so.1');
  434. is($sym->{minver}, '1', 'specific SYMVER_1 symbol');
  435. $sym = $sym_file->lookup_symbol('_ZN3NSB6Symver14symver_method1Ev@SYMVER_1', 'libpatterns.so.1');
  436. is($sym->{minver}, '1.method1', 'specific symbol preferred over pattern');
  437. $sym = $sym_file->lookup_symbol('_ZN3NSB6Symver14symver_method2Ev@SYMVER_1', 'libpatterns.so.1');
  438. is($sym->{minver}, '1.method2', 'c++ alias pattern preferred over generic pattern');
  439. is($sym->get_pattern()->get_symbolname(), 'NSB::Symver::symver_method2()@SYMVER_1',
  440. 'c++ alias pattern preferred over generic pattern, on demangled name');
  441. $sym = $sym_file->lookup_symbol('_ZN3NSB6SymverD1Ev@SYMVER_1', 'libpatterns.so.1');
  442. is ( $sym->{minver}, '1.generic', 'generic (c++ & symver) pattern covers the rest (destructor)' );
  443. ok($sym->get_pattern()->equals($sym_file->create_symbol('(c++|symver)SYMVER_1 1.generic')),
  444. 'generic (c++ & symver) pattern covers the rest (destructor), compared');
  445. # Test old style wildcard support
  446. load_patterns_symbols();
  447. $sym = $sym_file->create_symbol('*@SYMVEROPT_2 2');
  448. ok($sym->is_optional(), 'Old style wildcard is optional');
  449. is($sym->get_alias_type(), 'symver', 'old style wildcard is a symver pattern');
  450. is($sym->get_symbolname(), 'SYMVEROPT_2', 'wildcard pattern got renamed');
  451. $pat = $sym_file->lookup_pattern('(symver|optional)SYMVEROPT_2', 'libpatterns.so.1');
  452. $sym->{symbol_templ} = $pat->{symbol_templ};
  453. is_deeply($pat, $sym, 'old style wildcard is the same as (symver|optional)');
  454. # Get rid of all SymverOptional symbols
  455. foreach my $tmp (keys %{$obj->{dynsyms}}) {
  456. delete $obj->{dynsyms}{$tmp} if ($tmp =~ /SymverOptional/);
  457. }
  458. $sym_file->merge_symbols($obj, '100.MISSING');
  459. is_deeply ( [ map { $_->get_symbolname() } $pat->get_pattern_matches() ],
  460. [], 'old style wildcard matches nothing.');
  461. is($pat->{deprecated}, '100.MISSING', 'old style wildcard gets deprecated.');
  462. @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
  463. is_deeply(\@tmp, [], 'but old style wildcard is not LOST.');
  464. # 'Internal' pattern covers all internal symbols
  465. load_patterns_obj();
  466. @tmp = grep { $_->get_symbolname() =~ /Internal/ } $sym_file->get_symbols('libpatterns.so.1');
  467. $sym = $sym_file->create_symbol('(regex|c++)^_Z(T[ISV])?N3NSA6ClassA8Internal.*@Base$ 1.internal');
  468. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
  469. is_deeply ([ sort $pat->get_pattern_matches() ], [ sort @tmp ],
  470. 'Pattern covers all internal symbols');
  471. is($tmp[0]->{minver}, '1.internal', 'internal pattern covers first symbol');
  472. # Lookup private pattern
  473. my @private_symnames = sort qw(
  474. _ZN3NSA6ClassA7Private11privmethod1Ei@Base
  475. _ZN3NSA6ClassA7Private11privmethod2Ei@Base
  476. _ZN3NSA6ClassA7PrivateC1Ev@Base
  477. _ZN3NSA6ClassA7PrivateC2Ev@Base
  478. _ZN3NSA6ClassA7PrivateD0Ev@Base
  479. _ZN3NSA6ClassA7PrivateD1Ev@Base
  480. _ZN3NSA6ClassA7PrivateD2Ev@Base
  481. _ZTIN3NSA6ClassA7PrivateE@Base
  482. _ZTSN3NSA6ClassA7PrivateE@Base
  483. _ZTVN3NSA6ClassA7PrivateE@Base
  484. );
  485. $sym = $sym_file->create_symbol('(c++|regex|optional)NSA::ClassA::Private(::.*)?@Base 1');
  486. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
  487. isnt( $pat, undef, 'pattern for private class has been found' );
  488. is_deeply( [ sort map { $_->get_symbolname() } $pat->get_pattern_matches() ],
  489. \@private_symnames, 'private pattern matched expected symbols');
  490. ok( ($pat->get_pattern_matches())[0]->is_optional(),
  491. 'private symbol is optional like its pattern');
  492. ok( $sym_file->lookup_symbol(($pat->get_pattern_matches())[0], 'libpatterns.so.1'),
  493. 'lookup_symbol() finds symbols matched by pattern (after merge)');
  494. # Get rid of a private symbol, it should not be lost
  495. delete $obj->{dynsyms}{$private_symnames[0]};
  496. load_patterns_symbols();
  497. $sym_file->merge_symbols($obj, '100.MISSING');
  498. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
  499. @tmp = map { $_->{symbol}->get_symbolname() } $sym_file->get_lost_symbols($sym_file_dup);
  500. is_deeply(\@tmp, [], 'no LOST symbols when got rid of patterned optional symbol.');
  501. ok(!$pat->{deprecated}, 'there are still matches, pattern is not deprecated.');
  502. # Get rid of all private symbols, the pattern should be deprecated.
  503. foreach my $tmp (@private_symnames) {
  504. delete $obj->{dynsyms}{$tmp};
  505. }
  506. load_patterns_symbols();
  507. $sym_file->merge_symbols($obj, '100.MISSING');
  508. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1', 1);
  509. @tmp = $sym_file->get_lost_symbols($sym_file_dup);
  510. is_deeply( \@tmp, [ ],
  511. 'All private symbols gone, but pattern is not LOST because it is optional.');
  512. is( $pat->{deprecated}, '100.MISSING',
  513. 'All private symbols gone - pattern deprecated.');
  514. # Internal symbols. All covered by the pattern?
  515. @tmp = grep { $_->get_symbolname() =~ /Internal/ } values %{$sym_file->{objects}{'libpatterns.so.1'}{syms}};
  516. $sym = $sym_file->create_symbol('(regex|c++)^_Z(T[ISV])?N3NSA6ClassA8Internal.*@Base$ 1.internal');
  517. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
  518. is_deeply ([ sort $pat->get_pattern_matches() ], [ sort @tmp ],
  519. 'Pattern covers all internal symbols');
  520. is($tmp[0]->{minver}, '1.internal', 'internal pattern covers first symbol');
  521. # Delete matches of the non-optional pattern
  522. $sym = $sym_file->create_symbol('(c++)"non-virtual thunk to NSB::ClassD::generate_vt(char const*) const@Base" 1');
  523. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
  524. isnt( $pat, undef, 'lookup_pattern() finds alias-based pattern' );
  525. is(scalar($pat->get_pattern_matches()), 2, 'two matches for the generate_vt pattern');
  526. foreach my $tmp ($pat->get_pattern_matches()) {
  527. delete $obj->{dynsyms}{$tmp->get_symbolname()};
  528. }
  529. load_patterns_symbols();
  530. $sym_file->merge_symbols($obj, '100.MISSING');
  531. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1', 1);
  532. @tmp = map { scalar $sym_file->lookup_pattern($_->{symbol}, 'libpatterns.so.1', 1) }
  533. $sym_file->get_lost_symbols($sym_file_dup);
  534. is_deeply(\@tmp, [ $pat ], 'No matches - generate_vt() pattern is LOST.');
  535. is( $pat->{deprecated}, '100.MISSING',
  536. 'No matches - generate_vt() pattern is deprecated.');
  537. # Pattern undeprecation when matches are discovered
  538. load_patterns_obj();
  539. load_patterns_symbols();
  540. $pat = $sym_file_dup->lookup_pattern($sym, 'libpatterns.so.1');
  541. $pat->{deprecated} = '0.1-1';
  542. $pat = $sym_file->lookup_pattern($sym, 'libpatterns.so.1');
  543. $pat->{deprecated} = '0.1-1';
  544. $sym_file->merge_symbols($obj, '100.FOUND');
  545. ok( ! $pat->{deprecated},
  546. 'Previously deprecated pattern with matches got undeprecated');
  547. is( $pat->{minver}, '100.FOUND',
  548. 'Previously deprecated pattern with matches got minver bumped');
  549. @tmp = map { $_->{symbol}->get_symbolspec(1) } $sym_file->get_new_symbols($sym_file_dup);
  550. is_deeply( \@tmp, [ $pat->get_symbolspec(1) ],
  551. 'Previously deprecated pattern with matches is NEW. Matches themselves are not NEW.');
  552. foreach my $sym ($pat->get_pattern_matches()) {
  553. ok(!$sym->{deprecated}, $sym->get_symbolname() . ': not deprecated');
  554. is($sym->{minver}, '100.FOUND', $sym->get_symbolname() . ': version bumped');
  555. }