Skip to content

Commit 592ad6d

Browse files
arthaudfacebook-github-bot
authored andcommitted
Fix highlight locations for higher order function calls
Summary: As titled. Reviewed By: dkgi Differential Revision: D30205448 fbshipit-source-id: e6ae26aedcb49980226af173cea040e45c5f2f34
1 parent eec64a5 commit 592ad6d

File tree

5 files changed

+133
-9
lines changed

5 files changed

+133
-9
lines changed

source/interprocedural_analyses/taint/backwardAnalysis.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,12 @@ module AnalysisInstance (FunctionContext : FUNCTION_CONTEXT) = struct
590590
~higher_order_function
591591
~callable_argument
592592
=
593-
let lambda_index, { Call.Argument.value = lambda_callee; name = lambda_name } =
593+
let ( lambda_index,
594+
{
595+
Call.Argument.value = { location = lambda_location; _ } as lambda_callee;
596+
name = lambda_name;
597+
} )
598+
=
594599
lambda_argument
595600
in
596601
(* If we have a lambda `fn` getting passed into `hof`, we use the following strategy:
@@ -609,7 +614,7 @@ module AnalysisInstance (FunctionContext : FUNCTION_CONTEXT) = struct
609614
( lambda_index,
610615
{
611616
Call.Argument.value =
612-
Node.create_with_default_location (Expression.Name (Name.Identifier result));
617+
Node.create ~location:lambda_location (Expression.Name (Name.Identifier result));
613618
name = lambda_name;
614619
} )
615620
in
@@ -643,7 +648,9 @@ module AnalysisInstance (FunctionContext : FUNCTION_CONTEXT) = struct
643648
(* Simulate if branch. *)
644649
let if_branch_state =
645650
(* Simulate $result = fn( all, all). *)
646-
let all_argument = Node.create ~location (Expression.Name (Name.Identifier "$all")) in
651+
let all_argument =
652+
Node.create ~location:lambda_location (Expression.Name (Name.Identifier "$all"))
653+
in
647654
let arguments_with_all_value =
648655
List.map non_lambda_arguments ~f:snd
649656
|> List.map ~f:(fun argument -> { argument with Call.Argument.value = all_argument })

source/interprocedural_analyses/taint/forwardAnalysis.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,16 +644,25 @@ module AnalysisInstance (FunctionContext : FUNCTION_CONTEXT) = struct
644644
* $result = fn
645645
* hof(q, fn, x, y)
646646
*)
647-
let lambda_index, { Call.Argument.value = lambda_callee; name = lambda_name } =
647+
let ( lambda_index,
648+
{
649+
Call.Argument.value = { location = lambda_location; _ } as lambda_callee;
650+
name = lambda_name;
651+
} )
652+
=
648653
lambda_argument
649654
in
650655
let location = lambda_callee.Node.location in
651-
let result = Node.create ~location (Expression.Name (Name.Identifier "$result")) in
656+
let result =
657+
Node.create ~location:lambda_location (Expression.Name (Name.Identifier "$result"))
658+
in
652659

653660
(* Simulate if branch. *)
654661
let if_branch_state =
655662
(* Simulate `$all = {q, x, y}`. *)
656-
let all_argument = Node.create ~location (Expression.Name (Name.Identifier "$all")) in
663+
let all_argument =
664+
Node.create ~location:lambda_location (Expression.Name (Name.Identifier "$all"))
665+
in
657666
let state =
658667
let all_assignee =
659668
Node.create

source/interprocedural_analyses/taint/test/integration/higher_order_functions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,12 @@ def callable_class():
7878
c = Callable(_test_source())
7979
# Even if c is a callable, we should still propagate the taint on it.
8080
_test_sink(c)
81+
82+
83+
def sink_args(*args):
84+
for arg in args:
85+
_test_sink(arg)
86+
87+
88+
def test_location(x: int, y: Callable, z: int):
89+
sink_args(x, y, z)

source/interprocedural_analyses/taint/test/integration/higher_order_functions.py.cg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ higher_order_functions.has_tito (fun) -> []
1111
higher_order_functions.higher_order_function (fun) -> []
1212
higher_order_functions.higher_order_function_and_sink (fun) -> [_test_sink (fun)]
1313
higher_order_functions.higher_order_method (fun) -> [higher_order_functions.C::method_to_sink (method) higher_order_functions.higher_order_function (fun)]
14+
higher_order_functions.sink_args (fun) -> [_test_sink (fun) typing.Iterable::__iter__ (method) typing.Iterator::__next__ (method)]
1415
higher_order_functions.source_through_tito (fun) -> [_test_source (fun) higher_order_functions.apply (fun) higher_order_functions.has_tito (fun)]
1516
higher_order_functions.test_higher_order_function (fun) -> [_test_source (fun) higher_order_functions.goes_to_sink (fun) higher_order_functions.higher_order_function (fun)]
1617
higher_order_functions.test_higher_order_function_and_sink (fun) -> [_test_source (fun) higher_order_functions.goes_to_sink (fun) higher_order_functions.higher_order_function_and_sink (fun)]
1718
higher_order_functions.test_higher_order_method (fun) -> [_test_source (fun) higher_order_functions.higher_order_method (fun) object::__init__ (method) object::__new__ (method)]
1819
higher_order_functions.test_higher_order_method_self (fun) -> [_test_source (fun) higher_order_functions.C::self_to_sink (method) higher_order_functions.higher_order_function (fun)]
1920
higher_order_functions.test_higher_order_tito (fun) -> [higher_order_functions.has_tito (fun) higher_order_functions.higher_order_function (fun)]
21+
higher_order_functions.test_location (fun) -> [higher_order_functions.Callable::__call__ (method) higher_order_functions.sink_args (fun)]

source/interprocedural_analyses/taint/test/integration/higher_order_functions.py.models

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@
580580
"position": {
581581
"filename": "higher_order_functions.py",
582582
"line": 34,
583-
"start": 4,
584-
"end": 48
583+
"start": 26,
584+
"end": 42
585585
},
586586
"resolves_to": [ "higher_order_functions.C.method_to_sink" ],
587587
"port": "formal(arg)",
@@ -598,6 +598,29 @@
598598
]
599599
}
600600
}
601+
{
602+
"kind": "model",
603+
"data": {
604+
"callable": "higher_order_functions.sink_args",
605+
"sinks": [
606+
{
607+
"port": "formal(*rest0)[*]",
608+
"taint": [
609+
{
610+
"root": {
611+
"filename": "higher_order_functions.py",
612+
"line": 85,
613+
"start": 19,
614+
"end": 22
615+
},
616+
"leaves": [ { "kind": "Test", "name": "_test_sink" } ],
617+
"features": [ { "always-via": "special_sink" } ]
618+
}
619+
]
620+
}
621+
]
622+
}
623+
}
601624
{
602625
"kind": "model",
603626
"data": {
@@ -642,7 +665,7 @@
642665
"taint": [
643666
{
644667
"decl": null,
645-
"tito": [ { "line": 56, "start": 11, "end": 45 } ],
668+
"tito": [ { "line": 56, "start": 33, "end": 41 } ],
646669
"leaves": [ { "kind": "LocalReturn", "name": "", "depth": 0 } ],
647670
"features": [ { "via": "tito" }, { "always-via": "lambda" } ]
648671
}
@@ -651,3 +674,77 @@
651674
]
652675
}
653676
}
677+
{
678+
"kind": "model",
679+
"data": {
680+
"callable": "higher_order_functions.test_location",
681+
"sinks": [
682+
{
683+
"port": "formal(z)",
684+
"taint": [
685+
{
686+
"call": {
687+
"position": {
688+
"filename": "higher_order_functions.py",
689+
"line": 89,
690+
"start": 20,
691+
"end": 21
692+
},
693+
"resolves_to": [ "higher_order_functions.sink_args" ],
694+
"port": "formal(*rest0)[*]",
695+
"length": 1
696+
},
697+
"leaves": [ { "kind": "Test", "name": "_test_sink" } ],
698+
"features": [
699+
{ "always-type": "scalar" },
700+
{ "always-via": "special_sink" }
701+
]
702+
}
703+
]
704+
},
705+
{
706+
"port": "formal(y)",
707+
"taint": [
708+
{
709+
"call": {
710+
"position": {
711+
"filename": "higher_order_functions.py",
712+
"line": 89,
713+
"start": 17,
714+
"end": 18
715+
},
716+
"resolves_to": [ "higher_order_functions.sink_args" ],
717+
"port": "formal(*rest0)[*]",
718+
"length": 1
719+
},
720+
"leaves": [ { "kind": "Test", "name": "_test_sink" } ],
721+
"features": [ { "always-via": "special_sink" } ]
722+
}
723+
]
724+
},
725+
{
726+
"port": "formal(x)",
727+
"taint": [
728+
{
729+
"call": {
730+
"position": {
731+
"filename": "higher_order_functions.py",
732+
"line": 89,
733+
"start": 14,
734+
"end": 15
735+
},
736+
"resolves_to": [ "higher_order_functions.sink_args" ],
737+
"port": "formal(*rest0)[*]",
738+
"length": 1
739+
},
740+
"leaves": [ { "kind": "Test", "name": "_test_sink" } ],
741+
"features": [
742+
{ "always-type": "scalar" },
743+
{ "always-via": "special_sink" }
744+
]
745+
}
746+
]
747+
}
748+
]
749+
}
750+
}

0 commit comments

Comments
 (0)