1 /*
2  * Copyright 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 "struct_def.h"
18 
19 #include "fields/all_fields.h"
20 #include "util.h"
21 
StructDef(std::string name,FieldList fields)22 StructDef::StructDef(std::string name, FieldList fields) : StructDef(name, fields, nullptr) {}
StructDef(std::string name,FieldList fields,StructDef * parent)23 StructDef::StructDef(std::string name, FieldList fields, StructDef* parent)
24     : ParentDef(name, fields, parent), total_size_(GetSize(true)) {}
25 
GetNewField(const std::string & name,ParseLocation loc) const26 PacketField* StructDef::GetNewField(const std::string& name, ParseLocation loc) const {
27   if (fields_.HasBody()) {
28     return new VariableLengthStructField(name, name_, loc);
29   } else {
30     return new StructField(name, name_, total_size_, loc);
31   }
32 }
33 
GetDefinitionType() const34 TypeDef::Type StructDef::GetDefinitionType() const {
35   return TypeDef::Type::STRUCT;
36 }
37 
GenSpecialize(std::ostream & s) const38 void StructDef::GenSpecialize(std::ostream& s) const {
39   if (parent_ == nullptr) {
40     return;
41   }
42   s << "static " << name_ << "* Specialize(" << parent_->name_ << "* parent) {";
43   s << "ASSERT(" << name_ << "::IsInstance(*parent));";
44   s << "return static_cast<" << name_ << "*>(parent);";
45   s << "}";
46 }
47 
GenToString(std::ostream & s) const48 void StructDef::GenToString(std::ostream& s) const {
49   s << "std::string ToString() const {";
50   s << "std::stringstream ss;";
51   s << "ss << std::hex << std::showbase << \"" << name_ << " { \";";
52 
53   if (fields_.size() > 0) {
54     s << "ss";
55     bool firstfield = true;
56     for (const auto& field : fields_) {
57       if (field->GetFieldType() == ReservedField::kFieldType ||
58           field->GetFieldType() == ChecksumStartField::kFieldType ||
59           field->GetFieldType() == FixedScalarField::kFieldType || field->GetFieldType() == CountField::kFieldType ||
60           field->GetFieldType() == SizeField::kFieldType)
61         continue;
62 
63       s << (firstfield ? " << \"" : " << \", ") << field->GetName() << " = \" << ";
64 
65       field->GenStringRepresentation(s, field->GetName() + "_");
66 
67       if (firstfield) {
68         firstfield = false;
69       }
70     }
71     s << ";";
72   }
73 
74   s << "ss << \" }\";";
75   s << "return ss.str();";
76   s << "}\n";
77 }
78 
GenParse(std::ostream & s) const79 void StructDef::GenParse(std::ostream& s) const {
80   std::string iterator = (is_little_endian_ ? "Iterator<kLittleEndian>" : "Iterator<!kLittleEndian>");
81 
82   if (fields_.HasBody()) {
83     s << "static std::optional<" << iterator << ">";
84   } else {
85     s << "static " << iterator;
86   }
87 
88   s << " Parse(" << name_ << "* to_fill, " << iterator << " struct_begin_it ";
89 
90   if (parent_ != nullptr) {
91     s << ", bool fill_parent = true) {";
92   } else {
93     s << ") {";
94   }
95   s << "auto to_bound = struct_begin_it;";
96 
97   if (parent_ != nullptr) {
98     s << "if (fill_parent) {";
99     std::string parent_param = (parent_->parent_ == nullptr ? "" : ", true");
100     if (parent_->fields_.HasBody()) {
101       s << "auto parent_optional_it = " << parent_->name_ << "::Parse(to_fill, to_bound" << parent_param << ");";
102       if (fields_.HasBody()) {
103         s << "if (!parent_optional_it) { return {}; }";
104       } else {
105         s << "ASSERT(parent_optional_it.has_value());";
106       }
107     } else {
108       s << parent_->name_ << "::Parse(to_fill, to_bound" << parent_param << ");";
109     }
110     s << "}";
111   }
112 
113   if (!fields_.HasBody()) {
114     s << "size_t end_index = struct_begin_it.NumBytesRemaining();";
115     s << "if (end_index < " << GetSize().bytes() << ")";
116     s << "{ return struct_begin_it.Subrange(0,0);}";
117   }
118 
119   Size total_bits{0};
120   for (const auto& field : fields_) {
121     if (field->GetFieldType() != ReservedField::kFieldType && field->GetFieldType() != BodyField::kFieldType &&
122         field->GetFieldType() != FixedScalarField::kFieldType &&
123         field->GetFieldType() != ChecksumStartField::kFieldType && field->GetFieldType() != ChecksumField::kFieldType &&
124         field->GetFieldType() != CountField::kFieldType) {
125       total_bits += field->GetSize().bits();
126     }
127   }
128   s << "{";
129   s << "if (to_bound.NumBytesRemaining() < " << total_bits.bytes() << ")";
130   if (!fields_.HasBody()) {
131     s << "{ return to_bound.Subrange(to_bound.NumBytesRemaining(),0);}";
132   } else {
133     s << "{ return {};}";
134   }
135   s << "}";
136   for (const auto& field : fields_) {
137     if (field->GetFieldType() != ReservedField::kFieldType && field->GetFieldType() != BodyField::kFieldType &&
138         field->GetFieldType() != FixedScalarField::kFieldType && field->GetFieldType() != SizeField::kFieldType &&
139         field->GetFieldType() != ChecksumStartField::kFieldType && field->GetFieldType() != ChecksumField::kFieldType &&
140         field->GetFieldType() != CountField::kFieldType) {
141       s << "{";
142       int num_leading_bits =
143           field->GenBounds(s, GetStructOffsetForField(field->GetName()), Size(), field->GetStructSize());
144       s << "auto " << field->GetName() << "_ptr = &to_fill->" << field->GetName() << "_;";
145       field->GenExtractor(s, num_leading_bits, true);
146       s << "}";
147     }
148     if (field->GetFieldType() == CountField::kFieldType || field->GetFieldType() == SizeField::kFieldType) {
149       s << "{";
150       int num_leading_bits =
151           field->GenBounds(s, GetStructOffsetForField(field->GetName()), Size(), field->GetStructSize());
152       s << "auto " << field->GetName() << "_ptr = &to_fill->" << field->GetName() << "_extracted_;";
153       field->GenExtractor(s, num_leading_bits, true);
154       s << "}";
155     }
156   }
157   s << "return struct_begin_it + to_fill->size();";
158   s << "}";
159 }
160 
GenParseFunctionPrototype(std::ostream & s) const161 void StructDef::GenParseFunctionPrototype(std::ostream& s) const {
162   s << "std::unique_ptr<" << name_ << "> Parse" << name_ << "(";
163   if (is_little_endian_) {
164     s << "Iterator<kLittleEndian>";
165   } else {
166     s << "Iterator<!kLittleEndian>";
167   }
168   s << "it);";
169 }
170 
GenDefinition(std::ostream & s) const171 void StructDef::GenDefinition(std::ostream& s) const {
172   s << "class " << name_;
173   if (parent_ != nullptr) {
174     s << " : public " << parent_->name_;
175   } else {
176     if (is_little_endian_) {
177       s << " : public PacketStruct<kLittleEndian>";
178     } else {
179       s << " : public PacketStruct<!kLittleEndian>";
180     }
181   }
182   s << " {";
183   s << " public:";
184 
185   GenDefaultConstructor(s);
186   GenConstructor(s);
187 
188   s << " public:\n";
189   s << "  virtual ~" << name_ << "() = default;\n";
190 
191   GenSerialize(s);
192   s << "\n";
193 
194   GenParse(s);
195   s << "\n";
196 
197   GenSize(s);
198   s << "\n";
199 
200   GenInstanceOf(s);
201   s << "\n";
202 
203   GenSpecialize(s);
204   s << "\n";
205 
206   GenToString(s);
207   s << "\n";
208 
209   GenMembers(s);
210   for (const auto& field : fields_) {
211     if (field->GetFieldType() == CountField::kFieldType || field->GetFieldType() == SizeField::kFieldType) {
212       s << "\n private:\n";
213       s << " mutable " << field->GetDataType() << " " << field->GetName() << "_extracted_{0};";
214     }
215   }
216   s << "};\n";
217 
218   if (fields_.HasBody()) {
219     GenParseFunctionPrototype(s);
220   }
221   s << "\n";
222 }
223 
GenDefinitionPybind11(std::ostream & s) const224 void StructDef::GenDefinitionPybind11(std::ostream& s) const {
225   s << "py::class_<" << name_;
226   if (parent_ != nullptr) {
227     s << ", " << parent_->name_;
228   } else {
229     if (is_little_endian_) {
230       s << ", PacketStruct<kLittleEndian>";
231     } else {
232       s << ", PacketStruct<!kLittleEndian>";
233     }
234   }
235   s << ", std::shared_ptr<" << name_ << ">";
236   s << ">(m, \"" << name_ << "\")";
237   s << ".def(py::init<>())";
238   s << ".def(\"Serialize\", [](" << GetTypeName() << "& obj){";
239   s << "std::vector<uint8_t> bytes;";
240   s << "BitInserter bi(bytes);";
241   s << "obj.Serialize(bi);";
242   s << "return bytes;})";
243   s << ".def(\"Parse\", &" << name_ << "::Parse)";
244   s << ".def(\"size\", &" << name_ << "::size)";
245   for (const auto& field : fields_) {
246     if (field->GetBuilderParameterType().empty()) {
247       continue;
248     }
249     s << ".def_readwrite(\"" << field->GetName() << "\", &" << name_ << "::" << field->GetName() << "_)";
250   }
251   s << ";\n";
252 }
253 
254 // Generate constructor which provides default values for all struct fields.
GenDefaultConstructor(std::ostream & s) const255 void StructDef::GenDefaultConstructor(std::ostream& s) const {
256   if (parent_ != nullptr) {
257     s << name_ << "(const " << parent_->name_ << "& parent) : " << parent_->name_ << "(parent) {}";
258     s << name_ << "() : " << parent_->name_ << "() {";
259   } else {
260     s << name_ << "() {";
261   }
262 
263   // Get the list of parent params.
264   FieldList parent_params;
265   if (parent_ != nullptr) {
266     parent_params = parent_->GetParamList().GetFieldsWithoutTypes({
267         PayloadField::kFieldType,
268         BodyField::kFieldType,
269     });
270 
271     // Set constrained parent fields to their correct values.
272     for (const auto& field : parent_params) {
273       const auto& constraint = parent_constraints_.find(field->GetName());
274       if (constraint != parent_constraints_.end()) {
275         s << parent_->name_ << "::" << field->GetName() << "_ = ";
276         if (field->GetFieldType() == ScalarField::kFieldType) {
277           s << std::get<int64_t>(constraint->second) << ";";
278         } else if (field->GetFieldType() == EnumField::kFieldType) {
279           s << std::get<std::string>(constraint->second) << ";";
280         } else {
281           ERROR(field) << "Constraints on non enum/scalar fields should be impossible.";
282         }
283       }
284     }
285   }
286 
287   s << "}\n";
288 }
289 
290 // Generate constructor which inputs initial field values for all struct fields.
GenConstructor(std::ostream & s) const291 void StructDef::GenConstructor(std::ostream& s) const {
292   // Fetch the list of parameters and parent paremeters.
293   // GetParamList returns the list of all inherited fields that do not
294   // have a constrained value.
295   FieldList parent_params;
296   FieldList params = GetParamList().GetFieldsWithoutTypes({
297       PayloadField::kFieldType,
298       BodyField::kFieldType,
299   });
300 
301   if (parent_ != nullptr) {
302     parent_params = parent_->GetParamList().GetFieldsWithoutTypes({
303         PayloadField::kFieldType,
304         BodyField::kFieldType,
305     });
306   }
307 
308   // Generate constructor parameters for struct fields.
309   s << name_ << "(";
310   bool add_comma = false;
311   for (auto const& field : params) {
312     if (add_comma) {
313       s << ", ";
314     }
315     field->GenBuilderParameter(s);
316     add_comma = true;
317   }
318 
319   s << ")" << std::endl;
320 
321   if (params.size() > 0) {
322     s << " : ";
323   }
324 
325   // Invoke parent constructor with correct field values.
326   if (parent_ != nullptr) {
327     s << parent_->name_ << "(";
328     add_comma = false;
329     for (auto const& field : parent_params) {
330       if (add_comma) {
331         s << ", ";
332       }
333 
334       // Check for fields with constraint value.
335       const auto& constraint = parent_constraints_.find(field->GetName());
336       if (constraint != parent_constraints_.end()) {
337         s << "/* " << field->GetName() << " */ ";
338         if (field->GetFieldType() == ScalarField::kFieldType) {
339           s << std::get<int64_t>(constraint->second);
340         } else if (field->GetFieldType() == EnumField::kFieldType) {
341           s << std::get<std::string>(constraint->second);
342         } else {
343           ERROR(field) << "Constraints on non enum/scalar fields should be impossible.";
344         }
345       } else if (field->BuilderParameterMustBeMoved()) {
346         s << "std::move(" << field->GetName() << ")";
347       } else {
348         s << field->GetName();
349       }
350 
351       add_comma = true;
352     }
353 
354     s << ")";
355   }
356 
357   // Initialize remaining fields.
358   add_comma = parent_ != nullptr;
359   for (auto const& field : params) {
360     if (fields_.GetField(field->GetName()) == nullptr) {
361       continue;
362     }
363     if (add_comma) {
364       s << ", ";
365     }
366 
367     if (field->BuilderParameterMustBeMoved()) {
368       s << field->GetName() << "_(std::move(" << field->GetName() << "))";
369     } else {
370       s << field->GetName() << "_(" << field->GetName() << ")";
371     }
372 
373     add_comma = true;
374   }
375 
376   s << std::endl;
377   s << "{}\n";
378 }
379 
GetStructOffsetForField(std::string field_name) const380 Size StructDef::GetStructOffsetForField(std::string field_name) const {
381   auto size = Size(0);
382   for (auto it = fields_.begin(); it != fields_.end(); it++) {
383     // We've reached the field, end the loop.
384     if ((*it)->GetName() == field_name) break;
385     const auto& field = *it;
386     // When we need to parse this field, all previous fields should already be parsed.
387     if (field->GetStructSize().empty()) {
388       ERROR() << "Empty size for field " << (*it)->GetName() << " finding the offset for field: " << field_name;
389     }
390     size += field->GetStructSize();
391   }
392 
393   // We need the offset until a body field.
394   if (parent_ != nullptr) {
395     auto parent_body_offset = static_cast<StructDef*>(parent_)->GetStructOffsetForField("body");
396     if (parent_body_offset.empty()) {
397       ERROR() << "Empty offset for body in " << parent_->name_ << " finding the offset for field: " << field_name;
398     }
399     size += parent_body_offset;
400   }
401 
402   return size;
403 }
404