blob: 369fe9b1d6d7ba65b6c4071e727c49cb163f1efc [file] [log] [blame]
Tobin Ehlis35308dd2016-10-31 13:27:36 -06001#!/usr/bin/env python3
2# Copyright (c) 2015-2016 The Khronos Group Inc.
3# Copyright (c) 2015-2016 Valve Corporation
4# Copyright (c) 2015-2016 LunarG, Inc.
5# Copyright (c) 2015-2016 Google Inc.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Author: Tobin Ehlis <tobine@google.com>
20
21import argparse
22import os
23import sys
24import platform
25
26# vk_validation_stats.py overview
27# This script is intended to generate statistics on the state of validation code
28# based on information parsed from the source files and the database file
29# Here's what it currently does:
30# 1. Parse vk_validation_error_database.txt to store claimed state of validation checks
31# 2. Parse vk_validation_error_messages.h to verify the actual checks in header vs. the
32# claimed state of the checks
33# 3. Parse source files to identify which checks are implemented and verify that this
34# exactly matches the list of checks claimed to be implemented in the database
35# 4. Parse test file(s) and verify that reported tests exist
36# 5. Report out stats on number of checks, implemented checks, and duplicated checks
37#
Tobin Ehlis20e32582016-12-05 14:50:03 -070038# If a mis-match is found during steps 2, 3, or 4, then the script exits w/ a non-zero error code
39# otherwise, the script will exit(0)
40#
Tobin Ehlis35308dd2016-10-31 13:27:36 -060041# TODO:
42# 1. Would also like to report out number of existing checks that don't yet use new, unique enum
43# 2. Could use notes to store custom fields (like TODO) and print those out here
44# 3. Update test code to check if tests use new, unique enums to check for errors instead of strings
45
46db_file = 'vk_validation_error_database.txt'
47layer_source_files = [
48'core_validation.cpp',
49'descriptor_sets.cpp',
50'parameter_validation.cpp',
51'object_tracker.cpp',
Mike Weiblen6a27de52016-12-09 17:36:28 -070052'image.cpp',
53'swapchain.cpp'
Tobin Ehlis35308dd2016-10-31 13:27:36 -060054]
55header_file = 'vk_validation_error_messages.h'
56# TODO : Don't hardcode linux path format if we want this to run on windows
57test_file = '../tests/layer_validation_tests.cpp'
Tobin Ehlis225b59c2016-12-22 13:59:42 -070058# List of enums that are allowed to be used more than once so don't warn on their duplicates
59duplicate_exceptions = [
Tobin Ehlis1fd9eaf2016-12-22 14:31:01 -070060'VALIDATION_ERROR_00018', # This covers the broad case that all child objects must be destroyed at DestroyInstance time
61'VALIDATION_ERROR_00049', # This covers the broad case that all child objects must be destroyed at DestroyDevice time
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -070062'VALIDATION_ERROR_00515', # Covers valid shader module handle for both Compute & Graphics pipelines
Tobin Ehlise664a2b2016-12-22 14:10:21 -070063'VALIDATION_ERROR_00648', # This is a case for VkMappedMemoryRange struct that is used by both Flush & Invalidate MappedMemoryRange
Tobin Ehlis1fd9eaf2016-12-22 14:31:01 -070064'VALIDATION_ERROR_00741', # This is a blanket case for all invalid image aspect bit errors. The spec link has appropriate details for all separate cases.
65'VALIDATION_ERROR_00768', # This case covers two separate checks which are done independently
66'VALIDATION_ERROR_00769', # This case covers two separate checks which are done independently
67'VALIDATION_ERROR_00942', # This is a descriptor set write update error that we use for a couple copy cases as well
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -070068'VALIDATION_ERROR_00988', # Single error for mis-matched stageFlags of vkCmdPushConstants() that is flagged for no stage flags & mis-matched flags
69'VALIDATION_ERROR_01088', # Handles both depth/stencil & compressed image errors for vkCmdClearColorImage()
70'VALIDATION_ERROR_01223', # Used for the mipLevel check of both dst & src images on vkCmdCopyImage call
71'VALIDATION_ERROR_01224', # Used for the arraySize check of both dst & src images on vkCmdCopyImage call
72'VALIDATION_ERROR_01450', # Used for both x & y bounds of viewport
73'VALIDATION_ERROR_01489', # Used for both x & y value of scissors to make sure they're not negative
74'VALIDATION_ERROR_01926', # Surface of VkSwapchainCreateInfoKHR must be valid when creating both single or shared swapchains
75'VALIDATION_ERROR_02333', # Single error for both imageFormat & imageColorSpace requirements when creating swapchain
Tobin Ehlis1fd9eaf2016-12-22 14:31:01 -070076'VALIDATION_ERROR_02525', # Used twice for the same error codepath as both a param & to set a variable, so not really a duplicate
Tobin Ehlis225b59c2016-12-22 13:59:42 -070077]
Tobin Ehlis35308dd2016-10-31 13:27:36 -060078
79class ValidationDatabase:
80 def __init__(self, filename=db_file):
81 self.db_file = filename
82 self.delimiter = '~^~'
83 self.db_dict = {} # complete dict of all db values per error enum
84 # specialized data structs with slices of complete dict
85 self.db_implemented_enums = [] # list of all error enums claiming to be implemented in database file
86 self.db_enum_to_tests = {} # dict where enum is key to lookup list of tests implementing the enum
87 #self.src_implemented_enums
88 def read(self):
89 """Read a database file into internal data structures, format of each line is <enum><implemented Y|N?><testname><api><errormsg><notes>"""
90 #db_dict = {} # This is a simple db of just enum->errormsg, the same as is created from spec
91 #max_id = 0
92 with open(self.db_file, "r") as infile:
93 for line in infile:
94 line = line.strip()
95 if line.startswith('#') or '' == line:
96 continue
97 db_line = line.split(self.delimiter)
98 if len(db_line) != 6:
Tobin Ehlis027f3212016-12-09 12:15:26 -070099 print("ERROR: Bad database line doesn't have 6 elements: %s" % (line))
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600100 error_enum = db_line[0]
101 implemented = db_line[1]
102 testname = db_line[2]
103 api = db_line[3]
104 error_str = db_line[4]
105 note = db_line[5]
106 # Read complete database contents into our class var for later use
107 self.db_dict[error_enum] = {}
108 self.db_dict[error_enum]['check_implemented'] = implemented
109 self.db_dict[error_enum]['testname'] = testname
110 self.db_dict[error_enum]['api'] = api
111 self.db_dict[error_enum]['error_string'] = error_str
112 self.db_dict[error_enum]['note'] = note
113 # Now build custom data structs
114 if 'Y' == implemented:
115 self.db_implemented_enums.append(error_enum)
116 if testname.lower() not in ['unknown', 'none']:
117 self.db_enum_to_tests[error_enum] = testname.split(',')
118 #if len(self.db_enum_to_tests[error_enum]) > 1:
119 # print "Found check %s that has multiple tests: %s" % (error_enum, self.db_enum_to_tests[error_enum])
120 #else:
121 # print "Check %s has single test: %s" % (error_enum, self.db_enum_to_tests[error_enum])
122 #unique_id = int(db_line[0].split('_')[-1])
123 #if unique_id > max_id:
124 # max_id = unique_id
125 #print "Found %d total enums in database" % (len(self.db_dict.keys()))
126 #print "Found %d enums claiming to be implemented in source" % (len(self.db_implemented_enums))
127 #print "Found %d enums claiming to have tests implemented" % (len(self.db_enum_to_tests.keys()))
128
129class ValidationHeader:
130 def __init__(self, filename=header_file):
131 self.filename = header_file
132 self.enums = []
133 def read(self):
134 """Read unique error enum header file into internal data structures"""
135 grab_enums = False
136 with open(self.filename, "r") as infile:
137 for line in infile:
138 line = line.strip()
139 if 'enum UNIQUE_VALIDATION_ERROR_CODE {' in line:
140 grab_enums = True
141 continue
142 if grab_enums:
143 if 'VALIDATION_ERROR_MAX_ENUM' in line:
144 grab_enums = False
145 break # done
Tobin Ehlisf53eac32016-12-09 14:10:47 -0700146 elif 'VALIDATION_ERROR_UNDEFINED' in line:
147 continue
148 elif 'VALIDATION_ERROR_' in line:
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600149 enum = line.split(' = ')[0]
150 self.enums.append(enum)
151 #print "Found %d error enums. First is %s and last is %s." % (len(self.enums), self.enums[0], self.enums[-1])
152
153class ValidationSource:
154 def __init__(self, source_file_list):
155 self.source_files = source_file_list
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700156 self.enum_count_dict = {} # dict of enum values to the count of how much they're used, and location of where they're used
Tobin Ehlis3d9dd942016-11-23 13:08:01 -0700157 # 1790 is a special case that provides an exception when an extension is enabled. No specific error is flagged, but the exception is handled so add it here
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700158 self.enum_count_dict['VALIDATION_ERROR_01790'] = {}
159 self.enum_count_dict['VALIDATION_ERROR_01790']['count'] = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600160 def parse(self):
161 duplicate_checks = 0
162 for sf in self.source_files:
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700163 line_num = 0
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600164 with open(sf) as f:
165 for line in f:
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700166 line_num = line_num + 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600167 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
168 continue
169 # Find enums
170 #if 'VALIDATION_ERROR_' in line and True not in [ignore in line for ignore in ['[VALIDATION_ERROR_', 'UNIQUE_VALIDATION_ERROR_CODE']]:
Tobin Ehlisf53eac32016-12-09 14:10:47 -0700171 if ' VALIDATION_ERROR_' in line:
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600172 # Need to isolate the validation error enum
173 #print("Line has check:%s" % (line))
174 line_list = line.split()
Tobin Ehlis928742e2016-12-09 17:11:13 -0700175 enum_list = []
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600176 for str in line_list:
Tobin Ehlisf53eac32016-12-09 14:10:47 -0700177 if 'VALIDATION_ERROR_' in str and True not in [ignore_str in str for ignore_str in ['[VALIDATION_ERROR_', 'VALIDATION_ERROR_UNDEFINED', 'UNIQUE_VALIDATION_ERROR_CODE']]:
Tobin Ehlis928742e2016-12-09 17:11:13 -0700178 enum_list.append(str.strip(',);'))
179 #break
180 for enum in enum_list:
181 if enum != '':
182 if enum not in self.enum_count_dict:
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700183 self.enum_count_dict[enum] = {}
184 self.enum_count_dict[enum]['count'] = 1
185 self.enum_count_dict[enum]['file_line'] = []
186 self.enum_count_dict[enum]['file_line'].append('%s,%d' % (sf, line_num))
Tobin Ehlis928742e2016-12-09 17:11:13 -0700187 #print "Found enum %s implemented for first time in file %s" % (enum, sf)
188 else:
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700189 self.enum_count_dict[enum]['count'] = self.enum_count_dict[enum]['count'] + 1
190 self.enum_count_dict[enum]['file_line'].append('%s,%d' % (sf, line_num))
Tobin Ehlis928742e2016-12-09 17:11:13 -0700191 #print "Found enum %s implemented for %d time in file %s" % (enum, self.enum_count_dict[enum], sf)
192 duplicate_checks = duplicate_checks + 1
193 #else:
194 #print("Didn't find actual check in line:%s" % (line))
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600195 #print "Found %d unique implemented checks and %d are duplicated at least once" % (len(self.enum_count_dict.keys()), duplicate_checks)
196
197# Class to parse the validation layer test source and store testnames
198# TODO: Enhance class to detect use of unique error enums in the test
199class TestParser:
200 def __init__(self, test_file_list, test_group_name=['VkLayerTest', 'VkPositiveLayerTest', 'VkWsiEnabledLayerTest']):
201 self.test_files = test_file_list
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700202 self.test_to_errors = {} # Dict where testname maps to list of error enums found in that test
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600203 self.test_trigger_txt_list = []
204 for tg in test_group_name:
205 self.test_trigger_txt_list.append('TEST_F(%s' % tg)
206 #print('Test trigger test list: %s' % (self.test_trigger_txt_list))
207
208 # Parse test files into internal data struct
209 def parse(self):
210 # For each test file, parse test names into set
211 grab_next_line = False # handle testname on separate line than wildcard
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700212 testname = ''
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600213 for test_file in self.test_files:
214 with open(test_file) as tf:
215 for line in tf:
216 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
217 continue
218
219 if True in [ttt in line for ttt in self.test_trigger_txt_list]:
220 #print('Test wildcard in line: %s' % (line))
221 testname = line.split(',')[-1]
222 testname = testname.strip().strip(' {)')
223 #print('Inserting test: "%s"' % (testname))
224 if ('' == testname):
225 grab_next_line = True
226 continue
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700227 self.test_to_errors[testname] = []
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600228 if grab_next_line: # test name on its own line
229 grab_next_line = False
230 testname = testname.strip().strip(' {)')
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700231 self.test_to_errors[testname] = []
232 if ' VALIDATION_ERROR_' in line:
233 line_list = line.split()
234 for str in line_list:
235 if 'VALIDATION_ERROR_' in str and True not in [ignore_str in str for ignore_str in ['VALIDATION_ERROR_UNDEFINED', 'UNIQUE_VALIDATION_ERROR_CODE', 'VALIDATION_ERROR_MAX_ENUM']]:
236 print("Trying to add enums for line: %s")
237 print("Adding enum %s to test %s" % (str.strip(',);'), testname))
238 self.test_to_errors[testname].append(str.strip(',);'))
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600239
240# Little helper class for coloring cmd line output
241class bcolors:
242
243 def __init__(self):
244 self.GREEN = '\033[0;32m'
245 self.RED = '\033[0;31m'
246 self.YELLOW = '\033[1;33m'
247 self.ENDC = '\033[0m'
248 if 'Linux' != platform.system():
249 self.GREEN = ''
250 self.RED = ''
251 self.YELLOW = ''
252 self.ENDC = ''
253
254 def green(self):
255 return self.GREEN
256
257 def red(self):
258 return self.RED
259
260 def yellow(self):
261 return self.YELLOW
262
263 def endc(self):
264 return self.ENDC
265
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600266def main(argv=None):
Tobin Ehlis20e32582016-12-05 14:50:03 -0700267 result = 0 # Non-zero result indicates an error case
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600268 # parse db
269 val_db = ValidationDatabase()
270 val_db.read()
271 # parse header
272 val_header = ValidationHeader()
273 val_header.read()
274 # Create parser for layer files
275 val_source = ValidationSource(layer_source_files)
276 val_source.parse()
277 # Parse test files
278 test_parser = TestParser([test_file, ])
279 test_parser.parse()
280
281 # Process stats - Just doing this inline in main, could make a fancy class to handle
282 # all the processing of data and then get results from that
283 txt_color = bcolors()
284 print("Validation Statistics")
285 # First give number of checks in db & header and report any discrepancies
286 db_enums = len(val_db.db_dict.keys())
287 hdr_enums = len(val_header.enums)
288 print(" Database file includes %d unique checks" % (db_enums))
289 print(" Header file declares %d unique checks" % (hdr_enums))
290 tmp_db_dict = val_db.db_dict
291 db_missing = []
292 for enum in val_header.enums:
293 if not tmp_db_dict.pop(enum, False):
294 db_missing.append(enum)
295 if db_enums == hdr_enums and len(db_missing) == 0 and len(tmp_db_dict.keys()) == 0:
296 print(txt_color.green() + " Database and Header match, GREAT!" + txt_color.endc())
297 else:
298 print(txt_color.red() + " Uh oh, Database doesn't match Header :(" + txt_color.endc())
Tobin Ehlis20e32582016-12-05 14:50:03 -0700299 result = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600300 if len(db_missing) != 0:
301 print(txt_color.red() + " The following checks are in header but missing from database:" + txt_color.endc())
302 for missing_enum in db_missing:
303 print(txt_color.red() + " %s" % (missing_enum) + txt_color.endc())
304 if len(tmp_db_dict.keys()) != 0:
305 print(txt_color.red() + " The following checks are in database but haven't been declared in the header:" + txt_color.endc())
306 for extra_enum in tmp_db_dict:
307 print(txt_color.red() + " %s" % (extra_enum) + txt_color.endc())
308 # Report out claimed implemented checks vs. found actual implemented checks
309 imp_not_found = [] # Checks claimed to implemented in DB file but no source found
310 imp_not_claimed = [] # Checks found implemented but not claimed to be in DB
311 multiple_uses = False # Flag if any enums are used multiple times
312 for db_imp in val_db.db_implemented_enums:
313 if db_imp not in val_source.enum_count_dict:
314 imp_not_found.append(db_imp)
315 for src_enum in val_source.enum_count_dict:
Tobin Ehlis225b59c2016-12-22 13:59:42 -0700316 if val_source.enum_count_dict[src_enum]['count'] > 1 and src_enum not in duplicate_exceptions:
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600317 multiple_uses = True
318 if src_enum not in val_db.db_implemented_enums:
319 imp_not_claimed.append(src_enum)
320 print(" Database file claims that %d checks (%s) are implemented in source." % (len(val_db.db_implemented_enums), "{0:.0f}%".format(float(len(val_db.db_implemented_enums))/db_enums * 100)))
321 if len(imp_not_found) == 0 and len(imp_not_claimed) == 0:
322 print(txt_color.green() + " All claimed Database implemented checks have been found in source, and no source checks aren't claimed in Database, GREAT!" + txt_color.endc())
323 else:
Tobin Ehlis20e32582016-12-05 14:50:03 -0700324 result = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600325 print(txt_color.red() + " Uh oh, Database claimed implemented don't match Source :(" + txt_color.endc())
326 if len(imp_not_found) != 0:
Tobin Ehlis3f0b2772016-11-18 16:56:15 -0700327 print(txt_color.red() + " The following %d checks are claimed to be implemented in Database, but weren't found in source:" % (len(imp_not_found)) + txt_color.endc())
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600328 for not_imp_enum in imp_not_found:
329 print(txt_color.red() + " %s" % (not_imp_enum) + txt_color.endc())
330 if len(imp_not_claimed) != 0:
331 print(txt_color.red() + " The following checks are implemented in source, but not claimed to be in Database:" + txt_color.endc())
332 for imp_enum in imp_not_claimed:
333 print(txt_color.red() + " %s" % (imp_enum) + txt_color.endc())
334 if multiple_uses:
335 print(txt_color.yellow() + " Note that some checks are used multiple times. These may be good candidates for new valid usage spec language." + txt_color.endc())
336 print(txt_color.yellow() + " Here is a list of each check used multiple times with its number of uses:" + txt_color.endc())
337 for enum in val_source.enum_count_dict:
Tobin Ehlis225b59c2016-12-22 13:59:42 -0700338 if val_source.enum_count_dict[enum]['count'] > 1 and enum not in duplicate_exceptions:
Tobin Ehlis3d1f2bd2016-12-22 11:19:15 -0700339 print(txt_color.yellow() + " %s: %d uses in file,line:" % (enum, val_source.enum_count_dict[enum]['count']) + txt_color.endc())
340 for file_line in val_source.enum_count_dict[enum]['file_line']:
341 print(txt_color.yellow() + " \t%s" % (file_line) + txt_color.endc())
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600342 # Now check that tests claimed to be implemented are actual test names
343 bad_testnames = []
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700344 tests_missing_enum = {} # Report tests that don't use validation error enum to check for error case
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600345 for enum in val_db.db_enum_to_tests:
346 for testname in val_db.db_enum_to_tests[enum]:
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700347 if testname not in test_parser.test_to_errors:
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600348 bad_testnames.append(testname)
Tobin Ehlis9a68c982016-12-29 14:51:17 -0700349 else:
350 enum_found = False
351 for test_enum in test_parser.test_to_errors[testname]:
352 if test_enum == enum:
353 #print("Found test that correctly checks for enum: %s" % (enum))
354 enum_found = True
355 if not enum_found:
356 #print("Test %s is not using enum %s to check for error" % (testname, enum))
357 if testname not in tests_missing_enum:
358 tests_missing_enum[testname] = []
359 tests_missing_enum[testname].append(enum)
360 if tests_missing_enum:
361 print(txt_color.yellow() + " \nThe following tests do not use their reported enums to check for the validation error. You may want to update these to pass the expected enum to SetDesiredFailureMsg:" + txt_color.endc())
362 for testname in tests_missing_enum:
363 print(txt_color.yellow() + " Testname %s does not explicitly check for these ids:" % (testname) + txt_color.endc())
364 for enum in tests_missing_enum[testname]:
365 print(txt_color.yellow() + " %s" % (enum) + txt_color.endc())
366 # TODO : Go through all enums found in the test file and make sure they're correctly documented in the database file
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600367 print(" Database file claims that %d checks have tests written." % len(val_db.db_enum_to_tests))
368 if len(bad_testnames) == 0:
369 print(txt_color.green() + " All claimed tests have valid names. That's good!" + txt_color.endc())
370 else:
371 print(txt_color.red() + " The following testnames in Database appear to be invalid:")
Tobin Ehlis20e32582016-12-05 14:50:03 -0700372 result = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600373 for bt in bad_testnames:
Tobin Ehlisb04c2c62016-11-21 15:51:45 -0700374 print(txt_color.red() + " %s" % (bt) + txt_color.endc())
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600375
Tobin Ehlis20e32582016-12-05 14:50:03 -0700376 return result
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600377
378if __name__ == "__main__":
379 sys.exit(main())
380