Skip to content

Commit 287d43c

Browse files
authored
Merge pull request #9520 from atrick/access_work
2 parents 96ea67d + 45c24f6 commit 287d43c

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lib/SILOptimizer/Mandatory/DiagnoseStaticExclusivity.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static AccessedStorage findAccessedStorage(SILValue Source) {
454454

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

466-
if (isa<AllocBoxInst>(Iter) || isa<BeginAccessInst>(Iter) ||
467-
isa<SILFunctionArgument>(Iter)) {
468-
// Treat the instruction itself as the identity of the storage being
469-
// being accessed.
466+
switch (Iter->getKind()) {
467+
case ValueKind::AllocBoxInst:
468+
// An AllocBox is a fully identified memory location.
469+
LLVM_FALLTHROUGH;
470+
case ValueKind::BeginAccessInst:
471+
// The current access is nested within another access.
472+
// View the outer access as a separate location because nested accesses do
473+
// not conflict with each other.
474+
LLVM_FALLTHROUGH;
475+
case ValueKind::SILFunctionArgument:
476+
// A function argument is effectively a nested access, enforced
477+
// independently in the caller and callee.
478+
LLVM_FALLTHROUGH;
479+
case ValueKind::PointerToAddressInst:
480+
// An addressor provides access to a global or class property via a
481+
// RawPointer. Calling the address casts that raw pointer to an address.
470482
return AccessedStorage(Iter);
483+
default:
484+
DEBUG(llvm::dbgs() << "Bad memory access source: " << Iter);
485+
llvm_unreachable("Unexpected access source.");
471486
}
472-
473-
// For now we're still allowing arbitrary addresses here. Once
474-
// we start doing a best-effort static check for dynamically-enforced
475-
// accesses we should lock this down to only recognized sources.
476-
assert(Iter->getType().isAddress() || Iter->getType().is<SILBoxType>());
477-
return AccessedStorage(Iter);
478487
}
479488
}
480489

test/SILOptimizer/exclusivity_static_diagnostics.sil

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,15 @@ bb23:
462462
bbUnreachable:
463463
br bb22
464464
}
465+
466+
// Check that a pointer_to_address access passes diagnostics.
467+
//
468+
// CHECK-LABEL: sil hidden @pointerToAddr
469+
sil hidden @pointerToAddr : $@convention(thin) (Builtin.RawPointer) -> Int {
470+
bb0(%0: $Builtin.RawPointer):
471+
%adr = pointer_to_address %0 : $Builtin.RawPointer to [strict] $*Int
472+
%access = begin_access [read] [dynamic] %adr : $*Int
473+
%val = load [trivial] %access : $*Int
474+
end_access %access : $*Int
475+
return %val : $Int
476+
}

0 commit comments

Comments
 (0)