// Copyright 2021 The Tint Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef FUZZERS_TRANSFORM_BUILDER_H_ #define FUZZERS_TRANSFORM_BUILDER_H_ #include #include #include "include/tint/tint.h" #include "fuzzers/data_builder.h" namespace tint { namespace fuzzers { /// Fuzzer utility class to build inputs for transforms and setup the transform /// manager. class TransformBuilder { public: /// @brief Initialize the data source using a data buffer as a seed /// @param data - pointer to a data buffer to use as a seed /// @param size - size of data buffer explicit TransformBuilder(const uint8_t* data, size_t size) : builder_(data, size) {} ~TransformBuilder() {} transform::Manager* manager() { return &manager_; } transform::DataMap* data_map() { return &data_map_; } DataBuilder* builder() { return &builder_; } /// Adds a transform and needed data to |manager_| and |data_map_|. /// @tparam T - A class that inherits from transform::Transform and has an /// explicit specialization in AddTransformImpl. template void AddTransform() { static_assert(std::is_base_of::value, "T is not a transform::Transform"); AddTransformImpl::impl(this); } /// Helper that invokes Add*Transform for all of the platform independent /// passes. void AddPlatformIndependentPasses() { AddTransform(); AddTransform(); AddTransform(); AddTransform(); AddTransform(); AddTransform(); } private: DataBuilder builder_; transform::Manager manager_; transform::DataMap data_map_; /// Implementation of AddTransform, specialized for each transform that is /// implemented. Default implementation intentionally deleted to cause compile /// error if unimplemented type passed in. /// @tparam T - A fuzzer transform template struct AddTransformImpl; /// Implementation of AddTransform for transform::Robustness template <> struct AddTransformImpl { /// Add instance of transform::Robustness to TransformBuilder /// @param tb - TransformBuilder to add transform to static void impl(TransformBuilder* tb) { tb->manager()->Add(); } }; /// Implementation of AddTransform for transform::FirstIndexOffset template <> struct AddTransformImpl { /// Add instance of transform::FirstIndexOffset to TransformBuilder /// @param tb - TransformBuilder to add transform to static void impl(TransformBuilder* tb) { struct Config { uint32_t group; uint32_t binding; }; Config config = tb->builder()->build(); tb->data_map()->Add( config.binding, config.group); tb->manager()->Add(); } }; /// Implementation of AddTransform for transform::BindingRemapper template <> struct AddTransformImpl { /// Add instance of transform::BindingRemapper to TransformBuilder /// @param tb - TransformBuilder to add transform to static void impl(TransformBuilder* tb) { struct Config { uint8_t old_group; uint8_t old_binding; uint8_t new_group; uint8_t new_binding; ast::Access new_access; }; std::vector configs = tb->builder()->vector(); transform::BindingRemapper::BindingPoints binding_points; transform::BindingRemapper::AccessControls accesses; for (const auto& config : configs) { binding_points[{config.old_binding, config.old_group}] = { config.new_binding, config.new_group}; accesses[{config.old_binding, config.old_group}] = config.new_access; } tb->data_map()->Add( binding_points, accesses, tb->builder()->build()); tb->manager()->Add(); } }; /// Implementation of AddTransform for transform::Renamer template <> struct AddTransformImpl { /// Add instance of transform::Renamer to TransformBuilder /// @param tb - TransformBuilder to add transform to static void impl(TransformBuilder* tb) { tb->manager()->Add(); } }; /// Implementation of AddTransform for transform::SingleEntryPoint template <> struct AddTransformImpl { /// Add instance of transform::SingleEntryPoint to TransformBuilder /// @param tb - TransformBuilder to add transform to static void impl(TransformBuilder* tb) { auto input = tb->builder()->build(); transform::SingleEntryPoint::Config cfg(input); tb->data_map()->Add(cfg); tb->manager()->Add(); } }; // struct AddTransformImpl /// Implementation of AddTransform for transform::VertexPulling template <> struct AddTransformImpl { /// Add instance of transform::VertexPulling to TransformBuilder /// @param tb - TransformBuilder to add transform to static void impl(TransformBuilder* tb) { transform::VertexPulling::Config cfg; cfg.entry_point_name = tb->builder()->build(); cfg.vertex_state = tb->builder()->vector( GenerateVertexBufferLayoutDescriptor); cfg.pulling_group = tb->builder()->build(); tb->data_map()->Add(cfg); tb->manager()->Add(); } private: /// Generate an instance of transform::VertexAttributeDescriptor /// @param b - DataBuilder to use static transform::VertexAttributeDescriptor GenerateVertexAttributeDescriptor(DataBuilder* b) { transform::VertexAttributeDescriptor desc{}; desc.format = b->enum_class( static_cast(transform::VertexFormat::kLastEntry) + 1); desc.offset = b->build(); desc.shader_location = b->build(); return desc; } /// Generate an instance of VertexBufferLayoutDescriptor /// @param b - DataBuilder to use static transform::VertexBufferLayoutDescriptor GenerateVertexBufferLayoutDescriptor(DataBuilder* b) { transform::VertexBufferLayoutDescriptor desc; desc.array_stride = b->build(); desc.step_mode = b->enum_class( static_cast(transform::VertexStepMode::kLastEntry) + 1); desc.attributes = b->vector( GenerateVertexAttributeDescriptor); return desc; } }; }; // class TransformBuilder } // namespace fuzzers } // namespace tint #endif // FUZZERS_TRANSFORM_BUILDER_H_