xen-analysis.py: Enable Xen deviation tag at the end of the line
cover xen-analysis.py: Enable Xen deviation tag at the end of the line
This serie is enabling the xen-analysis tool to parse and substitute correctly a deviation tag put at the end of the line containing a deviation to be deviated.
Before this serie the only way to deviate a violation was to put the tag in the line above:
/* SAF-<id>-safe deviate the bla bla bla */
<line containing the violation>
But there are places in the code base where using the tag in the line above is not convinient, for example:
if ( (expression) &&
((expression with violation) ||
(expression) ){
[...]
}
In the above example is better to have the suppression comment at the end of the line:
if ( (expression) &&
((expression with violation) || /* SAF-<id>-safe deviate the bla bla bla */
(expression) ){
[...]
}
This clearly brings up the question about the code style line length, which in this case needs to be amended for Xen deviation tags that goes above the limit.
Luca Fancellu (3):
xen-analysis.py: Use named group for tag regex xen-analysis.py: Substitute only the comment instead of the line xen-analysis.py: Accept deviation comment at the end of the line
docs/misra/documenting-violations.rst | 10 ++++ xen/scripts/xen_analysis/cppcheck_analysis.py | 4 +- xen/scripts/xen_analysis/generic_analysis.py | 14 +++-- xen/scripts/xen_analysis/tag_database.py | 59 ++++++++++++++----- xen/scripts/xen_analysis/utils.py | 2 +- 5 files changed, 67 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 xen-analysis.py: Use named group for tag regex
Use named group for the regex matching a tag identifier, this is done to ease retrieving the matching group instead of using an index.
--- xen/scripts/xen_analysis/cppcheck_analysis.py | 4 ++-- xen/scripts/xen_analysis/tag_database.py | 24 +++++++++---------- xen/scripts/xen_analysis/utils.py | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-)
xen/scripts/xen_analysis/cppcheck_analysis.py +2 -2
@@ -41,7 +41,7 @@ def __generate_suppression_list(out_file): # The following lambda function will return a file if it contains lines with # a comment containing "cppcheck-suppress[*]" on a single line. grep_action = lambda x: utils.grep(x, - r'^[ \t]*/\* cppcheck-suppress\[(.*)\] \*/$') + r'^[ \t]*/\* cppcheck-suppress\[(?P<id>.*)\] \*/$') # Look for a list of .h files that matches the condition above headers = utils.recursive_find_file(settings.xen_dir, r'.*\.h$', grep_action)
@@ -97,7 +97,7 @@ def __generate_suppression_list(out_file): if (not comment_section) and comment_line_starts: comment_section = True if (len(line.strip()) != 0) and (not comment_section): - cppcheck_violation_id = entry["matches"][line_num][0] + cppcheck_violation_id = entry["matches"][line_num]['id'] break if comment_section and comment_line_stops: comment_section = False
xen/scripts/xen_analysis/tag_database.py +12 -12
@@ -18,11 +18,12 @@ tool_syntax = { def get_xen_tag_index_type_regex(tool): - return r'^SAF-(\d+)-(safe|false-positive-' + tool + ')$' + return rf'^SAF-(?P<id>\d+)-(?P<type>safe|false-positive-{tool})$' def get_xen_tag_comment_regex(tool): - return r'^[ \t]*/\* +(SAF-\d+-(?:safe|false-positive-' + tool + ')).*\*/$' + tag=rf'(?P<tag>SAF-\d+-(?:safe|false-positive-{tool}))' + return rf'^[ \t]*/\* +{tag}.*\*/$' # Returns a data structure containing dictionaries for safe and false-positive-*
@@ -60,12 +61,11 @@ def load_tag_database(tool, input_files, data_struct = None, schema = "safe"): if proprietary_id != "": comment=tool_syntax[tool].replace("VID",proprietary_id) # Regex to capture the index of the Xen tag and the schema - xen_tag = re.compile(get_xen_tag_index_type_regex(tool))\ - .match(entry["id"]) - if xen_tag and xen_tag.group(1) and xen_tag.group(2): + xen_tag = re.match(get_xen_tag_index_type_regex(tool), entry["id"]) + if xen_tag and xen_tag.group('id') and xen_tag.group('type'): # Save in safe or false-positive-* the key {#id: "comment"} - id_number = int(xen_tag.group(1)) - key = xen_tag.group(2) + id_number = int(xen_tag.group('id')) + key = xen_tag.group('type') ret[key][id_number] = "/* {} */\n".format(comment) else: raise TagDatabaseError(
@@ -95,11 +95,11 @@ def substitute_tags(tool, input_file, grep_struct, subs_rules): # information access the subs_rules dictionary to see if there is # a match for line_number in grep_struct["matches"]: - xen_tag = grep_struct["matches"][line_number][0] - xen_tag_regex_obj = re.compile( - get_xen_tag_index_type_regex(tool)).match(xen_tag) - id_number = int(xen_tag_regex_obj.group(1)) - key = xen_tag_regex_obj.group(2) + xen_tag = grep_struct["matches"][line_number]['tag'] + xen_tag_regex_obj = re.match(get_xen_tag_index_type_regex(tool), + xen_tag) + id_number = int(xen_tag_regex_obj.group('id')) + key = xen_tag_regex_obj.group('type') if id_number in subs_rules[key]: parsed_content[line_number-1] = subs_rules[key][id_number]
xen/scripts/xen_analysis/utils.py +1 -1
@@ -12,7 +12,7 @@ def grep(filepath, regex): for line in f: match = regObj.match(line) if match: - res["matches"][line_number] = match.groups() + res["matches"][line_number] = match line_number = line_number + 1 except Exception as e: print("WARNING: Can't open {}: {}".format(filepath, e))
Signature · 1 line
2.34.1
PATCH 2/3 xen-analysis.py: Substitute only the comment instead of the line
In reply to Luca FancelluChange the code in tag_database.py to substitute only the SAF-* comment instead of replacing the line.
--- xen/scripts/xen_analysis/tag_database.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
xen/scripts/xen_analysis/tag_database.py +13 -5
@@ -16,14 +16,18 @@ tool_syntax = { "eclair":"-E> hide VID 1 \"\"" } +def get_xen_tag_regex(tool): + return rf'(?P<tag>SAF-(?P<id>\d+)-(?P<type>safe|false-positive-{tool}))' + def get_xen_tag_index_type_regex(tool): - return rf'^SAF-(?P<id>\d+)-(?P<type>safe|false-positive-{tool})$' + return rf'^{get_xen_tag_regex(tool)}$' def get_xen_tag_comment_regex(tool): - tag=rf'(?P<tag>SAF-\d+-(?:safe|false-positive-{tool}))' - return rf'^[ \t]*/\* +{tag}.*\*/$' + before_comment = r'(?P<before>[ \t]*)' + comment = rf'(?P<comment>/\* +{get_xen_tag_regex(tool)}.*\*/)' + return rf'^(?P<full_line>{before_comment}{comment})$' # Returns a data structure containing dictionaries for safe and false-positive-*
@@ -66,7 +70,7 @@ def load_tag_database(tool, input_files, data_struct = None, schema = "safe"): # Save in safe or false-positive-* the key {#id: "comment"} id_number = int(xen_tag.group('id')) key = xen_tag.group('type') - ret[key][id_number] = "/* {} */\n".format(comment) + ret[key][id_number] = "/* {} */".format(comment) else: raise TagDatabaseError( "Error in database file, entry {} has unexpected "
@@ -101,7 +105,11 @@ def substitute_tags(tool, input_file, grep_struct, subs_rules): id_number = int(xen_tag_regex_obj.group('id')) key = xen_tag_regex_obj.group('type') if id_number in subs_rules[key]: - parsed_content[line_number-1] = subs_rules[key][id_number] + comment_in = grep_struct["matches"][line_number]['comment'] + comment_out = subs_rules[key][id_number] + parsed_content[line_number-1] = re.sub( + re.escape(comment_in), comment_out, + parsed_content[line_number-1]) outfile.writelines(parsed_content) except Exception as e:
Signature · 1 line
2.34.1
PATCH 3/3 xen-analysis.py: Accept deviation comment at the end of the line
In reply to Luca FancelluImplement the in-code suppression comment at the end of the line. Now it is possible to add a Xen deviaiton comment with the syntax described in the docs at the end of the line affected by the violation, to deviate it.
Eclair natively supports it, so the translation for the tool will be straighforward, but the other tool needs to translate an occurrence of the tag into a suppressino comment at the end of the previous line, this will have a corner case where the line number 1 of the file can't be deviated in this way for such tools.
Updated documentation.
--- docs/misra/documenting-violations.rst | 10 ++++++++ xen/scripts/xen_analysis/cppcheck_analysis.py | 2 +- xen/scripts/xen_analysis/generic_analysis.py | 14 +++++++---- xen/scripts/xen_analysis/tag_database.py | 25 ++++++++++++++++++- 4 files changed, 44 insertions(+), 7 deletions(-)
docs/misra/documenting-violations.rst +10 -0
@@ -210,3 +210,13 @@ will be an entry like the following and the violation id will be in the column Given the violation id "misra-c2012-20.7", the procedure above can be followed to justify this finding. + +Another way to justify the above violation is to put the in-code comment tag +at the end of the affected line:: + +| extern char _start[], _end[], start[]; /* SAF-1-safe [...] */ + +This way of deviating violations needs however to be used only when placing the +tag above the line can't be done. This option suffers from some limitation on +cppcheck and coverity tool that don't support natively the suppression comment +at the end of the line.
xen/scripts/xen_analysis/cppcheck_analysis.py +1 -1
@@ -41,7 +41,7 @@ def __generate_suppression_list(out_file): # The following lambda function will return a file if it contains lines with # a comment containing "cppcheck-suppress[*]" on a single line. grep_action = lambda x: utils.grep(x, - r'^[ \t]*/\* cppcheck-suppress\[(?P<id>.*)\] \*/$') + r'^.*/\* cppcheck-suppress\[(?P<id>.*)\] \*/$') # Look for a list of .h files that matches the condition above headers = utils.recursive_find_file(settings.xen_dir, r'.*\.h$', grep_action)
xen/scripts/xen_analysis/generic_analysis.py +9 -5
@@ -52,11 +52,15 @@ def parse_xen_tags(): os.rename(file, bkp_file) time_bkp_file = os.stat(bkp_file) # Create <file> from <file>.safparse but with the Xen tag parsed - tag_database.substitute_tags(settings.analysis_tool, bkp_file, entry, - subs_list) - # Set timestamp for file equal to bkp_file, so that if the file is - # modified during the process by the user, we can catch it - os.utime(file, (time_bkp_file.st_atime, time_bkp_file.st_mtime)) + try: + tag_database.substitute_tags(settings.analysis_tool, bkp_file, entry, + subs_list) + except Exception as e: + raise ParseTagPhaseError("{}".format(e)) + finally: + # Set timestamp for file equal to bkp_file, so that if the file is + # modified during the process by the user, we can catch it + os.utime(file, (time_bkp_file.st_atime, time_bkp_file.st_mtime)) def build_xen():
xen/scripts/xen_analysis/tag_database.py +24 -1
@@ -25,7 +25,7 @@ def get_xen_tag_index_type_regex(tool): def get_xen_tag_comment_regex(tool): - before_comment = r'(?P<before>[ \t]*)' + before_comment = r'(?P<before>.*)' comment = rf'(?P<comment>/\* +{get_xen_tag_regex(tool)}.*\*/)' return rf'^(?P<full_line>{before_comment}{comment})$'
@@ -106,7 +106,30 @@ def substitute_tags(tool, input_file, grep_struct, subs_rules): key = xen_tag_regex_obj.group('type') if id_number in subs_rules[key]: comment_in = grep_struct["matches"][line_number]['comment'] + before = grep_struct["matches"][line_number]['before'] comment_out = subs_rules[key][id_number] + if before != '' and not re.match(r'^[ \t]+$', before): + # The comment is at the end of some line with some code + if tool == "eclair": + # Eclair supports comment at the end of the line, so + # the only thing to do is use the right syntax in + # the comment, the default version of it is + # deviating the current line and the next one + comment_out = re.sub(r'\d+ ""', '0 ""', comment_out) + else: + # Other tool does not support deviating the same + # line of the comment, so we use a trick and we use + # the comment at the end of the previous line + if line_number-2 < 0: + raise TagDatabaseError( + "The comment {} using the tool '{}' can't " + "stay at the end of the line 1." + .format(comment_in, tool) + ) + parsed_content[line_number-2] = \ + parsed_content[line_number-2].replace("\n", + comment_out + '\n') + comment_out = '' parsed_content[line_number-1] = re.sub( re.escape(comment_in), comment_out, parsed_content[line_number-1])
Signature · 1 line
2.34.1
Quoted 23 lines · Luca Fancellu
This serie is enabling the xen-analysis tool to parse and substitute correctly a deviation tag put at the end of the line containing a deviation to be deviated.
Before this serie the only way to deviate a violation was to put the tag in the line above:
/* SAF-<id>-safe deviate the bla bla bla */
<line containing the violation>But there are places in the code base where using the tag in the line above is not convinient, for example:
if ( (expression) &&
((expression with violation) || (expression) ){
[...]
}In the above example is better to have the suppression comment at the end of the line:
if ( (expression) &&
Quoted 8 lines
((expression with violation) || /* SAF-<id>-safe deviate the bla bla bla */ (expression) ){
[...]
}This clearly brings up the question about the code style line length, which in this case needs to be amended for Xen deviation tags that goes above the limit.
Hi Luca,
I tested the series in a number of configurations and everything works as expected. Great!
For the whole series:
Hi Stefano,
Quoted 34 lines · Stefano Stabellini
Quoted 23 lines · Luca Fancellu
This serie is enabling the xen-analysis tool to parse and substitute correctly a deviation tag put at the end of the line containing a deviation to be deviated.
Before this serie the only way to deviate a violation was to put the tag in the line above:
/* SAF-<id>-safe deviate the bla bla bla */
<line containing the violation>But there are places in the code base where using the tag in the line above is not convinient, for example:
if ( (expression) &&
((expression with violation) || (expression) ) { [...] }In the above example is better to have the suppression comment at the end of the line:
if ( (expression) &&
Quoted 8 lines
((expression with violation) || /* SAF-<id>-safe deviate the bla bla bla */ (expression) ) { [...] }This clearly brings up the question about the code style line length, which in this case needs to be amended for Xen deviation tags that goes above the limit.
Quoted 8 lines
Hi Luca,
I tested the series in a number of configurations and everything works as expected. Great!
For the whole series:
This is now committed.
Cheers,
Signature · 1 line
Julien Grall