| 1 |
# We expect an aanalyzer warning with a path, referencing the #include |
| 2 |
# both in the warning and in the events within its execution path. |
| 3 |
# In textual form, we'd expect something like: |
| 4 |
# . In file included from PATH/include-chain-2.c:28: |
| 5 |
# . PATH/include-chain-2.h: In function 'test': |
| 6 |
# . PATH/include-chain-2.h:6:3: warning: double-'free' of 'ptr' [CWE-415] [-Wanalyzer-double-free] |
| 7 |
# . 6 | __builtin_free (ptr); |
| 8 |
# . | ^~~~~~~~~~~~~~~~~~~~ |
| 9 |
# . 'test': events 1-2 |
| 10 |
# . 5 | __builtin_free (ptr); |
| 11 |
# . | ^~~~~~~~~~~~~~~~~~~~ |
| 12 |
# . | | |
| 13 |
# . | (1) first 'free' here |
| 14 |
# . 6 | __builtin_free (ptr); |
| 15 |
# . | ~~~~~~~~~~~~~~~~~~~~ |
| 16 |
# . | | |
| 17 |
# . | (2) second 'free' here; first 'free' was at (1) |
| 18 |
|
| 19 |
from sarif import * |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
@pytest.fixture(scope='function', autouse=True) |
| 24 |
def sarif(): |
| 25 |
return sarif_from_env() |
| 26 |
|
| 27 |
def test_basics(sarif): |
| 28 |
schema = sarif['$schema'] |
| 29 |
assert schema == "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json" |
| 30 |
|
| 31 |
version = sarif['version'] |
| 32 |
assert version == "2.1.0" |
| 33 |
|
| 34 |
def test_execution_successful(sarif): |
| 35 |
runs = sarif['runs'] |
| 36 |
run = runs[0] |
| 37 |
|
| 38 |
invocations = run['invocations'] |
| 39 |
assert len(invocations) == 1 |
| 40 |
invocation = invocations[0] |
| 41 |
|
| 42 |
# We expect a mere 'warning' to allow executionSuccessful be true |
| 43 |
assert invocation['executionSuccessful'] == True |
| 44 |
|
| 45 |
def test_result(sarif): |
| 46 |
runs = sarif['runs'] |
| 47 |
run = runs[0] |
| 48 |
results = run['results'] |
| 49 |
|
| 50 |
assert len(results) == 1 |
| 51 |
|
| 52 |
result = results[0] |
| 53 |
assert result['ruleId'] == '-Wanalyzer-double-free' |
| 54 |
assert result['level'] == 'warning' |
| 55 |
assert result['message']['text'] == "double-'free' of 'ptr'" |
| 56 |
assert result["taxa"] == [{"id": "415", |
| 57 |
"toolComponent": {"name": "cwe"}}] |
| 58 |
|
| 59 |
def test_location_relationships(sarif): |
| 60 |
runs = sarif['runs'] |
| 61 |
run = runs[0] |
| 62 |
results = run['results'] |
| 63 |
|
| 64 |
assert len(results) == 1 |
| 65 |
|
| 66 |
result = results[0] |
| 67 |
locations = result['locations'] |
| 68 |
assert len(locations) == 1 |
| 69 |
|
| 70 |
location = locations[0] |
| 71 |
assert 'id' in location |
| 72 |
assert get_location_artifact_uri(location).endswith('include-chain-2.h') |
| 73 |
assert get_location_snippet_text(location) == " __builtin_free (ptr); // 2nd\n" |
| 74 |
|
| 75 |
# We expect one related location, describing the #include |
| 76 |
relatedLocations = result['relatedLocations'] |
| 77 |
assert len(relatedLocations) == 1 |
| 78 |
|
| 79 |
# The "#include "include-chain-1.h" line in include-chain-1.c: |
| 80 |
hash_include_2_h = relatedLocations[0] |
| 81 |
assert 'id' in hash_include_2_h |
| 82 |
assert get_location_snippet_text(hash_include_2_h) == '#include "include-chain-2.h"\n' |
| 83 |
assert get_location_artifact_uri(hash_include_2_h).endswith('include-chain-2.c') |
| 84 |
|
| 85 |
# We expect an execution path |
| 86 |
assert len(result['codeFlows']) == 1 |
| 87 |
codeFlow = result['codeFlows'][0] |
| 88 |
assert len(codeFlow['threadFlows']) == 1 |
| 89 |
threadFlow = codeFlow['threadFlows'][0] |
| 90 |
assert threadFlow['id'] == 'main' |
| 91 |
assert len(threadFlow['locations']) == 2 |
| 92 |
|
| 93 |
assert threadFlow['locations'][0]['location']['message']['text'] \ |
| 94 |
== "first 'free' here" |
| 95 |
assert threadFlow['locations'][0]['location']['physicalLocation']['contextRegion']['snippet']['text'] \ |
| 96 |
== " __builtin_free (ptr); // 1st\n" |
| 97 |
assert threadFlow['locations'][0]['kinds'] == ['release', 'memory'] |
| 98 |
assert threadFlow['locations'][0]['executionOrder'] == 1 |
| 99 |
|
| 100 |
# We should have an embedded link in this event's message to the |
| 101 |
# other event's location within the SARIF file: |
| 102 |
assert threadFlow['locations'][1]['location']['message']['text'] \ |
| 103 |
== "second 'free' here; first 'free' was at [(1)](sarif:/runs/0/results/0/codeFlows/0/threadFlows/0/locations/0)" |
| 104 |
assert threadFlow['locations'][1]['location']['physicalLocation']['contextRegion']['snippet']['text'] \ |
| 105 |
== " __builtin_free (ptr); // 2nd\n" |
| 106 |
assert threadFlow['locations'][1]['kinds'] == ['danger'] |
| 107 |
assert threadFlow['locations'][1]['executionOrder'] == 2 |
| 108 |
|
| 109 |
# Check the various relationships; we expect a directed graph of edges |
| 110 |
# representing the various "isIncludedBy" and "includes" relationships. |
| 111 |
|
| 112 |
# The primary location should be "isIncludedBy" the "#include "include-chain-2.h" line |
| 113 |
assert len(location['relationships']) == 1 |
| 114 |
assert location['relationships'][0]['target'] == hash_include_2_h['id'] |
| 115 |
assert location['relationships'][0]['kinds'] == ["isIncludedBy"] |
| 116 |
|
| 117 |
# Similarly, so should the locations within the threadFlow |
| 118 |
event0_rels = get_location_relationships(threadFlow['locations'][0]['location']) |
| 119 |
assert len(event0_rels) == 1 |
| 120 |
assert event0_rels[0]['target'] == hash_include_2_h['id'] |
| 121 |
assert event0_rels[0]['kinds'] == ["isIncludedBy"] |
| 122 |
|
| 123 |
event1_rels = get_location_relationships(threadFlow['locations'][1]['location']) |
| 124 |
assert len(event1_rels) == 1 |
| 125 |
assert event1_rels[0]['target'] == hash_include_2_h['id'] |
| 126 |
assert event1_rels[0]['kinds'] == ["isIncludedBy"] |
| 127 |
|
| 128 |
# The "#include "include-chain-2.h" line in include-chain-2.c should |
| 129 |
# have an "includes" relationship to the main location and to the |
| 130 |
# two locations in the execution path: |
| 131 |
assert len(hash_include_2_h['relationships']) == 3 |
| 132 |
assert hash_include_2_h['relationships'][0]['target'] == location['id'] |
| 133 |
assert hash_include_2_h['relationships'][0]['kinds'] == ["includes"] |
| 134 |
assert hash_include_2_h['relationships'][1]['target'] == threadFlow['locations'][0]['location']['id'] |
| 135 |
assert hash_include_2_h['relationships'][1]['kinds'] == ["includes"] |
| 136 |
assert hash_include_2_h['relationships'][2]['target'] == threadFlow['locations'][1]['location']['id'] |
| 137 |
assert hash_include_2_h['relationships'][2]['kinds'] == ["includes"] |