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::isIntegral).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::isIntegral).asUInt64();
84 }
85
GetString(const std::string & key) const86 std::string JsonObjectRef::GetString(const std::string &key) const {
87 return Get(key, json_empty_string, &Json::Value::isString).asString();
88 }
89
GetObject(const std::string & key) const90 JsonObjectRef JsonObjectRef::GetObject(const std::string &key) const {
91 return JsonObjectRef(Get(key, json_empty_object, &Json::Value::isObject),
92 ok_);
93 }
94
95 JsonArrayRef<JsonObjectRef>
GetObjects(const std::string & key) const96 JsonObjectRef::GetObjects(const std::string &key) const {
97 return JsonArrayRef<JsonObjectRef>(
98 Get(key, json_empty_array, &Json::Value::isArray), ok_);
99 }
100
101 JsonArrayRef<std::string>
GetStrings(const std::string & key) const102 JsonObjectRef::GetStrings(const std::string &key) const {
103 return JsonArrayRef<std::string>(
104 Get(key, json_empty_array, &Json::Value::isArray), ok_);
105 }
106
107 template <>
operator *() const108 JsonObjectRef JsonArrayRef<JsonObjectRef>::Iterator::operator*() const {
109 return JsonObjectRef(array_[index_], ok_);
110 }
111
operator *() const112 template <> std::string JsonArrayRef<std::string>::Iterator::operator*() const {
113 return array_[index_].asString();
114 }
115
GetAccess(const JsonObjectRef & type_decl)116 static AccessSpecifierIR GetAccess(const JsonObjectRef &type_decl) {
117 std::string access(type_decl.GetString("access"));
118 if (access.empty()) {
119 return default_access_ir;
120 }
121 return FindInMap(access_json_to_ir, access,
122 "Failed to convert JSON to AccessSpecifierIR");
123 }
124
125 static RecordTypeIR::RecordKind
GetRecordKind(const JsonObjectRef & record_type)126 GetRecordKind(const JsonObjectRef &record_type) {
127 std::string kind(record_type.GetString("record_kind"));
128 if (kind.empty()) {
129 return default_record_kind_ir;
130 }
131 return FindInMap(record_kind_json_to_ir, kind,
132 "Failed to convert JSON to RecordKind");
133 }
134
135 static VTableComponentIR::Kind
GetVTableComponentKind(const JsonObjectRef & vtable_component)136 GetVTableComponentKind(const JsonObjectRef &vtable_component) {
137 std::string kind(vtable_component.GetString("kind"));
138 if (kind.empty()) {
139 return default_vtable_component_kind_ir;
140 }
141 return FindInMap(vtable_component_kind_json_to_ir, kind,
142 "Failed to convert JSON to VTableComponentIR::Kind");
143 }
144
145 static ElfSymbolIR::ElfSymbolBinding
GetElfSymbolBinding(const JsonObjectRef & elf_symbol)146 GetElfSymbolBinding(const JsonObjectRef &elf_symbol) {
147 std::string binding(elf_symbol.GetString("binding"));
148 if (binding.empty()) {
149 return default_elf_symbol_binding_ir;
150 }
151 return FindInMap(elf_symbol_binding_json_to_ir, binding,
152 "Failed to convert JSON to ElfSymbolBinding");
153 }
154
ReadDumpImpl(const std::string & dump_file)155 bool JsonIRReader::ReadDumpImpl(const std::string &dump_file) {
156 Json::Value tu_json;
157 Json::Reader reader;
158 std::ifstream input(dump_file);
159
160 if (!reader.parse(input, tu_json, /* collectComments */ false)) {
161 llvm::errs() << "Failed to parse JSON: "
162 << reader.getFormattedErrorMessages() << "\n";
163 return false;
164 }
165 bool ok = true;
166 JsonObjectRef tu(tu_json, ok);
167 if (!ok) {
168 llvm::errs() << "Translation unit is not an object\n";
169 return false;
170 }
171
172 ReadFunctions(tu);
173 ReadGlobalVariables(tu);
174 ReadEnumTypes(tu);
175 ReadRecordTypes(tu);
176 ReadFunctionTypes(tu);
177 ReadArrayTypes(tu);
178 ReadPointerTypes(tu);
179 ReadQualifiedTypes(tu);
180 ReadBuiltinTypes(tu);
181 ReadLvalueReferenceTypes(tu);
182 ReadRvalueReferenceTypes(tu);
183 ReadElfFunctions(tu);
184 ReadElfObjects(tu);
185 if (!ok) {
186 llvm::errs() << "Failed to convert JSON to IR\n";
187 return false;
188 }
189 return true;
190 }
191
ReadTemplateInfo(const JsonObjectRef & type_decl,TemplatedArtifactIR * template_ir)192 void JsonIRReader::ReadTemplateInfo(const JsonObjectRef &type_decl,
193 TemplatedArtifactIR *template_ir) {
194 TemplateInfoIR template_info_ir;
195 for (auto &&referenced_type : type_decl.GetStrings("template_args")) {
196 TemplateElementIR template_element_ir(referenced_type);
197 template_info_ir.AddTemplateElement(std::move(template_element_ir));
198 }
199 template_ir->SetTemplateInfo(std::move(template_info_ir));
200 }
201
ReadTypeInfo(const JsonObjectRef & type_decl,TypeIR * type_ir)202 void JsonIRReader::ReadTypeInfo(const JsonObjectRef &type_decl,
203 TypeIR *type_ir) {
204 type_ir->SetLinkerSetKey(type_decl.GetString("linker_set_key"));
205 type_ir->SetSourceFile(type_decl.GetString("source_file"));
206 type_ir->SetName(type_decl.GetString("name"));
207 type_ir->SetReferencedType(type_decl.GetString("referenced_type"));
208 type_ir->SetSelfType(type_decl.GetString("self_type"));
209 type_ir->SetSize(type_decl.GetUint("size"));
210 type_ir->SetAlignment(type_decl.GetUint("alignment"));
211 }
212
ReadRecordFields(const JsonObjectRef & record_type,RecordTypeIR * record_ir)213 void JsonIRReader::ReadRecordFields(const JsonObjectRef &record_type,
214 RecordTypeIR *record_ir) {
215 for (auto &&field : record_type.GetObjects("fields")) {
216 RecordFieldIR record_field_ir(
217 field.GetString("field_name"), field.GetString("referenced_type"),
218 field.GetUint("field_offset"), GetAccess(field));
219 record_ir->AddRecordField(std::move(record_field_ir));
220 }
221 }
222
ReadBaseSpecifiers(const JsonObjectRef & record_type,RecordTypeIR * record_ir)223 void JsonIRReader::ReadBaseSpecifiers(const JsonObjectRef &record_type,
224 RecordTypeIR *record_ir) {
225 for (auto &&base_specifier : record_type.GetObjects("base_specifiers")) {
226 CXXBaseSpecifierIR record_base_ir(
227 base_specifier.GetString("referenced_type"),
228 base_specifier.GetBool("is_virtual"), GetAccess(base_specifier));
229 record_ir->AddCXXBaseSpecifier(std::move(record_base_ir));
230 }
231 }
232
ReadVTableLayout(const JsonObjectRef & record_type,RecordTypeIR * record_ir)233 void JsonIRReader::ReadVTableLayout(const JsonObjectRef &record_type,
234 RecordTypeIR *record_ir) {
235 VTableLayoutIR vtable_layout_ir;
236 for (auto &&vtable_component : record_type.GetObjects("vtable_components")) {
237 VTableComponentIR vtable_component_ir(
238 vtable_component.GetString("mangled_component_name"),
239 GetVTableComponentKind(vtable_component),
240 vtable_component.GetInt("component_value"),
241 vtable_component.GetBool("is_pure"));
242 vtable_layout_ir.AddVTableComponent(std::move(vtable_component_ir));
243 }
244 record_ir->SetVTableLayout(std::move(vtable_layout_ir));
245 }
246
ReadEnumFields(const JsonObjectRef & enum_type,EnumTypeIR * enum_ir)247 void JsonIRReader::ReadEnumFields(const JsonObjectRef &enum_type,
248 EnumTypeIR *enum_ir) {
249 for (auto &&field : enum_type.GetObjects("enum_fields")) {
250 EnumFieldIR enum_field_ir(field.GetString("name"),
251 field.GetInt("enum_field_value"));
252 enum_ir->AddEnumField(std::move(enum_field_ir));
253 }
254 }
255
ReadFunctionParametersAndReturnType(const JsonObjectRef & function,CFunctionLikeIR * function_ir)256 void JsonIRReader::ReadFunctionParametersAndReturnType(
257 const JsonObjectRef &function, CFunctionLikeIR *function_ir) {
258 function_ir->SetReturnType(function.GetString("return_type"));
259 for (auto &¶meter : function.GetObjects("parameters")) {
260 ParamIR param_ir(parameter.GetString("referenced_type"),
261 parameter.GetBool("default_arg"),
262 parameter.GetBool("is_this_ptr"));
263 function_ir->AddParameter(std::move(param_ir));
264 }
265 }
266
FunctionJsonToIR(const JsonObjectRef & function)267 FunctionIR JsonIRReader::FunctionJsonToIR(const JsonObjectRef &function) {
268 FunctionIR function_ir;
269 function_ir.SetLinkerSetKey(function.GetString("linker_set_key"));
270 function_ir.SetName(function.GetString("function_name"));
271 function_ir.SetAccess(GetAccess(function));
272 function_ir.SetSourceFile(function.GetString("source_file"));
273 ReadFunctionParametersAndReturnType(function, &function_ir);
274 ReadTemplateInfo(function, &function_ir);
275 return function_ir;
276 }
277
278 FunctionTypeIR
FunctionTypeJsonToIR(const JsonObjectRef & function_type)279 JsonIRReader::FunctionTypeJsonToIR(const JsonObjectRef &function_type) {
280 FunctionTypeIR function_type_ir;
281 ReadTypeInfo(function_type, &function_type_ir);
282 ReadFunctionParametersAndReturnType(function_type, &function_type_ir);
283 return function_type_ir;
284 }
285
286 RecordTypeIR
RecordTypeJsonToIR(const JsonObjectRef & record_type)287 JsonIRReader::RecordTypeJsonToIR(const JsonObjectRef &record_type) {
288 RecordTypeIR record_type_ir;
289 ReadTypeInfo(record_type, &record_type_ir);
290 ReadTemplateInfo(record_type, &record_type_ir);
291 record_type_ir.SetAccess(GetAccess(record_type));
292 ReadVTableLayout(record_type, &record_type_ir);
293 ReadRecordFields(record_type, &record_type_ir);
294 ReadBaseSpecifiers(record_type, &record_type_ir);
295 record_type_ir.SetRecordKind(GetRecordKind(record_type));
296 record_type_ir.SetAnonymity(record_type.GetBool("is_anonymous"));
297 return record_type_ir;
298 }
299
EnumTypeJsonToIR(const JsonObjectRef & enum_type)300 EnumTypeIR JsonIRReader::EnumTypeJsonToIR(const JsonObjectRef &enum_type) {
301 EnumTypeIR enum_type_ir;
302 ReadTypeInfo(enum_type, &enum_type_ir);
303 enum_type_ir.SetUnderlyingType(enum_type.GetString("underlying_type"));
304 enum_type_ir.SetAccess(GetAccess(enum_type));
305 ReadEnumFields(enum_type, &enum_type_ir);
306 return enum_type_ir;
307 }
308
ReadGlobalVariables(const JsonObjectRef & tu)309 void JsonIRReader::ReadGlobalVariables(const JsonObjectRef &tu) {
310 for (auto &&global_variable : tu.GetObjects("global_vars")) {
311 GlobalVarIR global_variable_ir;
312 global_variable_ir.SetName(global_variable.GetString("name"));
313 global_variable_ir.SetAccess(GetAccess(global_variable));
314 global_variable_ir.SetSourceFile(global_variable.GetString("source_file"));
315 global_variable_ir.SetReferencedType(
316 global_variable.GetString("referenced_type"));
317 global_variable_ir.SetLinkerSetKey(
318 global_variable.GetString("linker_set_key"));
319 module_->AddGlobalVariable(std::move(global_variable_ir));
320 }
321 }
322
ReadPointerTypes(const JsonObjectRef & tu)323 void JsonIRReader::ReadPointerTypes(const JsonObjectRef &tu) {
324 for (auto &&pointer_type : tu.GetObjects("pointer_types")) {
325 PointerTypeIR pointer_type_ir;
326 ReadTypeInfo(pointer_type, &pointer_type_ir);
327 module_->AddPointerType(std::move(pointer_type_ir));
328 }
329 }
330
ReadBuiltinTypes(const JsonObjectRef & tu)331 void JsonIRReader::ReadBuiltinTypes(const JsonObjectRef &tu) {
332 for (auto &&builtin_type : tu.GetObjects("builtin_types")) {
333 BuiltinTypeIR builtin_type_ir;
334 ReadTypeInfo(builtin_type, &builtin_type_ir);
335 builtin_type_ir.SetSignedness(builtin_type.GetBool("is_unsigned"));
336 builtin_type_ir.SetIntegralType(builtin_type.GetBool("is_integral"));
337 module_->AddBuiltinType(std::move(builtin_type_ir));
338 }
339 }
340
ReadQualifiedTypes(const JsonObjectRef & tu)341 void JsonIRReader::ReadQualifiedTypes(const JsonObjectRef &tu) {
342 for (auto &&qualified_type : tu.GetObjects("qualified_types")) {
343 QualifiedTypeIR qualified_type_ir;
344 ReadTypeInfo(qualified_type, &qualified_type_ir);
345 qualified_type_ir.SetConstness(qualified_type.GetBool("is_const"));
346 qualified_type_ir.SetVolatility(qualified_type.GetBool("is_volatile"));
347 qualified_type_ir.SetRestrictedness(
348 qualified_type.GetBool("is_restricted"));
349 module_->AddQualifiedType(std::move(qualified_type_ir));
350 }
351 }
352
ReadArrayTypes(const JsonObjectRef & tu)353 void JsonIRReader::ReadArrayTypes(const JsonObjectRef &tu) {
354 for (auto &&array_type : tu.GetObjects("array_types")) {
355 ArrayTypeIR array_type_ir;
356 ReadTypeInfo(array_type, &array_type_ir);
357 module_->AddArrayType(std::move(array_type_ir));
358 }
359 }
360
ReadLvalueReferenceTypes(const JsonObjectRef & tu)361 void JsonIRReader::ReadLvalueReferenceTypes(const JsonObjectRef &tu) {
362 for (auto &&lvalue_reference_type : tu.GetObjects("lvalue_reference_types")) {
363 LvalueReferenceTypeIR lvalue_reference_type_ir;
364 ReadTypeInfo(lvalue_reference_type, &lvalue_reference_type_ir);
365 module_->AddLvalueReferenceType(std::move(lvalue_reference_type_ir));
366 }
367 }
368
ReadRvalueReferenceTypes(const JsonObjectRef & tu)369 void JsonIRReader::ReadRvalueReferenceTypes(const JsonObjectRef &tu) {
370 for (auto &&rvalue_reference_type : tu.GetObjects("rvalue_reference_types")) {
371 RvalueReferenceTypeIR rvalue_reference_type_ir;
372 ReadTypeInfo(rvalue_reference_type, &rvalue_reference_type_ir);
373 module_->AddRvalueReferenceType(std::move(rvalue_reference_type_ir));
374 }
375 }
376
ReadFunctions(const JsonObjectRef & tu)377 void JsonIRReader::ReadFunctions(const JsonObjectRef &tu) {
378 for (auto &&function : tu.GetObjects("functions")) {
379 FunctionIR function_ir = FunctionJsonToIR(function);
380 module_->AddFunction(std::move(function_ir));
381 }
382 }
383
ReadRecordTypes(const JsonObjectRef & tu)384 void JsonIRReader::ReadRecordTypes(const JsonObjectRef &tu) {
385 for (auto &&record_type : tu.GetObjects("record_types")) {
386 RecordTypeIR record_type_ir = RecordTypeJsonToIR(record_type);
387 module_->AddRecordType(std::move(record_type_ir));
388 }
389 }
390
ReadFunctionTypes(const JsonObjectRef & tu)391 void JsonIRReader::ReadFunctionTypes(const JsonObjectRef &tu) {
392 for (auto &&function_type : tu.GetObjects("function_types")) {
393 FunctionTypeIR function_type_ir = FunctionTypeJsonToIR(function_type);
394 module_->AddFunctionType(std::move(function_type_ir));
395 }
396 }
397
ReadEnumTypes(const JsonObjectRef & tu)398 void JsonIRReader::ReadEnumTypes(const JsonObjectRef &tu) {
399 for (auto &&enum_type : tu.GetObjects("enum_types")) {
400 EnumTypeIR enum_type_ir = EnumTypeJsonToIR(enum_type);
401 module_->AddEnumType(std::move(enum_type_ir));
402 }
403 }
404
ReadElfFunctions(const JsonObjectRef & tu)405 void JsonIRReader::ReadElfFunctions(const JsonObjectRef &tu) {
406 for (auto &&elf_function : tu.GetObjects("elf_functions")) {
407 ElfFunctionIR elf_function_ir(elf_function.GetString("name"),
408 GetElfSymbolBinding(elf_function));
409 module_->AddElfFunction(std::move(elf_function_ir));
410 }
411 }
412
ReadElfObjects(const JsonObjectRef & tu)413 void JsonIRReader::ReadElfObjects(const JsonObjectRef &tu) {
414 for (auto &&elf_object : tu.GetObjects("elf_objects")) {
415 ElfObjectIR elf_object_ir(elf_object.GetString("name"),
416 GetElfSymbolBinding(elf_object));
417 module_->AddElfObject(std::move(elf_object_ir));
418 }
419 }
420
CreateJsonIRReader(const std::set<std::string> * exported_headers)421 std::unique_ptr<IRReader> CreateJsonIRReader(
422 const std::set<std::string> *exported_headers) {
423 return std::make_unique<JsonIRReader>(exported_headers);
424 }
425
426
427 } // namespace repr
428 } // header_checker
429