Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7428ca87 authored by Henri Chataing's avatar Henri Chataing Committed by Automerger Merge Worker
Browse files

Merge "packetgen: Add options --fuzzing, --testing to customize code generation" am: b632bc98

parents 527e6694 b632bc98
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -596,7 +596,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",
+20 −9
Original line number Diff line number Diff line
@@ -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,
@@ -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"
@@ -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 ||
@@ -120,6 +127,7 @@ bool generate_cpp_headers_one_file(
      ((CustomFieldDef*)c.second)->GenUsing(out_file);
    }
  }

  out_file <<
      R"(

@@ -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) {
@@ -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";
  }

+18 −1
Original line number Diff line number Diff line
@@ -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,
@@ -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=";
@@ -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
@@ -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 {
@@ -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;
      }
+26 −10
Original line number Diff line number Diff line
@@ -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 {";
@@ -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,
@@ -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";
@@ -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";
@@ -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";
@@ -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) {";
@@ -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();";
+3 −2
Original line number Diff line number Diff line
@@ -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;

@@ -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