Debian Codegraph @

internal/efw/error_reporting.go

raw symbols SCIP
1 //go:build windows
2
3 package efw
4
5 import (
6 "errors"
7 "fmt"
8 "os"
9 "syscall"
10 "testing"
11
12 "golang.org/x/sys/windows"
13 )
14
15 func init() {
16 if !testing.Testing() {
17 return
18 }
19
20 if isDebuggerPresent() {
21 return
22 }
23
24 if err := configureCRTErrorReporting(); err != nil {
25 fmt.Fprintln(os.Stderr, "WARNING: Could not configure CRT error reporting, tests may hang:", err)
26 }
27 }
28
29 var errErrorReportingAlreadyConfigured = errors.New("error reporting already configured")
30
31 // Configure built-in error reporting of the C runtime library.
32 //
33 // The C runtime emits assertion failures into a graphical message box by default.
34 // This causes a hang in CI environments. This function configures the CRT to
35 // log to stderr instead.
36 func configureCRTErrorReporting() error {
37 const ucrtDebug = "ucrtbased.dll"
38
39 // Constants from crtdbg.h
40 //
41 // See https://doxygen.reactos.org/da/d40/crt_2crtdbg_8h_source.html
42 const (
43 _CRT_ERROR = 1
44 _CRT_ASSERT = 2
45 _CRTDBG_MODE_FILE = 0x1
46 _CRTDBG_MODE_WNDW = 0x4
47 _CRTDBG_HFILE_ERROR = -2
48 _CRTDBG_FILE_STDERR = -4
49 )
50
51 // Load the efW API to trigger loading the CRT. This may fail, in which case
52 // we can't figure out which CRT is being used.
53 // In that case we rely on the error bubbling up via some other path.
54 _ = module.Load()
55
56 ucrtHandle, err := syscall.UTF16PtrFromString(ucrtDebug)
57 if err != nil {
58 return err
59 }
60
61 var handle windows.Handle
62 err = windows.GetModuleHandleEx(0, ucrtHandle, &handle)
63 if errors.Is(err, windows.ERROR_MOD_NOT_FOUND) {
64 // Loading the ebpf api did not pull in the debug UCRT, so there is
65 // nothing to configure.
66 return nil
67 } else if err != nil {
68 return err
69 }
70 defer windows.FreeLibrary(handle)
71
72 setReportModeAddr, err := windows.GetProcAddress(handle, "_CrtSetReportMode")
73 if err != nil {
74 return err
75 }
76
77 setReportMode := func(reportType int, reportMode int) (int, error) {
78 // See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreportmode?view=msvc-170
79 r1, _, err := syscall.SyscallN(setReportModeAddr, uintptr(reportType), uintptr(reportMode))
80 if int(r1) == -1 {
81 return 0, fmt.Errorf("set report mode for type %d: %w", reportType, err)
82 }
83 return int(r1), nil
84 }
85
86 setReportFileAddr, err := windows.GetProcAddress(handle, "_CrtSetReportFile")
87 if err != nil {
88 return err
89 }
90
91 setReportFile := func(reportType int, reportFile int) (int, error) {
92 // See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreportfile?view=msvc-170
93 r1, _, err := syscall.SyscallN(setReportFileAddr, uintptr(reportType), uintptr(reportFile))
94 if int(r1) == _CRTDBG_HFILE_ERROR {
95 return 0, fmt.Errorf("set report file for type %d: %w", reportType, err)
96 }
97 return int(r1), nil
98 }
99
100 reportToFile := func(reportType, defaultMode int) error {
101 oldMode, err := setReportMode(reportType, _CRTDBG_MODE_FILE)
102 if err != nil {
103 return err
104 }
105
106 if oldMode != defaultMode {
107 // Attempt to restore old mode if it was different from the expected default.
108 _, _ = setReportMode(reportType, oldMode)
109 return errErrorReportingAlreadyConfigured
110 }
111
112 oldFile, err := setReportFile(reportType, _CRTDBG_FILE_STDERR)
113 if err != nil {
114 return err
115 }
116
117 if oldFile != -1 {
118 // Attempt to restore old file if it was different from the expected default.
119 _, _ = setReportFile(reportType, oldFile)
120 return errErrorReportingAlreadyConfigured
121 }
122
123 return nil
124 }
125
126 // See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreportmode?view=msvc-170#remarks
127 // for defaults.
128 if err := reportToFile(_CRT_ASSERT, _CRTDBG_MODE_WNDW); err != nil {
129 return err
130 }
131
132 if err := reportToFile(_CRT_ERROR, _CRTDBG_MODE_WNDW); err != nil {
133 return err
134 }
135
136 return nil
137 }
138
139 // isDebuggerPresent returns true if the current process is being debugged.
140 //
141 // See https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-isdebuggerpresent
142 func isDebuggerPresent() bool {
143 kernel32Handle, err := windows.LoadLibrary("kernel32.dll")
144 if err != nil {
145 return false
146 }
147
148 isDebuggerPresentAddr, err := windows.GetProcAddress(kernel32Handle, "IsDebuggerPresent")
149 if err != nil {
150 return false
151 }
152
153 r1, _, _ := syscall.SyscallN(isDebuggerPresentAddr)
154 return r1 != 0
155 }