Hi Julia,

Holy mackerel, that's amazing. Thank you so much! Very fancy embedding
the ocaml in there, and just using a hash table. Neat idea.

I'm running it (well, a modified version, pasted below) on a codebase
and finding that it fails to replace the "struct rwlock x" with "struct
mtx x" in most cases, except one, which is strange. I wonder if it's the
parser choking on macros it doesn't understand in the code? Or on
something else?

Code is here, if you're curious:

$ git clone https://git.zx2c4.com/wireguard-freebsd
$ cd wireguard-freebsd/src
$ spatch.opt --sp-file doit.cocci -j 4 --recursive-includes 
--include-headers-for-types --include-headers --in-place .

I'll keep playing with it to see what's happening...

Jason

== doit.cocci ==

virtual after_start

@initialize:ocaml@
@@

let has_write_table = Hashtbl.create 101
let has_read_table = Hashtbl.create 101

let ok i m =
  let entry = (i,m) in
  Hashtbl.mem has_write_table entry && not(Hashtbl.mem has_read_table entry)

@hasw depends on !after_start@
identifier i,m;
struct i x;
@@

(
rw_wlock(&x.m)
|
rw_wunlock(&x.m)
)

@script:ocaml@
i << hasw.i;
m << hasw.m;
@@
Hashtbl.replace has_write_table (i,m) ()

@hasr depends on !after_start@
identifier i,m;
struct i x;
@@

(
rw_rlock(&x.m)
|
rw_runlock(&x.m)
)

@script:ocaml@
i << hasr.i;
m << hasr.m;
@@
Hashtbl.replace has_read_table (i,m) ()

@finalize:ocaml depends on !after_start@
wt << merge.has_write_table;
rt << merge.has_read_table;
@@

let redo ts dst =
  List.iter (Hashtbl.iter (fun k _ -> Hashtbl.add dst k ())) ts in
redo wt has_write_table;
redo rt has_read_table;

let it = new iteration() in
it#add_virtual_rule After_start;
it#register()

(* ----------------------------------------------------------- *)

@ty2 depends on after_start@
identifier i;
identifier m : script:ocaml(i) { ok i m };
@@

struct i {
  ...
- struct rwlock m;
+ struct mtx m;
  ...
}

@depends on after_start disable fld_to_ptr@
identifier m;
identifier i : script:ocaml(m) { ok i m };
struct i x;
@@

- rw_wlock
+ mtx_lock
   (&x.m)

@depends on after_start disable fld_to_ptr@
identifier m;
identifier i : script:ocaml(m) { ok i m };
struct i x;
@@

- rw_wunlock
+ mtx_unlock
   (&x.m)

@depends on after_start disable fld_to_ptr@
identifier m;
expression e;
identifier i : script:ocaml(m) { ok i m };
struct i x;
@@

- rw_init(&x.m, e);
+ mtx_init(&x.m, e, NULL, MTX_DEF);

@depends on after_start disable fld_to_ptr@
identifier m;
identifier i : script:ocaml(m) { ok i m };
struct i x;
@@

- rw_destroy
+ mtx_destroy
   (&x.m)

@depends on after_start disable fld_to_ptr, ptr_to_array@
identifier m;
identifier i : script:ocaml(m) { ok i m };
struct i *x;
@@

- rw_wlock
+ mtx_lock
   (&x->m)

@depends on after_start disable fld_to_ptr, ptr_to_array@
identifier m;
identifier i : script:ocaml(m) { ok i m };
struct i *x;
@@

- rw_wunlock
+ mtx_unlock
   (&x->m)

@depends on after_start disable fld_to_ptr, ptr_to_array@
identifier m;
expression e;
identifier i : script:ocaml(m) { ok i m };
struct i *x;
@@

- rw_init(&x->m, e);
+ mtx_init(&x->m, e, NULL, MTX_DEF);

@depends on after_start disable fld_to_ptr, ptr_to_array@
identifier m;
identifier i : script:ocaml(m) { ok i m };
struct i *x;
@@

- rw_destroy
+ mtx_destroy
   (&x->m)
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to