]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
memmod: register exception handler tables
authorJason A. Donenfeld <Jason@zx2c4.com>
Thu, 5 Aug 2021 12:56:48 +0000 (14:56 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 5 Aug 2021 12:56:48 +0000 (14:56 +0200)
Otherwise recent WDK binaries fail on ARM64, where an exception handler
is used for trapping an illegal instruction when ARMv8.1 atomics are
being tested for functionality.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
tun/wintun/memmod/memmod_windows.go
tun/wintun/memmod/syscall_windows.go

index 59450e785ff72c01e13c5818617f6eb67e1467fb..075c03a08ac72d19e33dfa2415b95e086001440c 100644 (file)
@@ -159,6 +159,16 @@ func (module *Module) finalizeSection(sectionData *sectionFinalizeData) error {
        return nil
 }
 
+var rtlAddFunctionTable = windows.NewLazySystemDLL("ntdll.dll").NewProc("RtlAddFunctionTable")
+
+func (module *Module) registerExceptionHandlers() {
+       directory := module.headerDirectory(IMAGE_DIRECTORY_ENTRY_EXCEPTION)
+       if directory.Size == 0 || directory.VirtualAddress == 0 {
+               return
+       }
+       rtlAddFunctionTable.Call(module.codeBase+uintptr(directory.VirtualAddress), uintptr(directory.Size)/unsafe.Sizeof(IMAGE_RUNTIME_FUNCTION_ENTRY{}), module.codeBase)
+}
+
 func (module *Module) finalizeSections() error {
        sections := module.headers.Sections()
        imageOffset := module.headers.OptionalHeader.imageOffset()
@@ -500,6 +510,9 @@ func LoadLibrary(data []byte) (module *Module, err error) {
                return
        }
 
+       // Register exception tables, if they exist.
+       module.registerExceptionHandlers()
+
        // TLS callbacks are executed BEFORE the main loading.
        module.executeTLS()
 
index b79be69e9e0e6a87dbdf626cfbd6dc7609e486bf..a111f92e7a2ddcb452a5a848498c02ae81fcdc28 100644 (file)
@@ -370,6 +370,12 @@ const (
        IMAGE_GUARD_CF_FUNCTION_TABLE_SIZE_SHIFT       = 28
 )
 
+type IMAGE_RUNTIME_FUNCTION_ENTRY struct {
+       BeginAddress      uint32
+       EndAddress        uint32
+       UnwindInfoAddress uint32
+}
+
 const (
        DLL_PROCESS_ATTACH = 1
        DLL_THREAD_ATTACH  = 2