-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dispatch-once-static-init] New checker to flag when dispatch_once is…
… called inside a static constructor Summary: See T205836062 for the description of the problem. Here we build the checker to be intra-procedural, we'll extend it to be inter-procedural next. Reviewed By: ngorogiannis Differential Revision: D65544362 fbshipit-source-id: dbaa1c8df02539a82e30c46a555fea9dd03909e7
- Loading branch information
1 parent
3a86ac0
commit a79ecc2
Showing
14 changed files
with
163 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
(* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*) | ||
open! IStd | ||
module F = Format | ||
|
||
module Mem = struct | ||
type t = {loc: Location.t} [@@deriving compare] | ||
|
||
let pp fmt {loc} = F.fprintf fmt "calls_dispatch_once at %a" Location.pp loc | ||
end | ||
|
||
module Domain = struct | ||
include AbstractDomain.FiniteSet (Mem) | ||
|
||
let add_calls_dispatch_once loc astate = add {Mem.loc} astate | ||
end | ||
|
||
module TransferFunctions = struct | ||
module Domain = Domain | ||
module CFG = ProcCfg.Normal | ||
|
||
type analysis_data = IntraproceduralAnalysis.t | ||
|
||
let pp_session_name _node fmt = F.pp_print_string fmt "DispatchOnceStaticInit" | ||
|
||
let exec_instr (astate : Domain.t) _ _cfg_node _ (instr : Sil.instr) = | ||
match instr with | ||
| Call (_, Exp.Const (Const.Cfun procname), _, loc, _) -> | ||
let calls_dispatch_once = String.equal "_dispatch_once" (Procname.get_method procname) in | ||
if calls_dispatch_once then Domain.add_calls_dispatch_once loc astate else astate | ||
| _ -> | ||
astate | ||
end | ||
|
||
module Analyzer = AbstractInterpreter.MakeWTO (TransferFunctions) | ||
|
||
let make_trace loc = | ||
let trace_elem = Errlog.make_trace_element 0 loc "Call to `dispatch_once` here" [] in | ||
[trace_elem] | ||
|
||
|
||
let report_issue proc_desc err_log {Mem.loc} = | ||
let ltr = make_trace loc in | ||
let message = | ||
F.asprintf | ||
"There is a call to `disptach_once` at line %a from a static constructor. This could cause a \ | ||
deadlock." | ||
Location.pp loc | ||
in | ||
Reporting.log_issue proc_desc err_log ~ltr ~loc DispatchOnceStaticInit | ||
IssueType.dispatch_once_in_static_init message | ||
|
||
|
||
let checker ({IntraproceduralAnalysis.proc_desc; err_log} as analysis_data) = | ||
let attributes = Procdesc.get_attributes proc_desc in | ||
if attributes.ProcAttributes.is_static_ctor then | ||
let initial = Domain.empty in | ||
match Analyzer.compute_post analysis_data ~initial proc_desc with | ||
| Some domain -> ( | ||
match Domain.choose_opt domain with | ||
| Some mem -> | ||
report_issue proc_desc err_log mem | ||
| None -> | ||
() ) | ||
| None -> | ||
() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
infer/tests/codetoanalyze/objc/dispatch-once-static-init/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
TESTS_DIR = ../../.. | ||
|
||
CLANG_OPTIONS = -c $(OBJC_CLANG_OPTIONS) -fobjc-arc | ||
INFER_OPTIONS = --dispatch-once-static-init-only --debug-exceptions --project-root $(TESTS_DIR) | ||
INFERPRINT_OPTIONS = \ | ||
--issues-tests-fields file,procedure,line_offset,bug_type,bucket,severity,bug_trace,taint_extra,transitive_callees_extra,autofix \ | ||
--issues-tests | ||
|
||
SOURCES = $(wildcard *.m) | ||
|
||
include $(TESTS_DIR)/clang.make | ||
include $(TESTS_DIR)/objc.make | ||
|
||
infer-out/report.json: $(MAKEFILE_LIST) |
1 change: 1 addition & 0 deletions
1
infer/tests/codetoanalyze/objc/dispatch-once-static-init/issues.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
codetoanalyze/objc/dispatch-once-static-init/DispatchOnceInStaticInit.m, initializer_test_intraproc_bad, 3, DISPATCH_ONCE_IN_STATIC_INIT, no_bucket, ERROR, [macro expanded here,Call to `dispatch_once` here] |