@ -19,18 +19,25 @@ module IgnoreVar = struct
end
end
(* * access path that does not ignore the type ( like the original AccessPath.t ) but which instead
(* * access path that does not ignore the type ( like the original AccessPath.t ) but which instead
ignores the root variable for comparisons ; this is taken care of by the root type * )
ignores the root variable for comparisons . * )
type path = ( IgnoreVar . t * Typ . t ) * AccessPath . access list [ @@ deriving compare , equal ]
type path = ( IgnoreVar . t * Typ . t ) * AccessPath . access list [ @@ deriving compare , equal ]
type root =
type t =
| Global of Mangled . t
| Global of { path : AccessPath . t } (* * [AccessPath] so as to include root var in comparison *)
| Class of Typ . name
| Class of { typename : Typ . Name . t } (* * Java-only class object identified by typename *)
| Parameter of int (* * method parameter represented by its 0-indexed position *)
| Parameter of { index : int ; path : path }
(* * method parameter represented by its 0-indexed position *)
[ @@ deriving compare , equal ]
[ @@ deriving compare , equal ]
type t = { root : root ; path : path } [ @@ deriving compare , equal ]
let get_typ tenv =
let class_type = Typ . ( mk ( Tstruct Name . Java . java_lang_class ) ) in
let some_ptr_to_class_type = Some Typ . ( mk ( Tptr ( class_type , Pk_pointer ) ) ) in
function
| Class _ ->
some_ptr_to_class_type
| Global { path } | Parameter { path } ->
AccessPath . get_typ path tenv
let get_typ tenv t = AccessPath . get_typ t . path tenv
let rec norm_path tenv ( ( typ , ( accesses : AccessPath . access list ) ) as path ) =
let rec norm_path tenv ( ( typ , ( accesses : AccessPath . access list ) ) as path ) =
match accesses with
match accesses with
@ -45,38 +52,18 @@ let rec norm_path tenv ((typ, (accesses : AccessPath.access list)) as path) =
let equal_across_threads tenv t1 t2 =
let equal_across_threads tenv t1 t2 =
match ( t1 . root , t2 . root ) with
match ( t1 , t2 ) with
| Global _ , Global _ | Class _ , Class _ ->
| Parameter { path = ( _ , typ1 ) , accesses1 } , Parameter { path = ( _ , typ2 ) , accesses2 } ->
(* globals and class objects must be identical across threads *)
equal t1 t2
| Parameter _ , Parameter _ ->
let ( ( _ , typ1 ) , accesses1 ) , ( ( _ , typ2 ) , accesses2 ) = ( t1 . path , t2 . path ) in
(* parameter position/names can be ignored across threads, if types and accesses are equal *)
(* parameter position/names can be ignored across threads, if types and accesses are equal *)
let path1 , path2 = ( norm_path tenv ( typ1 , accesses1 ) , norm_path tenv ( typ2 , accesses2 ) ) in
let path1 , path2 = ( norm_path tenv ( typ1 , accesses1 ) , norm_path tenv ( typ2 , accesses2 ) ) in
[ % equal : Typ . t * AccessPath . access list ] path1 path2
[ % equal : Typ . t * AccessPath . access list ] path1 path2
| _ , _ ->
| _ , _ ->
false
(* globals and class objects must be identical across threads *)
equal t1 t2
let is_class_object = function { root = Class _ } -> true | _ -> false
(* using an indentifier for a class object, create an access path representing that lock;
this is for synchronizing on Java class objects only * )
let path_of_java_class =
let typ = Typ . ( mk ( Tstruct Name . Java . java_lang_class ) ) in
let typ' = Typ . ( mk ( Tptr ( typ , Pk_pointer ) ) ) in
fun class_id ->
let ident = Ident . create_normal class_id 0 in
AccessPath . of_id ident typ'
let make_global path mangled = { root = Global mangled ; path }
let make_parameter path index = { root = Parameter index ; path }
let make_class path typename = { root = Class typename ; path }
let is_class_object = function Class _ -> true | _ -> false
(* * convert an expression to a canonical form for a lock identifier *)
let rec make formal_map ( hilexp : HilExp . t ) =
let rec make formal_map ( hilexp : HilExp . t ) =
match hilexp with
match hilexp with
| AccessExpression access_exp -> (
| AccessExpression access_exp -> (
@ -86,69 +73,58 @@ let rec make formal_map (hilexp : HilExp.t) =
(* ignore logical variables *)
(* ignore logical variables *)
None
None
| Var . ProgramVar pvar when Pvar . is_global pvar ->
| Var . ProgramVar pvar when Pvar . is_global pvar ->
Some ( make_global path ( Pvar . get_name pvar ) )
Some ( Global { path } )
| Var . ProgramVar _ ->
| Var . ProgramVar _ ->
FormalMap . get_formal_index ( fst path ) formal_map
FormalMap . get_formal_index ( fst path ) formal_map
(* ignores non-formals *)
(* ignores non-formals *)
| > Option . map ~ f : ( make_parameter path ) )
| > Option . map ~ f : ( fun index -> Parameter { index ; path } ) )
| Constant ( Cclass class_id ) ->
| Constant ( Cclass class_id ) ->
(* this is a synchronized/lock ( CLASSNAME.class ) construct *)
(* this is a synchronized ( CLASSNAME.class ) or class object construct *)
let path = path_of_java_class class_id in
let typename = Ident . name_to_string class_id | > Typ . Name . Java . from_string in
let typename = Ident . name_to_string class_id | > Typ . Name . Java . from_string in
Some ( make_class path typename )
Some ( Class { typename } )
| Cast ( _ , hilexp ) | Exception hilexp | UnaryOperator ( _ , hilexp , _ ) ->
| Cast ( _ , hilexp ) | Exception hilexp | UnaryOperator ( _ , hilexp , _ ) ->
make formal_map hilexp
make formal_map hilexp
| BinaryOperator _ | Closure _ | Constant _ | Sizeof _ ->
| BinaryOperator _ | Closure _ | Constant _ | Sizeof _ ->
None
None
let make_java_synchronized formals procname =
let pp fmt t =
match procname with
| Procname . Java java_pname when Procname . Java . is_static java_pname ->
(* this is crafted so as to match synchronized ( CLASSNAME.class ) constructs *)
let typename = Procname . Java . get_class_type_name java_pname in
let path = Typ . Name . name typename | > Ident . string_to_name | > path_of_java_class in
Some ( make_class path typename )
| Procname . Java _ ->
FormalMap . get_formal_base 0 formals | > Option . map ~ f : ( fun base -> make_parameter ( base , [] ) 0 )
| _ ->
L . die InternalError " Non-Java methods cannot be synchronized.@ \n "
let pp fmt { root ; path } =
let pp_path fmt ( ( var , typ ) , accesses ) =
let pp_path fmt ( ( var , typ ) , accesses ) =
F . fprintf fmt " (%a:%a) " Var . pp var ( Typ . pp_full Pp . text ) typ ;
F . fprintf fmt " (%a:%a) " Var . pp var ( Typ . pp_full Pp . text ) typ ;
if not ( List . is_empty accesses ) then F . fprintf fmt " .%a " AccessPath . pp_access_list accesses
if not ( List . is_empty accesses ) then F . fprintf fmt " .%a " AccessPath . pp_access_list accesses
in
in
match root with
match t with
| Global mangled ->
| Global { path } ->
F . fprintf fmt " G<%a>{%a} " Mangled . pp mangled pp_path path
F . fprintf fmt " G{%a} " pp_path path
| Class typename ->
| Class { typename } ->
F . fprintf fmt " C<%a>{%a} " Typ . Name . pp typename pp_path path
F . fprintf fmt " C{%s} " ( Typ . Name . name typename )
| Parameter idx ->
| Parameter { index ; path } ->
F . fprintf fmt " P<%i>{%a} " idx pp_path path
F . fprintf fmt " P<%i>{%a} " index pp_path path
let root_class { path = ( _ , { Typ . desc } ) , _ } =
let root_class = function
match desc with Typ . Tstruct name | Typ . Tptr ( { desc = Tstruct name } , _ ) -> Some name | _ -> None
| Class { typename } ->
Some typename
| Global { path = ( _ , { desc } ) , _ } | Parameter { path = ( _ , { desc } ) , _ } -> (
match desc with
| Tstruct typename | Tptr ( { desc = Tstruct typename } , _ ) ->
Some typename
| _ ->
None )
let describe fmt lock =
let describe fmt t =
let describe_lock fmt lock = ( MF . wrap_monospaced AccessPath . pp ) fmt lock . path in
let describe_root fmt t =
let describe_typename = MF . wrap_monospaced Typ . Name . pp in
root_class t | > Option . iter ~ f : ( F . fprintf fmt " in %a " ( MF . wrap_monospaced Typ . Name . pp ) )
let describe_owner fmt lock =
root_class lock | > Option . iter ~ f : ( F . fprintf fmt " in %a " describe_typename )
in
in
F . fprintf fmt " %a%a " describe_lock lock describe_owner lock
let describe_class_object fmt typename = F . fprintf fmt " %s.class " ( Typ . Name . name typename ) in
match t with
| Class { typename } ->
MF . wrap_monospaced describe_class_object fmt typename
| Global { path } | Parameter { path } ->
F . fprintf fmt " %a%a " ( MF . wrap_monospaced AccessPath . pp ) path describe_root t
let compare_wrt_reporting { path = ( _ , typ1 ) , _ } { path = ( _ , typ2 ) , _ } =
(* use string comparison on types as a stable order to decide whether to report a deadlock *)
String . compare ( Typ . to_string typ1 ) ( Typ . to_string typ2 )
(* * A substitution from formal position indices to actuals. Since we only care about locks, use
[ None ] to denote an argument that cannot be resolved to a lock object . * )
type subst = t option Array . t
type subst = t option Array . t
let pp_subst fmt subst =
let pp_subst fmt subst =
@ -169,15 +145,26 @@ let make_subst formal_map actuals =
subst
subst
let apply_subst ( subst : subst ) lock =
let apply_subst ( subst : subst ) t =
match lock. roo t with
match t with
| Global _ | Class _ ->
| Global _ | Class _ ->
Some lock
Some t
| Parameter index -> (
| Parameter { index ; path = _ , [] } -> (
try
(* Special case for when the parameter is used without additional accesses, eg [x] as opposed to [x.f[].g]. *)
subst . ( index )
with Invalid_argument _ -> None )
| Parameter { index ; path } -> (
try
try
(* Here we know that there are additional accesses on the parameter *)
match subst . ( index ) with
match subst . ( index ) with
| None ->
| None ->
None
None
| Some actual ->
| Some ( Class _ as t' ) as c ->
Some { actual with path = AccessPath . append actual . path ( snd lock . path ) }
L . internal_error " Cannot dereference class object %a in path %a@. " pp t' pp t ;
c
| Some ( Parameter param ) ->
Some ( Parameter { param with path = AccessPath . append param . path ( snd path ) } )
| Some ( Global global ) ->
Some ( Global { path = AccessPath . append global . path ( snd path ) } )
with Invalid_argument _ -> None )
with Invalid_argument _ -> None )