fix(monitors): 补齐联网权限并释放采样端口引用

This commit is contained in:
2026-11-03 21:59:49 +08:00
parent cecd646376
commit ea4d0e7f23
7 changed files with 89 additions and 4 deletions
+24
View File
@@ -0,0 +1,24 @@
import Foundation
import Darwin
@main struct MachPortChecks {
static func hostReferences() -> mach_port_urefs_t {
let host = mach_host_self()
defer { mach_port_deallocate(mach_task_self_, host) }
var references: mach_port_urefs_t = 0
precondition(mach_port_get_refs(mach_task_self_, host, mach_port_right_t(MACH_PORT_RIGHT_SEND), &references) == KERN_SUCCESS)
return references
}
static func main() {
var cpu = CPUMonitor()
_ = cpu.sample()
_ = MemoryMonitor.sample()
let initial = hostReferences()
for _ in 0..<1_000 { _ = cpu.sample() }
precondition(hostReferences() == initial, "CPU sampling leaked host send-right references")
for _ in 0..<1_000 { _ = MemoryMonitor.sample() }
precondition(hostReferences() == initial, "Memory sampling leaked host send-right references")
print("PASS: 1,000 CPU + 1,000 memory samples keep host send-right references at \(initial)")
}
}
+24
View File
@@ -0,0 +1,24 @@
import Foundation
import Darwin
/// Run only inside a temporary signed app with the built host's entitlements.
/// This opt-in integration check contacts the same HTTPS endpoint as the app.
@main struct SandboxNetworkChecks {
static func main() async {
var request = URLRequest(url: URL(string: "https://api.ipify.org")!)
request.timeoutInterval = 5
do {
let (_, response) = try await URLSession.shared.data(for: request)
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
print("FAIL: public-address endpoint did not return HTTP 200")
exit(1)
}
print("PASS: signed sandbox public-address HTTPS request returned HTTP 200")
} catch {
// Keep the public address and potentially identifying error URLs out of logs.
let error = error as NSError
print("FAIL: sandbox HTTPS request: \(error.domain) code \(error.code)")
exit(1)
}
}
}