1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2014-2020 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// Given a program P that links against a library L of version V
11 /// denoted L(V), this program checks if P is still ABI compatible
12 /// with a subsequent version of L denoted L(V+N), N being a positive
13 /// integer.  The result of the check is a report that is compared
14 /// against a reference report.  This program actually performs these
15 /// checks for a variety of tuple {P, L(V), L(V+N)}
16 ///
17 /// The set of input files and reference reports to consider should be
18 /// present in the source distribution.
19 
20 #include <cstring>
21 #include <string>
22 #include <fstream>
23 #include <iostream>
24 #include <cstdlib>
25 #include "abg-tools-utils.h"
26 #include "test-utils.h"
27 
28 using std::string;
29 using std::cerr;
30 
31 struct InOutSpec
32 {
33   const char* in_app_path;
34   const char* in_lib1_path;
35   const char* in_lib2_path;
36   const char* suppressions;
37   const char* options;
38   const char* in_report_path;
39   const char* out_report_path;
40 };
41 
42 InOutSpec in_out_specs[] =
43 {
44   {
45     "data/test-abicompat/test0-fn-changed-app",
46     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
47     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
48     "",
49     "--show-base-names --no-show-locs --no-redundant",
50     "data/test-abicompat/test0-fn-changed-report-0.txt",
51     "output/test-abicompat/test0-fn-changed-report-0.txt",
52   },
53   {
54     "data/test-abicompat/test0-fn-changed-app",
55     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
56     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
57     "data/test-abicompat/test0-fn-changed-0.suppr",
58     "--show-base-names --no-show-locs --no-redundant",
59     "data/test-abicompat/test0-fn-changed-report-1.txt",
60     "output/test-abicompat/test0-fn-changed-report-1.txt",
61   },
62   { // Previous test, but emitting loc info.
63     "data/test-abicompat/test0-fn-changed-app",
64     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
65     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
66     "",
67     "--show-base-names --no-redundant",
68     "data/test-abicompat/test0-fn-changed-report-2.txt",
69     "output/test-abicompat/test0-fn-changed-report-2.txt",
70   },
71   {
72     "data/test-abicompat/test0-fn-changed-app",
73     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
74     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
75     "data/test-abicompat/test0-fn-changed-1.suppr",
76     "--show-base-names --no-show-locs --no-redundant",
77     "data/test-abicompat/test0-fn-changed-report-3.txt",
78     "output/test-abicompat/test0-fn-changed-report-3.txt",
79   },
80   {
81     "data/test-abicompat/test1-fn-removed-app",
82     "data/test-abicompat/libtest1-fn-removed-v0.so",
83     "data/test-abicompat/libtest1-fn-removed-v1.so",
84     "",
85     "--show-base-names --no-show-locs --no-redundant",
86     "data/test-abicompat/test1-fn-removed-report-0.txt",
87     "output/test-abicompat/test1-fn-removed-report-0.txt",
88   },
89   {
90     "data/test-abicompat/test2-var-removed-app",
91     "data/test-abicompat/libtest2-var-removed-v0.so",
92     "data/test-abicompat/libtest2-var-removed-v1.so",
93     "",
94     "--show-base-names --no-show-locs --no-redundant",
95     "data/test-abicompat/test2-var-removed-report-0.txt",
96     "output/test-abicompat/test2-var-removed-report-0.txt",
97   },
98   {
99     "data/test-abicompat/test3-fn-removed-app",
100     "data/test-abicompat/libtest3-fn-removed-v0.so",
101     "data/test-abicompat/libtest3-fn-removed-v1.so",
102     "",
103     "--show-base-names --no-show-locs --no-redundant",
104     "data/test-abicompat/test3-fn-removed-report-0.txt",
105     "output/test-abicompat/test3-fn-removed-report-0.txt",
106   },
107   {
108     "data/test-abicompat/test4-soname-changed-app",
109     "data/test-abicompat/libtest4-soname-changed-v0.so",
110     "data/test-abicompat/libtest4-soname-changed-v1.so",
111     "",
112     "--show-base-names --no-show-locs --no-redundant",
113     "data/test-abicompat/test4-soname-changed-report-0.txt",
114     "output/test-abicompat/test4-soname-changed-report-0.txt",
115   },
116   {
117     "data/test-abicompat/test5-fn-changed-app",
118     "data/test-abicompat/libtest5-fn-changed-libapp-v1.so",
119     "",
120     "",
121     "--show-base-names --no-show-locs --weak-mode",
122     "data/test-abicompat/test5-fn-changed-report-0.txt",
123     "output/test-abicompat/test5-fn-changed-report-0.txt",
124   },
125   { // Previous test, but emitting loc info.
126     "data/test-abicompat/test5-fn-changed-app",
127     "data/test-abicompat/libtest5-fn-changed-libapp-v1.so",
128     "",
129     "",
130     "--show-base-names --weak-mode",
131     "data/test-abicompat/test5-fn-changed-report-1.txt",
132     "output/test-abicompat/test5-fn-changed-report-1.txt",
133   },
134   {
135     "data/test-abicompat/test6-var-changed-app",
136     "data/test-abicompat/libtest6-var-changed-libapp-v1.so",
137     "",
138     "",
139     "--show-base-names --no-show-locs --weak-mode",
140     "data/test-abicompat/test6-var-changed-report-0.txt",
141     "output/test-abicompat/test6-var-changed-report-0.txt",
142   },
143   { // Previous test, but emitting loc info.
144     "data/test-abicompat/test6-var-changed-app",
145     "data/test-abicompat/libtest6-var-changed-libapp-v1.so",
146     "",
147     "",
148     "--show-base-names --weak-mode",
149     "data/test-abicompat/test6-var-changed-report-1.txt",
150     "output/test-abicompat/test6-var-changed-report-1.txt",
151   },
152   {
153     "data/test-abicompat/test7-fn-changed-app",
154     "data/test-abicompat/libtest7-fn-changed-libapp-v0.so",
155     "data/test-abicompat/libtest7-fn-changed-libapp-v1.so",
156     "",
157     "--show-base-names --no-show-locs --no-redundant",
158     "data/test-abicompat/test7-fn-changed-report-0.txt",
159     "output/test-abicompat/test7-fn-changed-report-0.txt",
160   },
161   {
162     "data/test-abicompat/test7-fn-changed-app",
163     "data/test-abicompat/libtest7-fn-changed-libapp-v1.so",
164     "",
165     "",
166     "--show-base-names --no-show-locs --weak-mode",
167     "data/test-abicompat/test7-fn-changed-report-1.txt",
168     "output/test-abicompat/test7-fn-changed-report-1.txt",
169   },
170   { // Previous test, but emitting loc info.
171     "data/test-abicompat/test7-fn-changed-app",
172     "data/test-abicompat/libtest7-fn-changed-libapp-v1.so",
173     "",
174     "",
175     "--show-base-names --weak-mode",
176     "data/test-abicompat/test7-fn-changed-report-2.txt",
177     "output/test-abicompat/test7-fn-changed-report-2.txt",
178   },
179   {
180     "data/test-abicompat/test8-fn-changed-app",
181     "data/test-abicompat/libtest8-fn-changed-libapp-v1.so",
182     "",
183     "",
184     "--show-base-names --weak-mode",
185     "data/test-abicompat/test8-fn-changed-report-0.txt",
186     "output/test-abicompat/test8-fn-changed-report-0.txt",
187   },
188   {
189     "data/test-abicompat/test9-fn-changed-app",
190     "data/test-abicompat/libtest9-fn-changed-v1.so ",
191     "",
192     "",
193     "--show-base-names --weak-mode",
194     "data/test-abicompat/test9-fn-changed-report-0.txt",
195     "output/test-abicompat/test9-fn-changed-report-0.txt",
196   },
197   // This entry must be the last one.
198   {0, 0, 0, 0, 0, 0, 0}
199 };
200 
201 int
main()202 main()
203 {
204   using abigail::tests::get_src_dir;
205   using abigail::tests::get_build_dir;
206   using abigail::tools_utils::ensure_parent_dir_created;
207   using abigail::tools_utils::abidiff_status;
208 
209   bool is_ok = true;
210   string in_app_path, in_lib1_path, in_lib2_path, suppression_path,
211     abicompat_options, ref_report_path, out_report_path, abicompat, cmd;
212 
213   for (InOutSpec* s = in_out_specs; s->in_app_path; ++s)
214     {
215       in_app_path = string(get_src_dir()) + "/tests/" + s->in_app_path;
216       in_lib1_path = string(get_src_dir()) + "/tests/" + s->in_lib1_path;
217       if (s->in_lib2_path && strcmp(s->in_lib2_path, ""))
218 	in_lib2_path = string(get_src_dir()) + "/tests/" + s->in_lib2_path;
219       else
220 	in_lib2_path.clear();
221       if (s->suppressions == 0 || !strcmp(s->suppressions, ""))
222 	suppression_path.clear();
223       else
224 	suppression_path = string(get_src_dir()) + "/tests/" + s->suppressions;
225       abicompat_options = s->options;
226       ref_report_path = string(get_src_dir()) + "/tests/" + s->in_report_path;
227       out_report_path =
228 	string(get_build_dir()) + "/tests/" + s->out_report_path;
229 
230       if (!ensure_parent_dir_created(out_report_path))
231 	{
232 	  cerr << "could not create parent directory for "
233 	       << out_report_path;
234 	  is_ok = false;
235 	  continue;
236 	}
237 
238       abicompat = string(get_build_dir()) + "/tools/abicompat";
239       if (!suppression_path.empty())
240 	abicompat += " --suppressions " + suppression_path;
241       abicompat += " " + abicompat_options;
242 
243       cmd = abicompat + " " + in_app_path + " " + in_lib1_path;
244       if (!in_lib2_path.empty())
245 	cmd += string(" ") + in_lib2_path;
246 
247       cmd += " > " + out_report_path;
248 
249       bool abicompat_ok = true;
250       abidiff_status status = static_cast<abidiff_status>(system(cmd.c_str()));
251       if (abigail::tools_utils::abidiff_status_has_error(status))
252 	abicompat_ok = false;
253 
254       if (abicompat_ok)
255 	{
256 	  cmd = "diff -u " + ref_report_path + " " + out_report_path;
257 	  if (system(cmd.c_str()))
258 	    is_ok = false;
259 	}
260       else
261 	is_ok = false;
262     }
263 
264   return !is_ok;
265 }
266