Skip to content

Allow disabling ownership verification via blocklist #78109

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
Dec 11, 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
1 change: 1 addition & 0 deletions include/swift/Basic/BlockListAction.def
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ BLOCKLIST_ACTION(ShouldUseBinaryModule)
BLOCKLIST_ACTION(ShouldUseTextualModule)
BLOCKLIST_ACTION(DowngradeInterfaceVerificationFailure)
BLOCKLIST_ACTION(ShouldUseLayoutStringValueWitnesses)
BLOCKLIST_ACTION(ShouldDisableOwnershipVerification)

#undef BLOCKLIST_ACTION
42 changes: 27 additions & 15 deletions lib/SIL/Verifier/SILOwnershipVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,15 +802,24 @@ bool SILValueOwnershipChecker::checkUses() {
return true;
}

bool disableOwnershipVerification(const SILModule &mod) {
if (DisableOwnershipVerification)
return true;
if (mod.getASTContext().blockListConfig.hasBlockListAction(
mod.getSwiftModule()->getRealName().str(),
BlockListKeyKind::ModuleName,
BlockListAction::ShouldDisableOwnershipVerification)) {
return true;
}
return false;
}

//===----------------------------------------------------------------------===//
// Top Level Entrypoints
//===----------------------------------------------------------------------===//

void SILInstruction::verifyOperandOwnership(
SILModuleConventions *silConv) const {
if (DisableOwnershipVerification)
return;

if (isStaticInitializerInst())
return;

Expand All @@ -831,11 +840,13 @@ void SILInstruction::verifyOperandOwnership(
!getFunction()->shouldVerifyOwnership())
return;

if (disableOwnershipVerification(getModule()))
return;

// If we are testing the verifier, bail so we only print errors once when
// performing a full verification, instead of additionally in the SILBuilder.
if (IsSILOwnershipVerifierTestingEnabled)
return;

// If this is a terminator instruction, do not verify in SILBuilder. This is
// because when building a new function, one must create the destination block
// first which is an unnatural pattern and pretty brittle.
Expand Down Expand Up @@ -903,9 +914,6 @@ verifySILValueHelper(const SILFunction *f, SILValue value,
}

void SILValue::verifyOwnership(DeadEndBlocks *deadEndBlocks) const {
if (DisableOwnershipVerification)
return;

// Do not validate SILUndef values.
if (isa<SILUndef>(*this))
return;
Expand All @@ -931,18 +939,21 @@ void SILValue::verifyOwnership(DeadEndBlocks *deadEndBlocks) const {
}
}

// Since we do not have SILUndef, we now know that getFunction() should return
// a real function. Assert in case this assumption is no longer true.
auto *f = (*this)->getFunction();
assert(f && "Instructions and arguments should have a function");

if (disableOwnershipVerification(f->getModule()))
return;

// If we are testing the verifier, bail so we only print errors once when
// performing a full verification a function at a time by the
// OwnershipVerifierStateDumper pass, instead of additionally in the
// SILBuilder and in the actual SIL verifier that may be run by sil-opt.
if (IsSILOwnershipVerifierTestingEnabled)
return;

// Since we do not have SILUndef, we now know that getFunction() should return
// a real function. Assert in case this assumption is no longer true.
auto *f = (*this)->getFunction();
assert(f && "Instructions and arguments should have a function");

using BehaviorKind = LinearLifetimeChecker::ErrorBehaviorKind;
LinearLifetimeChecker::ErrorBuilder errorBuilder(
*f, BehaviorKind::PrintMessageAndAssert);
Expand All @@ -952,7 +963,7 @@ void SILValue::verifyOwnership(DeadEndBlocks *deadEndBlocks) const {
}

void SILModule::verifyOwnership() const {
if (DisableOwnershipVerification)
if (disableOwnershipVerification(*this))
return;

#ifdef NDEBUG
Expand All @@ -973,8 +984,6 @@ void SILModule::verifyOwnership() const {
}

void SILFunction::verifyOwnership(DeadEndBlocks *deadEndBlocks) const {
if (DisableOwnershipVerification)
return;
if (!getModule().getOptions().VerifySILOwnership)
return;

Expand All @@ -996,6 +1005,9 @@ void SILFunction::verifyOwnership(DeadEndBlocks *deadEndBlocks) const {
if (!hasOwnership() || !shouldVerifyOwnership())
return;

if (disableOwnershipVerification(getModule()))
return;

using BehaviorKind = LinearLifetimeChecker::ErrorBehaviorKind;
unsigned errorCounter = 0;
std::optional<LinearLifetimeChecker::ErrorBuilder> errorBuilder;
Expand Down
20 changes: 20 additions & 0 deletions test/SIL/disable_ossa_verification_blocklist.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %empty-directory(%t)

// RUN: echo "---" > %t/blocklist.yml
// RUN: echo "ShouldDisableOwnershipVerification:" >> %t/blocklist.yml
// RUN: echo " ModuleName:" >> %t/blocklist.yml
// RUN: echo " - Foo" >> %t/blocklist.yml

// RUN: %target-swift-frontend -emit-sil -module-name Foo -blocklist-file %t/blocklist.yml %s | %FileCheck %s

class Klass {}

// CHECK-LABEL: sil @invalid_destroy
// CHECK: strong_release
sil [ossa] @invalid_destroy : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : @guaranteed $Klass):
destroy_value %0 : $Klass
%t = tuple ()
return %t : $()
}