Skip to content

Commit

Permalink
Use free instead of write access for realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed May 4, 2022
1 parent 1a96db1 commit a8fd1c5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/analyses/libraryFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ struct
| `Read -> o
| `Free -> i

let readsFrees rs fs a x =
match a with
| `Write -> []
| `Read -> keep rs x
| `Free -> keep fs x

let onlyReads ns a x =
match a with
| `Write -> []
Expand Down Expand Up @@ -452,7 +458,7 @@ let invalidate_actions = [
"rand", readsAll; (*safe*)
"gethostname", writesAll; (*unsafe*)
"fork", readsAll; (*safe*)
"realloc", writes [1];(*unsafe*) (* TODO: replace write with free+read *)
"realloc", readsFrees [0; 1] [0]; (* read+free first argument, read second argument *)
"setrlimit", readsAll; (*safe*)
"getrlimit", writes [2]; (*keep [2]*)
"sem_init", readsAll; (*safe*)
Expand Down
15 changes: 15 additions & 0 deletions tests/regression/02-base/76-realloc.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// PARAM: --enable ana.race.free
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
Expand Down Expand Up @@ -30,8 +31,22 @@ void test2() {
realloc(p, sizeof(int)); // RACE!
}

void* test3_f(void *arg) {
int *p = arg;
int x = *p; // RACE!
return NULL;
}

void test3() {
int *p = malloc(sizeof(int));
pthread_t id;
pthread_create(&id, NULL, test3_f, p);
realloc(p, sizeof(int)); // RACE!
}

int main() {
test1();
test2();
test3();
return 0;
}
53 changes: 53 additions & 0 deletions tests/regression/02-base/78-realloc-free.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// PARAM: --disable ana.race.free
// copy of 02-base/76-realloc with different PARAM
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>

void test1_f() {
assert(1); // reachable
}

void test1() {
void (**fpp)(void) = malloc(sizeof(void(**)(void)));
*fpp = &test1_f;

fpp = realloc(fpp, sizeof(void(**)(void))); // same size

// (*fpp)();
void (*fp)(void) = *fpp;
fp(); // should call test1_f
}

void* test2_f(void *arg) {
int *p = arg;
*p = 1; // RACE!
return NULL;
}

void test2() {
int *p = malloc(sizeof(int));
pthread_t id;
pthread_create(&id, NULL, test2_f, p);
realloc(p, sizeof(int)); // RACE!
}

void* test3_f(void *arg) {
int *p = arg;
int x = *p; // NORACE
return NULL;
}

void test3() {
int *p = malloc(sizeof(int));
pthread_t id;
pthread_create(&id, NULL, test3_f, p);
realloc(p, sizeof(int)); // NORACE
}

int main() {
test1();
test2();
test3();
return 0;
}

0 comments on commit a8fd1c5

Please sign in to comment.