Skip to content

StackProtection: treat source-operands of memcpy and memmove intrinsics as read-only #66746

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 1 commit into from
Jun 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,19 @@ private struct NoStores : ValueDefUseWalker, AddressDefUseWalker {
var walkDownCache = WalkerCache<SmallProjectionPath>()

mutating func leafUse(value: Operand, path: SmallProjectionPath) -> WalkResult {
if let ptai = value.instruction as? PointerToAddressInst {
switch value.instruction {
case let ptai as PointerToAddressInst:
return walkDownUses(ofAddress: ptai, path: path)
case let bi as BuiltinInst:
switch bi.intrinsicID {
case .memcpy, .memmove:
return value.index != 0 ? .continueWalk : .abortWalk
default:
return .abortWalk
}
default:
return .abortWalk
}
return .abortWalk
}

mutating func leafUse(address: Operand, path: SmallProjectionPath) -> WalkResult {
Expand Down
4 changes: 4 additions & 0 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ final public class BuiltinInst : SingleValueInstruction {
return bridged.BuiltinInst_getID()
}

public var intrinsicID: BridgedInstruction.IntrinsicID {
return bridged.BuiltinInst_getIntrinsicID()
}

public var substitutionMap: SubstitutionMap {
SubstitutionMap(bridged.BuiltinInst_getSubstitutionMap())
}
Expand Down
13 changes: 13 additions & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,19 @@ struct BridgedInstruction {
return getAs<swift::BuiltinInst>()->getBuiltinInfo().ID;
}

enum class IntrinsicID {
memcpy, memmove,
unknown
};

IntrinsicID BuiltinInst_getIntrinsicID() const {
switch (getAs<swift::BuiltinInst>()->getIntrinsicInfo().ID) {
case llvm::Intrinsic::memcpy: return IntrinsicID::memcpy;
case llvm::Intrinsic::memmove: return IntrinsicID::memmove;
default: return IntrinsicID::unknown;
}
}

SWIFT_IMPORT_UNSAFE
swift::SubstitutionMap BuiltinInst_getSubstitutionMap() const {
return getAs<swift::BuiltinInst>()->getSubstitutions();
Expand Down
16 changes: 16 additions & 0 deletions test/SILOptimizer/stack_protection.sil
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,20 @@ bb0:
return %12 : $()
}

// CHECK-LABEL: sil @test_mem_intrinsics
// CHECK-NOT: copy_addr
// CHECK: } // end sil function 'test_mem_intrinsics'
sil @test_mem_intrinsics : $@convention(thin) (Builtin.RawPointer, Int64) -> () {
bb0(%0 : $Builtin.RawPointer, %1: $Int64):
%2 = alloc_stack $Int64
store %1 to %2 : $*Int64
%4 = address_to_pointer [stack_protection] %2 : $*Int64 to $Builtin.RawPointer
%5 = integer_literal $Builtin.Int64, 8
%6 = integer_literal $Builtin.Int1, 0
%7 = builtin "int_memcpy_RawPointer_RawPointer_Int64"(%0 : $Builtin.RawPointer, %4 : $Builtin.RawPointer, %5 : $Builtin.Int64, %6 : $Builtin.Int1) : $()
%8 = builtin "int_memmove_RawPointer_RawPointer_Int64"(%0 : $Builtin.RawPointer, %4 : $Builtin.RawPointer, %5 : $Builtin.Int64, %6 : $Builtin.Int1) : $()
dealloc_stack %2 : $*Int64
%10 = tuple ()
return %10 : $()
}

7 changes: 7 additions & 0 deletions test/SILOptimizer/stack_protection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ public func loadUnaligned(_ urp: UnsafeRawPointer) -> Int {
return urp.loadUnaligned(as: Int.self)
}

// CHECK-LABEL: sil @$s4test19storeBytesToPointeryySv_SitF :
// CHECK-NOT: copy_addr
// CHECK: } // end sil function '$s4test19storeBytesToPointeryySv_SitF'
public func storeBytesToPointer(_ p: UnsafeMutableRawPointer, _ i: Int) {
p.storeBytes(of: i, as: Int.self)
}