Skip to content

Commit bc4b34c

Browse files
Add tests
1 parent 6c0f9fd commit bc4b34c

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
5+
int g;
6+
pthread_once_t once = PTHREAD_ONCE_INIT;
7+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
8+
9+
void fun() {
10+
g++; //NORACE
11+
}
12+
13+
14+
void* thread(void* arg) {
15+
pthread_once(&once, fun);
16+
return NULL;
17+
}
18+
19+
int main(void) {
20+
pthread_t id;
21+
22+
for(int i=0; i < 100; i++) {
23+
// Will receive unknown TID
24+
pthread_create(&id, NULL, thread, NULL);
25+
}
26+
27+
return 0;
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
5+
int g;
6+
pthread_once_t once = PTHREAD_ONCE_INIT;
7+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
8+
9+
void fun() {
10+
g++; //NORACE
11+
g++; //NORACE
12+
}
13+
14+
15+
void* thread(void* arg) {
16+
pthread_once(&once, fun);
17+
return NULL;
18+
}
19+
20+
int main(void) {
21+
pthread_t id;
22+
23+
pthread_create(&id, NULL, thread, NULL);
24+
pthread_once(&once, fun);
25+
26+
return 0;
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce
2+
// Developer used tow different pthread_once_t variables by mistake
3+
#include <pthread.h>
4+
#include <stdio.h>
5+
6+
int g;
7+
pthread_once_t once = PTHREAD_ONCE_INIT;
8+
pthread_once_t once1 = PTHREAD_ONCE_INIT;
9+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
10+
11+
void fun() {
12+
g++; //RACE!
13+
g++; //RACE!
14+
}
15+
16+
17+
void* thread(void* arg) {
18+
pthread_once(&once, fun);
19+
return NULL;
20+
}
21+
22+
int main(void) {
23+
pthread_t id;
24+
25+
pthread_create(&id, NULL, thread, NULL);
26+
pthread_once(&once1, fun);
27+
28+
return 0;
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce
2+
// Function to be called by once is passed as a pointer
3+
#include <pthread.h>
4+
#include <stdio.h>
5+
6+
int g;
7+
void init0();
8+
9+
void* initp = &init0;
10+
pthread_once_t once = PTHREAD_ONCE_INIT;
11+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
12+
13+
void init0() {
14+
g++; //NORACE
15+
}
16+
17+
18+
void init1() {
19+
g++; //NORACE
20+
}
21+
22+
23+
void* thread(void* arg) {
24+
pthread_once(&once, initp);
25+
return NULL;
26+
}
27+
28+
int main(void) {
29+
pthread_t id;
30+
int top;
31+
32+
if(top) { initp = &init1; }
33+
34+
pthread_create(&id, NULL, thread, NULL);
35+
36+
pthread_once(&once, initp);
37+
38+
return 0;
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce
2+
// Function to be called by once is passed as a pointer
3+
#include <pthread.h>
4+
#include <stdio.h>
5+
6+
int g;
7+
void init0();
8+
9+
void* initp = &init0;
10+
pthread_once_t once = PTHREAD_ONCE_INIT;
11+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
12+
13+
void init0() {
14+
g++; //NORACE
15+
}
16+
17+
18+
void init1() {
19+
g++; //NORACE
20+
}
21+
22+
23+
void* thread(void* arg) {
24+
pthread_once(&once, initp);
25+
return NULL;
26+
}
27+
28+
int main(void) {
29+
pthread_t id;
30+
int top;
31+
32+
pthread_create(&id, NULL, thread, NULL);
33+
if(top) { initp = &init1; }
34+
pthread_once(&once, initp);
35+
36+
37+
return 0;
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce
2+
// pthread_once object is passed as a pointer (and it may change)
3+
#include <pthread.h>
4+
#include <stdio.h>
5+
6+
int g;
7+
void init0();
8+
9+
pthread_once_t* optr;
10+
pthread_once_t once = PTHREAD_ONCE_INIT;
11+
pthread_once_t once1 = PTHREAD_ONCE_INIT;
12+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
13+
14+
void init0() {
15+
g++; //RACE
16+
}
17+
18+
19+
void* thread(void* arg) {
20+
pthread_once(&once, init0);
21+
return NULL;
22+
}
23+
24+
int main(void) {
25+
pthread_t id;
26+
int top;
27+
28+
pthread_create(&id, NULL, thread, NULL);
29+
if(top) { optr = &once1; }
30+
pthread_once(optr, init0);
31+
32+
return 0;
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// PARAM: --set ana.activated[+] pthreadOnce --set ana.activated[+] threadJoins --set ana.path_sens[+] threadflag --set ana.base.privatization mutex-meet-tid
2+
// pthread_once object is passed as a pointer (and it may change)
3+
#include <pthread.h>
4+
#include <stdio.h>
5+
6+
int* g;
7+
pthread_once_t once = PTHREAD_ONCE_INIT;
8+
pthread_mutex_t mtx;
9+
10+
void init0() {
11+
g = malloc(sizeof(int));
12+
}
13+
14+
15+
void* thread(void* arg) {
16+
pthread_once(&once, init0);
17+
18+
pthread_mutex_lock(&mtx);
19+
if(g == NULL) {
20+
// Will not happen!
21+
// Also showing that g is not NULL here requires "Extended Support for pthread once" as explained in appendix C.
22+
// It was added manually here so we can show off that at the end we can exclude the in initial value even
23+
// though main didn't write
24+
exit();
25+
}
26+
*g = 4711; //NORACE
27+
pthread_mutex_unlock(&mtx);
28+
29+
return NULL;
30+
}
31+
32+
int main(void) {
33+
pthread_t id, id2;
34+
int top;
35+
36+
pthread_create(&id, NULL, thread, NULL);
37+
pthread_create(&id2, NULL, thread, NULL);
38+
39+
pthread_join(id, NULL);
40+
pthread_join(id2, NULL);
41+
42+
*g = 47; //NORACE
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)