Skip to content

Commit f78a2ab

Browse files
committed
[Exclusivity] Check that accesses are well-formed during diagnostics.
This can help catch assumptions in current implementation during testing. It does not need to be merged to 4.0.
1 parent c76269c commit f78a2ab

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-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

0 commit comments

Comments
 (0)