• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019, 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 "parser.h"
18 
19 #include <queue>
20 
21 #include "aidl_language_y.h"
22 #include "logging.h"
23 
24 void yylex_init(void**);
25 void yylex_destroy(void*);
26 void yyset_in(FILE* f, void*);
27 int yyparse(Parser*);
28 YY_BUFFER_STATE yy_scan_buffer(char*, size_t, void*);
29 void yy_delete_buffer(YY_BUFFER_STATE, void*);
30 
31 // For each union, generate nested "Tag" enum type so that "Tag" can be used as a valid type.
32 //    union Foo { int a; int b; } => union Foo { ... enum Tag { a, b }}
33 struct UnionTagGenerater : AidlVisitor {
VisitUnionTagGenerater34   void Visit(const AidlUnionDecl& decl) override {
35     std::vector<std::unique_ptr<AidlEnumerator>> enumerators;
36     for (const auto& field : decl.GetFields()) {
37       enumerators.push_back(std::make_unique<AidlEnumerator>(AIDL_LOCATION_HERE, field->GetName(),
38                                                              nullptr, field->GetComments()));
39     }
40     auto tag_enum = std::make_unique<AidlEnumDeclaration>(AIDL_LOCATION_HERE, "Tag", &enumerators,
41                                                           decl.GetPackage(), Comments{});
42     // Tag for @FixedSize union is limited to "byte" type so that it can be passed via FMQ with
43     // with lower overhead.
44     std::shared_ptr<AidlConstantValue> backing_type{
45         AidlConstantValue::String(AIDL_LOCATION_HERE, decl.IsFixedSize() ? "\"byte\"" : "\"int\"")};
46     std::vector<std::unique_ptr<AidlAnnotation>> annotations;
47     annotations.push_back(
48         AidlAnnotation::Parse(AIDL_LOCATION_HERE, "Backing", {{"type", backing_type}}, Comments{}));
49     tag_enum->Annotate(std::move(annotations));
50     const_cast<AidlUnionDecl&>(decl).AddType(std::move(tag_enum));
51   }
52 };
53 
Parse(const std::string & filename,const android::aidl::IoDelegate & io_delegate,AidlTypenames & typenames,bool is_preprocessed)54 const AidlDocument* Parser::Parse(const std::string& filename,
55                                   const android::aidl::IoDelegate& io_delegate,
56                                   AidlTypenames& typenames, bool is_preprocessed) {
57   auto clean_path = android::aidl::IoDelegate::CleanPath(filename);
58   // reuse pre-parsed document from typenames
59   for (auto& doc : typenames.AllDocuments()) {
60     if (doc->GetLocation().GetFile() == clean_path) {
61       return doc.get();
62     }
63   }
64   // Make sure we can read the file first, before trashing previous state.
65   unique_ptr<string> raw_buffer = io_delegate.GetFileContents(clean_path);
66   if (raw_buffer == nullptr) {
67     AIDL_ERROR(clean_path) << "Error while opening file for parsing";
68     return nullptr;
69   }
70 
71   // We're going to scan this buffer in place, and yacc demands we put two
72   // nulls at the end.
73   raw_buffer->append(2u, '\0');
74 
75   Parser parser(clean_path, *raw_buffer, is_preprocessed);
76 
77   if (yy::parser(&parser).parse() != 0 || parser.HasError()) {
78     return nullptr;
79   }
80 
81   // Preprocess parsed document before adding to typenames.
82   UnionTagGenerater v;
83   VisitTopDown(v, *parser.document_);
84 
85   // transfer ownership to AidlTypenames and return the raw pointer
86   const AidlDocument* result = parser.document_.get();
87   if (!typenames.AddDocument(std::move(parser.document_))) {
88     return nullptr;
89   }
90   return result;
91 }
92 
SetTypeParameters(AidlTypeSpecifier * type,std::vector<std::unique_ptr<AidlTypeSpecifier>> * type_args)93 void Parser::SetTypeParameters(AidlTypeSpecifier* type,
94                                std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args) {
95   if (type->IsArray()) {
96     AIDL_ERROR(type) << "Must specify type parameters (<>) before array ([]).";
97     AddError();
98   }
99   if (!type->SetTypeParameters(type_args)) {
100     AIDL_ERROR(type) << "Can only specify one set of type parameters.";
101     AddError();
102     delete type_args;
103   }
104 }
105 
CheckValidTypeName(const AidlToken & token,const AidlLocation & loc)106 void Parser::CheckValidTypeName(const AidlToken& token, const AidlLocation& loc) {
107   if (!is_preprocessed_ && token.GetText().find('.') != std::string::npos) {
108     AIDL_ERROR(loc) << "Type name can't be qualified. Use `package`.";
109     AddError();
110   }
111 }
112 
SetPackage(const std::string & package)113 void Parser::SetPackage(const std::string& package) {
114   if (is_preprocessed_) {
115     AIDL_ERROR(package) << "Preprocessed file can't declare package.";
116     AddError();
117   }
118   package_ = package;
119 }
120 
CheckNoRecursiveDefinition(const AidlNode & node)121 bool CheckNoRecursiveDefinition(const AidlNode& node) {
122   struct Visitor : AidlVisitor {
123     enum {
124       NOT_STARTED = 0,
125       STARTED = 1,
126       FINISHED = 2,
127     };
128     std::map<const AidlParcelable*, int> visited;
129     std::vector<std::string> path;
130     bool found_cycle = false;
131 
132     void Visit(const AidlStructuredParcelable& t) override { FindCycle(&t); }
133     void Visit(const AidlUnionDecl& t) override { FindCycle(&t); }
134     void Visit(const AidlParcelable& t) override { FindCycle(&t); }
135 
136     bool FindCycle(const AidlParcelable* p) {
137       // no need to search further
138       if (found_cycle) {
139         return true;
140       }
141       // we just found a cycle
142       if (visited[p] == STARTED) {
143         path.push_back(p->GetName());
144         AIDL_ERROR(p) << p->GetName()
145                       << " is a recursive parcelable: " << android::base::Join(path, "->");
146         return (found_cycle = true);
147       }
148       // we arrived here with a different route.
149       if (visited[p] == FINISHED) {
150         return false;
151       }
152       // start DFS
153       visited[p] = STARTED;
154       path.push_back(p->GetName());
155       for (const auto& f : p->GetFields()) {
156         const auto& ref = f->GetType();
157         if (!ref.IsArray() && !ref.IsHeapNullable()) {
158           const auto& type = ref.GetDefinedType();
159           if (type && type->AsParcelable()) {
160             if (FindCycle(type->AsParcelable())) {
161               return true;
162             }
163           }
164         }
165       }
166       path.pop_back();
167       visited[p] = FINISHED;
168       return false;
169     }
170   } v;
171   VisitTopDown(v, node);
172   return !v.found_cycle;
173 }
174 
175 // Each Visit*() method can use Scope() to get its scope(AidlDefinedType)
176 class ScopedVisitor : public AidlVisitor {
177  protected:
Scope() const178   const AidlDefinedType* Scope() const {
179     AIDL_FATAL_IF(scope_.empty(), AIDL_LOCATION_HERE) << "Scope is empty";
180     return scope_.back();
181   }
PushScope(const AidlDefinedType * scope)182   void PushScope(const AidlDefinedType* scope) { scope_.push_back(scope); }
PopScope()183   void PopScope() { scope_.pop_back(); }
184   // Keep user defined type as a defining scope
VisitScopedTopDown(const AidlNode & node)185   void VisitScopedTopDown(const AidlNode& node) {
186     std::function<void(const AidlNode&)> top_down = [&](const AidlNode& a) {
187       a.DispatchVisit(*this);
188       auto defined_type = AidlCast<AidlDefinedType>(a);
189       if (defined_type) PushScope(defined_type);
190       a.TraverseChildren(top_down);
191       if (defined_type) PopScope();
192     };
193     top_down(node);
194   }
195 
196  private:
197   std::vector<const AidlDefinedType*> scope_ = {};
198 };
199 
200 class TypeReferenceResolver : public ScopedVisitor {
201  public:
TypeReferenceResolver(TypeResolver & resolver)202   TypeReferenceResolver(TypeResolver& resolver) : resolver_(resolver) {}
203 
Visit(const AidlTypeSpecifier & t)204   void Visit(const AidlTypeSpecifier& t) override {
205     // We're visiting the same node again. This can happen when two constant references
206     // point to an ancestor of this node.
207     if (t.IsResolved()) {
208       return;
209     }
210     AidlTypeSpecifier& type = const_cast<AidlTypeSpecifier&>(t);
211     if (!resolver_(Scope(), &type)) {
212       AIDL_ERROR(type) << "Failed to resolve '" << type.GetUnresolvedName() << "'";
213       success_ = false;
214       return;
215     }
216 
217     // In case a new document is imported for the type reference, enqueue it for type resolution.
218     auto resolved = t.GetDefinedType();
219     if (resolved) {
220       queue_.push(&resolved->GetDocument());
221     }
222   }
223 
Resolve(const AidlDocument & document)224   bool Resolve(const AidlDocument& document) {
225     queue_.push(&document);
226     while (!queue_.empty()) {
227       auto doc = queue_.front();
228       queue_.pop();
229       // Skip the doc if it's visited already.
230       if (!visited_.insert(doc).second) {
231         continue;
232       }
233       VisitScopedTopDown(*doc);
234     }
235     return success_;
236   }
237 
238  private:
239   TypeResolver& resolver_;
240   bool success_ = true;
241   std::queue<const AidlDocument*> queue_ = {};
242   std::set<const AidlDocument*> visited_ = {};
243 };
244 
245 class ConstantReferenceResolver : public ScopedVisitor {
246  public:
247   ConstantReferenceResolver() = default;
248 
Visit(const AidlConstantReference & v)249   void Visit(const AidlConstantReference& v) override {
250     if (IsCircularReference(&v)) {
251       success_ = false;
252       return;
253     }
254 
255     const AidlConstantValue* resolved = v.Resolve(Scope());
256     if (!resolved) {
257       AIDL_ERROR(v) << "Unknown reference '" << v.Literal() << "'";
258       success_ = false;
259       return;
260     }
261 
262     // On error, skip recursive visiting to avoid redundant messages
263     if (!success_) {
264       return;
265     }
266     // resolve recursive references
267     PushConstRef(&v);
268     VisitScopedTopDown(*resolved);
269     PopConstRef();
270   }
271 
Resolve(const AidlDocument & document)272   bool Resolve(const AidlDocument& document) {
273     VisitScopedTopDown(document);
274     return success_;
275   }
276 
277  private:
PushConstRef(const AidlConstantReference * ref)278   void PushConstRef(const AidlConstantReference* ref) {
279     stack_.push_back(ref);
280     if (ref->GetRefType()) {
281       PushScope(ref->GetRefType()->GetDefinedType());
282     }
283   }
284 
PopConstRef()285   void PopConstRef() {
286     if (stack_.back()->GetRefType()) {
287       PopScope();
288     }
289     stack_.pop_back();
290   }
291 
IsCircularReference(const AidlConstantReference * ref)292   bool IsCircularReference(const AidlConstantReference* ref) {
293     auto it =
294         std::find_if(stack_.begin(), stack_.end(), [&](const auto& elem) { return elem == ref; });
295     if (it == stack_.end()) {
296       return false;
297     }
298     std::vector<std::string> path;
299     while (it != stack_.end()) {
300       path.push_back((*it)->Literal());
301       ++it;
302     }
303     path.push_back(ref->Literal());
304     AIDL_ERROR(ref) << "Found a circular reference: " << android::base::Join(path, " -> ");
305     return true;
306   }
307 
308   bool success_ = true;
309   std::vector<const AidlConstantReference*> stack_ = {};
310 };
311 
312 // Resolve references(types/constants) in the "main" document.
ResolveReferences(const AidlDocument & document,TypeResolver & type_resolver)313 bool ResolveReferences(const AidlDocument& document, TypeResolver& type_resolver) {
314   // Types are resolved first before resolving constant references so that every referenced document
315   // gets imported.
316   if (!TypeReferenceResolver(type_resolver).Resolve(document)) {
317     return false;
318   }
319   if (!ConstantReferenceResolver().Resolve(document)) {
320     return false;
321   }
322   if (!CheckNoRecursiveDefinition(document)) {
323     return false;
324   }
325   return true;
326 }
327 
Parser(const std::string & filename,std::string & raw_buffer,bool is_preprocessed)328 Parser::Parser(const std::string& filename, std::string& raw_buffer, bool is_preprocessed)
329     : filename_(filename), is_preprocessed_(is_preprocessed) {
330   yylex_init(&scanner_);
331   buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
332 }
333 
~Parser()334 Parser::~Parser() {
335   yy_delete_buffer(buffer_, scanner_);
336   yylex_destroy(scanner_);
337 }
338 
MakeDocument(const AidlLocation & location,const Comments & comments,std::vector<std::string> imports,std::vector<std::unique_ptr<AidlDefinedType>> defined_types)339 void Parser::MakeDocument(const AidlLocation& location, const Comments& comments,
340                           std::vector<std::string> imports,
341                           std::vector<std::unique_ptr<AidlDefinedType>> defined_types) {
342   AIDL_FATAL_IF(document_.get(), location);
343   document_ = std::make_unique<AidlDocument>(location, comments, std::move(imports),
344                                              std::move(defined_types), is_preprocessed_);
345 }
346