Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine Points-To set on locking a mutex #1343

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/analyses/mutexEventsAnalysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@ struct
let eval_exp_addr (a: Queries.ask) exp = a.f (Queries.MayPointTo exp)

let lock ctx rw may_fail nonzero_return_when_aquired a lv_opt arg =
let compute_refine_split (e:Mutexes.elt) = match e with
| Addr a ->
let e' = BinOp(Eq, arg, AddrOf ((PreValueDomain.Mval.to_cil a)), intType) in
[Events.SplitBranch (e',true)]
| _ -> []
in
match lv_opt with
| None ->
Queries.AD.iter (fun e ->
ctx.split () [Events.Lock (e, rw)]
ctx.split () (Events.Lock (e, rw) :: compute_refine_split e)
) (eval_exp_addr a arg);
if may_fail then
ctx.split () [];
raise Analyses.Deadcode
| Some lv ->
let sb = Events.SplitBranch (Lval lv, nonzero_return_when_aquired) in
Queries.AD.iter (fun e ->
ctx.split () [sb; Events.Lock (e, rw)];
ctx.split () (sb :: Events.Lock (e, rw) :: compute_refine_split e);
) (eval_exp_addr a arg);
if may_fail then (
let fail_exp = if nonzero_return_when_aquired then Lval lv else BinOp(Gt, Lval lv, zero, intType) in
Expand Down
20 changes: 20 additions & 0 deletions tests/regression/79-mutex2/01-split.c
michael-schwarz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<pthread.h>

pthread_mutex_t m1;
pthread_mutex_t m2;

int main(int argc, char const *argv[])
{
int top;
pthread_mutex_t* ptr;
ptr = &m1;

if(top) {
ptr = &m2;
}

pthread_mutex_lock(ptr);
pthread_mutex_unlock(ptr); //NOWARN

return 0;
}
37 changes: 37 additions & 0 deletions tests/regression/79-mutex2/02-split-mt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<pthread.h>

pthread_mutex_t m1;
pthread_mutex_t m2;
pthread_mutex_t* ptr;

void other() {
int top;
ptr = &m2;

if(top) {
ptr = &m1;
}
}

int main(int argc, char const *argv[])
{
int top;

ptr = &m1;

if(top) {
ptr = &m2;
}

pthread_t mischievous;
pthread_create(&mischievous, NULL, other, NULL);


pthread_mutex_lock(ptr);

// This has to produce a warning, as the other thread may have changed what
// ptr points to such that it's not the same mutex being unlocked here.
pthread_mutex_unlock(ptr); //WARN

return 0;
}
Loading