Skip to content

Fix MemoryLifetimeVerifier for enums with empty/non-uniform element types #39173

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 15 additions & 1 deletion lib/SIL/Utils/MemoryLocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,21 @@ bool MemoryLocations::analyzeLocationUsesRecursively(SILValue V, unsigned locIdx
if (cast<DebugValueInst>(user)->hasAddrVal())
break;
return false;
case SILInstructionKind::InjectEnumAddrInst:
case SILInstructionKind::InjectEnumAddrInst: {
auto *ieai = cast<InjectEnumAddrInst>(user);
if (ieai->getElement()->hasAssociatedValues()) {
auto eleType = ieai->getOperand()->getType().getEnumElementType(
ieai->getElement(), ieai->getFunction());
if (isEmptyType(eleType, ieai->getFunction()))
return false;
auto key = std::make_pair(locIdx, /* fieldNr*/ 0);
unsigned subLocIdx = subLocationMap[key];
if (subLocIdx > 0 &&
getLocation(subLocIdx)->representativeValue->getType() != eleType)
return false;
}
break;
}
case SILInstructionKind::SelectEnumAddrInst:
case SILInstructionKind::ExistentialMetatypeInst:
case SILInstructionKind::ValueMetatypeInst:
Expand Down
57 changes: 57 additions & 0 deletions test/SIL/memory_lifetime.sil
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,60 @@ bb0(%0 : $Int, %1 : @guaranteed $@callee_guaranteed (@in_guaranteed (Int, ((), (
dealloc_stack %2 : $*(Int, ((), ()))
return %5 : $Int
}

enum Result<T1, T2>{
case success(T1)
case failure(T2)
}

sil @try_get_error : $@convention(thin) () -> @error Error

sil [ossa] @test_init_enum_empty_case : $@convention(thin) () -> @error Error {
bb0:
%0 = alloc_stack $Result<(), Error>
%1 = function_ref @try_get_error : $@convention(thin) () -> @error Error
try_apply %1() : $@convention(thin) () -> @error Error, normal bb1, error bb2

bb1(%3 : $()):
inject_enum_addr %0 : $*Result<(), Error>, #Result.success!enumelt
br bb3

bb2(%6 : @owned $Error):
%7 = init_enum_data_addr %0 : $*Result<(), Error>, #Result.failure!enumelt
store %6 to [init] %7 : $*Error
inject_enum_addr %0 : $*Result<(), Error>, #Result.failure!enumelt
br bb3

bb3:
%11 = load [take] %0 : $*Result<(), Error>
destroy_value %11 : $Result<(), Error>
dealloc_stack %0 : $*Result<(), Error>
%14 = tuple ()
return %14 : $()
}

sil [ossa] @test_init_enum_trivial_case : $@convention(thin) () -> @error Error {
bb0:
%0 = alloc_stack $Result<Int, Error>
%1 = function_ref @try_get_error : $@convention(thin) () -> @error Error
try_apply %1() : $@convention(thin) () -> @error Error, normal bb1, error bb2

bb1(%3 : $()):
inject_enum_addr %0 : $*Result<Int, Error>, #Result.success!enumelt
br bb3


bb2(%7 : @owned $Error):
%8 = init_enum_data_addr %0 : $*Result<Int, Error>, #Result.failure!enumelt
store %7 to [init] %8 : $*Error
inject_enum_addr %0 : $*Result<Int, Error>, #Result.failure!enumelt
br bb3

bb3:
%12 = load [take] %0 : $*Result<Int, Error>
Copy link
Contributor

@eeckstein eeckstein Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be considered as illegal SIL. This load [take] is loading an uninitialized value (the Int-case coming from bb1). It will not crash, because the only thing which is done is to destroy the value. But still, loading an uninitialized value is not something we should be allowing in SIL.

In my PR (#39200) I have put this test case into memory_lifetime_failures.sil

destroy_value %12 : $Result<Int, Error>
dealloc_stack %0 : $*Result<Int, Error>
%15 = tuple ()
return %15 : $()
}