Skip to content

Sema: Reexport SPI via Swift exported imports but not clang's #70566

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

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions include/swift/AST/ImportCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ class ImportSet final :
unsigned HasHeaderImportModule : 1;
unsigned NumTopLevelImports : 31;
unsigned NumTransitiveImports;
unsigned NumTransitiveSwiftOnlyImports;

ImportSet(bool hasHeaderImportModule,
ArrayRef<ImportedModule> topLevelImports,
ArrayRef<ImportedModule> transitiveImports);
ArrayRef<ImportedModule> transitiveImports,
ArrayRef<ImportedModule> transitiveSwiftOnlyImports);

ImportSet(const ImportSet &) = delete;
void operator=(const ImportSet &) = delete;
Expand All @@ -73,7 +75,8 @@ class ImportSet final :
ArrayRef<ImportedModule> topLevelImports);

size_t numTrailingObjects(OverloadToken<ImportedModule>) const {
return NumTopLevelImports + NumTransitiveImports;
return NumTopLevelImports + NumTransitiveImports +
NumTransitiveSwiftOnlyImports;
}

/// This is a bit of a hack to make module name lookup work properly.
Expand All @@ -94,6 +97,12 @@ class ImportSet final :
NumTransitiveImports};
}

ArrayRef<ImportedModule> getTransitiveSwiftOnlyImports() const {
return {getTrailingObjects<ImportedModule>() +
NumTopLevelImports + NumTransitiveImports,
NumTransitiveSwiftOnlyImports};
}

ArrayRef<ImportedModule> getAllImports() const {
return {getTrailingObjects<ImportedModule>(),
NumTopLevelImports + NumTransitiveImports};
Expand All @@ -115,6 +124,9 @@ class alignas(ImportedModule) ImportCache {
const ModuleDecl *,
const DeclContext *>,
ArrayRef<ImportPath::Access>> ShadowCache;
llvm::DenseMap<std::tuple<const ModuleDecl *,
const DeclContext *>,
bool> SwiftOnlyCache;

ImportPath::Access EmptyAccessPath;

Expand Down Expand Up @@ -142,6 +154,12 @@ class alignas(ImportedModule) ImportCache {
return !getAllVisibleAccessPaths(mod, dc).empty();
}

/// Is `mod` imported from `dc` via a purely Swift access path?
/// Always returns false if `dc` is a non-Swift module and only takes
/// into account re-exports declared from Swift modules for transitive imports.
bool isImportedByViaSwiftOnly(const ModuleDecl *mod,
const DeclContext *dc);

/// Returns all access paths in 'mod' that are visible from 'dc' if we
/// subtract imports of 'other'.
ArrayRef<ImportPath::Access>
Expand Down
4 changes: 0 additions & 4 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,6 @@ class ModuleDecl
/// Returns the associated clang module if one exists.
const clang::Module *findUnderlyingClangModule() const;

/// Does this module or the underlying clang module defines export_as with
/// a value corresponding to the \p other module?
bool isExportedAs(const ModuleDecl *other) const;

/// Returns a generator with the components of this module's full,
/// hierarchical name.
///
Expand Down
87 changes: 79 additions & 8 deletions lib/AST/ImportCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ using namespace namelookup;

ImportSet::ImportSet(bool hasHeaderImportModule,
ArrayRef<ImportedModule> topLevelImports,
ArrayRef<ImportedModule> transitiveImports)
ArrayRef<ImportedModule> transitiveImports,
ArrayRef<ImportedModule> transitiveSwiftOnlyImports)
: HasHeaderImportModule(hasHeaderImportModule),
NumTopLevelImports(topLevelImports.size()),
NumTransitiveImports(transitiveImports.size()) {
NumTransitiveImports(transitiveImports.size()),
NumTransitiveSwiftOnlyImports(transitiveSwiftOnlyImports.size()) {
auto buffer = getTrailingObjects<ImportedModule>();
std::uninitialized_copy(topLevelImports.begin(), topLevelImports.end(),
buffer);
std::uninitialized_copy(transitiveImports.begin(), transitiveImports.end(),
buffer + topLevelImports.size());
std::uninitialized_copy(transitiveSwiftOnlyImports.begin(),
transitiveSwiftOnlyImports.end(),
buffer + topLevelImports.size() +
transitiveImports.size());

#ifndef NDEBUG
llvm::SmallDenseSet<ImportedModule, 8> unique;
Expand All @@ -47,6 +53,15 @@ ImportSet::ImportSet(bool hasHeaderImportModule,
auto result = unique.insert(import).second;
assert(result && "Duplicate imports in import set");
}

unique.clear();
for (auto import : topLevelImports) {
unique.insert(import);
}
for (auto import : transitiveSwiftOnlyImports) {
auto result = unique.insert(import).second;
assert(result && "Duplicate imports in import set");
}
#endif
}

Expand Down Expand Up @@ -82,10 +97,14 @@ void ImportSet::dump() const {
}

static void collectExports(ImportedModule next,
SmallVectorImpl<ImportedModule> &stack) {
SmallVectorImpl<ImportedModule> &stack,
bool onlySwiftExports) {
SmallVector<ImportedModule, 4> exports;
next.importedModule->getImportedModulesForLookup(exports);
for (auto exported : exports) {
if (onlySwiftExports && exported.importedModule->isNonSwiftModule())
continue;

if (next.accessPath.empty())
stack.push_back(exported);
else if (exported.accessPath.empty()) {
Expand Down Expand Up @@ -135,7 +154,7 @@ ImportCache::getImportSet(ASTContext &ctx,

SmallVector<ImportedModule, 4> stack;
for (auto next : topLevelImports) {
collectExports(next, stack);
collectExports(next, stack, /*onlySwiftExports*/false);
}

while (!stack.empty()) {
Expand All @@ -148,7 +167,26 @@ ImportCache::getImportSet(ASTContext &ctx,
if (next.importedModule == headerImportModule)
hasHeaderImportModule = true;

collectExports(next, stack);
collectExports(next, stack, /*onlySwiftExports*/false);
}

// Now collect transitive imports through Swift reexported imports only.
SmallVector<ImportedModule, 4> transitiveSwiftOnlyImports;
visited.clear();
stack.clear();
for (auto next : topLevelImports) {
if (!visited.insert(next).second)
continue;
collectExports(next, stack, /*onlySwiftExports*/true);
}

while (!stack.empty()) {
auto next = stack.pop_back_val();
if (!visited.insert(next).second)
continue;

transitiveSwiftOnlyImports.push_back(next);
collectExports(next, stack, /*onlySwiftExports*/true);
}

// Find the insert position again, in case the above traversal invalidated
Expand All @@ -157,12 +195,15 @@ ImportCache::getImportSet(ASTContext &ctx,
if (ImportSet *result = ImportSets.FindNodeOrInsertPos(ID, InsertPos))
return *result;

size_t bytes = ImportSet::totalSizeToAlloc<ImportedModule>(topLevelImports.size() + transitiveImports.size());
size_t bytes = ImportSet::totalSizeToAlloc<ImportedModule>(
topLevelImports.size() + transitiveImports.size() +
transitiveSwiftOnlyImports.size());
void *mem = ctx.Allocate(bytes, alignof(ImportSet), AllocationArena::Permanent);

auto *result = new (mem) ImportSet(hasHeaderImportModule,
topLevelImports,
transitiveImports);
transitiveImports,
transitiveSwiftOnlyImports);
ImportSets.InsertNode(result, InsertPos);

return *result;
Expand Down Expand Up @@ -249,6 +290,36 @@ ImportCache::getAllVisibleAccessPaths(const ModuleDecl *mod,
return result;
}

bool ImportCache::isImportedByViaSwiftOnly(const ModuleDecl *mod,
const DeclContext *dc) {
dc = dc->getModuleScopeContext();
if (dc->getParentModule()->isNonSwiftModule())
return false;

auto &ctx = mod->getASTContext();
auto key = std::make_pair(mod, dc);
auto found = SwiftOnlyCache.find(key);
if (found != SwiftOnlyCache.end()) {
if (ctx.Stats)
++ctx.Stats->getFrontendCounters().ModuleVisibilityCacheHit;
return found->second;
}

if (ctx.Stats)
++ctx.Stats->getFrontendCounters().ModuleVisibilityCacheMiss;

bool result = false;
for (auto next : getImportSet(dc).getTransitiveSwiftOnlyImports()) {
if (next.importedModule == mod) {
result = true;
break;
}
}

SwiftOnlyCache[key] = result;
return result;
}

ArrayRef<ImportPath::Access>
ImportCache::getAllAccessPathsNotShadowedBy(const ModuleDecl *mod,
const ModuleDecl *other,
Expand Down Expand Up @@ -307,7 +378,7 @@ ImportCache::getAllAccessPathsNotShadowedBy(const ModuleDecl *mod,
accessPaths.push_back(next.accessPath);
}

collectExports(next, stack);
collectExports(next, stack, /*onlySwiftExports*/false);
}

auto result = allocateArray(ctx, accessPaths);
Expand Down
12 changes: 2 additions & 10 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2649,14 +2649,6 @@ const clang::Module *ModuleDecl::findUnderlyingClangModule() const {
return nullptr;
}

bool ModuleDecl::isExportedAs(const ModuleDecl *other) const {
auto clangModule = findUnderlyingClangModule();
if (!clangModule)
return false;

return other->getRealName().str() == clangModule->ExportAsModule;
}

void ModuleDecl::collectBasicSourceFileInfo(
llvm::function_ref<void(const BasicSourceFileInfo &)> callback) const {
for (const FileUnit *fileUnit : getFiles()) {
Expand Down Expand Up @@ -3386,8 +3378,8 @@ void SourceFile::lookupImportedSPIGroups(
for (auto &import : *Imports) {
if (import.options.contains(ImportFlags::SPIAccessControl) &&
(importedModule == import.module.importedModule ||
(imports.isImportedBy(importedModule, import.module.importedModule) &&
importedModule->isExportedAs(import.module.importedModule)))) {
imports.isImportedByViaSwiftOnly(importedModule,
import.module.importedModule))) {
spiGroups.insert(import.spiGroups.begin(), import.spiGroups.end());
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Serialization/SerializedModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,8 @@ void SerializedASTFile::lookupImportedSPIGroups(

if (dep.Import->importedModule == importedModule ||
(imports.isImportedBy(importedModule, dep.Import->importedModule) &&
importedModule->isExportedAs(dep.Import->importedModule))) {
imports.isImportedByViaSwiftOnly(importedModule,
dep.Import->importedModule))) {
spiGroups.insert(dep.spiGroups.begin(), dep.spiGroups.end());
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/SPI/accidental-reexport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// Guard that we don't reexport the SPIs accidentally through the common
/// reexported imports between clang modules.

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -emit-module %t/Overlayed.swift -I %t -o %t
// RUN: %target-swift-frontend -typecheck -verify %t/Client.swift -I %t
// RUN: %target-swift-frontend -typecheck -verify %t/LowerClient.swift -I %t

//--- module.modulemap
module Overlayed {
header "Overlayed.h"
}

module Middle {
header "Middle.h"
export *
}

module LowerMiddle {
header "LowerMiddle.h"
export *
}

//--- Overlayed.h
//--- Overlayed.swift
@_exported import Overlayed

public func funcPublic() {}

@_spi(X)
public func funcSPI() {}

//--- Middle.h
#include <Overlayed.h>

//--- LowerMiddle.h
#include <Middle.h>

//--- Client.swift
@_spi(X) import Middle

funcPublic()
funcSPI() // expected-error {{cannot find 'funcSPI' in scope}}

//--- LowerClient.swift
@_spi(X) import LowerMiddle

funcPublic()
funcSPI() // expected-error {{cannot find 'funcSPI' in scope}}
8 changes: 6 additions & 2 deletions test/SPI/reexported-spi-groups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
// RUN: %t/Client_FileA.swift %t/Client_FileB.swift\
// RUN: -swift-version 5 -I %t -verify

/// Test that SPIs aren't avaible from a reexport without export_as
/// SPIs are now reexported by any `@_exported` from Swift modules, no matter
/// if `export_as` is present or not.
// RUN: %target-swift-frontend -typecheck \
// RUN: %t/NonExportedAsClient.swift \
// RUN: -swift-version 5 -I %t -verify
Expand Down Expand Up @@ -92,6 +93,8 @@ public func exportedPublicFunc() {}

@_spi(X) public struct ExportedSpiType {}

@_spi(O) public func exportedSpiFuncOtherGroup() {}

//--- Exporter.swift

@_exported import Exported
Expand Down Expand Up @@ -130,6 +133,7 @@ public func clientA() {
exportedPublicFunc()
exportedSpiFunc()
exporterSpiFunc()
exportedSpiFuncOtherGroup() // expected-error {{cannot find 'exportedSpiFuncOtherGroup' in scope}}
}

@inlinable
Expand Down Expand Up @@ -159,7 +163,7 @@ public func clientB() {

public func client() {
exportedPublicFunc()
exportedSpiFunc() // expected-error {{cannot find 'exportedSpiFunc' in scope}}
exportedSpiFunc()
exporterSpiFunc()
}

Expand Down