-
Notifications
You must be signed in to change notification settings - Fork 871
Fix type updating for indirect call effects #8874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cb25670
642bd0f
fe3bda5
e7b1c1e
c811b0d
05bde0c
0a14948
6e2bdfd
55ea574
4938e09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,73 @@ | |
|
|
||
| namespace wasm { | ||
|
|
||
| namespace { | ||
|
|
||
| // Copy over `indirectCallEffects` when types are rewritten. When rewriting a | ||
| // type A to type B, A will lose its effects and B will gain A's effects. If the | ||
| // destination type already existed in the program but had no effects recorded, | ||
| // we must assume the worst (e.g. there may have been an import of type B) and | ||
| // clear its entry in the effects map. OTOH if the destination type is a brand | ||
| // new type, then it can only have effects from the source type. If the source | ||
| // type didn't exist, it must have been created by a pass sometime after | ||
| // GlobalEffects last ran. We again assume that effects are unknown. | ||
| std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>> | ||
| updateIndirectCallEffects( | ||
| const Module& wasm, | ||
| const InsertOrderedMap<HeapType, ModuleUtils::HeapTypeInfo>& typeInfo, | ||
| const GlobalTypeRewriter::TypeMap& oldToNewTypes) { | ||
|
|
||
| std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>> | ||
| newTypeEffects; | ||
|
|
||
| std::unordered_set<HeapType> unknownNewTypes; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment could clarify what "unknown" means in this variable name. |
||
|
|
||
| std::unordered_set<HeapType> allOldTypes; | ||
| for (auto [oldType, _] : typeInfo) { | ||
| allOldTypes.insert(oldType); | ||
| } | ||
| for (auto& [oldType, effects] : wasm.indirectCallEffects) { | ||
|
stevenfontanella marked this conversation as resolved.
|
||
| allOldTypes.insert(oldType); | ||
| } | ||
|
|
||
| for (auto oldType : allOldTypes) { | ||
| HeapType newType; | ||
| { | ||
| auto it = oldToNewTypes.find(oldType); | ||
| if (it == oldToNewTypes.end()) { | ||
| newType = oldType; | ||
| } else { | ||
| newType = it->second; | ||
| } | ||
| } | ||
|
kripken marked this conversation as resolved.
|
||
|
|
||
| if (unknownNewTypes.contains(newType)) { | ||
| continue; | ||
| } | ||
|
|
||
| const std::shared_ptr<const EffectAnalyzer>* oldEffects = | ||
| find_or_null(wasm.indirectCallEffects, oldType); | ||
|
|
||
| if (!oldEffects) { | ||
| // oldType has no entry, which means its effects are explicitly unknown. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the comment above says that if it is a new type, then not having an entry does not mean we must assume the worst? Perhaps I am not following what "explicitly unknown" means - can you clarify that in the comment?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I remember we discussed that term here in the PR, but (1) I don't remember the details 😄 and (2) it is good to define terms in the code, so it reads independently from the PR) |
||
| unknownNewTypes.insert(newType); | ||
| newTypeEffects.erase(newType); | ||
| continue; | ||
| } | ||
|
|
||
| auto [it, inserted] = newTypeEffects.emplace(newType, *oldEffects); | ||
| if (!inserted) { | ||
| auto merged = std::make_shared<EffectAnalyzer>(*it->second); | ||
| merged->mergeIn(**oldEffects); | ||
| it->second = std::move(merged); | ||
| } | ||
|
stevenfontanella marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return newTypeEffects; | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| GlobalTypeRewriter::GlobalTypeRewriter(Module& wasm, WorldMode worldMode) | ||
| : wasm(wasm), publicGroups(wasm.features) { | ||
| // Find the heap types that are not publicly observable. Even in a closed | ||
|
|
@@ -212,6 +279,11 @@ GlobalTypeRewriter::rebuildTypes(std::vector<HeapType> types) { | |
| } | ||
|
|
||
| void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) { | ||
| if (!wasm.indirectCallEffects.empty()) { | ||
| wasm.indirectCallEffects = | ||
| updateIndirectCallEffects(wasm, typeInfo, oldToNewTypes); | ||
| } | ||
|
|
||
| // Replace all the old types in the module with the new ones. | ||
| struct CodeUpdater | ||
| : public WalkerPass< | ||
|
|
@@ -325,35 +397,6 @@ void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) { | |
| for (auto& tag : wasm.tags) { | ||
| tag->type = updater.getNew(tag->type); | ||
| } | ||
|
|
||
| // Update indirect call effects per type. | ||
| // When A is rewritten to B, B inherits the effects of A and A loses its | ||
| // effects. | ||
| std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>> | ||
| newTypeEffects; | ||
|
|
||
| for (const auto& [oldType, newType] : oldToNewTypes) { | ||
| std::shared_ptr<const EffectAnalyzer>* oldEffects = | ||
| find_or_null(wasm.indirectCallEffects, oldType); | ||
| std::shared_ptr<const EffectAnalyzer>* targetEffects = | ||
| find_or_null(wasm.indirectCallEffects, newType); | ||
|
|
||
| if (!targetEffects) { | ||
| // Nothing to update, we already know nothing and assume all effects. | ||
| continue; | ||
| } | ||
|
|
||
| if (!oldEffects) { | ||
| targetEffects->reset(); | ||
| continue; | ||
| } | ||
|
|
||
| auto merged = std::make_shared<EffectAnalyzer>(**targetEffects); | ||
| merged->mergeIn(**oldEffects); | ||
| *targetEffects = std::move(merged); | ||
| } | ||
|
|
||
| wasm.indirectCallEffects = std::move(newTypeEffects); | ||
| } | ||
|
|
||
| void GlobalTypeRewriter::mapTypeNamesAndIndices(const TypeMap& oldToNewTypes) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright 2026 WebAssembly Community Group participants | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| // Matchers for EffectAnalyzer | ||
|
|
||
| #ifndef WASM_TEST_GTEST_MATCHERS_EFFECTS_H | ||
| #define WASM_TEST_GTEST_MATCHERS_EFFECTS_H | ||
|
|
||
| #include "gmock/gmock.h" | ||
|
|
||
| namespace wasm { | ||
|
|
||
| MATCHER(BranchesOut, "") { return arg->branchesOut; } | ||
| MATCHER(Calls, "") { return arg->calls; } | ||
| MATCHER(ReadsMemory, "") { return arg->readsMemory; } | ||
| MATCHER(WritesMemory, "") { return arg->writesMemory; } | ||
| MATCHER(ReadsSharedMemory, "") { return arg->readsSharedMemory; } | ||
| MATCHER(WritesSharedMemory, "") { return arg->writesSharedMemory; } | ||
| MATCHER(ReadsTable, "") { return arg->readsTable; } | ||
| MATCHER(WritesTable, "") { return arg->writesTable; } | ||
| MATCHER(ReadsMutableStruct, "") { return arg->readsMutableStruct; } | ||
| MATCHER(WritesStruct, "") { return arg->writesStruct; } | ||
| MATCHER(ReadsSharedMutableStruct, "") { return arg->readsSharedMutableStruct; } | ||
| MATCHER(WritesSharedStruct, "") { return arg->writesSharedStruct; } | ||
| MATCHER(ReadsMutableArray, "") { return arg->readsMutableArray; } | ||
| MATCHER(WritesArray, "") { return arg->writesArray; } | ||
| MATCHER(ReadsSharedMutableArray, "") { return arg->readsSharedMutableArray; } | ||
| MATCHER(WritesSharedArray, "") { return arg->writesSharedArray; } | ||
| MATCHER(Traps, "") { return arg->trap; } | ||
| MATCHER(ImplicitTraps, "") { return arg->implicitTrap; } | ||
| MATCHER(Throws, "") { return arg->throws_; } | ||
| MATCHER(DanglingPop, "") { return arg->danglingPop; } | ||
| MATCHER(MayNotReturn, "") { return arg->mayNotReturn; } | ||
| MATCHER(HasReturnCallThrow, "") { return arg->hasReturnCallThrow; } | ||
|
stevenfontanella marked this conversation as resolved.
|
||
|
|
||
| } // namespace wasm | ||
|
|
||
| #endif // WASM_TEST_GTEST_MATCHERS_EFFECTS_H | ||
Uh oh!
There was an error while loading. Please reload this page.