Skip to content

Commit c19d70e

Browse files
Example of heap usage
1 parent 8ef160f commit c19d70e

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// CRAM
2+
// Like 05, but x is a global pointer to a dynamically allocated integer.
3+
#include<pthread.h>
4+
#include<stdlib.h>
5+
#include<goblint.h>
6+
extern void __VERIFIER_atomic_begin();
7+
extern void __VERIFIER_atomic_end();
8+
9+
int *x;
10+
11+
12+
void fun() {
13+
int y = *x;
14+
while (y> 0) {
15+
y--;
16+
}
17+
18+
(*x)++;
19+
}
20+
21+
int main(void) {
22+
int z;
23+
int top;
24+
25+
x = malloc(sizeof(int));
26+
27+
if(top) { *x = 10000; }
28+
29+
pthread_t thread;
30+
pthread_create(&thread, NULL, (void*)fun, NULL);
31+
32+
while(z != 0) {
33+
z--;
34+
}
35+
36+
(*x)++;
37+
38+
pthread_join(thread, NULL);
39+
40+
top = 8;
41+
top = 3;
42+
43+
return 0;
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Run witness validation with `phaseGhost` on the empire example, but with `x`
2+
as a global pointer to a dynamically allocated integer.
3+
4+
$ goblint --disable warn.race --disable warn.integer --enable warn.deterministic --enable ana.sv-comp.functions --set witness.yaml.validate 11-empire-detect-bounds-malloc.yml --set ana.activated[+] phaseGhost --set ana.activated[+] phaseGhostSplit --set ana.path_sens[+] threadflag --set ana.activated[+] threadJoins --set lib.activated[+] sv-comp --set ana.base.privatization protection-atomic-ghost --enable ana.int.interval --set colors never 11-empire-detect-bounds-malloc.c
5+
[Info][Deadcode] Logical lines of code (LLoC) summary:
6+
live: 17
7+
dead: 0
8+
total lines: 17
9+
[Info][Witness] phaseGhost: global ghost_a is only accessed by unique thread [main, fun@11-empire-detect-bounds-malloc.c:30:5-30:51] and is only ever increased by one
10+
[Info][Witness] phaseGhost: global ghost_b is only accessed by unique thread [main] and is only ever increased by one
11+
[Info][Witness] witness validation summary:
12+
confirmed: 3
13+
unconfirmed: 0
14+
refuted: 0
15+
error: 0
16+
unchecked: 0
17+
unsupported: 0
18+
disabled: 0
19+
total validation entries: 3
20+
[Success][Witness] invariant confirmed: (ghost_b == 0 && *x >= 1) || (ghost_b == 1 && *x >= 2) (11-empire-detect-bounds-malloc.c:19:1)
21+
[Success][Witness] invariant confirmed: ghost_a == 1 && *x >= 2 (11-empire-detect-bounds-malloc.c:40:5)
22+
[Success][Witness] invariant confirmed: ghost_b == 1 && *x >= 2 (11-empire-detect-bounds-malloc.c:40:5)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
- entry_type: ghost_instrumentation
2+
metadata:
3+
format_version: "2.1-goblint"
4+
uuid: 00000000-0000-0000-0000-000000000033
5+
creation_time: 2026-04-21T00:00:00Z
6+
producer:
7+
name: Test
8+
version: "0.0"
9+
task:
10+
input_files:
11+
- 11-empire-detect-bounds-malloc.c
12+
input_file_hashes:
13+
11-empire-detect-bounds-malloc.c: "0000000000000000000000000000000000000000000000000000000000000000"
14+
data_model: LP64
15+
language: C
16+
content:
17+
ghost_variables:
18+
- name: ghost_a
19+
scope: global
20+
type: int
21+
initial:
22+
value: "0"
23+
format: c_expression
24+
- name: ghost_b
25+
scope: global
26+
type: int
27+
initial:
28+
value: "0"
29+
format: c_expression
30+
ghost_updates:
31+
- location:
32+
file_name: 11-empire-detect-bounds-malloc.c
33+
line: 18
34+
column: 5
35+
function: fun
36+
updates:
37+
- variable: ghost_a
38+
value: "ghost_a + 1"
39+
format: c_expression
40+
- location:
41+
file_name: 11-empire-detect-bounds-malloc.c
42+
line: 36
43+
column: 5
44+
function: main
45+
updates:
46+
- variable: ghost_b
47+
value: "ghost_b + 1"
48+
format: c_expression
49+
- entry_type: invariant_set
50+
metadata:
51+
format_version: "2.0"
52+
uuid: 00000000-0000-0000-0000-000000000034
53+
creation_time: 2026-04-21T00:00:00Z
54+
producer:
55+
name: Test
56+
version: "0.0"
57+
task:
58+
input_files:
59+
- 11-empire-detect-bounds-malloc.c
60+
input_file_hashes:
61+
11-empire-detect-bounds-malloc.c: "0000000000000000000000000000000000000000000000000000000000000000"
62+
data_model: LP64
63+
language: C
64+
content:
65+
- invariant:
66+
type: location_invariant
67+
location:
68+
file_name: 11-empire-detect-bounds-malloc.c
69+
line: 19
70+
column: 1
71+
function: fun
72+
value: (ghost_b == 0 && *x >= 1) || (ghost_b == 1 && *x >= 2)
73+
format: c_expression
74+
- invariant:
75+
type: location_invariant
76+
location:
77+
file_name: 11-empire-detect-bounds-malloc.c
78+
line: 40
79+
column: 5
80+
function: main
81+
value: ghost_a == 1 && *x >= 2
82+
format: c_expression
83+
- invariant:
84+
type: location_invariant
85+
location:
86+
file_name: 11-empire-detect-bounds-malloc.c
87+
line: 40
88+
column: 5
89+
function: main
90+
value: ghost_b == 1 && *x >= 2
91+
format: c_expression

0 commit comments

Comments
 (0)