Skip to content

Commit c8b8aea

Browse files
arthaudfacebook-github-bot
authored andcommitted
Treat parse errors in pysa files as model verification errors (closes #431)
Summary: We currently throw an exception on syntax errors in pysa files. This means that `pyre query validate_taint_models()` returns an error with a different format than a regular model verification error. This is problematic for the VSCode plugin we are trying to implement, which relies on `pyre query` returning errors in a given format. Reviewed By: dkgi Differential Revision: D28950011 fbshipit-source-id: 0792108da6f301abddd4b88320a88bcfc4f5750b
1 parent 561ab61 commit c8b8aea

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

source/interprocedural_analyses/taint/modelParser.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,17 +2371,21 @@ let create_model_from_attribute
23712371

23722372

23732373
let create ~resolution ~path ~configuration ~rule_filter source =
2374+
let open Core.Result in
23742375
let sources_to_keep, sinks_to_keep =
23752376
compute_sources_and_sinks_to_keep ~configuration ~rule_filter
23762377
in
23772378
let signatures_and_queries, errors =
23782379
String.split ~on:'\n' source
2379-
|> Parser.parse_exn
2380-
|> Source.create
2381-
|> Source.statements
2382-
|> List.map ~f:(parse_statement ~resolution ~path ~configuration)
2383-
|> List.partition_result
2384-
|> fun (results, errors) -> List.concat results, errors
2380+
|> Parser.parse
2381+
>>| Source.create
2382+
>>| Source.statements
2383+
>>| List.map ~f:(parse_statement ~resolution ~path ~configuration)
2384+
>>| List.partition_result
2385+
|> function
2386+
| Ok (results, errors) -> List.concat results, errors
2387+
| Error { Parser.Error.location; _ } ->
2388+
[], [model_verification_error ~path ~location ParseError]
23852389
in
23862390
let create_model_or_query = function
23872391
| ParsedSignature parsed_signature ->

source/interprocedural_analyses/taint/modelVerificationError.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module T = struct
1919
[@@deriving sexp, compare, eq, show]
2020

2121
type kind =
22+
| ParseError
2223
| InvalidDefaultValue of {
2324
callable_name: string;
2425
name: string;
@@ -91,6 +92,7 @@ include T
9192

9293
let description error =
9394
match error with
95+
| ParseError -> "Syntax error."
9496
| InvalidDefaultValue { callable_name; name; expression } ->
9597
Format.sprintf
9698
"Default values of `%s`'s parameters must be `...`. Did you mean to write `%s: %s`?"
@@ -177,7 +179,7 @@ let description error =
177179
Format.sprintf
178180
"Invalid identifier: `%s`. Expected a fully-qualified name."
179181
(Expression.show expression)
180-
| UnexpectedStatement _ -> "Unexpected statement"
182+
| UnexpectedStatement _ -> "Unexpected statement."
181183
| ClassBodyNotEllipsis class_name ->
182184
Format.sprintf "Class model for `%s` must have a body of `...`." class_name
183185
| DefineBodyNotEllipsis model_name ->
@@ -241,6 +243,7 @@ let code { kind; _ } =
241243
| ClassBodyNotEllipsis _ -> 22
242244
| DefineBodyNotEllipsis _ -> 23
243245
| InvalidNameClause _ -> 24
246+
| ParseError -> 25
244247

245248

246249
let display { kind = error; path; location } =

source/interprocedural_analyses/taint/modelVerificationError.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module T : sig
1717
[@@deriving sexp, compare, eq]
1818

1919
type kind =
20+
| ParseError
2021
| InvalidDefaultValue of {
2122
callable_name: string;
2223
name: string;

source/interprocedural_analyses/taint/test/modelTest.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,8 @@ let test_invalid_models context =
11981198
let assert_valid_model ?source ?sources ~model_source () =
11991199
assert_invalid_model ?source ?sources ~model_source ~expect:"no failure" ()
12001200
in
1201-
assert_invalid_model ~model_source:"import foo" ~expect:"Unexpected statement" ();
1201+
assert_invalid_model ~model_source:"1 + import" ~expect:"Syntax error." ();
1202+
assert_invalid_model ~model_source:"import foo" ~expect:"Unexpected statement." ();
12021203
assert_invalid_model
12031204
~model_source:"def test.sink(parameter: TaintSink[X, Unsupported]) -> TaintSource[A]: ..."
12041205
~expect:

0 commit comments

Comments
 (0)