| 1 |
# Coccinelle output module to display Firehose XML |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
""" |
| 6 |
You need Firehose to use this module. |
| 7 |
You can get it at https://github.com/fedora-static-analysis/firehose |
| 8 |
or directly via Pypi: # pip install firehose |
| 9 |
|
| 10 |
In your semantic patches, import Firehose like this: |
| 11 |
coccilib.xml_firehose.import_firehose() |
| 12 |
""" |
| 13 |
|
| 14 |
def import_firehose(): |
| 15 |
""" |
| 16 |
inserts Firehose module into globals() |
| 17 |
|
| 18 |
It is required to proceed this way, as long as we can't use the |
| 19 |
classical approach (from firehose.model import Foo[...]), mainly |
| 20 |
because this module is imported by coccinelle whether or not it |
| 21 |
will be used by the semantic patch (and we don't want to display |
| 22 |
an error message to non-Firehose users. |
| 23 |
""" |
| 24 |
try: |
| 25 |
globals()['firehose'] = __import__('firehose.model') |
| 26 |
except ImportError: |
| 27 |
print("Error: Firehose is not installed or not in the Python path.") |
| 28 |
|
| 29 |
class Analysis(object): |
| 30 |
""" |
| 31 |
An Analysis object wraps the work to keep tracks of results, |
| 32 |
and print the Firehose Analysis at the end. |
| 33 |
""" |
| 34 |
|
| 35 |
def __init__(self, |
| 36 |
use_env_variables=True, |
| 37 |
sut_type=None, |
| 38 |
sut_name=None, |
| 39 |
sut_version=None, |
| 40 |
sut_buildarch=None, |
| 41 |
generator_version="", |
| 42 |
root_path="", |
| 43 |
blank_if_no_results=False): |
| 44 |
""" |
| 45 |
Initializes an analysis and sets its useful variables |
| 46 |
|
| 47 |
Arguments: |
| 48 |
use_env_variables: if set to True, looks for the variables in bash |
| 49 |
environment, otherwise it uses the below parameters |
| 50 |
Do for example before calling spatch: |
| 51 |
export COCCI_SUT_TYPE="debian-source" |
| 52 |
export COCCI_SUT_NAME="package name" |
| 53 |
export COCCI_SUT_VERSION="package version" |
| 54 |
export COCCI_SUT_BUILARCH="pakage buildarch" |
| 55 |
export COCCI_GENERATOR_VERSION="1.0" |
| 56 |
export COCCI_ROOT_PATH="/path/to/project/" |
| 57 |
export COCCI_BLANK_IF_NO_RESULTS=1 |
| 58 |
|
| 59 |
sut_type: "debian-source", "debian-binary" or "source-rpm" |
| 60 |
sut_name: the name of the package or software being analysed |
| 61 |
sut_version: its version |
| 62 |
sut_builarch: its architecture (only for "debian-binary", "source-rpm") |
| 63 |
generator_version: the version of Coccinelle |
| 64 |
root_path: the prefix to remove from the files paths |
| 65 |
e.g. file = /path/to/project/path/to/file |
| 66 |
root_path = /path/to/project/ |
| 67 |
file becomes path/to/file |
| 68 |
blank_if_no_results: defaults to False. If set to True, doesn't |
| 69 |
print anything if the analysis has 0 result. |
| 70 |
""" |
| 71 |
if use_env_variables: |
| 72 |
import os |
| 73 |
|
| 74 |
self.sut_type = os.environ.get("COCCI_SUT_TYPE") or "" |
| 75 |
self.sut_name = os.environ.get("COCCI_SUT_NAME") or "" |
| 76 |
self.sut_version = os.environ.get("COCCI_SUT_VERSION") or "" |
| 77 |
self.sut_buildarch = os.environ.get("COCCI_SUT_BUILDARCH") or "" |
| 78 |
self.generator_version = os.environ.get( |
| 79 |
"COCCI_GENERATOR_VERSION") or "" |
| 80 |
self.root_path = os.environ.get("COCCI_ROOT_PATH") or "" |
| 81 |
self.blank_if_no_results = os.environ.get( |
| 82 |
"COCCI_BLANK_IF_NO_RESULTS") or False |
| 83 |
else: |
| 84 |
self.sut_type = sut_type |
| 85 |
self.sut_name = sut_name |
| 86 |
self.sut_version = sut_version |
| 87 |
self.sut_buildarch = sut_buildarch |
| 88 |
self.generator_version = generator_version |
| 89 |
self.root_path = root_path |
| 90 |
self.blank_if_no_results = blank_if_no_results |
| 91 |
|
| 92 |
self.results = [] |
| 93 |
|
| 94 |
def add_result(self, location, message, cwe=None, testid=None, notes=None, |
| 95 |
severity=None, customfields=None): |
| 96 |
""" |
| 97 |
Adds a result in the analysis |
| 98 |
|
| 99 |
Arguments: |
| 100 |
location: a coccinelle position (cocci.elems.Location) |
| 101 |
message: the message to display with |
| 102 |
""" |
| 103 |
self.results.append(self.__build_issue(location, message, cwe=cwe, |
| 104 |
testid=testid, notes=notes, |
| 105 |
severity=severity, |
| 106 |
customfields=customfields)) |
| 107 |
|
| 108 |
|
| 109 |
def __build_analysis(self, |
| 110 |
sut = None, file_ = None, stats = None, |
| 111 |
gen_name="coccinelle", gen_version=""): |
| 112 |
"""Creates a new Analysis() object. |
| 113 |
|
| 114 |
Keyword arguments: |
| 115 |
sut -- software under test |
| 116 |
file_ -- |
| 117 |
stats -- |
| 118 |
gen_name -- the generator name, default 'coccinelle' |
| 119 |
gen_version -- the generator version |
| 120 |
""" |
| 121 |
|
| 122 |
generator = firehose.model.Generator(name=gen_name, |
| 123 |
version=gen_version) |
| 124 |
metadata = firehose.model.Metadata(generator, sut, file_, stats) |
| 125 |
analysis = firehose.model.Analysis(metadata, []) |
| 126 |
|
| 127 |
return analysis |
| 128 |
|
| 129 |
def __build_issue(self, |
| 130 |
location, message, |
| 131 |
cwe=None, testid=None, notes=None, |
| 132 |
severity=None, customfields=None): |
| 133 |
"""Creates a new Issue() object. |
| 134 |
|
| 135 |
Arguments: |
| 136 |
location -- a coccinelle position (cocci.elems.Location) |
| 137 |
message -- |
| 138 |
|
| 139 |
Keyword arguments: |
| 140 |
cwe -- |
| 141 |
testid -- |
| 142 |
notes -- |
| 143 |
trace -- |
| 144 |
severity -- |
| 145 |
customfields -- |
| 146 |
|
| 147 |
""" |
| 148 |
location = self.__coccilocation_to_firehoselocation(location[0]) |
| 149 |
message = firehose.model.Message(message) |
| 150 |
|
| 151 |
if notes is not None: |
| 152 |
notes = firehose.model.Notes(notes) |
| 153 |
|
| 154 |
if customfields is not None: |
| 155 |
customfields = firehose.model.CustomFields(customfields) |
| 156 |
|
| 157 |
# TODO: trace -> how to pass a trace from spatch to xml_firehose |
| 158 |
trace = None |
| 159 |
|
| 160 |
issue = firehose.model.Issue(cwe, testid, location, message, notes, |
| 161 |
trace, severity=severity, |
| 162 |
customfields=customfields) |
| 163 |
|
| 164 |
return issue |
| 165 |
|
| 166 |
def __coccilocation_to_firehoselocation(self, cocciloc): |
| 167 |
"""Converts a Coccinelle Location object to a Firehose one. |
| 168 |
The range attribute is used (single point not yet supported). |
| 169 |
|
| 170 |
Arguments: |
| 171 |
cocciloc -- coccilib.elems.Location instance |
| 172 |
|
| 173 |
""" |
| 174 |
# removes root_path from the file name |
| 175 |
filename = cocciloc.file |
| 176 |
if filename.startswith(self.root_path): |
| 177 |
filename = filename[len(self.root_path):] |
| 178 |
# removes the slash at begin of the filename if there's one: |
| 179 |
if filename.startswith("/"): |
| 180 |
filename = filename[1:] |
| 181 |
|
| 182 |
file_ = firehose.model.File(filename, "") |
| 183 |
function = firehose.model.Function(cocciloc.current_element) |
| 184 |
range_ = firehose.model.Range( |
| 185 |
firehose.model.Point(int(cocciloc.line), |
| 186 |
int(cocciloc.column)), |
| 187 |
firehose.model.Point(int(cocciloc.line_end), |
| 188 |
int(cocciloc.column_end)) |
| 189 |
) |
| 190 |
|
| 191 |
return firehose.model.Location(file_, function, range_=range_) |
| 192 |
|
| 193 |
def print_analysis(self): |
| 194 |
"""Displays the Firehose XML output of an issue. |
| 195 |
This function is intended to be called from a .cocci file. |
| 196 |
""" |
| 197 |
|
| 198 |
def _extract_version_release(long_version): |
| 199 |
""" |
| 200 |
extracts the version and the release from long_version |
| 201 |
# e.g. 1.5-2-4.2 -> 1.5-2 -- 4.2 |
| 202 |
""" |
| 203 |
sep = long_version.rfind("-") |
| 204 |
if sep == -1: |
| 205 |
version = long_version |
| 206 |
release = "" |
| 207 |
else: |
| 208 |
version = long_version[:sep] |
| 209 |
release = long_version[sep+1:] |
| 210 |
return version, release |
| 211 |
|
| 212 |
# if the analysis has 0 result and blank_if_no_results is True, |
| 213 |
# we don't print anything |
| 214 |
if not(self.results) and self.blank_if_no_results: |
| 215 |
sys.stdout.write("\n") |
| 216 |
return |
| 217 |
|
| 218 |
if self.sut_type == "source-rpm": |
| 219 |
self.sut_release = "" # TODO for RPM |
| 220 |
sut = firehose.model.SourceRpm(self.sut_name, self.sut_version, |
| 221 |
self.sut_release, self.sut_buildarch) |
| 222 |
elif self.sut_type == "debian-binary": |
| 223 |
self.sut_version, self.sut_release = _extract_version_release( |
| 224 |
self.sut_version) |
| 225 |
|
| 226 |
sut = firehose.model.DebianBinary( |
| 227 |
self.sut_name, self.sut_version, |
| 228 |
self.sut_release, self.sut_buildarch) |
| 229 |
|
| 230 |
elif self.sut_type == "debian-source": |
| 231 |
self.sut_version, self.sut_release = _extract_version_release( |
| 232 |
self.sut_version) |
| 233 |
sut = firehose.model.DebianSource( |
| 234 |
self.sut_name, self.sut_version, self.sut_release) |
| 235 |
|
| 236 |
else: |
| 237 |
sut = None |
| 238 |
sys.stderr.write("Warning: sut_type incorrect, should be " |
| 239 |
"debian-source, debian-binary or source-rpm\n") |
| 240 |
|
| 241 |
analysis = self.__build_analysis(sut=sut, |
| 242 |
gen_version=self.generator_version) |
| 243 |
analysis.results = self.results |
| 244 |
|
| 245 |
sys.stdout.write(str(analysis.to_xml_bytes())) |
| 246 |
sys.stdout.write("\n") |