cubefs
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
5package windows6
7import (8"sync"9"sync/atomic"10"syscall"11"unsafe"12)
13
14// We need to use LoadLibrary and GetProcAddress from the Go runtime, because
15// the these symbols are loaded by the system linker and are required to
16// dynamically load additional symbols. Note that in the Go runtime, these
17// return syscall.Handle and syscall.Errno, but these are the same, in fact,
18// as windows.Handle and windows.Errno, and we intend to keep these the same.
19
20//go:linkname syscall_loadlibrary syscall.loadlibrary
21func syscall_loadlibrary(filename *uint16) (handle Handle, err Errno)22
23//go:linkname syscall_getprocaddress syscall.getprocaddress
24func syscall_getprocaddress(handle Handle, procname *uint8) (proc uintptr, err Errno)25
26// DLLError describes reasons for DLL load failures.
27type DLLError struct {28Err error29ObjName string30Msg string31}
32
33func (e *DLLError) Error() string { return e.Msg }34
35func (e *DLLError) Unwrap() error { return e.Err }36
37// A DLL implements access to a single DLL.
38type DLL struct {39Name string40Handle Handle
41}
42
43// LoadDLL loads DLL file into memory.
44//
45// Warning: using LoadDLL without an absolute path name is subject to
46// DLL preloading attacks. To safely load a system DLL, use LazyDLL
47// with System set to true, or use LoadLibraryEx directly.
48func LoadDLL(name string) (dll *DLL, err error) {49namep, err := UTF16PtrFromString(name)50if err != nil {51return nil, err52}53h, e := syscall_loadlibrary(namep)54if e != 0 {55return nil, &DLLError{56Err: e,57ObjName: name,58Msg: "Failed to load " + name + ": " + e.Error(),59}60}61d := &DLL{62Name: name,63Handle: h,64}65return d, nil66}
67
68// MustLoadDLL is like LoadDLL but panics if load operation failes.
69func MustLoadDLL(name string) *DLL {70d, e := LoadDLL(name)71if e != nil {72panic(e)73}74return d75}
76
77// FindProc searches DLL d for procedure named name and returns *Proc
78// if found. It returns an error if search fails.
79func (d *DLL) FindProc(name string) (proc *Proc, err error) {80namep, err := BytePtrFromString(name)81if err != nil {82return nil, err83}84a, e := syscall_getprocaddress(d.Handle, namep)85if e != 0 {86return nil, &DLLError{87Err: e,88ObjName: name,89Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),90}91}92p := &Proc{93Dll: d,94Name: name,95addr: a,96}97return p, nil98}
99
100// MustFindProc is like FindProc but panics if search fails.
101func (d *DLL) MustFindProc(name string) *Proc {102p, e := d.FindProc(name)103if e != nil {104panic(e)105}106return p107}
108
109// FindProcByOrdinal searches DLL d for procedure by ordinal and returns *Proc
110// if found. It returns an error if search fails.
111func (d *DLL) FindProcByOrdinal(ordinal uintptr) (proc *Proc, err error) {112a, e := GetProcAddressByOrdinal(d.Handle, ordinal)113name := "#" + itoa(int(ordinal))114if e != nil {115return nil, &DLLError{116Err: e,117ObjName: name,118Msg: "Failed to find " + name + " procedure in " + d.Name + ": " + e.Error(),119}120}121p := &Proc{122Dll: d,123Name: name,124addr: a,125}126return p, nil127}
128
129// MustFindProcByOrdinal is like FindProcByOrdinal but panics if search fails.
130func (d *DLL) MustFindProcByOrdinal(ordinal uintptr) *Proc {131p, e := d.FindProcByOrdinal(ordinal)132if e != nil {133panic(e)134}135return p136}
137
138// Release unloads DLL d from memory.
139func (d *DLL) Release() (err error) {140return FreeLibrary(d.Handle)141}
142
143// A Proc implements access to a procedure inside a DLL.
144type Proc struct {145Dll *DLL146Name string147addr uintptr148}
149
150// Addr returns the address of the procedure represented by p.
151// The return value can be passed to Syscall to run the procedure.
152func (p *Proc) Addr() uintptr {153return p.addr154}
155
156//go:uintptrescapes
157
158// Call executes procedure p with arguments a. It will panic, if more than 15 arguments
159// are supplied.
160//
161// The returned error is always non-nil, constructed from the result of GetLastError.
162// Callers must inspect the primary return value to decide whether an error occurred
163// (according to the semantics of the specific function being called) before consulting
164// the error. The error will be guaranteed to contain windows.Errno.
165func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {166switch len(a) {167case 0:168return syscall.Syscall(p.Addr(), uintptr(len(a)), 0, 0, 0)169case 1:170return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], 0, 0)171case 2:172return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], 0)173case 3:174return syscall.Syscall(p.Addr(), uintptr(len(a)), a[0], a[1], a[2])175case 4:176return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], 0, 0)177case 5:178return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], 0)179case 6:180return syscall.Syscall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5])181case 7:182return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], 0, 0)183case 8:184return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], 0)185case 9:186return syscall.Syscall9(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8])187case 10:188return syscall.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)189case 11:190return syscall.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)191case 12:192return syscall.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])193case 13:194return syscall.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)195case 14:196return syscall.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)197case 15:198return syscall.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])199default:200panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".")201}202}
203
204// A LazyDLL implements access to a single DLL.
205// It will delay the load of the DLL until the first
206// call to its Handle method or to one of its
207// LazyProc's Addr method.
208type LazyDLL struct {209Name string210
211// System determines whether the DLL must be loaded from the212// Windows System directory, bypassing the normal DLL search213// path.214System bool215
216mu sync.Mutex217dll *DLL // non nil once DLL is loaded218}
219
220// Load loads DLL file d.Name into memory. It returns an error if fails.
221// Load will not try to load DLL, if it is already loaded into memory.
222func (d *LazyDLL) Load() error {223// Non-racy version of:224// if d.dll != nil {225if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {226return nil227}228d.mu.Lock()229defer d.mu.Unlock()230if d.dll != nil {231return nil232}233
234// kernel32.dll is special, since it's where LoadLibraryEx comes from.235// The kernel already special-cases its name, so it's always236// loaded from system32.237var dll *DLL238var err error239if d.Name == "kernel32.dll" {240dll, err = LoadDLL(d.Name)241} else {242dll, err = loadLibraryEx(d.Name, d.System)243}244if err != nil {245return err246}247
248// Non-racy version of:249// d.dll = dll250atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))251return nil252}
253
254// mustLoad is like Load but panics if search fails.
255func (d *LazyDLL) mustLoad() {256e := d.Load()257if e != nil {258panic(e)259}260}
261
262// Handle returns d's module handle.
263func (d *LazyDLL) Handle() uintptr {264d.mustLoad()265return uintptr(d.dll.Handle)266}
267
268// NewProc returns a LazyProc for accessing the named procedure in the DLL d.
269func (d *LazyDLL) NewProc(name string) *LazyProc {270return &LazyProc{l: d, Name: name}271}
272
273// NewLazyDLL creates new LazyDLL associated with DLL file.
274func NewLazyDLL(name string) *LazyDLL {275return &LazyDLL{Name: name}276}
277
278// NewLazySystemDLL is like NewLazyDLL, but will only
279// search Windows System directory for the DLL if name is
280// a base name (like "advapi32.dll").
281func NewLazySystemDLL(name string) *LazyDLL {282return &LazyDLL{Name: name, System: true}283}
284
285// A LazyProc implements access to a procedure inside a LazyDLL.
286// It delays the lookup until the Addr method is called.
287type LazyProc struct {288Name string289
290mu sync.Mutex291l *LazyDLL292proc *Proc293}
294
295// Find searches DLL for procedure named p.Name. It returns
296// an error if search fails. Find will not search procedure,
297// if it is already found and loaded into memory.
298func (p *LazyProc) Find() error {299// Non-racy version of:300// if p.proc == nil {301if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {302p.mu.Lock()303defer p.mu.Unlock()304if p.proc == nil {305e := p.l.Load()306if e != nil {307return e308}309proc, e := p.l.dll.FindProc(p.Name)310if e != nil {311return e312}313// Non-racy version of:314// p.proc = proc315atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))316}317}318return nil319}
320
321// mustFind is like Find but panics if search fails.
322func (p *LazyProc) mustFind() {323e := p.Find()324if e != nil {325panic(e)326}327}
328
329// Addr returns the address of the procedure represented by p.
330// The return value can be passed to Syscall to run the procedure.
331// It will panic if the procedure cannot be found.
332func (p *LazyProc) Addr() uintptr {333p.mustFind()334return p.proc.Addr()335}
336
337//go:uintptrescapes
338
339// Call executes procedure p with arguments a. It will panic, if more than 15 arguments
340// are supplied. It will also panic if the procedure cannot be found.
341//
342// The returned error is always non-nil, constructed from the result of GetLastError.
343// Callers must inspect the primary return value to decide whether an error occurred
344// (according to the semantics of the specific function being called) before consulting
345// the error. The error will be guaranteed to contain windows.Errno.
346func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {347p.mustFind()348return p.proc.Call(a...)349}
350
351var canDoSearchSystem32Once struct {352sync.Once353v bool354}
355
356func initCanDoSearchSystem32() {357// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:358// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows359// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on360// systems that have KB2533623 installed. To determine whether the361// flags are available, use GetProcAddress to get the address of the362// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories363// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*364// flags can be used with LoadLibraryEx."365canDoSearchSystem32Once.v = (modkernel32.NewProc("AddDllDirectory").Find() == nil)366}
367
368func canDoSearchSystem32() bool {369canDoSearchSystem32Once.Do(initCanDoSearchSystem32)370return canDoSearchSystem32Once.v371}
372
373func isBaseName(name string) bool {374for _, c := range name {375if c == ':' || c == '/' || c == '\\' {376return false377}378}379return true380}
381
382// loadLibraryEx wraps the Windows LoadLibraryEx function.
383//
384// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
385//
386// If name is not an absolute path, LoadLibraryEx searches for the DLL
387// in a variety of automatic locations unless constrained by flags.
388// See: https://msdn.microsoft.com/en-us/library/ff919712%28VS.85%29.aspx
389func loadLibraryEx(name string, system bool) (*DLL, error) {390loadDLL := name391var flags uintptr392if system {393if canDoSearchSystem32() {394flags = LOAD_LIBRARY_SEARCH_SYSTEM32395} else if isBaseName(name) {396// WindowsXP or unpatched Windows machine397// trying to load "foo.dll" out of the system398// folder, but LoadLibraryEx doesn't support399// that yet on their system, so emulate it.400systemdir, err := GetSystemDirectory()401if err != nil {402return nil, err403}404loadDLL = systemdir + "\\" + name405}406}407h, err := LoadLibraryEx(loadDLL, 0, flags)408if err != nil {409return nil, err410}411return &DLL{Name: name, Handle: h}, nil412}
413
414type errString string415
416func (s errString) Error() string { return string(s) }417