Closed adam-fowler closed 3 years ago
Here are fixed versions of the functions using pointee
outside of the withMemoryRebound
closure from Pointers.swift. I can add a PR for these if you want.
func withClassValuePointer<Value, Result>(
of value: inout Value,
_ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result {
return try withUnsafePointer(to: &value) {
return try $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
try body($0.pointee)
}
}
}
func withExistentialValuePointer<Value, Result>(
of value: inout Value,
_ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result {
return try withUnsafePointer(to: &value) { ptr in
try ptr.withMemoryRebound(to: ExistentialContainer.self, capacity: 1) {
let container = $0.pointee
let info = try metadata(of: container.type)
if info.kind == .class || info.size > ExistentialContainerBuffer.size() {
return try ptr.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
let base = $0.pointee
if info.kind == .struct {
return try body(base.advanced(by: existentialHeaderSize))
} else {
return try body(base)
}
}
} else {
return try body($0.mutable.raw)
}
}
}
}
Ah good catch. Yea I originally started doing that in a couple places when reading the metadata, and those addresses would always be static so it wasn't too much of an issue. Obviously got to use to it and it should not be done here 😅. But Id be happy to merge a PR that fixes it!
If you run
You get the following error
Looks like this is because you are assuming you can use the
pointee
value outside of awithMemoryRebound
. Although if I fix this the address sanitizer just fails elsewhere.On Linux both thread and address sanitizers fail