Skip to content

[Exclusivity] Check that accesses are well-formed during diagnostics. #9520

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 2 commits into from
May 12, 2017
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
31 changes: 20 additions & 11 deletions lib/SILOptimizer/Mandatory/DiagnoseStaticExclusivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static AccessedStorage findAccessedStorage(SILValue Source) {

if (auto *REA = dyn_cast<RefElementAddrInst>(Iter)) {
// Do a best-effort to find the identity of the object being projected
// from. It is OK to unsound here (i.e., miss when two ref_element_addrs
// from. It is OK to be unsound here (i.e. miss when two ref_element_addrs
// actually refer the same address) because these will be dynamically
// checked.
SILValue Object = findUnderlyingObject(REA->getOperand());
Expand All @@ -463,18 +463,27 @@ static AccessedStorage findAccessedStorage(SILValue Source) {
return AccessedStorage(AccessedStorageKind::ClassProperty, OP);
}

if (isa<AllocBoxInst>(Iter) || isa<BeginAccessInst>(Iter) ||
isa<SILFunctionArgument>(Iter)) {
// Treat the instruction itself as the identity of the storage being
// being accessed.
switch (Iter->getKind()) {
case ValueKind::AllocBoxInst:
// An AllocBox is a fully identified memory location.
LLVM_FALLTHROUGH;
case ValueKind::BeginAccessInst:
// The current access is nested within another access.
// View the outer access as a separate location because nested accesses do
// not conflict with each other.
LLVM_FALLTHROUGH;
case ValueKind::SILFunctionArgument:
// A function argument is effectively a nested access, enforced
// independently in the caller and callee.
LLVM_FALLTHROUGH;
case ValueKind::PointerToAddressInst:
// An addressor provides access to a global or class property via a
// RawPointer. Calling the address casts that raw pointer to an address.
return AccessedStorage(Iter);
default:
DEBUG(llvm::dbgs() << "Bad memory access source: " << Iter);
llvm_unreachable("Unexpected access source.");
}

// For now we're still allowing arbitrary addresses here. Once
// we start doing a best-effort static check for dynamically-enforced
// accesses we should lock this down to only recognized sources.
assert(Iter->getType().isAddress() || Iter->getType().is<SILBoxType>());
return AccessedStorage(Iter);
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/SILOptimizer/exclusivity_static_diagnostics.sil
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,15 @@ bb23:
bbUnreachable:
br bb22
}

// Check that a pointer_to_address access passes diagnostics.
//
// CHECK-LABEL: sil hidden @pointerToAddr
sil hidden @pointerToAddr : $@convention(thin) (Builtin.RawPointer) -> Int {
bb0(%0: $Builtin.RawPointer):
%adr = pointer_to_address %0 : $Builtin.RawPointer to [strict] $*Int
%access = begin_access [read] [dynamic] %adr : $*Int
%val = load [trivial] %access : $*Int
end_access %access : $*Int
return %val : $Int
}