github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/src/syscall/dll_windows.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package syscall 6 7 import ( 8 "internal/syscall/windows/sysdll" 9 "sync" 10 "sync/atomic" 11 "unsafe" 12 ) 13 14 // DLLError describes reasons for DLL load failures. 15 type DLLError struct { 16 Err error 17 ObjName string 18 Msg string 19 } 20 21 func (e *DLLError) Error() string { return e.Msg } 22 23 // Implemented in ../runtime/syscall_windows.go. 24 func Syscall(trap, nargs, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) 25 func Syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) 26 func Syscall9(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno) 27 func Syscall12(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 uintptr) (r1, r2 uintptr, err Errno) 28 func Syscall15(trap, nargs, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2 uintptr, err Errno) 29 func loadlibrary(filename *uint16) (handle uintptr, err Errno) 30 func loadsystemlibrary(filename *uint16) (handle uintptr, err Errno) 31 func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno) 32 33 // A DLL implements access to a single DLL. 34 type DLL struct { 35 Name string 36 Handle Handle 37 } 38 39 // LoadDLL loads the named DLL file into memory. 40 // 41 // If name is not an absolute path and is not a known system DLL used by 42 // Go, Windows will search for the named DLL in many locations, causing 43 // potential DLL preloading attacks. 44 // 45 // Use LazyDLL in golang.org/x/sys/windows for a secure way to 46 // load system DLLs. 47 func LoadDLL(name string) (*DLL, error) { 48 namep, err := UTF16PtrFromString(name) 49 if err != nil { 50 return nil, err 51 } 52 var h uintptr 53 var e Errno 54 if sysdll.IsSystemDLL[name] { 55 h, e = loadsystemlibrary(namep) 56 } else { 57 h, e = loadlibrary(namep) 58 } 59 if e != 0 { 60 return nil, &DLLError{ 61 Err: e, 62 ObjName: name, 63 Msg: "Failed to load " + name + ": " + e.Error(), 64 } 65 } 66 d := &DLL{ 67 Name: name, 68 Handle: Handle(h), 69 } 70 return d, nil 71 } 72 73 // MustLoadDLL is like LoadDLL but panics if load operation fails. 74 func MustLoadDLL(name string) *DLL { 75 d, e := LoadDLL(name) 76 if e != nil { 77 panic(e) 78 } 79 return d 80 } 81 82 // FindProc searches DLL d for procedure named name and returns *Proc 83 // if found. It returns an error if search fails. 84 func (d *DLL) FindProc(name string) (proc *Proc, err error) { 85 namep, err := BytePtrFromString(name) 86 if err != nil { 87 return nil, err 88 } 89 a, e := getprocaddress(uintptr(d.Handle), namep) 90 use(unsafe.Pointer(namep)) 91 if e != 0 { 92 return nil, &DLLError{ 93 Err: e, 94 ObjName: name, 95 Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(), 96 } 97 } 98 p := &Proc{ 99 Dll: d, 100 Name: name, 101 addr: a, 102 } 103 return p, nil 104 } 105 106 // MustFindProc is like FindProc but panics if search fails. 107 func (d *DLL) MustFindProc(name string) *Proc { 108 p, e := d.FindProc(name) 109 if e != nil { 110 panic(e) 111 } 112 return p 113 } 114 115 // Release unloads DLL d from memory. 116 func (d *DLL) Release() (err error) { 117 return FreeLibrary(d.Handle) 118 } 119 120 // A Proc implements access to a procedure inside a DLL. 121 type Proc struct { 122 Dll *DLL 123 Name string 124 addr uintptr 125 } 126 127 // Addr returns the address of the procedure represented by p. 128 // The return value can be passed to Syscall to run the procedure. 129 func (p *Proc) Addr() uintptr { 130 return p.addr 131 } 132 133 //go:uintptrescapes 134 135 // Call executes procedure p with arguments a. It will panic, if more than 15 arguments 136 // are supplied. 137 // 138 // The returned error is always non-nil, constructed from the result of GetLastError. 139 // Callers must inspect the primary return value to decide whether an error occurred 140 // (according to the semantics of the specific function being called) before consulting 141 // the error. The error will be guaranteed to contain syscall.Errno. 142 func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) { 143 switch len(a) { 144 case 0: 145 return Syscall(p.Addr(), uintptr(len(a)), 0, 0, 0) 146 case 1: 147 return Syscall(p.Addr(), uintptr(len(a)), a[0], 0, 0) 148 case 2: 149 return Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], 0) 150 case 3: 151 return Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], a[2]) 152 case 4: 153 return Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], 0, 0) 154 case 5: 155 return Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], 0) 156 case 6: 157 return Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5]) 158 case 7: 159 return Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], 0, 0) 160 case 8: 161 return Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], 0) 162 case 9: 163 return Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]) 164 case 10: 165 return Syscall12(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], 0, 0) 166 case 11: 167 return Syscall12(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], 0) 168 case 12: 169 return Syscall12(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11]) 170 case 13: 171 return Syscall15(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], 0, 0) 172 case 14: 173 return Syscall15(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], 0) 174 case 15: 175 return Syscall15(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14]) 176 default: 177 panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".") 178 } 179 return 180 } 181 182 // A LazyDLL implements access to a single DLL. 183 // It will delay the load of the DLL until the first 184 // call to its Handle method or to one of its 185 // LazyProc's Addr method. 186 // 187 // LazyDLL is subject to the same DLL preloading attacks as documented 188 // on LoadDLL. 189 // 190 // Use LazyDLL in golang.org/x/sys/windows for a secure way to 191 // load system DLLs. 192 type LazyDLL struct { 193 mu sync.Mutex 194 dll *DLL // non nil once DLL is loaded 195 Name string 196 } 197 198 // Load loads DLL file d.Name into memory. It returns an error if fails. 199 // Load will not try to load DLL, if it is already loaded into memory. 200 func (d *LazyDLL) Load() error { 201 // Non-racy version of: 202 // if d.dll == nil { 203 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) == nil { 204 d.mu.Lock() 205 defer d.mu.Unlock() 206 if d.dll == nil { 207 dll, e := LoadDLL(d.Name) 208 if e != nil { 209 return e 210 } 211 // Non-racy version of: 212 // d.dll = dll 213 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll)) 214 } 215 } 216 return nil 217 } 218 219 // mustLoad is like Load but panics if search fails. 220 func (d *LazyDLL) mustLoad() { 221 e := d.Load() 222 if e != nil { 223 panic(e) 224 } 225 } 226 227 // Handle returns d's module handle. 228 func (d *LazyDLL) Handle() uintptr { 229 d.mustLoad() 230 return uintptr(d.dll.Handle) 231 } 232 233 // NewProc returns a LazyProc for accessing the named procedure in the DLL d. 234 func (d *LazyDLL) NewProc(name string) *LazyProc { 235 return &LazyProc{l: d, Name: name} 236 } 237 238 // NewLazyDLL creates new LazyDLL associated with DLL file. 239 func NewLazyDLL(name string) *LazyDLL { 240 return &LazyDLL{Name: name} 241 } 242 243 // A LazyProc implements access to a procedure inside a LazyDLL. 244 // It delays the lookup until the Addr method is called. 245 type LazyProc struct { 246 mu sync.Mutex 247 Name string 248 l *LazyDLL 249 proc *Proc 250 } 251 252 // Find searches DLL for procedure named p.Name. It returns 253 // an error if search fails. Find will not search procedure, 254 // if it is already found and loaded into memory. 255 func (p *LazyProc) Find() error { 256 // Non-racy version of: 257 // if p.proc == nil { 258 if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil { 259 p.mu.Lock() 260 defer p.mu.Unlock() 261 if p.proc == nil { 262 e := p.l.Load() 263 if e != nil { 264 return e 265 } 266 proc, e := p.l.dll.FindProc(p.Name) 267 if e != nil { 268 return e 269 } 270 // Non-racy version of: 271 // p.proc = proc 272 atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc)) 273 } 274 } 275 return nil 276 } 277 278 // mustFind is like Find but panics if search fails. 279 func (p *LazyProc) mustFind() { 280 e := p.Find() 281 if e != nil { 282 panic(e) 283 } 284 } 285 286 // Addr returns the address of the procedure represented by p. 287 // The return value can be passed to Syscall to run the procedure. 288 func (p *LazyProc) Addr() uintptr { 289 p.mustFind() 290 return p.proc.Addr() 291 } 292 293 //go:uintptrescapes 294 295 // Call executes procedure p with arguments a. It will panic, if more than 15 arguments 296 // are supplied. 297 // 298 // The returned error is always non-nil, constructed from the result of GetLastError. 299 // Callers must inspect the primary return value to decide whether an error occurred 300 // (according to the semantics of the specific function being called) before consulting 301 // the error. The error will be guaranteed to contain syscall.Errno. 302 func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) { 303 p.mustFind() 304 return p.proc.Call(a...) 305 }