跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

hcs_api_windows.go

286 行 · 8.4 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package hcs
5
6import (
7 "context"
8 "fmt"
9 "syscall"
10 "time"
11 "unsafe"
12
13 "github.com/sirupsen/logrus"
14 "golang.org/x/sys/windows"
15)
16
17// ---------------------------------------------------------------------------
18// computecore.dll (HCS) bindings
19// ---------------------------------------------------------------------------
20
21var (
22 modcomputecore = windows.NewLazySystemDLL("computecore.dll")
23
24 procHcsCreateOperation = modcomputecore.NewProc("HcsCreateOperation")
25 procHcsCloseOperation = modcomputecore.NewProc("HcsCloseOperation")
26 procHcsWaitForOperationResult = modcomputecore.NewProc("HcsWaitForOperationResult")
27 procHcsCreateComputeSystem = modcomputecore.NewProc("HcsCreateComputeSystem")
28 procHcsStartComputeSystem = modcomputecore.NewProc("HcsStartComputeSystem")
29 procHcsTerminateComputeSystem = modcomputecore.NewProc("HcsTerminateComputeSystem")
30 procHcsCloseComputeSystem = modcomputecore.NewProc("HcsCloseComputeSystem")
31 procHcsGrantVMAccess = modcomputecore.NewProc("HcsGrantVmAccess")
32 procHcsOpenComputeSystem = modcomputecore.NewProc("HcsOpenComputeSystem")
33 procHcsWaitForComputeSystemExit = modcomputecore.NewProc("HcsWaitForComputeSystemExit")
34 procHcsGetComputeSystemProperties = modcomputecore.NewProc("HcsGetComputeSystemProperties")
35
36 modapiMsWinCoreComL110 = windows.NewLazySystemDLL("api-ms-win-core-com-l1-1-0.dll")
37 procCoTaskMemFree = modapiMsWinCoreComL110.NewProc("CoTaskMemFree")
38)
39
40type (
41 hcsOperation syscall.Handle
42 hcsSystem syscall.Handle
43)
44
45const (
46 infiniteTimeout = 0xFFFFFFFF
47 syscallWatcherTimeout = 4 * time.Minute
48)
49
50// hresultErr converts an HRESULT return value into a Go error, unwrapping
51// FACILITY_WIN32 the same way hcsshim's generated bindings do.
52func hresultErr(r0 uintptr) error {
53 if int32(r0) >= 0 {
54 return nil
55 }
56 if r0&0x1fff0000 == 0x00070000 {
57 r0 &= 0xffff
58 }
59 return syscall.Errno(r0)
60}
61
62// coString copies a CoTaskMem-allocated PWSTR into a Go string and frees it.
63func coString(p *uint16) string {
64 if p == nil {
65 return ""
66 }
67 s := windows.UTF16PtrToString(p)
68 windows.CoTaskMemFree(unsafe.Pointer(p))
69 return s
70}
71
72func hcsCreateOperation() (hcsOperation, error) {
73 r0, _, e1 := syscall.SyscallN(procHcsCreateOperation.Addr(), 0, 0)
74 if r0 == 0 {
75 return 0, fmt.Errorf("HcsCreateOperation: %w", e1)
76 }
77 return hcsOperation(r0), nil
78}
79
80func hcsCloseOperation(op hcsOperation) {
81 _, _, _ = syscall.SyscallN(procHcsCloseOperation.Addr(), uintptr(op))
82}
83
84// hcsWait drives one async HCS call to completion and returns the result
85// document (which carries structured error info on failure).
86func hcsWait(op hcsOperation, what string, callErr error) (string, error) {
87 if callErr != nil {
88 return "", fmt.Errorf("%s: %w", what, callErr)
89 }
90 var result *uint16
91 r0, _, _ := syscall.SyscallN(procHcsWaitForOperationResult.Addr(),
92 uintptr(op), uintptr(infiniteTimeout), uintptr(unsafe.Pointer(&result)))
93 doc := coString(result)
94 if err := hresultErr(r0); err != nil {
95 return doc, fmt.Errorf("%s: %w (result: %s)", what, err, doc)
96 }
97 return doc, nil
98}
99
100func hcsCreateComputeSystem(id, configuration string) (hcsSystem, error) {
101 op, err := hcsCreateOperation()
102 if err != nil {
103 return 0, err
104 }
105 defer hcsCloseOperation(op)
106
107 idP, err := syscall.UTF16PtrFromString(id)
108 if err != nil {
109 return 0, err
110 }
111 cfgP, err := syscall.UTF16PtrFromString(configuration)
112 if err != nil {
113 return 0, err
114 }
115 var system hcsSystem
116 r0, _, _ := syscall.SyscallN(procHcsCreateComputeSystem.Addr(),
117 uintptr(unsafe.Pointer(idP)), uintptr(unsafe.Pointer(cfgP)),
118 uintptr(op), 0, uintptr(unsafe.Pointer(&system)))
119 if _, err := hcsWait(op, "HcsCreateComputeSystem", hresultErr(r0)); err != nil {
120 return 0, err
121 }
122 return system, nil
123}
124
125func hcsStartComputeSystem(system hcsSystem) error {
126 op, err := hcsCreateOperation()
127 if err != nil {
128 return err
129 }
130 defer hcsCloseOperation(op)
131 r0, _, _ := syscall.SyscallN(procHcsStartComputeSystem.Addr(),
132 uintptr(system), uintptr(op), 0)
133 _, err = hcsWait(op, "HcsStartComputeSystem", hresultErr(r0))
134 return err
135}
136
137func hcsTerminateComputeSystem(system hcsSystem) error {
138 op, err := hcsCreateOperation()
139 if err != nil {
140 return err
141 }
142 defer hcsCloseOperation(op)
143 r0, _, _ := syscall.SyscallN(procHcsTerminateComputeSystem.Addr(),
144 uintptr(system), uintptr(op), 0)
145 _, err = hcsWait(op, "HcsTerminateComputeSystem", hresultErr(r0))
146 return err
147}
148
149func execute(ctx context.Context, timeout time.Duration, f func() error) error {
150 done := make(chan error, 1)
151 go func() { done <- f() }()
152
153 var watcher <-chan time.Time
154 if timeout > 0 {
155 t := time.NewTimer(timeout)
156 defer t.Stop()
157 watcher = t.C
158 }
159
160 for {
161 select {
162 case err := <-done:
163 return err
164 case <-watcher:
165 logrus.WithField("timeout", timeout).
166 Warn("HCS syscall exceeded timeout; still waiting to avoid use-after-free in computecore.dll")
167 watcher = nil
168 case <-ctx.Done():
169 logrus.WithError(ctx.Err()).
170 Warn("HCS syscall context canceled; still waiting to avoid use-after-free in computecore.dll")
171 ctx = context.WithoutCancel(ctx)
172 }
173 }
174}
175
176func hcsGetComputeSystemProperties(ctx context.Context, system hcsSystem, operation hcsOperation, propertyQuery string) (hr error) {
177 return execute(ctx, syscallWatcherTimeout, func() error {
178 return _hcsGetComputeSystemProperties(system, operation, propertyQuery)
179 })
180}
181
182func _hcsGetComputeSystemProperties(system hcsSystem, operation hcsOperation, propertyQuery string) (hr error) {
183 var _p0 *uint16
184 _p0, hr = syscall.UTF16PtrFromString(propertyQuery)
185 if hr != nil {
186 return hr
187 }
188
189 hr = procHcsGetComputeSystemProperties.Find()
190 if hr != nil {
191 return hr
192 }
193 r0, _, _ := syscall.SyscallN(procHcsGetComputeSystemProperties.Addr(), uintptr(system), uintptr(operation), uintptr(unsafe.Pointer(_p0)))
194 if int32(r0) < 0 {
195 if r0&0x1fff0000 == 0x00070000 {
196 r0 &= 0xffff
197 }
198 hr = syscall.Errno(r0)
199 }
200 return hr
201}
202
203func hcsOpenComputeSystem(ctx context.Context, id string, requestedAccess uint32) (computeSystem hcsSystem, hr error) {
204 hr = execute(ctx, syscallWatcherTimeout, func() error {
205 return _hcsOpenComputeSystem(id, requestedAccess, &computeSystem)
206 })
207 return computeSystem, hr
208}
209
210func _hcsOpenComputeSystem(id string, requestedAccess uint32, computeSystem *hcsSystem) (hr error) {
211 var _p0 *uint16
212 _p0, hr = syscall.UTF16PtrFromString(id)
213 if hr != nil {
214 return hr
215 }
216
217 hr = procHcsOpenComputeSystem.Find()
218 if hr != nil {
219 return hr
220 }
221 r0, _, _ := syscall.SyscallN(procHcsOpenComputeSystem.Addr(), uintptr(unsafe.Pointer(_p0)), uintptr(requestedAccess), uintptr(unsafe.Pointer(computeSystem)))
222 if int32(r0) < 0 {
223 if r0&0x1fff0000 == 0x00070000 {
224 r0 &= 0xffff
225 }
226 hr = syscall.Errno(r0)
227 }
228
229 return hr
230}
231
232func coTaskMemFree(buffer unsafe.Pointer) {
233 _, _, _ = syscall.SyscallN(procCoTaskMemFree.Addr(), uintptr(buffer))
234}
235
236func convertAndFreeCoTaskMemString(buffer *uint16) string {
237 str := syscall.UTF16ToString((*[1 << 29]uint16)(unsafe.Pointer(buffer))[:])
238 coTaskMemFree(unsafe.Pointer(buffer))
239 return str
240}
241
242func hcsWaitForComputeSystemExit(ctx context.Context, system hcsSystem, timeoutMs uint32) (result string, hr error) {
243 return result, execute(ctx, syscallWatcherTimeout, func() error {
244 var resultp *uint16
245 err := _hcsWaitForComputeSystemExit(system, timeoutMs, &resultp)
246 if resultp != nil {
247 result = convertAndFreeCoTaskMemString(resultp)
248 }
249 return err
250 })
251}
252
253func _hcsWaitForComputeSystemExit(system hcsSystem, timeoutMs uint32, result **uint16) (hr error) {
254 hr = procHcsWaitForComputeSystemExit.Find()
255 if hr != nil {
256 return hr
257 }
258 r0, _, _ := syscall.SyscallN(procHcsWaitForComputeSystemExit.Addr(), uintptr(system), uintptr(timeoutMs), uintptr(unsafe.Pointer(result)))
259 if int32(r0) < 0 {
260 if r0&0x1fff0000 == 0x00070000 {
261 r0 &= 0xffff
262 }
263 hr = syscall.Errno(r0)
264 }
265 return hr
266}
267
268func hcsCloseComputeSystem(system hcsSystem) {
269 _, _, _ = syscall.SyscallN(procHcsCloseComputeSystem.Addr(), uintptr(system))
270}
271
272// hcsGrantVMAccess ACLs a host file so the VM worker process may read it
273// (hcsshim does the equivalent before every kernel-direct boot).
274func hcsGrantVMAccess(vmID, path string) error {
275 idP, err := syscall.UTF16PtrFromString(vmID)
276 if err != nil {
277 return err
278 }
279 pathP, err := syscall.UTF16PtrFromString(path)
280 if err != nil {
281 return err
282 }
283 r0, _, _ := syscall.SyscallN(procHcsGrantVMAccess.Addr(),
284 uintptr(unsafe.Pointer(idP)), uintptr(unsafe.Pointer(pathP)))
285 return hresultErr(r0)
286}