Skip to content

Commit

Permalink
analysis of method call
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0mz committed Jul 11, 2024
1 parent 2b09e86 commit 38ca8fa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
1 change: 0 additions & 1 deletion lib/ast/normalize.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,6 @@ and normalize_body_element (is_declaration : bool) (class_id : m Identifier.t) (
let assign = Option.map (Statement.StaticUpdate.build (loc_f loc) class_proto (Identifier.get_name id) false) val_expr in
val_stmts @ Option.to_list assign

(* TODO : saw some *)
| PrivateField _ -> []

and get_key_identifier (key : ('M, 'T) Ast'.Expression.Object.Property.key) : m Identifier.t =
Expand Down
18 changes: 12 additions & 6 deletions lib/ast/structures/grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ and Statement : sig

module AssignMetCallStatic : sig
type 'M t = {
id : int;
id_call : int;
id_retn : int;
left : 'M Identifier.t;
(* -- right -- *)
_object : 'M Expression.t;
Expand All @@ -492,7 +493,8 @@ and Statement : sig

module AssignMetCallDynmic : sig
type 'M t = {
id : int;
id_call : int;
id_retn : int;
left : 'M Identifier.t;
(* -- right -- *)
_object : 'M Expression.t;
Expand Down Expand Up @@ -1090,7 +1092,8 @@ end = struct

module AssignMetCallStatic = struct
type 'M t = {
id : int;
id_call : int;
id_retn : int;
left : 'M Identifier.t;
(* -- right -- *)
_object : 'M Expression.t;
Expand All @@ -1102,7 +1105,8 @@ end = struct

let build (metadata : 'M) (left' : 'M Identifier.t) (_object' : 'M Expression.t) (property' : string) (is_literal' : bool) (arguments' : 'M Expression.t list) : 'M Statement.t =
let assign_info = Statement.AssignMetCallStatic {
id = get_id ();
id_call = get_id ();
id_retn = get_id ();
left = left';
_object = _object';
property = property';
Expand All @@ -1115,7 +1119,8 @@ end = struct

module AssignMetCallDynmic = struct
type 'M t = {
id : int;
id_call : int;
id_retn : int;
left : 'M Identifier.t;
(* -- right -- *)
_object : 'M Expression.t;
Expand All @@ -1125,7 +1130,8 @@ end = struct

let build (metadata : 'M) (left' : 'M Identifier.t) (_object' : 'M Expression.t) (property' : 'M Expression.t) (arguments' : 'M Expression.t list) : 'M Statement.t =
let assign_info = Statement.AssignMetCallDynmic {
id = get_id ();
id_call = get_id ();
id_retn = get_id ();
left = left';
_object = _object';
property = property';
Expand Down
60 changes: 51 additions & 9 deletions lib/mdg/analyse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ and analyse (state : state) (statement : m Statement.t) : unit =
| loc, AssignBinary {left; opLeft; opRght; id; _} ->
let _L1, _L2 = eval_expr opLeft, eval_expr opRght in
let l_i = alloc id in
LocationSet.iter (flip add_dep_edge l_i) (LocationSet.union _L1 _L2);
LocationSet.apply (flip add_dep_edge l_i) (LocationSet.union _L1 _L2);
store_update left (LocationSet.singleton l_i);
(* add node info*)
add_node l_i (Identifier.get_name left) loc

| loc, AssignUnary {left; argument; id; _} ->
let _L1 = eval_expr argument in
let l_i = alloc id in
LocationSet.iter (flip add_dep_edge l_i) _L1;
LocationSet.apply (flip add_dep_edge l_i) _L1;
store_update left (LocationSet.singleton l_i);
(* add node info*)
add_node l_i (Identifier.get_name left) loc
Expand Down Expand Up @@ -125,8 +125,8 @@ and analyse (state : state) (statement : m Statement.t) : unit =

let _L1, _L2 = eval_expr _object, eval_expr right in
let _L1' = new_version store _L1 property id add_node' in
LocationSet.iter ( fun l_1 ->
LocationSet.iter (fun l_2 ->
LocationSet.apply ( fun l_1 ->
LocationSet.apply (fun l_2 ->
add_prop_edge l_1 l_2 (Some property)
) _L2
) _L1';
Expand All @@ -140,8 +140,8 @@ and analyse (state : state) (statement : m Statement.t) : unit =
eval_expr right in

let _L1' = new_version' store _L1 _L2 id add_node' in
LocationSet.iter ( fun l_1 ->
LocationSet.iter (fun l_3 ->
LocationSet.apply ( fun l_1 ->
LocationSet.apply (fun l_3 ->
add_prop_edge l_1 l_3 None
) _L3
) _L1';
Expand All @@ -166,9 +166,10 @@ and analyse (state : state) (statement : m Statement.t) : unit =
let params = map_default get_param_names [] f_id in
List.iteri ( fun i _Ls ->
let param_name = Option.value (List.nth_opt params i) ~default:"undefined" in
LocationSet.iter (fun l -> add_arg_edge l l_call i param_name) _Ls
LocationSet.apply (fun l -> add_arg_edge l l_call (string_of_int i) param_name) _Ls
) _Lss;

(* checks if it is a sink and process it accordingly *)
(* TODO : only as temporary solution *)
(* TODO : also there is node per sink, rn we only accept if the sink is "eval" *)
if (f = "eval") then (
Expand All @@ -177,7 +178,7 @@ and analyse (state : state) (statement : m Statement.t) : unit =
add_sink_edge l_call l_tsink f;

List.iter ( fun _Ls ->
LocationSet.iter (fun l -> add_dep_edge l l_tsink) _Ls
LocationSet.apply (fun l -> add_dep_edge l l_tsink) _Ls
) _Lss;
);

Expand All @@ -190,6 +191,15 @@ and analyse (state : state) (statement : m Statement.t) : unit =
let l_f = Graph.get_func_node graph id in
add_call_edge l_call (Option.get l_f)
) f_id;

| loc, AssignMetCallStatic {left; _object; property; arguments; id_call; id_retn; _} ->
analyse_method_call state loc left _object property arguments id_call id_retn;

| loc, AssignMetCallDynmic {left; _object; property; arguments; id_call; id_retn; _} ->
let property' = Expression.get_id property in
analyse_method_call state loc left _object property' arguments id_call id_retn;



(* -------- I F -------- *)
| _, If {consequent; alternate; _} ->
Expand Down Expand Up @@ -230,7 +240,7 @@ and analyse (state : state) (statement : m Statement.t) : unit =

if (Option.is_some _L) then (
let _L = Option.get _L in
LocationSet.iter (flip add_dep_edge l_retn) _L
LocationSet.apply (flip add_dep_edge l_retn) _L
);

add_node l_retn "PDG_RETURN" loc;
Expand Down Expand Up @@ -269,8 +279,40 @@ and ifp (f : state -> unit) (state : state) : unit =
if not (Store.equal state.store store')
then ifp f state

and analyse_method_call (state : state) (loc : Location.t) (left : m Identifier.t) (_object : m Expression.t) (property : property) (arguments : m Expression.t list) (id_call : int) (id_retn : int) : unit =
(* aliases *)
let eval_expr = eval_expr state.store state.this in
let store_update = Store.update state.store in
let alloc = Graph.alloc state.graph in
let add_node = Graph.add_obj_node state.graph in
let add_ret_edge = Graph.add_ret_edge state.graph in
let add_arg_edge = Graph.add_arg_edge state.graph in



let _Lss = List.map eval_expr arguments in

let _Lthis = eval_expr _object in
let l_call = alloc id_call in
let l_retn = alloc id_retn in

(* get function definition information *)
let f = Expression.get_id _object ^ "." ^ property in

(* node information *)
add_node l_call (f ^ "()") loc;
add_node l_retn (Identifier.get_name left) loc;

(* ! graphjs only adds edge for this property *)
LocationSet.apply (fun l_this -> add_arg_edge l_this l_call "this" "this") _Lthis;
List.iteri ( fun i _Ls ->
LocationSet.apply (fun l -> add_arg_edge l l_call (string_of_int i) "undefined") _Ls
) _Lss;

(* return edge *)
add_ret_edge l_call l_retn;
store_update left (LocationSet.singleton l_retn);

and eval_expr (store : Store.t) (this : LocationSet.t) (expr : m Expression.t) : LocationSet.t =
match expr with
| (_, Identifier _) as id ->
Expand Down
26 changes: 13 additions & 13 deletions lib/mdg/structures/graph'.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module Edge = struct
| Property of property option
| Version of property option
| Dependency
| Argument of int (* argument index *) * string (* argument name *)
| Argument of string (* argument index *) * string (* argument name *)
| Parameter of string
| Call
| Return
Expand Down Expand Up @@ -180,7 +180,7 @@ module Edge = struct

let get_arg_i (edge : t) : string =
match edge._type with
(* | Argument (i, _) -> string_of_int i *)
(* | Argument (i, _) -> i *)
| _ -> ""

let get_par_i (edge : t) : string =
Expand Down Expand Up @@ -450,7 +450,7 @@ let add_version_edge (graph : t) (from : location) (_to : location) (property :
let edge = {Edge._to = _to; _type = Version property} in
add_edge graph edge _to from

let add_arg_edge (graph : t) (from : location) (_to : location) (index : int) (identifier : string) : unit =
let add_arg_edge (graph : t) (from : location) (_to : location) (index : string) (identifier : string) : unit =
let edge = {Edge._to = _to; _type = Argument (index, identifier)} in
add_edge graph edge _to from

Expand Down Expand Up @@ -499,10 +499,10 @@ let get_param_locations (graph : t) (func_id : Functions.Id.t) : Store.t =


let staticAddProperty (graph : t) (_L : LocationSet.t) (property : property) (id : int) (add_node : location -> unit) : unit =
LocationSet.iter (fun l ->
LocationSet.apply (fun l ->
let l_Os = orig graph l in

LocationSet.iter (fun l_o ->
LocationSet.apply (fun l_o ->
if not (has_property graph l_o (Some property))
(* Add Known Property - Non-Existing *)
then (let l_i = alloc graph id in
Expand All @@ -512,20 +512,20 @@ let staticAddProperty (graph : t) (_L : LocationSet.t) (property : property) (id
) _L

let dynamicAddProperty (graph : t) (_L_obj : LocationSet.t) (_L_prop : LocationSet.t) (id : int) (add_node : location -> unit): unit =
LocationSet.iter (fun l ->
LocationSet.apply (fun l ->
let l_Os = orig graph l in

LocationSet.iter (fun l_o ->
LocationSet.apply (fun l_o ->
if has_property graph l_o None then
(* Add Unknown Property - Existing*)
let l' = get_property graph l_o None in
LocationSet.iter (flip (add_dep_edge graph) l') _L_prop
LocationSet.apply (flip (add_dep_edge graph) l') _L_prop
else (
(* Add Unknown Property - Non-Existing*)
let l_i = alloc graph id in
add_node l_i;
add_prop_edge graph l_o l_i None;
LocationSet.iter (flip (add_dep_edge graph) l_i) _L_prop
LocationSet.apply (flip (add_dep_edge graph) l_i) _L_prop
)
) l_Os

Expand All @@ -545,7 +545,7 @@ let sNVWeakUpdate (graph : t) (store : Store.t) (_L : LocationSet.t) (property :
let l_i = alloc graph id in
add_node l_i;

LocationSet.iter ( fun l ->
LocationSet.apply ( fun l ->
(* add version edges *)
add_version_edge graph l l_i (Some property);

Expand All @@ -569,7 +569,7 @@ let dNVStrongUpdate (graph : t) (store : Store.t) (l_obj : location) (_L_prop :
add_version_edge graph l_obj l_i None;

(* add dependency edges *)
LocationSet.iter (fun l_prop ->
LocationSet.apply (fun l_prop ->
add_dep_edge graph l_prop l_i
) _L_prop;

Expand All @@ -583,7 +583,7 @@ let dNVWeakUpdate (graph : t) (store : Store.t) (_L_obj : LocationSet.t) (_L_pro
let l_i = alloc graph id in
add_node l_i;

LocationSet.iter ( fun l ->
LocationSet.apply ( fun l ->
(* add version edges *)
add_version_edge graph l l_i None;

Expand All @@ -594,7 +594,7 @@ let dNVWeakUpdate (graph : t) (store : Store.t) (_L_obj : LocationSet.t) (_L_pro
Store.update' store (get_node_name graph l_i) (LocationSet.singleton l_i);

(* add dependency edges *)
LocationSet.iter (fun l_prop ->
LocationSet.apply (fun l_prop ->
add_dep_edge graph l_prop l_i
) _L_prop;

Expand Down

0 comments on commit 38ca8fa

Please sign in to comment.