1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TESTING_LIBFUZZER_RESEARCH_DOMATOLPM_DOMATOLPM_H_ 6 #define TESTING_LIBFUZZER_RESEARCH_DOMATOLPM_DOMATOLPM_H_ 7 8 #include <string> 9 #include <string_view> 10 #include <unordered_map> 11 #include <unordered_set> 12 13 #include "base/strings/string_number_conversions.h" 14 15 namespace domatolpm { 16 17 // SampleBuilder is used by the internal runtime DomatoLPM generated code to 18 // build the current sample. 19 // This is a very dumb interface, but allows for modularity and extensibility 20 // without changing the generated code. 21 class SampleBuilder { 22 public: 23 virtual ~SampleBuilder() = default; 24 virtual std::string_view view() = 0; 25 virtual void append(std::string_view) = 0; 26 }; 27 28 // TextSampleBuilder builds a sample into a string. 29 class TextSampleBuilder : public SampleBuilder { 30 public: 31 TextSampleBuilder() = default; 32 ~TextSampleBuilder() override = default; 33 std::string_view view() override; 34 void append(std::string_view v) override; 35 36 private: 37 std::string data_; 38 }; 39 40 // Context is used by the internal runtime DomatoLPM generated code to interact 41 // with the current context. It is used to retrieve the current sample builder 42 // and interact with existing variables. 43 class Context { 44 public: 45 Context() = default; 46 SampleBuilder* GetBuilder(); 47 bool HasVar(const std::string& var_type); 48 void SetVar(const std::string& var_type, const std::string& var_name); 49 std::string_view GetVar(const std::string& var_type, int32_t id); 50 std::string GetNewID(); 51 52 private: 53 TextSampleBuilder builder_; 54 std::unordered_map<std::string, std::unordered_set<std::string>> vars_; 55 uint64_t id_ = 0; 56 }; 57 58 } // namespace domatolpm 59 60 #endif // TESTING_LIBFUZZER_RESEARCH_DOMATOLPM_DOMATOLPM_H_ 61