| 1 |
# We expect a warning with this textual form: |
| 2 |
# |
| 3 |
# . warning: deadlock due to inconsistent lock acquisition order |
| 4 |
# . 17 | acquire_lock_a (); /* { dg-warning "deadlock due to inconsistent lock acquisition order" } */ |
| 5 |
# . | ^~~~~~~~~~~~~~~~~ |
| 6 |
# . Thread: 'Thread 1' |
| 7 |
# . 'foo': event 1 |
| 8 |
# . | |
| 9 |
# . | 9 | { |
| 10 |
# . | | ^ |
| 11 |
# . | | | |
| 12 |
# . | | (1) entering 'foo' |
| 13 |
# . | |
| 14 |
# . +--> 'foo': event 2 |
| 15 |
# . | |
| 16 |
# . | 10 | acquire_lock_a (); |
| 17 |
# . | | ^~~~~~~~~~~~~~~~~ |
| 18 |
# . | | | |
| 19 |
# . | | (2) lock a is now held by thread 1 |
| 20 |
# . | |
| 21 |
# . |
| 22 |
# . Thread: 'Thread 2' |
| 23 |
# . 'bar': event 3 |
| 24 |
# . | |
| 25 |
# . | 15 | { |
| 26 |
# . | | ^ |
| 27 |
# . | | | |
| 28 |
# . | | (3) entering 'bar' |
| 29 |
# . | |
| 30 |
# . +--> 'bar': event 4 |
| 31 |
# . | |
| 32 |
# . | 16 | acquire_lock_b (); |
| 33 |
# . | | ^~~~~~~~~~~~~~~~~ |
| 34 |
# . | | | |
| 35 |
# . | | (4) lock b is now held by thread 2 |
| 36 |
# . | |
| 37 |
|
| 38 |
from sarif import * |
| 39 |
|
| 40 |
import pytest |
| 41 |
|
| 42 |
@pytest.fixture(scope='function', autouse=True) |
| 43 |
def sarif(): |
| 44 |
return sarif_from_env() |
| 45 |
|
| 46 |
def test_basics(sarif): |
| 47 |
schema = sarif['$schema'] |
| 48 |
assert schema == "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json" |
| 49 |
|
| 50 |
version = sarif['version'] |
| 51 |
assert version == "2.1.0" |
| 52 |
|
| 53 |
def test_execution_successful(sarif): |
| 54 |
runs = sarif['runs'] |
| 55 |
run = runs[0] |
| 56 |
|
| 57 |
invocations = run['invocations'] |
| 58 |
assert len(invocations) == 1 |
| 59 |
invocation = invocations[0] |
| 60 |
|
| 61 |
# We expect a mere 'warning' to allow executionSuccessful be true |
| 62 |
assert invocation['executionSuccessful'] == True |
| 63 |
|
| 64 |
def test_result(sarif): |
| 65 |
runs = sarif['runs'] |
| 66 |
run = runs[0] |
| 67 |
results = run['results'] |
| 68 |
|
| 69 |
assert len(results) == 1 |
| 70 |
|
| 71 |
result = results[0] |
| 72 |
assert result['level'] == 'warning' |
| 73 |
assert result['message']['text'] == "deadlock due to inconsistent lock acquisition order" |
| 74 |
|
| 75 |
code_flows = result['codeFlows'] |
| 76 |
assert len(code_flows) == 1 |
| 77 |
|
| 78 |
code_flow = code_flows[0] |
| 79 |
|
| 80 |
thread_flows = code_flow['threadFlows'] |
| 81 |
assert len(thread_flows) == 2 |
| 82 |
|
| 83 |
tf0 = thread_flows[0] |
| 84 |
assert tf0['id'] == 'Thread 1' |
| 85 |
|
| 86 |
tf1 = thread_flows[1] |
| 87 |
assert tf1['id'] == 'Thread 2' |
| 88 |
|
| 89 |
assert len(tf0['locations']) == 3 |
| 90 |
assert tf0['locations'][0]['executionOrder'] == 1 |
| 91 |
assert tf0['locations'][0]['location']['message']['text'] \ |
| 92 |
== "entering 'foo'" |
| 93 |
assert tf0['locations'][1]['executionOrder'] == 2 |
| 94 |
assert tf0['locations'][1]['location']['message']['text'] \ |
| 95 |
== "lock a is now held by thread 1" |
| 96 |
assert tf0['locations'][2]['executionOrder'] == 5 |
| 97 |
assert tf0['locations'][2]['location']['message']['text'] \ |
| 98 |
== "deadlocked due to waiting for lock b in thread 1 (acquired by thread 2 at (4))..." |
| 99 |
|
| 100 |
assert len(tf1['locations']) == 3 |
| 101 |
assert tf1['locations'][0]['executionOrder'] == 3 |
| 102 |
assert tf1['locations'][0]['location']['message']['text'] \ |
| 103 |
== "entering 'bar'" |
| 104 |
assert tf1['locations'][1]['executionOrder'] == 4 |
| 105 |
assert tf1['locations'][1]['location']['message']['text'] \ |
| 106 |
== "lock b is now held by thread 2" |
| 107 |
assert tf1['locations'][2]['executionOrder'] == 6 |
| 108 |
assert tf1['locations'][2]['location']['message']['text'] \ |
| 109 |
== "...whilst waiting for lock a in thread 2 (acquired by thread 1 at (2))" |