Skip to content

Commit

Permalink
Add test for UAF in multi-threaded context
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstanb committed Jul 1, 2023
1 parent fd5a513 commit a00814f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/regression/71-use_after_free/12-multi-threaded-uaf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//PARAM: --set ana.activated[+] useAfterFree
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

int* gptr;

// Mutex to ensure we don't get race warnings, but the UAF warnings we actually care about
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

void *t_other(void* p) {
pthread_mutex_lock(&mtx);
free(gptr); //WARN
pthread_mutex_unlock(&mtx);
}

int main() {
gptr = malloc(sizeof(int));
*gptr = 42;

pthread_t thread;
pthread_create(&thread, NULL, t_other, NULL);

pthread_mutex_lock(&mtx);
*gptr = 43; //WARN
free(gptr); //WARN
pthread_mutex_unlock(&mtx);

return 0;
}

0 comments on commit a00814f

Please sign in to comment.