Debian Codegraph @

crt/aws-c-common/scripts/appverifier_xml.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 # SPDX-License-Identifier: Apache-2.0.
5
6 # Built-in
7 import xml.etree.ElementTree as ElementTree
8 import argparse
9
10 s_AppVerifier_LogText = "{Application Verifier}logSession"
11 s_AppVerifier_EntryText = "{Application Verifier}logEntry"
12 s_AppVerifier_ErrorSeverities = ["Warning", "Error", "UNKNOWN"]
13
14 # A dictionary to take the error codes and convert them to basic information
15 # on what went wrong.
16 #
17 # How to adjust/learn more:
18 # To add/remove from this list, run "appverif" in a Windows terminal with
19 # administrator privileges and then press F1 to get the help page. Then search
20 # for the error code you got (minus the "0x" part at the beginning) and use the
21 # information there to add/adjust the entry in the dictionary below.
22 s_AppVerifier_ErrorCodeHelp = {
23 "Exceptions": {
24 "0x650": "The application is trying to run code from an address that is non-executable or free"
25 },
26 "Handles": {
27 "0x300": "The function on the top of the stack passed an invalid handle to system routines",
28 "0x301": "The function on the top of the stack passed an invalid TLS index to TLS system routines",
29 "0x302": "The function on the top of the stack called WaitForMultipleObjects with NULL as the address "
30 "of the array of handles to wait for or with zero as the number of handles",
31 "0x303": "The function on the top of the stack passed a NULL handle to system routines",
32 "0x304": "The current thread is currently running code inside the DllMain function of one "
33 "of the DLLs loaded in the current process and it calls WaitForSingleObject or "
34 "WaitForMultipleObjects to wait on a thread handle in the same process",
35 "0x305": "The current thread is calling an API with a handle to an object with an incorrect object type"
36 },
37 "Heaps": {
38 "0x01": "Unknown error encountered that cannot be determined/classified by AppVerifier",
39 "0x02": "The application touched non-accessible page. Typically is caused by a buffer overrun error",
40 "0x03": "A heap created with HEAP_NO_SERIALIZE flag was accessed simultaneously from two threads",
41 "0x04": "The size of the block in a 'HeapAlloc' or 'HeapReAlloc' operation was above any reasonable value",
42 "0x05": "Heap structure did not include magic value from AppVerifier - meaning somehow the internal heap "
43 "structure was corrupted or a bogus value was used as heap handle",
44 "0x06": "Typically means block was allocated in one heap and freed in another",
45 "0x07": "Block was freed twice",
46 "0x08": "Generic error due to corruption in the heap block that AppVerifier cannot place more specifically",
47 "0x09": "Tried to destroy the default process heap",
48 "0x0A": "Access violation raised while executing heap manager code",
49 "0x0B": "AppVerifier could not determine any particular type of corruption for the block. "
50 "Generally means heap points to non-accessible memory area",
51 "0x0C": "AppVerifier could not determine any particular type of corruption for the block. "
52 "Generally happens if during heap free operation you pass an address that poins to a non-accessible memory area. "
53 "Can also occur with double free situations",
54 "0x0D": "Block of memory is written to after being freed",
55 "0x0E": "Freed block marked as non-accessible had access attempt",
56 "0x0F": "Magic pattern added by AppVerifier at end of heap block changed. "
57 "Typically means buffer overrun errors",
58 "0x10": "Buffer underruns",
59 "0x11": "Buffer underruns",
60 "0x12": "Buffer underruns",
61 "0x13": "Non-accessible page at end of heap allocation was touched. Typically caused by a buffer overrun error",
62 "0x14": "Page heap manager detected internal inconsistencies while calling GetProcessHeaps"
63 },
64 "Leak": {
65 "0x900": "Owner DLL of the allocation was dynamically unloaded while owning resources",
66 "0x901": "Owner DLL of the handle was dynamically unloaded while owning resources",
67 "0x902": "Owner DLL of the registry key was dynamically unloaded while owning resources",
68 "0x903": "Owner DLL of the virtual reservation was dynamically unloaded while owning resources",
69 "0x904": "Owner DLL of the SysString was dynamically unloaded while owning resources",
70 "0x905": "DLL registered for power notification was dynamically unloaded without registering",
71 "0x906": "Owner DLL of the COM allocation was dynamically unloaded while owning resources"
72 },
73 "Locks": {
74 "0x200": "A thread is terminated, suspended, or in a state in which it cannot hold a critical section",
75 "0x201": "A DLL has a global variable containing a critical section and the DLL is unloaded but the "
76 "critical section has not been deleted",
77 "0x202": "A heap allocation contains a critical section, the allocation is freed, and the critical section "
78 "has not been deleted",
79 "0x203": "Typicaly means a critical section has been initialized more than once. May mean the critical section "
80 "or its debug information structure has been corrupted",
81 "0x204": "Memory containing a critical section was freed but the critical section has not been deleted using 'DeleteCriticalSection'",
82 "0x205": "The DebugInfo field of the critical section is pointing to freed memory",
83 "0x206": "The owner thread ID is invalid in the current context",
84 "0x207": "The recursion count field of the critical section structure is invalid in the current context",
85 "0x208": "A critical section is owned by a thread if it is deleted or if the critical section is uninitialized",
86 "0x209": "A critical section is released more times than the current thread acquired it",
87 "0x210": "A critical section is used without being initialized or after it has been deleted",
88 "0x211": "A critical section is reinitialized by the current thread",
89 "0x212": "The current thread is calling VirtualFree on a memory block that contains an active critical section",
90 "0x213": "The current thread is calling UnmapViewOfFile on a memory block that contains an active critical section",
91 "0x214": "The current thread calling LeaveCriticalSection but does not own any critical section",
92 "0x215": "The current thread tries to use a private lock that lives inside another DLL"
93 },
94 "Memory": {
95 "0x600": "AppVerifier detects a VirtualFree or a DLL unload with an invalid start adress or size of the memory allocation",
96 "0x601": "AppVerifier detects a VirtualAlloc call with an invalid start adress or size of the memory allocation",
97 "0x602": "AppVerifier detects a MapViewOfFile call with an invalid base address or size of the mapping",
98 "0x603": "AppVerifier detects an IsBadXXXPtr call with an invalid address for the memory buffer to be probed",
99 "0x604": "AppVerifier detects an IsBadXXXPtr call for a memory allocation that is free",
100 "0x605": "AppVerifier detects an IsBadXXXPtr call for a memory allocation that contains at least one GUARD_PAGE",
101 "0x606": "AppVerifier detects an IsBadXXXPtr call with a NULL address",
102 "0x607": "AppVerifier detects an IsBadXXXPtr call with an invalid start address or invalid size for the memory buffer to be probed",
103 "0x608": "AppVerifier detects a DLL unload with an invalid start address for the size of the DLL memory range",
104 "0x609": "AppVerifier detects a VirtualFree for a block of memory that is actually part of the current thread's stack",
105 "0x60A": "AppVerifier detects a VirtualFree with an incorrect value for the FreeType parameter",
106 "0x60B": "AppVerifier detects a VirtualFree for an address that is already free",
107 "0x60C": "AppVerifier detects a VirtualFree with a non-zero value for the dwSize parameter",
108 "0x60D": "A DLL's entry point function is raising an exception",
109 "0x60E": "A thread function is raising an exception",
110 "0x60F": "An exception occured during an IsBadXXXPtr call",
111 "0x610": "AppVerifier detects a VirtualFree call with a NULL first parameter",
112 "0x612": "AppVerifier detects a HeapFree for a block of memory that is actually part of the current thread's stack",
113 "0x613": "AppVerifier detects an UnmapViewOfFile for a block of memory that is actually part of the current thread's stack",
114 "0x614": "The application is trying to use NULL or some other incorrect address as the address of a valid object",
115 "0x615": "The application is trying to use NULL or some other incorrect address as the address of a valid object",
116 "0x616": "The application is trying to run code from an address that is non-executable or free",
117 "0x617": "An exception occurred while initializing a buffer specified as output parameter for a Win32 or (non-AWS) CRT API",
118 "0x618": "An exception occurred while calling HeapSize for a heap block that is being freed",
119 "0x619": "The program is calling VirtualFree with an IpAddress parameter that is not the base address returned by "
120 "the VirtualAlloc or VirtualAllocEx function when the region of pages was reserved",
121 "0x61A": "The program is calling UnmapViewOfFile with an IpBaseAddress parameter that is not identical to the value returned"
122 "by a previous call to the MapViewOfFile or MapViewOfFileEx function",
123 "0x61B": "A callback function in the threadpool thread is rasing an exception",
124 "0x61C": "The application is trying to run code from an address that is non-executable or free",
125 "0x61D": "The application is created an executable heap",
126 "0x61E": "The application is allocating executable memory"
127 },
128 "SRWLock": {
129 "0x250": "A thread tried to use SRW lock that is not initalized",
130 "0x251": "The SRW lock is being re-initialized",
131 "0x252": "The SRW lock is being released with a wrong release API",
132 "0x253": "The SRW lock is being acquired recursively by the same thread",
133 "0x254": "The thread that owns the SRW lock is exiting or being terminated",
134 "0x255": "The SRW lock is being released by the thread that did not acquire the lock",
135 "0x256": "The memory address being freed contains an active SRW lock that is still in use",
136 "0x257": "The DLL being unloaded contains an active SRW lock that is still in use"
137 },
138 "Threadpool": {
139 "0x700": "Thread priority is changed when thread is returned to threadpool",
140 "0x701": "Thread affinity is changed when thread is returned to threadpool",
141 "0x702": "One or more messages left as unprocessed when threadpool thread is returned to the threadpool",
142 "0x703": "Any window is kept alive when threadpool thread is returned to the threadpool",
143 "0x704": "ExitThread is called on a threadpool thread",
144 "0x705": "Callback function changed the thread token to impersonate another user and forgot to reset it before "
145 "returning it to the threadpool",
146 "0x706": "Windows API that requires dedicated or persistent thread called from threadpool",
147 "0x707": "Callback function forgot to close or reset the current transaction handle",
148 "0x708": "Callback function called CoInit and CoUnInit in differing amounts (unbalanced)",
149 "0x709": "The period to signal the timer is not zero when the timer is set to signal only once with the WT_EXECUTEONLYONCE flag",
150 "0x70A": "The loader lock is held within the callback and is not released when the thread is returned to the threadpool",
151 "0x70B": "The preferred language is set within the callback and is not cleared when the thread is returned to the threadpool",
152 "0x70C": "The background priority is set within the callback and is not disabled when the thread is returned to the threadpool",
153 "0x70D": "TerminateThread called on a threadpool thread",
154 },
155 "TLS": {
156 "0x350": "A DLL that allocated a TLS index is being unloaded before freeing that TLS index",
157 "0x351": "The internal verifier structures used to store the state of TLS slots for thread are corrupted",
158 "0x352": "An invalid TLS index is used"
159 }
160 }
161
162
163 def parseXML(filepath, dump_xml_on_error):
164 xml_is_app_verifier = False
165 app_verifier_entries = []
166
167 print("Looking for AppVerifier XML file...")
168 xml_tree = ElementTree.parse(filepath)
169
170 # Go through every element in the XML tree
171 for elem in xml_tree.iter():
172 if (elem.tag == s_AppVerifier_LogText):
173 xml_is_app_verifier = True
174 elif (elem.tag == s_AppVerifier_EntryText):
175 app_verifier_entries.append(elem)
176
177 # If the XML does not have any AppVerifier data, then something went wrong!
178 if (xml_is_app_verifier == False):
179 print("ERROR: XML File from AppVerifier does not include a AppVerifier session!")
180 return -1
181
182 # If we have AppVerifier entries, then a test or tests failed, so process the data,
183 # print it, and then return with an error to stop the GitHub action from passing
184 if (len(app_verifier_entries) > 0):
185 print("WARNING: AppVerifier entries found:")
186 severity_error_found = False
187
188 for entry in app_verifier_entries:
189 element_time = entry.attrib.get("Time", "UNKNOWN")
190 element_layer_name = entry.attrib.get("LayerName", "UNKNOWN")
191 element_code = entry.attrib.get("StopCode", "UNKNOWN")
192 element_severity = entry.attrib.get("Severity", "UNKNOWN")
193
194 print_red = False
195 if (element_severity in s_AppVerifier_ErrorSeverities):
196 severity_error_found = True
197 print_red = True
198
199 if (print_red):
200 print(
201 f"ERROR: [{element_time}] {element_severity.upper()} - Test: {element_layer_name} - Stop Code: {element_code}")
202 else:
203 print(
204 f"[{element_time}] {element_severity.upper()} - Test: {element_layer_name} - Stop Code: {element_code}")
205 print(f"\t{getErrorCodeMeaning(element_layer_name, element_code)}")
206
207 print(
208 "\nNOTE: The error codes and information provided are just guesses based on the error code.\n"
209 "\tRun AppVerifier locally and use WinDBG combined with the AppVerifier help to discover more "
210 "about the error from its error code and how to debug it.")
211
212 if (severity_error_found == True and dump_xml_on_error != None):
213 if (dump_xml_on_error == True):
214 print("\nERROR: Raw XML output for errors found:\n")
215 for entry in app_verifier_entries:
216 print(ElementTree.tostring(
217 entry, encoding="unicode"))
218
219 if (severity_error_found == True):
220 print(
221 "\nERROR: Failed due to AppVerifier finding entries marked as severe")
222 return -1
223 else:
224 print("SUCCESS: AppVerifier entries were not marked as severe")
225 return 0
226 else:
227 print("SUCCESS: No AppVerifier entries found! AppVerifier ran successfully and did not generate any entries")
228 return 0
229
230
231 def getErrorCodeMeaning(element_layer_name, element_code):
232 if (element_layer_name in s_AppVerifier_ErrorCodeHelp):
233 layer_codes = s_AppVerifier_ErrorCodeHelp[element_layer_name]
234 if (element_code in layer_codes):
235 return layer_codes[element_code]
236 else:
237 return "Util-script unknown error: " + element_code + " for layer " + element_layer_name
238 return "Util-script unknown layer: " + element_layer_name + " and error code: " + element_code
239
240
241 def booleanString(string):
242 string = string.lower()
243 if string not in {"false", "true"}:
244 raise ValueError("Boolean is not true or false!")
245 return string == "true"
246
247
248 def main():
249 argument_parser = argparse.ArgumentParser(
250 description="AppVerifier XML output util")
251
252 argument_parser.add_argument("--xml_file", metavar="<C:\\example\\file.xml>",
253 required=False, help="Path to XML file from AppVerifier")
254 argument_parser.add_argument("--dump_xml_on_error", metavar="<True/False>", default=True, required=False,
255 type=booleanString, help="If true, the XML for found issues will be printed to the console")
256
257 parsed_commands = argument_parser.parse_args()
258
259 print("\nStarting AppVerifier XML check...", flush=True)
260 print(parsed_commands.dump_xml_on_error)
261 xml_result = parseXML(parsed_commands.xml_file,
262 parsed_commands.dump_xml_on_error)
263 print("\n")
264 exit(xml_result)
265
266
267 if __name__ == "__main__":
268 main()