Skip to content

[ownership] Fix a corner case in the linear lifetime checker. #23937

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
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
19 changes: 19 additions & 0 deletions lib/SIL/LinearLifetimeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,25 @@ LinearLifetimeError swift::valueHasLinearLifetime(
// have been detected by initializing our consuming uses. So we are done.
if (consumingUses.size() == 1 &&
consumingUses[0].getParent() == value->getParentBlock()) {
// Check if any of our non consuming uses are not in the parent block. We
// flag those as additional use after frees. Any in the same block, we would
// have flagged.
if (llvm::any_of(nonConsumingUses, [&](BranchPropagatedUser user) {
return user.getParent() != value->getParentBlock();
})) {
state.error.handleUseAfterFree([&] {
llvm::errs() << "Function: '" << value->getFunction()->getName()
<< "'\n"
<< "Found use after free due to unvisited non lifetime "
"ending uses?!\n"
<< "Value: " << *value << " Remaining Users:\n";
for (const auto &user : nonConsumingUses) {
llvm::errs() << "User: " << *user.getInst();
}
llvm::errs() << "\n";
});
}

return state.error;
}

Expand Down
39 changes: 39 additions & 0 deletions test/SIL/ownership-verifier/over_consume.sil
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ case some(T)
case none
}

protocol Error {}

struct NativeObjectPair {
var obj1 : Builtin.NativeObject
var obj2 : Builtin.NativeObject
}

class SuperKlass {
func doSomething()
}
Expand Down Expand Up @@ -485,3 +492,35 @@ bb0(%0 : @guaranteed $ClassProtConformingRef, %1 : @owned $ClassProtConformingRe
%5 = tuple(%3 : $ClassProt, %4 : $ClassProt)
return %5 : $(ClassProt, ClassProt)
}

sil [ossa] @eliminate_copy_try_apple_callee : $@convention(thin) (@owned Builtin.NativeObject) -> @error Error {
entry(%0 : @owned $Builtin.NativeObject):
%9999 = tuple()
return %9999 : $()
}


// CHECK-LABEL: Function: 'use_after_free_consume_in_same_block'
// CHECK: Found use after free due to unvisited non lifetime ending uses?!
// CHECK: Value: %3 = copy_value %2 : $Builtin.NativeObject
// CHECK: Remaining Users:
// CHECK: User: %10 = apply %7(%3) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
sil [ossa] @use_after_free_consume_in_same_block : $@convention(thin) (@owned NativeObjectPair) -> @error Error {
bb0(%0 : @owned $NativeObjectPair):
%1 = begin_borrow %0 : $NativeObjectPair
%2 = struct_extract %1 : $NativeObjectPair, #NativeObjectPair.obj1
%3 = copy_value %2 : $Builtin.NativeObject
end_borrow %1 : $NativeObjectPair
destroy_value %0 : $NativeObjectPair
%4 = function_ref @eliminate_copy_try_apple_callee : $@convention(thin) (@owned Builtin.NativeObject) -> @error Error
%5 = function_ref @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
try_apply %4(%3) : $@convention(thin) (@owned Builtin.NativeObject) -> @error Error, normal bb1, error bb2

bb1(%errorEmptyTup: $()):
apply %5(%3) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
%9999 = tuple()
return %9999 : $()

bb2(%error : @owned $Error):
throw %error : $Error
}