Skip to content

[Module] Use stricter module hash #75986

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 1 commit into from
Sep 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
33 changes: 24 additions & 9 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2071,8 +2071,24 @@ StringRef InterfaceSubContextDelegateImpl::computeCachedOutputPath(
std::string
InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath,
StringRef sdkPath) {
auto normalizedTargetTriple =
getTargetSpecificModuleTriple(genericSubInvocation.getLangOptions().Target);
// When doing dependency scanning for explicit module, use strict context hash
// to ensure sound module hash.
bool useStrictCacheHash =
genericSubInvocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::ScanDependencies;

// Include the normalized target triple when not using strict hash.
// Otherwise, use the full target to ensure soundness of the hash. In
// practice, .swiftinterface files will be in target-specific subdirectories
// and would have target-specific pieces #if'd out. However, it doesn't hurt
// to include it, and it guards against mistakenly reusing cached modules
// across targets. Note that this normalization explicitly doesn't include the
// minimum deployment target (e.g. the '12.0' in 'ios12.0').
auto targetToHash = useStrictCacheHash
? genericSubInvocation.getLangOptions().Target
: getTargetSpecificModuleTriple(
genericSubInvocation.getLangOptions().Target);

std::string sdkBuildVersion = getSDKBuildVersion(sdkPath);
const auto ExtraArgs = genericSubInvocation.getClangImporterOptions()
.getReducedExtraArgsForSwiftModuleDependency();
Expand All @@ -2089,13 +2105,8 @@ InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath,
// anyways.
useInterfacePath,

// Include the normalized target triple. In practice, .swiftinterface
// files will be in target-specific subdirectories and would have
// target-specific pieces #if'd out. However, it doesn't hurt to include
// it, and it guards against mistakenly reusing cached modules across
// targets. Note that this normalization explicitly doesn't include the
// minimum deployment target (e.g. the '12.0' in 'ios12.0').
normalizedTargetTriple.str(),
// The target triple to hash.
targetToHash.str(),

// The SDK path is going to affect how this module is imported, so
// include it.
Expand All @@ -2121,6 +2132,10 @@ InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath,
// module.
llvm::hash_combine_range(ExtraArgs.begin(), ExtraArgs.end()),

/// Application extension.
unsigned(
genericSubInvocation.getLangOptions().EnableAppExtensionRestrictions),

// Whether or not OSSA modules are enabled.
//
// If OSSA modules are enabled, we use a separate namespace of modules to
Expand Down
2 changes: 1 addition & 1 deletion test/ScanDependencies/module_hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -O \
// RUN: -o %t/deps-3.json -Xcc -fapplication-extension -I %t/include

/// Check module hash for the swiftmodule. 1 and 2 should match, but not 3.
/// Check module hash for the swiftmodule. They should all not match.
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps-1.json Library modulePath > %t/path-1
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps-2.json Library modulePath > %t/path-2
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps-3.json Library modulePath > %t/path-3
Expand Down
47 changes: 47 additions & 0 deletions test/ScanDependencies/module_hash_macos.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// REQUIRES: OS=macosx, objc_interop
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

/// Different version should not match.
// RUN: %target-swift-frontend -target %module-target-triple -scan-dependencies -module-name Test %t/main.swift -module-cache-path %t/clang-module-cache \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -O \
// RUN: -o %t/deps-now.json -I %t/include
// RUN: %target-swift-frontend -target %module-target-future -scan-dependencies -module-name Test %t/main.swift -module-cache-path %t/clang-module-cache \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -O \
// RUN: -o %t/deps-future.json -I %t/include
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps-now.json Library modulePath > %t/path-now
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps-future.json Library modulePath > %t/path-future
// RUN: not diff %t/path-now %t/path-future

/// Application extension should not match.
// RUN: %target-swift-frontend -scan-dependencies -module-name Test %t/main.swift -module-cache-path %t/clang-module-cache \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -O \
// RUN: -o %t/deps.json -I %t/include
// RUN: %target-swift-frontend -scan-dependencies -module-name Test %t/main.swift -module-cache-path %t/clang-module-cache \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -O \
// RUN: -o %t/deps-ae.json -I %t/include -application-extension
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps.json Library modulePath > %t/path
// RUN: %{python} %S/../CAS/Inputs/SwiftDepsExtractor.py %t/deps-ae.json Library modulePath > %t/path-ae
// RUN: not diff %t/path %t/path-ae

//--- main.swift
import Library

//--- include/Library.swiftinterface
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name Library -O -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -user-module-version 1.0
import Swift
@_exported import A
@inlinable
public func test() {
foo()
}

//--- include/a.h
void foo(void);

//--- include/module.modulemap
module A {
header "a.h"
export *
}