• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "repr/json/ir_reader.h"
16 
17 #include "repr/ir_dumper.h"
18 #include "repr/ir_reader.h"
19 #include "repr/ir_representation_internal.h"
20 #include "repr/json/api.h"
21 #include "repr/json/converter.h"
22 
23 #include <json/reader.h>
24 #include <json/writer.h>
25 
26 #include <llvm/Support/raw_ostream.h>
27 
28 #include <cstdlib>
29 #include <fstream>
30 #include <sstream>
31 #include <string>
32 
33 
34 namespace header_checker {
35 namespace repr {
36 
37 
38 static const std::map<std::string, AccessSpecifierIR>
39     access_json_to_ir(CreateInverseMap(access_ir_to_json));
40 
41 static const std::map<std::string, RecordTypeIR::RecordKind>
42     record_kind_json_to_ir(CreateInverseMap(record_kind_ir_to_json));
43 
44 static const std::map<std::string, VTableComponentIR::Kind>
45     vtable_component_kind_json_to_ir(
46         CreateInverseMap(vtable_component_kind_ir_to_json));
47 
48 static const std::map<std::string, ElfSymbolIR::ElfSymbolBinding>
49     elf_symbol_binding_json_to_ir(
50         CreateInverseMap(elf_symbol_binding_ir_to_json));
51 
52 
JsonObjectRef(const Json::Value & json_value,bool & ok)53 JsonObjectRef::JsonObjectRef(const Json::Value &json_value, bool &ok)
54     : object_(json_value.isObject() ? json_value : json_empty_object), ok_(ok) {
55   if (!json_value.isObject()) {
56     ok_ = false;
57   }
58 }
59 
60 const Json::Value &
Get(const std::string & key,const Json::Value & default_value,IsExpectedJsonType is_expected_type) const61 JsonObjectRef::Get(const std::string &key, const Json::Value &default_value,
62                    IsExpectedJsonType is_expected_type) const {
63   if (!object_.isMember(key)) {
64     return default_value;
65   }
66   const Json::Value &value = object_[key];
67   if (!(value.*is_expected_type)()) {
68     ok_ = false;
69     return default_value;
70   }
71   return value;
72 }
73 
GetBool(const std::string & key) const74 bool JsonObjectRef::GetBool(const std::string &key) const {
75   return Get(key, json_false, &Json::Value::isBool).asBool();
76 }
77 
GetInt(const std::string & key) const78 int64_t JsonObjectRef::GetInt(const std::string &key) const {
79   return Get(key, json_0, &Json::Value::isInt64).asInt64();
80 }
81 
GetUint(const std::string & key) const82 uint64_t JsonObjectRef::GetUint(const std::string &key) const {
83   return Get(key, json_0, &Json::Value::isUInt64).asUInt64();
84 }
85 
GetIntegralValue(const std::string & key) const86 const Json::Value &JsonObjectRef::GetIntegralValue(
87     const std::string &key) const {
88   return Get(key, json_0, &Json::Value::isIntegral);
89 }
90 
GetString(const std::string & key) const91 std::string JsonObjectRef::GetString(const std::string &key) const {
92   return Get(key, json_empty_string, &Json::Value::isString).asString();
93 }
94 
GetObject(const std::string & key) const95 JsonObjectRef JsonObjectRef::GetObject(const std::string &key) const {
96   return JsonObjectRef(Get(key, json_empty_object, &Json::Value::isObject),
97                        ok_);
98 }
99 
100 JsonArrayRef<JsonObjectRef>
GetObjects(const std::string & key) const101 JsonObjectRef::GetObjects(const std::string &key) const {
102   return JsonArrayRef<JsonObjectRef>(
103       Get(key, json_empty_array, &Json::Value::isArray), ok_);
104 }
105 
106 JsonArrayRef<std::string>
GetStrings(const std::string & key) const107 JsonObjectRef::GetStrings(const std::string &key) const {
108   return JsonArrayRef<std::string>(
109       Get(key, json_empty_array, &Json::Value::isArray), ok_);
110 }
111 
112 template <>
operator *() const113 JsonObjectRef JsonArrayRef<JsonObjectRef>::Iterator::operator*() const {
114   return JsonObjectRef(array_[index_], ok_);
115 }
116 
operator *() const117 template <> std::string JsonArrayRef<std::string>::Iterator::operator*() const {
118   return array_[index_].asString();
119 }
120 
GetAccess(const JsonObjectRef & type_decl)121 static AccessSpecifierIR GetAccess(const JsonObjectRef &type_decl) {
122   std::string access(type_decl.GetString("access"));
123   if (access.empty()) {
124     return default_access_ir;
125   }
126   return FindInMap(access_json_to_ir, access,
127                    "Failed to convert JSON to AccessSpecifierIR");
128 }
129 
130 static RecordTypeIR::RecordKind
GetRecordKind(const JsonObjectRef & record_type)131 GetRecordKind(const JsonObjectRef &record_type) {
132   std::string kind(record_type.GetString("record_kind"));
133   if (kind.empty()) {
134     return default_record_kind_ir;
135   }
136   return FindInMap(record_kind_json_to_ir, kind,
137                    "Failed to convert JSON to RecordKind");
138 }
139 
140 static VTableComponentIR::Kind
GetVTableComponentKind(const JsonObjectRef & vtable_component)141 GetVTableComponentKind(const JsonObjectRef &vtable_component) {
142   std::string kind(vtable_component.GetString("kind"));
143   if (kind.empty()) {
144     return default_vtable_component_kind_ir;
145   }
146   return FindInMap(vtable_component_kind_json_to_ir, kind,
147                    "Failed to convert JSON to VTableComponentIR::Kind");
148 }
149 
150 static ElfSymbolIR::ElfSymbolBinding
GetElfSymbolBinding(const JsonObjectRef & elf_symbol)151 GetElfSymbolBinding(const JsonObjectRef &elf_symbol) {
152   std::string binding(elf_symbol.GetString("binding"));
153   if (binding.empty()) {
154     return default_elf_symbol_binding_ir;
155   }
156   return FindInMap(elf_symbol_binding_json_to_ir, binding,
157                    "Failed to convert JSON to ElfSymbolBinding");
158 }
159 
ReadDumpImpl(const std::string & dump_file)160 bool JsonIRReader::ReadDumpImpl(const std::string &dump_file) {
161   Json::Value tu_json;
162   Json::CharReaderBuilder builder;
163   builder["collectComments"] = false;
164   std::ifstream input(dump_file);
165 
166   std::string errorMessage;
167   if (!Json::parseFromStream(builder, input, &tu_json, &errorMessage)) {
168     llvm::errs() << "Failed to parse JSON: " << errorMessage << "\n";
169     return false;
170   }
171   bool ok = true;
172   JsonObjectRef tu(tu_json, ok);
173   if (!ok) {
174     llvm::errs() << "Translation unit is not an object\n";
175     return false;
176   }
177 
178   ReadFunctions(tu);
179   ReadGlobalVariables(tu);
180   ReadEnumTypes(tu);
181   ReadRecordTypes(tu);
182   ReadFunctionTypes(tu);
183   ReadArrayTypes(tu);
184   ReadPointerTypes(tu);
185   ReadQualifiedTypes(tu);
186   ReadBuiltinTypes(tu);
187   ReadLvalueReferenceTypes(tu);
188   ReadRvalueReferenceTypes(tu);
189   ReadElfFunctions(tu);
190   ReadElfObjects(tu);
191   if (!ok) {
192     llvm::errs() << "Failed to convert JSON to IR\n";
193     return false;
194   }
195   return true;
196 }
197 
ReadTemplateInfo(const JsonObjectRef & type_decl,TemplatedArtifactIR * template_ir)198 void JsonIRReader::ReadTemplateInfo(const JsonObjectRef &type_decl,
199                                     TemplatedArtifactIR *template_ir) {
200   TemplateInfoIR template_info_ir;
201   for (auto &&referenced_type : type_decl.GetStrings("template_args")) {
202     TemplateElementIR template_element_ir(referenced_type);
203     template_info_ir.AddTemplateElement(std::move(template_element_ir));
204   }
205   template_ir->SetTemplateInfo(std::move(template_info_ir));
206 }
207 
ReadTypeInfo(const JsonObjectRef & type_decl,TypeIR * type_ir)208 void JsonIRReader::ReadTypeInfo(const JsonObjectRef &type_decl,
209                                 TypeIR *type_ir) {
210   type_ir->SetLinkerSetKey(type_decl.GetString("linker_set_key"));
211   type_ir->SetSourceFile(type_decl.GetString("source_file"));
212   type_ir->SetName(type_decl.GetString("name"));
213   type_ir->SetReferencedType(type_decl.GetString("referenced_type"));
214   type_ir->SetSelfType(type_decl.GetString("self_type"));
215   type_ir->SetSize(type_decl.GetUint("size"));
216   type_ir->SetAlignment(type_decl.GetUint("alignment"));
217 }
218 
ReadRecordFields(const JsonObjectRef & record_type,RecordTypeIR * record_ir)219 void JsonIRReader::ReadRecordFields(const JsonObjectRef &record_type,
220                                     RecordTypeIR *record_ir) {
221   for (auto &&field : record_type.GetObjects("fields")) {
222     RecordFieldIR record_field_ir(
223         field.GetString("field_name"), field.GetString("referenced_type"),
224         field.GetUint("field_offset"), GetAccess(field));
225     record_ir->AddRecordField(std::move(record_field_ir));
226   }
227 }
228 
ReadBaseSpecifiers(const JsonObjectRef & record_type,RecordTypeIR * record_ir)229 void JsonIRReader::ReadBaseSpecifiers(const JsonObjectRef &record_type,
230                                       RecordTypeIR *record_ir) {
231   for (auto &&base_specifier : record_type.GetObjects("base_specifiers")) {
232     CXXBaseSpecifierIR record_base_ir(
233         base_specifier.GetString("referenced_type"),
234         base_specifier.GetBool("is_virtual"), GetAccess(base_specifier));
235     record_ir->AddCXXBaseSpecifier(std::move(record_base_ir));
236   }
237 }
238 
ReadVTableLayout(const JsonObjectRef & record_type,RecordTypeIR * record_ir)239 void JsonIRReader::ReadVTableLayout(const JsonObjectRef &record_type,
240                                     RecordTypeIR *record_ir) {
241   VTableLayoutIR vtable_layout_ir;
242   for (auto &&vtable_component : record_type.GetObjects("vtable_components")) {
243     VTableComponentIR vtable_component_ir(
244         vtable_component.GetString("mangled_component_name"),
245         GetVTableComponentKind(vtable_component),
246         vtable_component.GetInt("component_value"),
247         vtable_component.GetBool("is_pure"));
248     vtable_layout_ir.AddVTableComponent(std::move(vtable_component_ir));
249   }
250   record_ir->SetVTableLayout(std::move(vtable_layout_ir));
251 }
252 
ReadEnumFields(const JsonObjectRef & enum_type,EnumTypeIR * enum_ir)253 void JsonIRReader::ReadEnumFields(const JsonObjectRef &enum_type,
254                                   EnumTypeIR *enum_ir) {
255   for (auto &&field : enum_type.GetObjects("enum_fields")) {
256     std::string name = field.GetString("name");
257     const Json::Value &value = field.GetIntegralValue("enum_field_value");
258     if (value.isUInt64()) {
259       enum_ir->AddEnumField(EnumFieldIR(name, value.asUInt64()));
260     } else {
261       enum_ir->AddEnumField(EnumFieldIR(name, value.asInt64()));
262     }
263   }
264 }
265 
ReadFunctionParametersAndReturnType(const JsonObjectRef & function,CFunctionLikeIR * function_ir)266 void JsonIRReader::ReadFunctionParametersAndReturnType(
267     const JsonObjectRef &function, CFunctionLikeIR *function_ir) {
268   function_ir->SetReturnType(function.GetString("return_type"));
269   for (auto &&parameter : function.GetObjects("parameters")) {
270     ParamIR param_ir(parameter.GetString("referenced_type"),
271                      parameter.GetBool("default_arg"),
272                      parameter.GetBool("is_this_ptr"));
273     function_ir->AddParameter(std::move(param_ir));
274   }
275 }
276 
FunctionJsonToIR(const JsonObjectRef & function)277 FunctionIR JsonIRReader::FunctionJsonToIR(const JsonObjectRef &function) {
278   FunctionIR function_ir;
279   function_ir.SetLinkerSetKey(function.GetString("linker_set_key"));
280   function_ir.SetName(function.GetString("function_name"));
281   function_ir.SetAccess(GetAccess(function));
282   function_ir.SetSourceFile(function.GetString("source_file"));
283   ReadFunctionParametersAndReturnType(function, &function_ir);
284   ReadTemplateInfo(function, &function_ir);
285   return function_ir;
286 }
287 
288 FunctionTypeIR
FunctionTypeJsonToIR(const JsonObjectRef & function_type)289 JsonIRReader::FunctionTypeJsonToIR(const JsonObjectRef &function_type) {
290   FunctionTypeIR function_type_ir;
291   ReadTypeInfo(function_type, &function_type_ir);
292   ReadFunctionParametersAndReturnType(function_type, &function_type_ir);
293   return function_type_ir;
294 }
295 
296 RecordTypeIR
RecordTypeJsonToIR(const JsonObjectRef & record_type)297 JsonIRReader::RecordTypeJsonToIR(const JsonObjectRef &record_type) {
298   RecordTypeIR record_type_ir;
299   ReadTypeInfo(record_type, &record_type_ir);
300   ReadTemplateInfo(record_type, &record_type_ir);
301   record_type_ir.SetAccess(GetAccess(record_type));
302   ReadVTableLayout(record_type, &record_type_ir);
303   ReadRecordFields(record_type, &record_type_ir);
304   ReadBaseSpecifiers(record_type, &record_type_ir);
305   record_type_ir.SetRecordKind(GetRecordKind(record_type));
306   record_type_ir.SetAnonymity(record_type.GetBool("is_anonymous"));
307   return record_type_ir;
308 }
309 
EnumTypeJsonToIR(const JsonObjectRef & enum_type)310 EnumTypeIR JsonIRReader::EnumTypeJsonToIR(const JsonObjectRef &enum_type) {
311   EnumTypeIR enum_type_ir;
312   ReadTypeInfo(enum_type, &enum_type_ir);
313   enum_type_ir.SetUnderlyingType(enum_type.GetString("underlying_type"));
314   enum_type_ir.SetAccess(GetAccess(enum_type));
315   ReadEnumFields(enum_type, &enum_type_ir);
316   return enum_type_ir;
317 }
318 
ReadGlobalVariables(const JsonObjectRef & tu)319 void JsonIRReader::ReadGlobalVariables(const JsonObjectRef &tu) {
320   for (auto &&global_variable : tu.GetObjects("global_vars")) {
321     GlobalVarIR global_variable_ir;
322     global_variable_ir.SetName(global_variable.GetString("name"));
323     global_variable_ir.SetAccess(GetAccess(global_variable));
324     global_variable_ir.SetSourceFile(global_variable.GetString("source_file"));
325     global_variable_ir.SetReferencedType(
326         global_variable.GetString("referenced_type"));
327     global_variable_ir.SetLinkerSetKey(
328         global_variable.GetString("linker_set_key"));
329     module_->AddGlobalVariable(std::move(global_variable_ir));
330   }
331 }
332 
ReadPointerTypes(const JsonObjectRef & tu)333 void JsonIRReader::ReadPointerTypes(const JsonObjectRef &tu) {
334   for (auto &&pointer_type : tu.GetObjects("pointer_types")) {
335     PointerTypeIR pointer_type_ir;
336     ReadTypeInfo(pointer_type, &pointer_type_ir);
337     module_->AddPointerType(std::move(pointer_type_ir));
338   }
339 }
340 
ReadBuiltinTypes(const JsonObjectRef & tu)341 void JsonIRReader::ReadBuiltinTypes(const JsonObjectRef &tu) {
342   for (auto &&builtin_type : tu.GetObjects("builtin_types")) {
343     BuiltinTypeIR builtin_type_ir;
344     ReadTypeInfo(builtin_type, &builtin_type_ir);
345     builtin_type_ir.SetSignedness(builtin_type.GetBool("is_unsigned"));
346     builtin_type_ir.SetIntegralType(builtin_type.GetBool("is_integral"));
347     module_->AddBuiltinType(std::move(builtin_type_ir));
348   }
349 }
350 
ReadQualifiedTypes(const JsonObjectRef & tu)351 void JsonIRReader::ReadQualifiedTypes(const JsonObjectRef &tu) {
352   for (auto &&qualified_type : tu.GetObjects("qualified_types")) {
353     QualifiedTypeIR qualified_type_ir;
354     ReadTypeInfo(qualified_type, &qualified_type_ir);
355     qualified_type_ir.SetConstness(qualified_type.GetBool("is_const"));
356     qualified_type_ir.SetVolatility(qualified_type.GetBool("is_volatile"));
357     qualified_type_ir.SetRestrictedness(
358         qualified_type.GetBool("is_restricted"));
359     module_->AddQualifiedType(std::move(qualified_type_ir));
360   }
361 }
362 
ReadArrayTypes(const JsonObjectRef & tu)363 void JsonIRReader::ReadArrayTypes(const JsonObjectRef &tu) {
364   for (auto &&array_type : tu.GetObjects("array_types")) {
365     ArrayTypeIR array_type_ir;
366     ReadTypeInfo(array_type, &array_type_ir);
367     array_type_ir.SetUnknownBound(array_type.GetBool("is_of_unknown_bound"));
368     module_->AddArrayType(std::move(array_type_ir));
369   }
370 }
371 
ReadLvalueReferenceTypes(const JsonObjectRef & tu)372 void JsonIRReader::ReadLvalueReferenceTypes(const JsonObjectRef &tu) {
373   for (auto &&lvalue_reference_type : tu.GetObjects("lvalue_reference_types")) {
374     LvalueReferenceTypeIR lvalue_reference_type_ir;
375     ReadTypeInfo(lvalue_reference_type, &lvalue_reference_type_ir);
376     module_->AddLvalueReferenceType(std::move(lvalue_reference_type_ir));
377   }
378 }
379 
ReadRvalueReferenceTypes(const JsonObjectRef & tu)380 void JsonIRReader::ReadRvalueReferenceTypes(const JsonObjectRef &tu) {
381   for (auto &&rvalue_reference_type : tu.GetObjects("rvalue_reference_types")) {
382     RvalueReferenceTypeIR rvalue_reference_type_ir;
383     ReadTypeInfo(rvalue_reference_type, &rvalue_reference_type_ir);
384     module_->AddRvalueReferenceType(std::move(rvalue_reference_type_ir));
385   }
386 }
387 
ReadFunctions(const JsonObjectRef & tu)388 void JsonIRReader::ReadFunctions(const JsonObjectRef &tu) {
389   for (auto &&function : tu.GetObjects("functions")) {
390     FunctionIR function_ir = FunctionJsonToIR(function);
391     module_->AddFunction(std::move(function_ir));
392   }
393 }
394 
ReadRecordTypes(const JsonObjectRef & tu)395 void JsonIRReader::ReadRecordTypes(const JsonObjectRef &tu) {
396   for (auto &&record_type : tu.GetObjects("record_types")) {
397     RecordTypeIR record_type_ir = RecordTypeJsonToIR(record_type);
398     module_->AddRecordType(std::move(record_type_ir));
399   }
400 }
401 
ReadFunctionTypes(const JsonObjectRef & tu)402 void JsonIRReader::ReadFunctionTypes(const JsonObjectRef &tu) {
403   for (auto &&function_type : tu.GetObjects("function_types")) {
404     FunctionTypeIR function_type_ir = FunctionTypeJsonToIR(function_type);
405     module_->AddFunctionType(std::move(function_type_ir));
406   }
407 }
408 
ReadEnumTypes(const JsonObjectRef & tu)409 void JsonIRReader::ReadEnumTypes(const JsonObjectRef &tu) {
410   for (auto &&enum_type : tu.GetObjects("enum_types")) {
411     EnumTypeIR enum_type_ir = EnumTypeJsonToIR(enum_type);
412     module_->AddEnumType(std::move(enum_type_ir));
413   }
414 }
415 
ReadElfFunctions(const JsonObjectRef & tu)416 void JsonIRReader::ReadElfFunctions(const JsonObjectRef &tu) {
417   for (auto &&elf_function : tu.GetObjects("elf_functions")) {
418     ElfFunctionIR elf_function_ir(elf_function.GetString("name"),
419                                   GetElfSymbolBinding(elf_function));
420     module_->AddElfFunction(std::move(elf_function_ir));
421   }
422 }
423 
ReadElfObjects(const JsonObjectRef & tu)424 void JsonIRReader::ReadElfObjects(const JsonObjectRef &tu) {
425   for (auto &&elf_object : tu.GetObjects("elf_objects")) {
426     ElfObjectIR elf_object_ir(elf_object.GetString("name"),
427                               GetElfSymbolBinding(elf_object));
428     module_->AddElfObject(std::move(elf_object_ir));
429   }
430 }
431 
CreateJsonIRReader(const std::set<std::string> * exported_headers)432 std::unique_ptr<IRReader> CreateJsonIRReader(
433     const std::set<std::string> *exported_headers) {
434   return std::make_unique<JsonIRReader>(exported_headers);
435 }
436 
437 
438 }  // namespace repr
439 }  // header_checker
440