From 0c22b1aaea8b164afd0b57f6eb820ea74e5315cf Mon Sep 17 00:00:00 2001 From: Shiyu Liu Date: Tue, 10 Aug 2021 16:43:15 +0000 Subject: [PATCH] Update node_id_map & FindMutators structure Added function in node_id_map to check a given id is valid and fresh. Currently, the structure of FindMutators declares node_id_map as const, which causes issues when we want to call `GetFreshId` from the argument that is passed by reference. A simple work around is to pass a non-const node_id_map as argument directly. That way `GetFreshId` function in node_id_map can continue to be non-const and conveniently update next fresh id whenever a fresh id has been taken. Change-Id: Ia7e1d247cf92dfefd2ef7e7c1b4bf32363d9ce3f Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/61100 Reviewed-by: Paul Thomson Kokoro: Kokoro Commit-Queue: Paul Thomson --- fuzzers/tint_ast_fuzzer/mutation_finder.h | 2 +- .../mutation_finders/replace_identifiers.cc | 6 +++--- .../mutation_finders/replace_identifiers.h | 2 +- fuzzers/tint_ast_fuzzer/mutator.cc | 2 +- fuzzers/tint_ast_fuzzer/node_id_map.cc | 8 ++++++-- fuzzers/tint_ast_fuzzer/node_id_map.h | 7 +++++++ 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/fuzzers/tint_ast_fuzzer/mutation_finder.h b/fuzzers/tint_ast_fuzzer/mutation_finder.h index 546dce440a..1dd8732d30 100644 --- a/fuzzers/tint_ast_fuzzer/mutation_finder.h +++ b/fuzzers/tint_ast_fuzzer/mutation_finder.h @@ -54,7 +54,7 @@ class MutationFinder { /// @return all the found mutations. virtual MutationList FindMutations( const tint::Program& program, - const NodeIdMap& node_id_map, + NodeIdMap* node_id_map, ProbabilityContext* probability_context) const = 0; /// @brief Compute a probability of applying a single mutation, returned by diff --git a/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.cc b/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.cc index 7eb3f6d9f4..aa23dab3e1 100644 --- a/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.cc +++ b/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.cc @@ -29,7 +29,7 @@ namespace ast_fuzzer { MutationList MutationFinderReplaceIdentifiers::FindMutations( const tint::Program& program, - const NodeIdMap& node_id_map, + NodeIdMap* node_id_map, ProbabilityContext* probability_context) const { MutationList result; @@ -61,8 +61,8 @@ MutationList MutationFinderReplaceIdentifiers::FindMutations( candidate_variables)]; result.push_back(std::make_unique( - node_id_map.GetId(user->Declaration()), - node_id_map.GetId(replacement->Declaration()))); + node_id_map->GetId(user->Declaration()), + node_id_map->GetId(replacement->Declaration()))); } } diff --git a/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.h b/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.h index 957abf290e..70a0b104b1 100644 --- a/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.h +++ b/fuzzers/tint_ast_fuzzer/mutation_finders/replace_identifiers.h @@ -29,7 +29,7 @@ class MutationFinderReplaceIdentifiers : public MutationFinder { public: MutationList FindMutations( const tint::Program& program, - const NodeIdMap& node_id_map, + NodeIdMap* node_id_map, ProbabilityContext* probability_context) const override; uint32_t GetChanceOfApplyingMutation( ProbabilityContext* probability_context) const override; diff --git a/fuzzers/tint_ast_fuzzer/mutator.cc b/fuzzers/tint_ast_fuzzer/mutator.cc index f8ebb7cecd..d1b5efede8 100644 --- a/fuzzers/tint_ast_fuzzer/mutator.cc +++ b/fuzzers/tint_ast_fuzzer/mutator.cc @@ -141,7 +141,7 @@ tint::Program Mutate(tint::Program program, // Get all applicable mutations from some mutation finder. const auto& mutation_finder = finders[probability_context->GetRandomIndex(finders)]; - auto mutations = mutation_finder->FindMutations(program, node_id_map, + auto mutations = mutation_finder->FindMutations(program, &node_id_map, probability_context); const auto old_applied_mutations = applied_mutations; diff --git a/fuzzers/tint_ast_fuzzer/node_id_map.cc b/fuzzers/tint_ast_fuzzer/node_id_map.cc index bdb22ebc2a..7987fcf868 100644 --- a/fuzzers/tint_ast_fuzzer/node_id_map.cc +++ b/fuzzers/tint_ast_fuzzer/node_id_map.cc @@ -40,17 +40,21 @@ const ast::Node* NodeIdMap::GetNode(IdType id) const { void NodeIdMap::Add(const ast::Node* node, IdType id) { assert(!node_to_id_.count(node) && "The node already exists in the map"); - assert(!id_to_node_.count(id) && "Id already exists in the map"); + assert(IdIsFreshAndValid(id) && "Id already exists in the map or Id is zero"); assert(node && "`node` can't be a nullptr"); - assert(id && "`id` can't be equal to 0"); node_to_id_[node] = id; id_to_node_[id] = node; + if (id >= fresh_id_) { fresh_id_ = id + 1; } } +bool NodeIdMap::IdIsFreshAndValid(IdType id) { + return id && !id_to_node_.count(id); +} + NodeIdMap::IdType NodeIdMap::TakeFreshId() { assert(fresh_id_ != 0 && "`NodeIdMap` id has overflowed"); return fresh_id_++; diff --git a/fuzzers/tint_ast_fuzzer/node_id_map.h b/fuzzers/tint_ast_fuzzer/node_id_map.h index e6e2e3dc4a..3105e4d149 100644 --- a/fuzzers/tint_ast_fuzzer/node_id_map.h +++ b/fuzzers/tint_ast_fuzzer/node_id_map.h @@ -67,6 +67,13 @@ class NodeIdMap { /// @param id - may not be 0 and can't be present in this map. void Add(const ast::Node* node, IdType id); + /// @brief Returns whether the id is fresh by checking if it exists in + /// the id map and the id is not 0. + /// @param id - an id that is used to check in the map. + /// @return true the given id is fresh and valid (non-zero). + /// @return false otherwise. + bool IdIsFreshAndValid(IdType id); + /// @brief Returns an id that is guaranteed to be unoccupied in this map. /// /// This will effectively increase the counter. This means that two