|
| 1 | +/* |
| 2 | + * Copyright 2021 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "gtest/gtest.h" |
| 18 | + |
| 19 | +#include "ir/effects.h" |
| 20 | +#include "ir/type-updating.h" |
| 21 | +#include "parser/wat-parser.h" |
| 22 | +#include "support/result.h" |
| 23 | +#include "wasm.h" |
| 24 | + |
| 25 | +using namespace wasm; |
| 26 | +using namespace testing; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +class IndirectCallEffectsTest : public Test { |
| 31 | +protected: |
| 32 | + Module wasm; |
| 33 | + PassOptions options; |
| 34 | + |
| 35 | + void SetUp() override { |
| 36 | + auto moduleText = R"wasm( |
| 37 | + (module |
| 38 | + (rec |
| 39 | + (type $A (func)) |
| 40 | + (type $B (func)) |
| 41 | + (type $C (func)) |
| 42 | + (type $D (func)) |
| 43 | + ) |
| 44 | + ) |
| 45 | + )wasm"; |
| 46 | + |
| 47 | + if (auto err = wasm::WATParser::parseModule(wasm, moduleText).getErr()) { |
| 48 | + Fatal() << err->msg; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + std::unordered_map<std::string, std::shared_ptr<const EffectAnalyzer>> |
| 53 | + updateEffects( |
| 54 | + const std::unordered_map<std::string, |
| 55 | + std::shared_ptr<const EffectAnalyzer>>& oldEffects, |
| 56 | + const std::unordered_map<std::string, std::string>& typeMap) { |
| 57 | + |
| 58 | + std::unordered_map<Name, HeapType> types; |
| 59 | + for (const auto& [type, name] : wasm.typeNames) { |
| 60 | + types[name.name] = type; |
| 61 | + } |
| 62 | + |
| 63 | + for (const auto& [name, effects] : oldEffects) { |
| 64 | + wasm.indirectCallEffects[types.at(name)] = effects; |
| 65 | + } |
| 66 | + |
| 67 | + GlobalTypeRewriter::TypeMap map; |
| 68 | + for (const auto& [oldName, newName] : typeMap) { |
| 69 | + map[types.at(oldName)] = types.at(newName); |
| 70 | + } |
| 71 | + |
| 72 | + GlobalTypeRewriter rewriter(wasm, WorldMode::Open); |
| 73 | + rewriter.mapTypes(map); |
| 74 | + |
| 75 | + std::unordered_map<std::string, std::shared_ptr<const EffectAnalyzer>> |
| 76 | + result; |
| 77 | + for (const auto& [type, effects] : wasm.indirectCallEffects) { |
| 78 | + auto name = wasm.typeNames.at(type).name.toString(); |
| 79 | + result[name] = effects; |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +} // anonymous namespace |
| 86 | + |
| 87 | +TEST_F(IndirectCallEffectsTest, SrcHasNoEffects) { |
| 88 | + auto effectsB = std::make_shared<EffectAnalyzer>(options, wasm); |
| 89 | + effectsB->writesMemory = true; |
| 90 | + |
| 91 | + auto merged = |
| 92 | + updateEffects(/*oldEffects=*/{{"B", effectsB}}, /*typeMap=*/{{"A", "B"}}); |
| 93 | + |
| 94 | + EXPECT_FALSE(merged.contains("A")); |
| 95 | + ASSERT_TRUE(merged.contains("B")); |
| 96 | + |
| 97 | + // Pointer comparison |
| 98 | + EXPECT_EQ(merged.at("B"), effectsB); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_F(IndirectCallEffectsTest, DestHasNoEffects) { |
| 102 | + auto effectsA = std::make_shared<EffectAnalyzer>(options, wasm); |
| 103 | + effectsA->calls = true; |
| 104 | + |
| 105 | + auto merged = |
| 106 | + updateEffects(/*oldEffects=*/{{"A", effectsA}}, /*typeMap=*/{{"A", "B"}}); |
| 107 | + |
| 108 | + EXPECT_FALSE(merged.contains("A")); |
| 109 | + ASSERT_TRUE(merged.contains("B")); |
| 110 | + |
| 111 | + // Pointer comparison |
| 112 | + EXPECT_EQ(merged.at("B"), effectsA); |
| 113 | +} |
| 114 | + |
| 115 | +TEST_F(IndirectCallEffectsTest, BothHaveNoEffects) { |
| 116 | + auto merged = updateEffects(/*oldEffects=*/{}, /*typeMap=*/{{"A", "B"}}); |
| 117 | + |
| 118 | + EXPECT_FALSE(merged.contains("A")); |
| 119 | + EXPECT_FALSE(merged.contains("B")); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_F(IndirectCallEffectsTest, BothHaveEffects) { |
| 123 | + auto effectsA = std::make_shared<EffectAnalyzer>(options, wasm); |
| 124 | + effectsA->calls = true; |
| 125 | + auto effectsB = std::make_shared<EffectAnalyzer>(options, wasm); |
| 126 | + effectsB->writesMemory = true; |
| 127 | + |
| 128 | + auto merged = updateEffects(/*oldEffects=*/{{"A", effectsA}, {"B", effectsB}}, |
| 129 | + /*typeMap=*/{{"A", "B"}}); |
| 130 | + |
| 131 | + EXPECT_FALSE(merged.contains("A")); |
| 132 | + ASSERT_TRUE(merged.contains("B")); |
| 133 | + EXPECT_TRUE(merged.at("B")->calls); |
| 134 | + EXPECT_TRUE(merged.at("B")->writesMemory); |
| 135 | +} |
| 136 | + |
| 137 | +TEST_F(IndirectCallEffectsTest, SrcHasNullptrEffects) { |
| 138 | + auto effectsB = std::make_shared<EffectAnalyzer>(options, wasm); |
| 139 | + effectsB->writesMemory = true; |
| 140 | + |
| 141 | + auto merged = updateEffects(/*oldEffects=*/{{"A", nullptr}, {"B", effectsB}}, |
| 142 | + /*typeMap=*/{{"A", "B"}}); |
| 143 | + |
| 144 | + EXPECT_FALSE(merged.contains("A")); |
| 145 | + ASSERT_TRUE(merged.contains("B")); |
| 146 | + EXPECT_EQ(merged.at("B"), nullptr); |
| 147 | +} |
| 148 | + |
| 149 | +TEST_F(IndirectCallEffectsTest, DestHasNullptrEffects) { |
| 150 | + auto effectsA = std::make_shared<EffectAnalyzer>(options, wasm); |
| 151 | + effectsA->calls = true; |
| 152 | + |
| 153 | + auto merged = updateEffects(/*oldEffects=*/{{"A", effectsA}, {"B", nullptr}}, |
| 154 | + /*typeMap=*/{{"A", "B"}}); |
| 155 | + |
| 156 | + EXPECT_FALSE(merged.contains("A")); |
| 157 | + ASSERT_TRUE(merged.contains("B")); |
| 158 | + EXPECT_EQ(merged.at("B"), nullptr); |
| 159 | +} |
| 160 | + |
| 161 | +TEST_F(IndirectCallEffectsTest, MergeThreeTypes) { |
| 162 | + auto effectsA = std::make_shared<EffectAnalyzer>(options, wasm); |
| 163 | + effectsA->calls = true; |
| 164 | + auto effectsB = std::make_shared<EffectAnalyzer>(options, wasm); |
| 165 | + effectsB->writesMemory = true; |
| 166 | + auto effectsC = std::make_shared<EffectAnalyzer>(options, wasm); |
| 167 | + effectsC->throws_ = true; |
| 168 | + |
| 169 | + auto merged = updateEffects( |
| 170 | + /*oldEffects=*/{{"A", effectsA}, {"B", effectsB}, {"C", effectsC}}, |
| 171 | + /*typeMap=*/{{"A", "D"}, {"B", "D"}, {"C", "D"}}); |
| 172 | + |
| 173 | + EXPECT_FALSE(merged.contains("A")); |
| 174 | + EXPECT_FALSE(merged.contains("B")); |
| 175 | + EXPECT_FALSE(merged.contains("C")); |
| 176 | + ASSERT_TRUE(merged.contains("D")); |
| 177 | + EXPECT_TRUE(merged.at("D")->calls); |
| 178 | + EXPECT_TRUE(merged.at("D")->writesMemory); |
| 179 | + EXPECT_TRUE(merged.at("D")->throws_); |
| 180 | +} |
0 commit comments