Skip to content

Commit

Permalink
[dispatch-once-static-init] Remove fp around dispatch_async
Browse files Browse the repository at this point in the history
Summary: Any time static constructor calls dispatch_async then thread execution changes and calling dispatch_once is not an issue.

Reviewed By: ngorogiannis

Differential Revision: D66708246

fbshipit-source-id: 04ad2848a9778a03035897fdc4a8b94e810affd6
  • Loading branch information
dulmarod authored and facebook-github-bot committed Dec 4, 2024
1 parent 57fc95d commit 1c28e93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
35 changes: 19 additions & 16 deletions infer/src/checkers/DispatchOnceStaticInit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,25 @@ module TransferFunctions = struct
(instr : Sil.instr) =
match instr with
| Call (_, Exp.Const (Const.Cfun procname), _, loc, _) ->
let call_site = CallSite.make procname loc in
let astate =
match analyze_dependency procname with
| Ok summary ->
L.d_printf "Applying summary of callee `%a`@\n" Procname.pp procname ;
L.d_printf "Summary: %a @\n" Summary.pp summary ;
if not (Summary.is_empty summary) then
let astate' = Summary.add_call {call_site; kind= `Call} summary in
Summary.join astate astate'
else astate
| Error _ ->
astate
in
let calls_dispatch_once = String.equal "_dispatch_once" (Procname.get_method procname) in
if calls_dispatch_once then Summary.add_call {call_site; kind= `Dispatch_once} astate
else astate
(* Any time static constructor calls dispatch_async then thread execution changes and calling dispatch_once is not an issue. *)
if String.equal "dispatch_async" (Procname.get_method procname) then astate
else
let call_site = CallSite.make procname loc in
let astate =
match analyze_dependency procname with
| Ok summary ->
L.d_printf "Applying summary of callee `%a`@\n" Procname.pp procname ;
L.d_printf "Summary: %a @\n" Summary.pp summary ;
if not (Summary.is_empty summary) then
let astate' = Summary.add_call {call_site; kind= `Call} summary in
Summary.join astate astate'
else astate
| Error _ ->
astate
in
let calls_dispatch_once = String.equal "_dispatch_once" (Procname.get_method procname) in
if calls_dispatch_once then Summary.add_call {call_site; kind= `Dispatch_once} astate
else astate
| _ ->
astate
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ + (instancetype)getInstance {
[Manager getInstance];
}
}

void dispatch_async(dispatch_queue_t _Nonnull queue,
dispatch_block_t _Nonnull block) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
block();
});
}

__attribute__((constructor)) static void initializer_test_interproc_good() {
dispatch_async(dispatch_get_main_queue(), ^{
int x = 0;
});
}

0 comments on commit 1c28e93

Please sign in to comment.