我正在编写一个需要处理内存中私钥的快速应用程序。由于此类对象的敏感性,因此在释放对象时需要清除键(也就是将其写为全零),并且无法将内存分页到磁盘(通常使用mlock()完成)。
在Objective-C中,您可以提供一个自定义CFAllocator对象,该对象允许您使用自己的函数来分配/取消分配/重新分配对象使用的内存。
因此,一种解决方案是仅在Objective-C中实现一个“ SecureData”对象,该对象使用自定义CFAllocator(也在Objective- C中)在内部创建NSMutableData对象。
但是,有什么方法可以为纯swift对象(例如struct或[UInt8])提供自己的自定义内存分配功能吗?还是有一种更好的,“适当”的方法来快速实现这样的安全内存?
如果要完全控制自己分配的内存区域,则可以使用UnsafePointer和co:
UnsafePointer
// allocate enough memory for ten Ints var ump = UnsafeMutablePointer<Int>.alloc(10) // memory is in an uninitialized raw state // initialize that memory with Int objects // (here, from a collection) ump.initializeFrom(reverse(0..<10)) // memory property gives you access to the underlying value ump.memory // 9 // UnsafeMutablePointer acts like an IndexType ump.successor().memory // 8 // and it has a subscript, but it's not a CollectionType ump[3] // = 6 // wrap it in an UnsafeMutableBufferPointer to treat it // like a collection (or UnsafeBufferPointer if you don't // need to be able to alter the values) let col = UnsafeMutableBufferPointer(start: ump, count: 10) col[3] = 99 println(",".join(map(col,toString))) // prints 9,8,7,99,5,4,3,2,1,0 ump.destroy(10) // now the allocated memory is back in a raw state // you could re-allocate it... ump.initializeFrom(0..<10) ump.destroy(10) // when you're done, deallocate the memory ump.dealloc(10)
您还可以UnsafePointer指向其他内存,例如某些C API处理的内存。
UnsafePointer可以传递给C函数,这些函数采用指向连续内存块的指针。因此,出于您的目的,您可以将该指针传递给类似mlock以下的函数:
mlock
let count = 10 let ump = UnsafeMutablePointer.allocate<Int>(count) mlock(ump, UInt(sizeof(Int) * count)) // initialize, use, and destroy the memory munlock(ump, UInt(sizeof(Int) * count)) ump.dealloc(count)
您甚至可以保留自己的自定义类型:
struct MyStruct { let a: Int let b: Int } var pointerToStruct = UnsafeMutablePointer<MyStruct>.alloc(1) pointerToStruct.initialize(MyStruct(a: 1, b: 2)) pointerToStruct.memory.b // 2 pointerToStruct.destroy() pointerToStruct.dealloc(1)
但是请 注意,如果对类,甚至数组或字符串(或包含它们的结构)执行此操作,则将要保存在内存中的只是指向这些对象分配和拥有的其他内存的指针。如果这对您很重要(例如,您正在为此内存做一些特殊的事情,例如保护它),那么这可能不是您想要的。
因此,您需要使用固定大小的对象,或者进一步利用UnsafePointer持有指向更多内存区域的指针。如果他们不需要动态调整大小,则只需分配一个不安全的指针(可能包装在UnsafeBufferPointerCollection接口中)即可。
UnsafeBufferPointer
如果需要更多动态行为,下面是一个非常简单的集合实现,可以根据需要调整大小,可以对其进行增强以涵盖特殊的内存处理逻辑:
// Note this is a class not a struct, so it does NOT have value semantics, // changing a copy changes all copies. public class UnsafeCollection<T> { private var _len: Int = 0 private var _buflen: Int = 0 private var _buf: UnsafeMutablePointer<T> = nil public func removeAll(keepCapacity: Bool = false) { _buf.destroy(_len) _len = 0 if !keepCapacity { _buf.dealloc(_buflen) _buflen = 0 _buf = nil } } public required init() { } deinit { self.removeAll(keepCapacity: false) } public var count: Int { return _len } public var isEmpty: Bool { return _len == 0 } }
为了满足以下要求MutableCollectionType(即CollectionType加上可分配的下标):
MutableCollectionType
CollectionType
extension UnsafeCollection: MutableCollectionType { typealias Index = Int public var startIndex: Int { return 0 } public var endIndex: Int { return _len } public subscript(idx: Int) -> T { get { precondition(idx < _len) return _buf[idx] } set(newElement) { precondition(idx < _len) let ptr = _buf.advancedBy(idx) ptr.destroy() ptr.initialize(newElement) } } typealias Generator = IndexingGenerator<UnsafeCollection> public func generate() -> Generator { return Generator(self) } }
并且ExtensibleCollectionType,为了实现动态增长:
ExtensibleCollectionType
extension UnsafeCollection: ExtensibleCollectionType { public func reserveCapacity(n: Index.Distance) { if n > _buflen { let newBuf = UnsafeMutablePointer<T>.alloc(n) newBuf.moveInitializeBackwardFrom(_buf, count: _len) _buf.dealloc(_buflen) _buf = newBuf _buflen = n } } public func append(x: T) { if _len == _buflen { reserveCapacity(Int(Double(_len) * 1.6) + 1) } _buf.advancedBy(_len++).initialize(x) } public func extend<S: SequenceType where S.Generator.Element == T> (newElements: S) { var g = newElements.generate() while let x: T = g.next() { self.append(x) } } }