Debian Codegraph @

libstdc++-v3/testsuite/util/testsuite_abi.cc

raw
1 // -*- C++ -*-
2
3 // Copyright (C) 2004-2025 Free Software Foundation, Inc.
4
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3, or (at
8 // your option) any later version.
9
10 // This library is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19
20 // Benjamin Kosnik <[email protected]>
21
22 #include "testsuite_abi.h"
23 #include <cstdlib>
24 #include <sstream>
25 #include <fstream>
26 #include <iostream>
27 #include <vector>
28 #include <algorithm>
29
30 using namespace std;
31
32 void
33 symbol::init(string& data)
34 {
35 const char delim = ':';
36 const char version_delim = '@';
37 const string::size_type npos = string::npos;
38 string::size_type n = 0;
39
40 // Set the type.
41 if (data.find("FUNC") == 0)
42 type = symbol::function;
43 else if (data.find("OBJECT") == 0)
44 type = symbol::object;
45 else if (data.find("TLS") == 0)
46 type = symbol::tls;
47
48 n = data.find_first_of(delim);
49 if (n != npos)
50 data.erase(data.begin(), data.begin() + n + 1);
51
52 // Iff object or TLS, get size info.
53 if (type == symbol::object || type == symbol::tls)
54 {
55 n = data.find_first_of(delim);
56 if (n != npos)
57 {
58 string objectsize(data.begin(), data.begin() + n);
59 istringstream iss(objectsize);
60 int x;
61 iss >> x;
62 if (!iss.fail())
63 size = x;
64 data.erase(data.begin(), data.begin() + n + 1);
65 }
66 }
67
68 // Set the name and raw_name.
69 raw_name = string(data.begin(), data.end());
70 n = data.find_first_of(version_delim);
71 if (n != npos)
72 {
73 // Found version string.
74 name = string(data.begin(), data.begin() + n);
75 n = data.find_last_of(version_delim);
76 data.erase(data.begin(), data.begin() + n + 1);
77
78 // Set version name.
79 version_name = data;
80 }
81 else
82 {
83 // No versioning info.
84 name = string(data.begin(), data.end());
85 version_status = symbol::none;
86 }
87
88 // Set the demangled name.
89 demangled_name = demangle(name);
90 }
91
92 void
93 symbol::print() const
94 {
95 const char tab = '\t';
96 cout << name << endl;
97
98 if (demangled_name != name)
99 cout << demangled_name << endl;
100
101 string vers;
102 switch (version_status)
103 {
104 case none:
105 vers = "none";
106 break;
107 case compatible:
108 vers = "compatible";
109 break;
110 case incompatible:
111 vers = "incompatible";
112 break;
113 case unversioned:
114 vers = "unversioned";
115 break;
116 default:
117 vers = "<default>";
118 }
119 cout << "version status: " << vers << endl;
120
121 if (version_name.size()
122 && (version_status == compatible || version_status == incompatible))
123 cout << version_name << endl;
124
125 string type_string;
126 switch (type)
127 {
128 case function:
129 type_string = "function";
130 break;
131 case object:
132 type_string = "object";
133 break;
134 case tls:
135 type_string = "tls";
136 break;
137 case uncategorized:
138 type_string = "uncategorized";
139 break;
140 default:
141 type_string = "<default>";
142 }
143 cout << "type: " << type_string << endl;
144
145 if (type == object || type == tls)
146 cout << "type size: " << size << endl;
147
148 string status_string;
149 switch (status)
150 {
151 case added:
152 status_string = "added";
153 break;
154 case subtracted:
155 status_string = "subtracted";
156 break;
157 case undesignated:
158 status_string = "undesignated";
159 break;
160 default:
161 status_string = "<default>";
162 }
163 cout << "status: " << status_string << endl;
164
165 cout << endl;
166 }
167
168
169 bool
170 check_version(symbol& test, bool added)
171 {
172 // Construct list of compatible versions.
173 typedef std::vector<std::string> compat_list;
174 static compat_list known_versions;
175 if (known_versions.empty())
176 {
177 // NB: First version here must be the default version for this
178 // version of DT_SONAME.
179 known_versions.push_back("GLIBCXX_3.4");
180 known_versions.push_back("GLIBCXX_LDBL_3.4");
181 known_versions.push_back("GLIBCXX_3.4.1");
182 known_versions.push_back("GLIBCXX_3.4.2");
183 known_versions.push_back("GLIBCXX_3.4.3");
184 known_versions.push_back("GLIBCXX_3.4.4");
185 known_versions.push_back("GLIBCXX_3.4.5");
186 known_versions.push_back("GLIBCXX_3.4.6");
187 known_versions.push_back("GLIBCXX_3.4.7");
188 known_versions.push_back("GLIBCXX_LDBL_3.4.7");
189 known_versions.push_back("GLIBCXX_3.4.8");
190 known_versions.push_back("GLIBCXX_3.4.9");
191 known_versions.push_back("GLIBCXX_3.4.10");
192 known_versions.push_back("GLIBCXX_LDBL_3.4.10");
193 known_versions.push_back("GLIBCXX_3.4.11");
194 known_versions.push_back("GLIBCXX_3.4.12");
195 known_versions.push_back("GLIBCXX_3.4.13");
196 known_versions.push_back("GLIBCXX_3.4.14");
197 known_versions.push_back("GLIBCXX_3.4.15");
198 known_versions.push_back("GLIBCXX_3.4.16");
199 known_versions.push_back("GLIBCXX_3.4.17");
200 known_versions.push_back("GLIBCXX_3.4.18");
201 known_versions.push_back("GLIBCXX_3.4.19");
202 known_versions.push_back("GLIBCXX_3.4.20");
203 known_versions.push_back("GLIBCXX_3.4.21");
204 known_versions.push_back("GLIBCXX_LDBL_3.4.21");
205 known_versions.push_back("GLIBCXX_3.4.22");
206 known_versions.push_back("GLIBCXX_3.4.23");
207 known_versions.push_back("GLIBCXX_3.4.24");
208 known_versions.push_back("GLIBCXX_3.4.25");
209 known_versions.push_back("GLIBCXX_3.4.26");
210 known_versions.push_back("GLIBCXX_3.4.27");
211 known_versions.push_back("GLIBCXX_3.4.28");
212 known_versions.push_back("GLIBCXX_3.4.29");
213 known_versions.push_back("GLIBCXX_LDBL_3.4.29");
214 known_versions.push_back("GLIBCXX_3.4.30");
215 known_versions.push_back("GLIBCXX_3.4.31");
216 known_versions.push_back("GLIBCXX_3.4.32");
217 known_versions.push_back("GLIBCXX_3.4.33");
218 known_versions.push_back("GLIBCXX_3.4.34");
219 known_versions.push_back("GLIBCXX_LDBL_3.4.31");
220 known_versions.push_back("GLIBCXX_IEEE128_3.4.29");
221 known_versions.push_back("GLIBCXX_IEEE128_3.4.30");
222 known_versions.push_back("GLIBCXX_IEEE128_3.4.31");
223 known_versions.push_back("CXXABI_1.3");
224 known_versions.push_back("CXXABI_LDBL_1.3");
225 known_versions.push_back("CXXABI_1.3.1");
226 known_versions.push_back("CXXABI_1.3.2");
227 known_versions.push_back("CXXABI_1.3.3");
228 known_versions.push_back("CXXABI_1.3.4");
229 known_versions.push_back("CXXABI_1.3.5");
230 known_versions.push_back("CXXABI_1.3.6");
231 known_versions.push_back("CXXABI_1.3.7");
232 known_versions.push_back("CXXABI_1.3.8");
233 known_versions.push_back("CXXABI_1.3.9");
234 known_versions.push_back("CXXABI_1.3.10");
235 known_versions.push_back("CXXABI_1.3.11");
236 known_versions.push_back("CXXABI_1.3.12");
237 known_versions.push_back("CXXABI_1.3.13");
238 known_versions.push_back("CXXABI_1.3.14");
239 known_versions.push_back("CXXABI_1.3.15");
240 #ifdef __riscv
241 known_versions.push_back("CXXABI_1.3.16");
242 #endif
243 known_versions.push_back("CXXABI_IEEE128_1.3.13");
244 known_versions.push_back("CXXABI_TM_1");
245 known_versions.push_back("CXXABI_FLOAT128");
246 }
247 compat_list::iterator begin = known_versions.begin();
248 compat_list::iterator end = known_versions.end();
249
250 // Check for compatible version.
251 if (test.version_name.size())
252 {
253 compat_list::iterator it1 = find(begin, end, test.version_name);
254 compat_list::iterator it2 = find(begin, end, test.name);
255 if (it1 != end)
256 test.version_status = symbol::compatible;
257 else
258 test.version_status = symbol::incompatible;
259
260 // Check that added symbols are added in the latest pre-release version.
261 bool latestp = (test.version_name == "GLIBCXX_3.4.34"
262 || test.version_name == "CXXABI_1.3.16"
263 || test.version_name == "CXXABI_FLOAT128"
264 || test.version_name == "CXXABI_TM_1");
265 if (added && !latestp)
266 test.version_status = symbol::incompatible;
267
268 // Check that long double compatibility symbols demangled as
269 // __float128 and regular __float128 symbols are put into some _LDBL_
270 // or _FLOAT128 version name.
271 if (added && test.demangled_name.find("__float128") != std::string::npos
272 && test.demangled_name.find("std::__cxx11::") != 0)
273 {
274 if (test.version_name.find("_LDBL_") == std::string::npos
275 && test.version_name.find("_FLOAT128") == std::string::npos
276 && test.version_name.find("_IEEE128") == std::string::npos)
277 test.version_status = symbol::incompatible;
278 }
279
280 // Check that IEEE128 long double compatibility symbols demangled as
281 // __ieee128 are put into some _LDBL_IEEE version name.
282 // XXX is this right? might not want *everything* for __ieee128 in here.
283 if (added && test.demangled_name.find("__ieee128") != std::string::npos)
284 {
285 if (test.version_name.find("_IEEE128") == std::string::npos)
286 test.version_status = symbol::incompatible;
287 }
288
289 // Check for weak label.
290 if (it1 == end && it2 == end)
291 test.version_status = symbol::incompatible;
292
293 // Check that
294 // GLIBCXX_3.4
295 // GLIBCXX_3.4.5
296 // version as compatible
297 // XXX
298 }
299 else
300 {
301 if (added)
302 {
303 // New version labels are ok. The rest are not.
304 compat_list::iterator it2 = find(begin, end, test.name);
305 if (it2 != end)
306 test.version_status = symbol::compatible;
307 else
308 test.version_status = symbol::incompatible;
309 }
310 }
311 return test.version_status == symbol::compatible;
312 }
313
314 bool
315 check_compatible(symbol& lhs, symbol& rhs, bool verbose)
316 {
317 bool ret = true;
318 const char tab = '\t';
319
320 // Check to see if symbol_objects are compatible.
321 if (lhs.type != rhs.type)
322 {
323 ret = false;
324 if (verbose)
325 cout << tab << "incompatible types" << endl;
326 }
327
328 if (lhs.name != rhs.name)
329 {
330 ret = false;
331 if (verbose)
332 cout << tab << "incompatible names" << endl;
333 }
334
335 if (lhs.size != rhs.size)
336 {
337 ret = false;
338 if (verbose)
339 {
340 cout << tab << "incompatible sizes" << endl;
341 cout << tab << lhs.size << endl;
342 cout << tab << rhs.size << endl;
343 }
344 }
345
346 if (lhs.version_name != rhs.version_name
347 && !check_version(lhs) && !check_version(rhs))
348 {
349 ret = false;
350 if (verbose)
351 {
352 cout << tab << "incompatible versions" << endl;
353 cout << tab << lhs.version_name << endl;
354 cout << tab << rhs.version_name << endl;
355 }
356 }
357
358 if (verbose)
359 cout << endl;
360
361 return ret;
362 }
363
364
365 inline bool
366 has_symbol(const string& name, const symbols& s) throw()
367 { return s.find(name) != s.end(); }
368
369 const symbol&
370 get_symbol(const string& name, const symbols& s)
371 {
372 symbols::const_iterator i = s.find(name);
373 if (i != s.end())
374 {
375 return i->second;
376 }
377 else
378 {
379 ostringstream os;
380 os << "get_symbol failed for symbol " << name;
381 __throw_logic_error(os.str().c_str());
382 }
383 }
384
385 void
386 examine_symbol(const char* name, const char* file)
387 {
388 symbols s = create_symbols(file);
389 const symbol& sym = get_symbol(name, s);
390 sym.print();
391 }
392
393 int
394 compare_symbols(const char* baseline_file, const char* test_file,
395 bool verbose)
396 {
397 // Input both lists of symbols into container.
398 symbols baseline = create_symbols(baseline_file);
399 symbols test = create_symbols(test_file);
400
401 // Sanity check results.
402 if (!baseline.size() || !test.size())
403 {
404 cerr << "Problems parsing the list of exported symbols." << endl;
405 exit(2);
406 }
407
408 // Check to see if any long double compatibility symbols are produced.
409 bool ld_version_found(false);
410 symbols::iterator li(test.begin());
411 while (!ld_version_found && li != test.end())
412 {
413 if (li->second.version_name.find("_LDBL_") != std::string::npos)
414 ld_version_found = true;
415 ++li;
416 }
417
418 // Similarly for IEEE128 symbols.
419 bool ieee_version_found(false);
420 for (li = test.begin(); li != test.end(); ++li)
421 if (li->second.version_name.find("_IEEE128_") != std::string::npos)
422 {
423 ieee_version_found = true;
424 break;
425 }
426
427 // Sort out names.
428 // Assuming all baseline names and test names are both unique w/ no
429 // duplicates.
430 //
431 // The names added to missing_names are baseline names not found in
432 // test names
433 // -> symbols that have been deleted.
434 //
435 // The names added to added_names are test names not in
436 // baseline names
437 // -> symbols that have been added.
438 typedef std::vector<std::string> symbol_names;
439 symbol_names shared_names;
440 symbol_names missing_names;
441 symbol_names added_names;
442 for (li = test.begin(); li != test.end(); ++li)
443 added_names.push_back(li->first);
444
445 for (symbols::iterator i = baseline.begin(); i != baseline.end(); ++i)
446 {
447 string name(i->first);
448 symbol_names::iterator end = added_names.end();
449 symbol_names::iterator it = find(added_names.begin(), end, name);
450 if (it != end)
451 {
452 // Found.
453 shared_names.push_back(name);
454 added_names.erase(it);
455 }
456 else
457 {
458 // Iff no test long double compatibility symbols at all and the symbol
459 // missing is a baseline long double compatibility symbol, skip.
460 string version_name(i->second.version_name.size()
461 ? i->second.version_name : i->first);
462 bool base_ld(version_name.find("_LDBL_") != std::string::npos);
463 bool base_ieee(version_name.find("_IEEE128_") != std::string::npos);
464 if ((!base_ld || (base_ld && ld_version_found))
465 && (!base_ieee || (base_ieee && ieee_version_found)))
466 missing_names.push_back(name);
467 }
468 }
469
470 // Fill out list of incompatible symbols.
471 typedef pair<symbol, symbol> symbol_pair;
472 vector<symbol_pair> incompatible;
473
474 // Fill out list of undesignated symbols.
475 vector<symbol> undesignated;
476
477 // Check missing names for compatibility.
478 for (size_t j = 0; j < missing_names.size(); ++j)
479 {
480 symbol& sbase = baseline[missing_names[j]];
481 sbase.status = symbol::subtracted;
482 incompatible.push_back(symbol_pair(sbase, sbase));
483 }
484
485 // Check shared names for compatibility.
486 const symbol_names::size_type shared_size = shared_names.size();
487 for (size_t k = 0; k < shared_size; ++k)
488 {
489 symbol& sbase = baseline[shared_names[k]];
490 symbol& stest = test[shared_names[k]];
491 stest.status = symbol::existing;
492 if (!check_compatible(sbase, stest))
493 incompatible.push_back(symbol_pair(sbase, stest));
494 }
495
496 // Check added names for compatibility.
497 const symbol_names::size_type added_size = added_names.size();
498 for (size_t l = 0; l < added_size; ++l)
499 {
500 symbol& stest = test[added_names[l]];
501
502 // Mark TLS as undesignated, remove from added.
503 if (stest.type == symbol::tls)
504 {
505 stest.status = symbol::undesignated;
506 if (!check_version(stest, false))
507 incompatible.push_back(symbol_pair(stest, stest));
508 else
509 undesignated.push_back(stest);
510 }
511 // See PR libstdc++/103407 - abi_check FAILs on Solaris
512 else if (stest.type == symbol::function
513 && stest.name.compare(0, 23, "_ZSt10from_charsPKcS0_R") == 0
514 && stest.name.find_first_of("def", 23) == 23
515 && (stest.version_name == "GLIBCXX_3.4.29"
516 || stest.version_name == "GLIBCXX_3.4.30"))
517 {
518 stest.status = symbol::undesignated;
519 if (!check_version(stest, false))
520 incompatible.push_back(symbol_pair(stest, stest));
521 else
522 undesignated.push_back(stest);
523 }
524 else
525 {
526 stest.status = symbol::added;
527 if (!check_version(stest, true))
528 incompatible.push_back(symbol_pair(stest, stest));
529 }
530 }
531
532 // Normalize added names and undesignated names.
533 const size_t undesignated_size = undesignated.size();
534 for (size_t l = 0; l < undesignated_size; ++l)
535 {
536 symbol& sundes = undesignated[l];
537 symbol_names::iterator end = added_names.end();
538 symbol_names::iterator it = find(added_names.begin(), end, sundes.name);
539 if (it != end)
540 {
541 // Found.
542 added_names.erase(it);
543 }
544 else
545 __throw_runtime_error(sundes.name.c_str());
546 }
547
548
549 // Report results.
550 if (verbose && added_names.size())
551 {
552 cout << endl << added_names.size() << " added symbols " << endl;
553 for (size_t j = 0; j < added_names.size() ; ++j)
554 {
555 cout << j << endl;
556 test[added_names[j]].print();
557 }
558 }
559
560 if (verbose && missing_names.size())
561 {
562 cout << endl << missing_names.size() << " missing symbols " << endl;
563 for (size_t j = 0; j < missing_names.size() ; ++j)
564 {
565 cout << j << endl;
566 baseline[missing_names[j]].print();
567 }
568 }
569
570 if (verbose && undesignated.size())
571 {
572 cout << endl << undesignated.size() << " undesignated symbols " << endl;
573 for (size_t j = 0; j < undesignated.size() ; ++j)
574 {
575 // First, print index.
576 cout << j << endl;
577
578 // Second, report name.
579 symbol& s = undesignated[j];
580 s.print();
581 }
582 }
583
584 if (verbose && incompatible.size())
585 {
586 cout << endl << incompatible.size() << " incompatible symbols " << endl;
587 for (size_t j = 0; j < incompatible.size() ; ++j)
588 {
589 // First, print index.
590 cout << j << endl;
591
592 // Second, report name.
593 symbol& sbase = incompatible[j].first;
594 symbol& stest = incompatible[j].second;
595 stest.print();
596
597 // Third, report reason or reasons incompatible.
598 check_compatible(sbase, stest, true);
599 }
600 }
601
602 cout << "\n\t\t==== libstdc++-v3 check-abi Summary ====" << endl;
603 cout << endl;
604 cout << "# of added symbols:\t\t " << added_names.size() << endl;
605 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
606 cout << "# of undesignated symbols:\t " << undesignated.size() << endl;
607 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
608 cout << endl;
609 cout << "using: " << baseline_file << endl;
610
611 return !(missing_names.size() || incompatible.size());
612 }
613
614
615 symbols
616 create_symbols(const char* file)
617 {
618 symbols s;
619 ifstream ifs(file);
620 if (ifs.is_open())
621 {
622 // Organize file data into an associated container (symbols) of symbol
623 // objects mapped to mangled names without versioning
624 // information.
625 const string empty;
626 string line = empty;
627 while (getline(ifs, line).good())
628 {
629 symbol tmp;
630 tmp.init(line);
631 s[tmp.name] = tmp;
632 line = empty;
633 }
634 }
635 else
636 {
637 ostringstream os;
638 os << "create_symbols failed for file " << file;
639 __throw_runtime_error(os.str().c_str());
640 }
641 return s;
642 }
643
644
645 std::string
646 demangle(const std::string& mangled)
647 {
648 std::string name;
649 if (mangled[0] != '_' || mangled[1] != 'Z')
650 {
651 // This is not a mangled symbol, thus has "C" linkage.
652 name = mangled;
653 }
654 else
655 {
656 // Use __cxa_demangle to demangle.
657 int status = 0;
658 char* ptr = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
659 if (ptr)
660 {
661 name = ptr;
662 free(ptr);
663 }
664 else
665 {
666 switch (status)
667 {
668 case 0:
669 name = "error code = 0: success";
670 break;
671 case -1:
672 name = "error code = -1: memory allocation failure";
673 break;
674 case -2:
675 name = "error code = -2: invalid mangled name";
676 break;
677 case -3:
678 name = "error code = -3: invalid arguments";
679 break;
680 default:
681 name = "error code unknown - who knows what happened";
682 }
683 }
684 }
685 return name;
686 }