1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "DeclarationDatabase.h"
18 
19 #include <err.h>
20 
21 #include <iostream>
22 #include <map>
23 #include <mutex>
24 #include <set>
25 #include <sstream>
26 #include <string>
27 #include <utility>
28 
29 #include <clang/AST/AST.h>
30 #include <clang/AST/Attr.h>
31 #include <clang/AST/Mangle.h>
32 #include <clang/AST/RecursiveASTVisitor.h>
33 #include <clang/Frontend/ASTUnit.h>
34 #include <llvm/Support/raw_ostream.h>
35 
36 using namespace clang;
37 
shouldMangle(MangleContext * mangler,NamedDecl * decl)38 static bool shouldMangle(MangleContext* mangler, NamedDecl* decl) {
39   // Passing a decl with static linkage to the mangler gives incorrect results.
40   // Check some things ourselves before handing it off to the mangler.
41   if (auto FD = dyn_cast<FunctionDecl>(decl)) {
42     if (FD->isExternC()) {
43       return false;
44     }
45 
46     if (FD->isInExternCContext()) {
47       return false;
48     }
49   }
50 
51   return mangler->shouldMangleDeclName(decl);
52 }
53 
54 class Visitor : public RecursiveASTVisitor<Visitor> {
55   HeaderDatabase& database;
56   CompilationType type;
57   SourceManager& src_manager;
58   std::unique_ptr<MangleContext> mangler;
59 
60  public:
Visitor(HeaderDatabase & database,CompilationType type,ASTContext & ctx)61   Visitor(HeaderDatabase& database, CompilationType type, ASTContext& ctx)
62       : database(database), type(type), src_manager(ctx.getSourceManager()) {
63     mangler.reset(ItaniumMangleContext::create(ctx, ctx.getDiagnostics()));
64   }
65 
getDeclName(NamedDecl * decl)66   std::string getDeclName(NamedDecl* decl) {
67     if (auto var_decl = dyn_cast<VarDecl>(decl)) {
68       if (!var_decl->isFileVarDecl()) {
69         return "<local var>";
70       }
71     }
72 
73     // <math.h> maps fool onto foo on 32-bit, since long double is the same as double.
74     if (auto asm_attr = decl->getAttr<AsmLabelAttr>()) {
75       return asm_attr->getLabel().str();
76     }
77 
78     // The decl might not have a name (e.g. bitfields).
79     if (auto identifier = decl->getIdentifier()) {
80       if (shouldMangle(mangler.get(), decl)) {
81         std::string mangled;
82         llvm::raw_string_ostream ss(mangled);
83         mangler->mangleName(decl, ss);
84         return mangled;
85       }
86 
87       return identifier->getName().str();
88     }
89 
90     return "<unnamed>";
91   }
92 
VisitDeclaratorDecl(DeclaratorDecl * decl,SourceRange range)93   bool VisitDeclaratorDecl(DeclaratorDecl* decl, SourceRange range) {
94     // Skip declarations inside of functions (function arguments, variable declarations inside of
95     // inline functions, etc).
96     if (decl->getParentFunctionOrMethod()) {
97       return true;
98     }
99 
100     auto named_decl = dyn_cast<NamedDecl>(decl);
101     if (!named_decl) {
102       return true;
103     }
104 
105     std::string declaration_name = getDeclName(named_decl);
106     bool is_extern = named_decl->getFormalLinkage() == ExternalLinkage;
107     bool is_definition = false;
108     bool no_guard = false;
109     bool fortify_inline = false;
110 
111     if (auto function_decl = dyn_cast<FunctionDecl>(decl)) {
112       is_definition = function_decl->isThisDeclarationADefinition();
113     } else if (auto var_decl = dyn_cast<VarDecl>(decl)) {
114       if (!var_decl->isFileVarDecl()) {
115         return true;
116       }
117 
118       switch (var_decl->isThisDeclarationADefinition()) {
119         case VarDecl::DeclarationOnly:
120           is_definition = false;
121           break;
122 
123         case VarDecl::Definition:
124           is_definition = true;
125           break;
126 
127         case VarDecl::TentativeDefinition:
128           // Forbid tentative definitions in headers.
129           fprintf(stderr, "ERROR: declaration '%s' is a tentative definition\n",
130                   declaration_name.c_str());
131           decl->dump();
132           abort();
133       }
134     } else {
135       // We only care about function and variable declarations.
136       return true;
137     }
138 
139     if (decl->hasAttr<UnavailableAttr>()) {
140       // Skip declarations that exist only for compile-time diagnostics.
141       return true;
142     }
143 
144     DeclarationAvailability availability;
145 
146     // Find and parse __ANDROID_AVAILABILITY_DUMP__ annotations.
147     for (const AnnotateAttr* attr : decl->specific_attrs<AnnotateAttr>()) {
148       llvm::StringRef annotation = attr->getAnnotation();
149       if (annotation == "versioner_no_guard") {
150         no_guard = true;
151       } else if (annotation == "versioner_fortify_inline") {
152         fortify_inline = true;
153       } else {
154         llvm::SmallVector<llvm::StringRef, 2> fragments;
155         annotation.split(fragments, "=");
156         if (fragments.size() != 2) {
157           continue;
158         }
159 
160         auto& global_availability = availability.global_availability;
161         auto& arch_availability = availability.arch_availability;
162         std::map<std::string, std::vector<int*>> prefix_map = {
163           { "introduced_in", { &global_availability.introduced } },
164           { "deprecated_in", { &global_availability.deprecated } },
165           { "obsoleted_in", { &global_availability.obsoleted } },
166           { "introduced_in_arm", { &arch_availability[Arch::arm].introduced } },
167           { "introduced_in_x86", { &arch_availability[Arch::x86].introduced } },
168           { "introduced_in_32",
169             { &arch_availability[Arch::arm].introduced,
170               &arch_availability[Arch::x86].introduced } },
171           { "introduced_in_64",
172             { &arch_availability[Arch::arm64].introduced,
173               &arch_availability[Arch::x86_64].introduced } },
174         };
175 
176         if (auto it = prefix_map.find(fragments[0].str()); it != prefix_map.end()) {
177           int value;
178           if (fragments[1].getAsInteger(10, value)) {
179             errx(1, "invalid __ANDROID_AVAILABILITY_DUMP__ annotation: '%s'",
180                  annotation.str().c_str());
181           }
182 
183           for (int* ptr : it->second) {
184             *ptr = value;
185           }
186         }
187       }
188     }
189 
190     auto symbol_it = database.symbols.find(declaration_name);
191     if (symbol_it == database.symbols.end()) {
192       Symbol symbol = {.name = declaration_name };
193       bool unused;
194       std::tie(symbol_it, unused) = database.symbols.insert({declaration_name, symbol});
195     }
196 
197     auto expansion_range = src_manager.getExpansionRange(range);
198     auto filename = src_manager.getFilename(expansion_range.getBegin());
199     if (filename != src_manager.getFilename(expansion_range.getEnd())) {
200       errx(1, "expansion range filenames don't match");
201     }
202 
203     Location location = {
204       .filename = filename.str(),
205       .start = {
206         .line = src_manager.getExpansionLineNumber(expansion_range.getBegin()),
207         .column = src_manager.getExpansionColumnNumber(expansion_range.getBegin()),
208       },
209       .end = {
210         .line = src_manager.getExpansionLineNumber(expansion_range.getEnd()),
211         .column = src_manager.getExpansionColumnNumber(expansion_range.getEnd()),
212       }
213     };
214 
215     // Find or insert an entry for the declaration.
216     if (auto declaration_it = symbol_it->second.declarations.find(location);
217         declaration_it != symbol_it->second.declarations.end()) {
218       if (declaration_it->second.is_extern != is_extern ||
219           declaration_it->second.is_definition != is_definition ||
220           declaration_it->second.no_guard != no_guard ||
221           declaration_it->second.fortify_inline != fortify_inline) {
222         errx(1, "varying declaration of '%s' at %s:%u:%u", declaration_name.c_str(),
223              location.filename.c_str(), location.start.line, location.start.column);
224       }
225       declaration_it->second.availability.insert(std::make_pair(type, availability));
226     } else {
227       Declaration declaration;
228       declaration.name = declaration_name;
229       declaration.location = location;
230       declaration.is_extern = is_extern;
231       declaration.is_definition = is_definition;
232       declaration.no_guard = no_guard;
233       declaration.fortify_inline = fortify_inline;
234       declaration.availability.insert(std::make_pair(type, availability));
235       symbol_it->second.declarations.insert(std::make_pair(location, declaration));
236     }
237 
238     return true;
239   }
240 
VisitDeclaratorDecl(DeclaratorDecl * decl)241   bool VisitDeclaratorDecl(DeclaratorDecl* decl) {
242     return VisitDeclaratorDecl(decl, decl->getSourceRange());
243   }
244 
TraverseLinkageSpecDecl(LinkageSpecDecl * decl)245   bool TraverseLinkageSpecDecl(LinkageSpecDecl* decl) {
246     // Make sure that we correctly calculate the SourceRange of a declaration that has a non-braced
247     // extern "C"/"C++".
248     if (!decl->hasBraces()) {
249       DeclaratorDecl* child = nullptr;
250       for (auto child_decl : decl->decls()) {
251         if (child != nullptr) {
252           errx(1, "LinkageSpecDecl has multiple children");
253         }
254 
255         if (DeclaratorDecl* declarator_decl = dyn_cast<DeclaratorDecl>(child_decl)) {
256           child = declarator_decl;
257         } else {
258           errx(1, "child of LinkageSpecDecl is not a DeclaratorDecl");
259         }
260       }
261 
262       return VisitDeclaratorDecl(child, decl->getSourceRange());
263     }
264 
265     for (auto child : decl->decls()) {
266       if (!TraverseDecl(child)) {
267         return false;
268       }
269     }
270     return true;
271   }
272 };
273 
merge(const DeclarationAvailability & other)274 bool DeclarationAvailability::merge(const DeclarationAvailability& other) {
275 #define check_avail(expr) error |= (!this->expr.empty() && this->expr != other.expr);
276   bool error = false;
277 
278   if (!other.global_availability.empty()) {
279     check_avail(global_availability);
280     this->global_availability = other.global_availability;
281   }
282 
283   for (Arch arch : supported_archs) {
284     if (!other.arch_availability[arch].empty()) {
285       check_avail(arch_availability[arch]);
286       this->arch_availability[arch] = other.arch_availability[arch];
287     }
288   }
289 #undef check_avail
290 
291   return !error;
292 }
293 
calculateAvailability(DeclarationAvailability * output) const294 bool Declaration::calculateAvailability(DeclarationAvailability* output) const {
295   DeclarationAvailability avail;
296   for (const auto& it : this->availability) {
297     if (!avail.merge(it.second)) {
298       return false;
299     }
300   }
301   *output = avail;
302   return true;
303 }
304 
calculateAvailability(DeclarationAvailability * output) const305 bool Symbol::calculateAvailability(DeclarationAvailability* output) const {
306   DeclarationAvailability avail;
307   for (const auto& it : this->declarations) {
308     // Don't merge availability for inline functions (because they shouldn't have any).
309     if (it.second.is_definition) {
310       continue;
311     }
312 
313     DeclarationAvailability decl_availability;
314     if (!it.second.calculateAvailability(&decl_availability)) {
315       return false;
316       abort();
317     }
318 
319     if (!avail.merge(decl_availability)) {
320       return false;
321     }
322   }
323   *output = avail;
324   return true;
325 }
326 
hasDeclaration(const CompilationType & type) const327 bool Symbol::hasDeclaration(const CompilationType& type) const {
328   for (const auto& decl_it : this->declarations) {
329     for (const auto& compilation_it : decl_it.second.availability) {
330       if (compilation_it.first == type) {
331         return true;
332       }
333     }
334   }
335   return false;
336 }
337 
parseAST(CompilationType type,ASTContext & ctx)338 void HeaderDatabase::parseAST(CompilationType type, ASTContext& ctx) {
339   std::unique_lock<std::mutex> lock(this->mutex);
340   Visitor visitor(*this, type, ctx);
341   visitor.TraverseDecl(ctx.getTranslationUnitDecl());
342 }
343 
to_string(const AvailabilityValues & av)344 std::string to_string(const AvailabilityValues& av) {
345   std::stringstream ss;
346 
347   if (av.introduced != 0) {
348     ss << "introduced = " << av.introduced << ", ";
349   }
350 
351   if (av.deprecated != 0) {
352     ss << "deprecated = " << av.deprecated << ", ";
353   }
354 
355   if (av.obsoleted != 0) {
356     ss << "obsoleted = " << av.obsoleted << ", ";
357   }
358 
359   std::string result = ss.str();
360   if (!result.empty()) {
361     result = result.substr(0, result.length() - 2);
362   }
363   return result;
364 }
365 
to_string(const DeclarationType & type)366 std::string to_string(const DeclarationType& type) {
367   switch (type) {
368     case DeclarationType::function:
369       return "function";
370     case DeclarationType::variable:
371       return "variable";
372     case DeclarationType::inconsistent:
373       return "inconsistent";
374   }
375   abort();
376 }
377 
to_string(const DeclarationAvailability & decl_av)378 std::string to_string(const DeclarationAvailability& decl_av) {
379   std::stringstream ss;
380   if (!decl_av.global_availability.empty()) {
381     ss << to_string(decl_av.global_availability) << ", ";
382   }
383 
384   for (const auto& it : decl_av.arch_availability) {
385     if (!it.second.empty()) {
386       ss << to_string(it.first) << ": " << to_string(it.second) << ", ";
387     }
388   }
389 
390   std::string result = ss.str();
391   if (result.size() == 0) {
392     return "no availability";
393   }
394 
395   return result.substr(0, result.length() - 2);
396 }
397 
to_string(const Location & loc)398 std::string to_string(const Location& loc) {
399   std::stringstream ss;
400   ss << loc.filename << ":" << loc.start.line << ":" << loc.start.column;
401   return ss.str();
402 }
403