Loading system/gd/Android.bp +1 −1 Original line number Diff line number Diff line Loading @@ -593,7 +593,7 @@ genrule { tools: [ "bluetooth_packetgen", ], cmd: "$(location bluetooth_packetgen) --include=packages/modules/Bluetooth/system/gd --out=$(genDir) $(in)", cmd: "$(location bluetooth_packetgen) --fuzzing --testing --include=packages/modules/Bluetooth/system/gd --out=$(genDir) $(in)", srcs: [ "hci/hci_packets.pdl", "l2cap/l2cap_packets.pdl", Loading system/gd/packet/parser/gen_cpp.cc +20 −9 Original line number Diff line number Diff line Loading @@ -52,6 +52,8 @@ void generate_namespace_close(const std::vector<std::string>& token, std::ostrea bool generate_cpp_headers_one_file( const Declarations& decls, bool generate_fuzzing, bool generate_tests, const std::filesystem::path& input_file, const std::filesystem::path& include_dir, const std::filesystem::path& out_dir, Loading Loading @@ -84,7 +86,6 @@ bool generate_cpp_headers_one_file( #include <string> #include <type_traits> #include "os/log.h" #include "packet/base_packet_builder.h" #include "packet/bit_inserter.h" #include "packet/custom_field_fixed_size_interface.h" Loading @@ -92,14 +93,20 @@ bool generate_cpp_headers_one_file( #include "packet/packet_builder.h" #include "packet/packet_struct.h" #include "packet/packet_view.h" #include "packet/checksum_type_checker.h" #include "packet/custom_type_checker.h" #include "os/log.h" )"; if (generate_fuzzing || generate_tests) { out_file << R"( #if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING) || defined(FUZZ_TARGET) #include "packet/raw_builder.h" #endif #include "packet/checksum_type_checker.h" #include "packet/custom_type_checker.h" )"; } for (const auto& c : decls.type_defs_queue_) { if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM || Loading @@ -120,6 +127,7 @@ bool generate_cpp_headers_one_file( ((CustomFieldDef*)c.second)->GenUsing(out_file); } } out_file << R"( Loading @@ -132,14 +140,17 @@ using ::bluetooth::packet::kLittleEndian; using ::bluetooth::packet::PacketBuilder; using ::bluetooth::packet::PacketStruct; using ::bluetooth::packet::PacketView; using ::bluetooth::packet::parser::ChecksumTypeChecker; )"; if (generate_fuzzing || generate_tests) { out_file << R"( #if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING) || defined(FUZZ_TARGET) using ::bluetooth::packet::RawBuilder; #endif using ::bluetooth::packet::parser::ChecksumTypeChecker; )"; } for (const auto& e : decls.type_defs_queue_) { if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) { Loading Loading @@ -193,12 +204,12 @@ using ::bluetooth::packet::parser::ChecksumTypeChecker; } for (const auto& packet_def : decls.packet_defs_queue_) { packet_def.second->GenParserDefinition(out_file); packet_def.second->GenParserDefinition(out_file, generate_fuzzing, generate_tests); out_file << "\n\n"; } for (const auto& packet_def : decls.packet_defs_queue_) { packet_def.second->GenBuilderDefinition(out_file); packet_def.second->GenBuilderDefinition(out_file, generate_fuzzing, generate_tests); out_file << "\n\n"; } Loading system/gd/packet/parser/main.cc +18 −1 Original line number Diff line number Diff line Loading @@ -39,6 +39,8 @@ void yyset_in(FILE*, void*); bool generate_cpp_headers_one_file( const Declarations& decls, bool generate_fuzzing, bool generate_tests, const std::filesystem::path& input_file, const std::filesystem::path& include_dir, const std::filesystem::path& out_dir, Loading Loading @@ -134,6 +136,8 @@ int main(int argc, const char** argv) { // Number of shards per output pybind11 cc file size_t num_shards = 1; bool generate_rust = false; bool generate_fuzzing = false; bool generate_tests = false; std::queue<std::filesystem::path> input_files; const std::string arg_out = "--out="; Loading @@ -141,6 +145,8 @@ int main(int argc, const char** argv) { const std::string arg_namespace = "--root_namespace="; const std::string arg_num_shards = "--num_shards="; const std::string arg_rust = "--rust"; const std::string arg_fuzzing = "--fuzzing"; const std::string arg_testing = "--testing"; const std::string arg_source_root = "--source_root="; // Parse the source root first (if it exists) since it will be used for other Loading @@ -164,6 +170,10 @@ int main(int argc, const char** argv) { num_shards = std::stoul(arg.substr(arg_num_shards.size())); } else if (arg.find(arg_rust) == 0) { generate_rust = true; } else if (arg.find(arg_fuzzing) == 0) { generate_fuzzing = true; } else if (arg.find(arg_testing) == 0) { generate_tests = true; } else if (arg.find(arg_source_root) == 0) { // Do nothing (just don't treat it as input_files) } else { Loading Loading @@ -192,7 +202,14 @@ int main(int argc, const char** argv) { } } else { std::cout << "generating c++ and pybind11" << std::endl; if (!generate_cpp_headers_one_file(declarations, input_files.front(), include_dir, out_dir, root_namespace)) { if (!generate_cpp_headers_one_file( declarations, generate_fuzzing, generate_tests, input_files.front(), include_dir, out_dir, root_namespace)) { std::cerr << "Didn't generate cpp headers for " << input_files.front() << std::endl; return 3; } Loading system/gd/packet/parser/packet_def.cc +26 −10 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ PacketField* PacketDef::GetNewField(const std::string&, ParseLocation) const { return nullptr; // Packets can't be fields } void PacketDef::GenParserDefinition(std::ostream& s) const { void PacketDef::GenParserDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const { s << "class " << name_ << "View"; if (parent_ != nullptr) { s << " : public " << parent_->name_ << "View {"; Loading @@ -49,7 +49,9 @@ void PacketDef::GenParserDefinition(std::ostream& s) const { s << "{ return " << name_ << "View(std::move(packet)); }"; } if (generate_fuzzing || generate_tests) { GenTestingParserFromBytes(s); } std::set<std::string> fixed_types = { FixedScalarField::kFieldType, Loading Loading @@ -347,7 +349,7 @@ void PacketDef::GenParserToString(std::ostream& s) const { s << "}\n"; } void PacketDef::GenBuilderDefinition(std::ostream& s) const { void PacketDef::GenBuilderDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const { s << "class " << name_ << "Builder"; if (parent_ != nullptr) { s << " : public " << parent_->name_ << "Builder"; Loading @@ -366,9 +368,11 @@ void PacketDef::GenBuilderDefinition(std::ostream& s) const { GenBuilderCreate(s); s << "\n"; if (generate_fuzzing || generate_tests) { GenTestingFromView(s); s << "\n"; } } GenSerialize(s); s << "\n"; Loading @@ -386,12 +390,21 @@ void PacketDef::GenBuilderDefinition(std::ostream& s) const { GenMembers(s); s << "};\n"; if (generate_tests) { GenTestDefine(s); s << "\n"; } if (generate_fuzzing || generate_tests) { GenReflectTestDefine(s); s << "\n"; } if (generate_fuzzing) { GenFuzzTestDefine(s); s << "\n"; } } void PacketDef::GenTestingFromView(std::ostream& s) const { s << "#if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING) || defined(FUZZ_TARGET)\n"; Loading Loading @@ -483,7 +496,7 @@ void PacketDef::GenTestDefine(std::ostream& s) const { s << "\n#endif"; } void PacketDef::GenFuzzTestDefine(std::ostream& s) const { void PacketDef::GenReflectTestDefine(std::ostream& s) const { s << "#if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING)\n"; s << "#define DEFINE_" << name_ << "ReflectionFuzzTest() "; s << "void Run" << name_ << "ReflectionFuzzTest(const uint8_t* data, size_t size) {"; Loading @@ -497,6 +510,9 @@ void PacketDef::GenFuzzTestDefine(std::ostream& s) const { s << "packet->Serialize(it);"; s << "}"; s << "\n#endif\n"; } void PacketDef::GenFuzzTestDefine(std::ostream& s) const { s << "#ifdef PACKET_FUZZ_TESTING\n"; s << "#define DEFINE_AND_REGISTER_" << name_ << "ReflectionFuzzTest(REGISTRY) "; s << "DEFINE_" << name_ << "ReflectionFuzzTest();"; Loading system/gd/packet/parser/packet_def.h +3 −2 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ class PacketDef : public ParentDef { PacketField* GetNewField(const std::string& name, ParseLocation loc) const; void GenParserDefinition(std::ostream& s) const; void GenParserDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const; void GenTestingParserFromBytes(std::ostream& s) const; Loading @@ -45,12 +45,13 @@ class PacketDef : public ParentDef { TypeDef::Type GetDefinitionType() const; void GenBuilderDefinition(std::ostream& s) const; void GenBuilderDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const; void GenBuilderDefinitionPybind11(std::ostream& s) const; void GenTestDefine(std::ostream& s) const; void GenReflectTestDefine(std::ostream& s) const; void GenFuzzTestDefine(std::ostream& s) const; FieldList GetParametersToValidate() const; Loading Loading
system/gd/Android.bp +1 −1 Original line number Diff line number Diff line Loading @@ -593,7 +593,7 @@ genrule { tools: [ "bluetooth_packetgen", ], cmd: "$(location bluetooth_packetgen) --include=packages/modules/Bluetooth/system/gd --out=$(genDir) $(in)", cmd: "$(location bluetooth_packetgen) --fuzzing --testing --include=packages/modules/Bluetooth/system/gd --out=$(genDir) $(in)", srcs: [ "hci/hci_packets.pdl", "l2cap/l2cap_packets.pdl", Loading
system/gd/packet/parser/gen_cpp.cc +20 −9 Original line number Diff line number Diff line Loading @@ -52,6 +52,8 @@ void generate_namespace_close(const std::vector<std::string>& token, std::ostrea bool generate_cpp_headers_one_file( const Declarations& decls, bool generate_fuzzing, bool generate_tests, const std::filesystem::path& input_file, const std::filesystem::path& include_dir, const std::filesystem::path& out_dir, Loading Loading @@ -84,7 +86,6 @@ bool generate_cpp_headers_one_file( #include <string> #include <type_traits> #include "os/log.h" #include "packet/base_packet_builder.h" #include "packet/bit_inserter.h" #include "packet/custom_field_fixed_size_interface.h" Loading @@ -92,14 +93,20 @@ bool generate_cpp_headers_one_file( #include "packet/packet_builder.h" #include "packet/packet_struct.h" #include "packet/packet_view.h" #include "packet/checksum_type_checker.h" #include "packet/custom_type_checker.h" #include "os/log.h" )"; if (generate_fuzzing || generate_tests) { out_file << R"( #if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING) || defined(FUZZ_TARGET) #include "packet/raw_builder.h" #endif #include "packet/checksum_type_checker.h" #include "packet/custom_type_checker.h" )"; } for (const auto& c : decls.type_defs_queue_) { if (c.second->GetDefinitionType() == TypeDef::Type::CUSTOM || Loading @@ -120,6 +127,7 @@ bool generate_cpp_headers_one_file( ((CustomFieldDef*)c.second)->GenUsing(out_file); } } out_file << R"( Loading @@ -132,14 +140,17 @@ using ::bluetooth::packet::kLittleEndian; using ::bluetooth::packet::PacketBuilder; using ::bluetooth::packet::PacketStruct; using ::bluetooth::packet::PacketView; using ::bluetooth::packet::parser::ChecksumTypeChecker; )"; if (generate_fuzzing || generate_tests) { out_file << R"( #if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING) || defined(FUZZ_TARGET) using ::bluetooth::packet::RawBuilder; #endif using ::bluetooth::packet::parser::ChecksumTypeChecker; )"; } for (const auto& e : decls.type_defs_queue_) { if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) { Loading Loading @@ -193,12 +204,12 @@ using ::bluetooth::packet::parser::ChecksumTypeChecker; } for (const auto& packet_def : decls.packet_defs_queue_) { packet_def.second->GenParserDefinition(out_file); packet_def.second->GenParserDefinition(out_file, generate_fuzzing, generate_tests); out_file << "\n\n"; } for (const auto& packet_def : decls.packet_defs_queue_) { packet_def.second->GenBuilderDefinition(out_file); packet_def.second->GenBuilderDefinition(out_file, generate_fuzzing, generate_tests); out_file << "\n\n"; } Loading
system/gd/packet/parser/main.cc +18 −1 Original line number Diff line number Diff line Loading @@ -39,6 +39,8 @@ void yyset_in(FILE*, void*); bool generate_cpp_headers_one_file( const Declarations& decls, bool generate_fuzzing, bool generate_tests, const std::filesystem::path& input_file, const std::filesystem::path& include_dir, const std::filesystem::path& out_dir, Loading Loading @@ -134,6 +136,8 @@ int main(int argc, const char** argv) { // Number of shards per output pybind11 cc file size_t num_shards = 1; bool generate_rust = false; bool generate_fuzzing = false; bool generate_tests = false; std::queue<std::filesystem::path> input_files; const std::string arg_out = "--out="; Loading @@ -141,6 +145,8 @@ int main(int argc, const char** argv) { const std::string arg_namespace = "--root_namespace="; const std::string arg_num_shards = "--num_shards="; const std::string arg_rust = "--rust"; const std::string arg_fuzzing = "--fuzzing"; const std::string arg_testing = "--testing"; const std::string arg_source_root = "--source_root="; // Parse the source root first (if it exists) since it will be used for other Loading @@ -164,6 +170,10 @@ int main(int argc, const char** argv) { num_shards = std::stoul(arg.substr(arg_num_shards.size())); } else if (arg.find(arg_rust) == 0) { generate_rust = true; } else if (arg.find(arg_fuzzing) == 0) { generate_fuzzing = true; } else if (arg.find(arg_testing) == 0) { generate_tests = true; } else if (arg.find(arg_source_root) == 0) { // Do nothing (just don't treat it as input_files) } else { Loading Loading @@ -192,7 +202,14 @@ int main(int argc, const char** argv) { } } else { std::cout << "generating c++ and pybind11" << std::endl; if (!generate_cpp_headers_one_file(declarations, input_files.front(), include_dir, out_dir, root_namespace)) { if (!generate_cpp_headers_one_file( declarations, generate_fuzzing, generate_tests, input_files.front(), include_dir, out_dir, root_namespace)) { std::cerr << "Didn't generate cpp headers for " << input_files.front() << std::endl; return 3; } Loading
system/gd/packet/parser/packet_def.cc +26 −10 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ PacketField* PacketDef::GetNewField(const std::string&, ParseLocation) const { return nullptr; // Packets can't be fields } void PacketDef::GenParserDefinition(std::ostream& s) const { void PacketDef::GenParserDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const { s << "class " << name_ << "View"; if (parent_ != nullptr) { s << " : public " << parent_->name_ << "View {"; Loading @@ -49,7 +49,9 @@ void PacketDef::GenParserDefinition(std::ostream& s) const { s << "{ return " << name_ << "View(std::move(packet)); }"; } if (generate_fuzzing || generate_tests) { GenTestingParserFromBytes(s); } std::set<std::string> fixed_types = { FixedScalarField::kFieldType, Loading Loading @@ -347,7 +349,7 @@ void PacketDef::GenParserToString(std::ostream& s) const { s << "}\n"; } void PacketDef::GenBuilderDefinition(std::ostream& s) const { void PacketDef::GenBuilderDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const { s << "class " << name_ << "Builder"; if (parent_ != nullptr) { s << " : public " << parent_->name_ << "Builder"; Loading @@ -366,9 +368,11 @@ void PacketDef::GenBuilderDefinition(std::ostream& s) const { GenBuilderCreate(s); s << "\n"; if (generate_fuzzing || generate_tests) { GenTestingFromView(s); s << "\n"; } } GenSerialize(s); s << "\n"; Loading @@ -386,12 +390,21 @@ void PacketDef::GenBuilderDefinition(std::ostream& s) const { GenMembers(s); s << "};\n"; if (generate_tests) { GenTestDefine(s); s << "\n"; } if (generate_fuzzing || generate_tests) { GenReflectTestDefine(s); s << "\n"; } if (generate_fuzzing) { GenFuzzTestDefine(s); s << "\n"; } } void PacketDef::GenTestingFromView(std::ostream& s) const { s << "#if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING) || defined(FUZZ_TARGET)\n"; Loading Loading @@ -483,7 +496,7 @@ void PacketDef::GenTestDefine(std::ostream& s) const { s << "\n#endif"; } void PacketDef::GenFuzzTestDefine(std::ostream& s) const { void PacketDef::GenReflectTestDefine(std::ostream& s) const { s << "#if defined(PACKET_FUZZ_TESTING) || defined(PACKET_TESTING)\n"; s << "#define DEFINE_" << name_ << "ReflectionFuzzTest() "; s << "void Run" << name_ << "ReflectionFuzzTest(const uint8_t* data, size_t size) {"; Loading @@ -497,6 +510,9 @@ void PacketDef::GenFuzzTestDefine(std::ostream& s) const { s << "packet->Serialize(it);"; s << "}"; s << "\n#endif\n"; } void PacketDef::GenFuzzTestDefine(std::ostream& s) const { s << "#ifdef PACKET_FUZZ_TESTING\n"; s << "#define DEFINE_AND_REGISTER_" << name_ << "ReflectionFuzzTest(REGISTRY) "; s << "DEFINE_" << name_ << "ReflectionFuzzTest();"; Loading
system/gd/packet/parser/packet_def.h +3 −2 Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ class PacketDef : public ParentDef { PacketField* GetNewField(const std::string& name, ParseLocation loc) const; void GenParserDefinition(std::ostream& s) const; void GenParserDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const; void GenTestingParserFromBytes(std::ostream& s) const; Loading @@ -45,12 +45,13 @@ class PacketDef : public ParentDef { TypeDef::Type GetDefinitionType() const; void GenBuilderDefinition(std::ostream& s) const; void GenBuilderDefinition(std::ostream& s, bool generate_fuzzing, bool generate_tests) const; void GenBuilderDefinitionPybind11(std::ostream& s) const; void GenTestDefine(std::ostream& s) const; void GenReflectTestDefine(std::ostream& s) const; void GenFuzzTestDefine(std::ostream& s) const; FieldList GetParametersToValidate() const; Loading