Skip to content

Commit a00814f

Browse files
committed
Add test for UAF in multi-threaded context
1 parent fd5a513 commit a00814f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//PARAM: --set ana.activated[+] useAfterFree
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <pthread.h>
5+
6+
int* gptr;
7+
8+
// Mutex to ensure we don't get race warnings, but the UAF warnings we actually care about
9+
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
10+
11+
void *t_other(void* p) {
12+
pthread_mutex_lock(&mtx);
13+
free(gptr); //WARN
14+
pthread_mutex_unlock(&mtx);
15+
}
16+
17+
int main() {
18+
gptr = malloc(sizeof(int));
19+
*gptr = 42;
20+
21+
pthread_t thread;
22+
pthread_create(&thread, NULL, t_other, NULL);
23+
24+
pthread_mutex_lock(&mtx);
25+
*gptr = 43; //WARN
26+
free(gptr); //WARN
27+
pthread_mutex_unlock(&mtx);
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)