From 982616148eb8c78af6c4a1213d0eeed269e0e9b1 Mon Sep 17 00:00:00 2001 From: Dulma Rodriguez Date: Mon, 26 Oct 2015 06:26:52 -0700 Subject: [PATCH] Adding endtoend test that shows that dispatch once works well Reviewed By: ddino Differential Revision: D2570191 fb-gh-sync-id: 01be61c --- .../objc/frontend/block/dispatch.dot | 69 ++++++++++++------- .../objc/frontend/block/dispatch.m | 26 +++---- .../endtoend/objc/BlockDispatchTest.java | 63 +++++++++++++++++ 3 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 infer/tests/endtoend/objc/BlockDispatchTest.java diff --git a/infer/tests/codetoanalyze/objc/frontend/block/dispatch.dot b/infer/tests/codetoanalyze/objc/frontend/block/dispatch.dot index d81e73299..fb4e00397 100644 --- a/infer/tests/codetoanalyze/objc/frontend/block/dispatch.dot +++ b/infer/tests/codetoanalyze/objc/frontend/block/dispatch.dot @@ -1,34 +1,55 @@ digraph iCFG { -30 [label="30: DeclStmt \n n$3=_fun_A_sharedInstance() [line 50]\n *&b:class A *=n$3 [line 50]\n REMOVE_TEMPS(n$3); [line 50]\n " shape="box"] +32 [label="32: DeclStmt \n n$3=_fun_A_sharedInstance() [line 45]\n *&b:class A *=n$3 [line 45]\n REMOVE_TEMPS(n$3); [line 45]\n " shape="box"] - 30 -> 29 ; -29 [label="29: Message Call: setX: \n n$0=*&b:class A * [line 51]\n _fun_A_setX:(n$0:class A *,17:int ) virtual [line 51]\n REMOVE_TEMPS(n$0); [line 51]\n NULLIFY(&b,false); [line 51]\n " shape="box"] + 32 -> 31 ; +31 [label="31: DeclStmt \n *&p:int *=0 [line 46]\n " shape="box"] - 29 -> 25 ; -28 [label="28: BinaryOperatorStmt: Assign \n n$1=*&self:class A * [line 14]\n n$2=*&x:int [line 14]\n *n$1._x:int =n$2 [line 14]\n REMOVE_TEMPS(n$1,n$2); [line 14]\n NULLIFY(&self,false); [line 14]\n NULLIFY(&x,false); [line 14]\n APPLY_ABSTRACTION; [line 14]\n " shape="box"] + 31 -> 26 ; +30 [label="30: Return Stmt \n NULLIFY(&p,false); [line 48]\n *&return:int =0 [line 48]\n APPLY_ABSTRACTION; [line 48]\n " shape="box"] - 28 -> 27 ; -27 [label="27: Exit A_setX: \n " color=yellow style=filled] + 30 -> 24 ; +29 [label="29: Return Stmt \n n$1=*&p:int * [line 47]\n n$2=*n$1:int [line 47]\n *&return:int =n$2 [line 47]\n REMOVE_TEMPS(n$1,n$2); [line 47]\n NULLIFY(&p,false); [line 47]\n APPLY_ABSTRACTION; [line 47]\n " shape="box"] -26 [label="26: Start A_setX: (generated)\nFormals: self:class A * x:int \nLocals: \n DECLARE_LOCALS(&return); [line 14]\n " color=yellow style=filled] + 29 -> 24 ; +28 [label="28: Prune (false branch) \n PRUNE(((n$0 == 0) == 0), false); [line 47]\n REMOVE_TEMPS(n$0); [line 47]\n " shape="invhouse"] + 28 -> 30 ; +27 [label="27: Prune (true branch) \n PRUNE(((n$0 == 0) != 0), true); [line 47]\n REMOVE_TEMPS(n$0); [line 47]\n " shape="invhouse"] + + + 27 -> 29 ; +26 [label="26: BinaryOperatorStmt: EQ \n n$0=*&b:class A * [line 47]\n NULLIFY(&b,false); [line 47]\n " shape="box"] + + + 26 -> 27 ; 26 -> 28 ; -25 [label="25: Return Stmt \n *&return:int =0 [line 52]\n APPLY_ABSTRACTION; [line 52]\n " shape="box"] +25 [label="25: + \n NULLIFY(&b,false); [line 47]\n NULLIFY(&p,false); [line 47]\n " ] 25 -> 24 ; 24 [label="24: Exit main \n " color=yellow style=filled] -23 [label="23: Start main\nFormals: \nLocals: b:class A * \n DECLARE_LOCALS(&return,&b); [line 49]\n NULLIFY(&b,false); [line 49]\n " color=yellow style=filled] +23 [label="23: Start main\nFormals: \nLocals: p:int * b:class A * \n DECLARE_LOCALS(&return,&p,&b); [line 44]\n NULLIFY(&b,false); [line 44]\n NULLIFY(&p,false); [line 44]\n " color=yellow style=filled] + + + 23 -> 32 ; +22 [label="22: BinaryOperatorStmt: Assign \n n$15=*&self:class A * [line 14]\n n$16=*&x:int [line 14]\n *n$15._x:int =n$16 [line 14]\n REMOVE_TEMPS(n$15,n$16); [line 14]\n NULLIFY(&self,false); [line 14]\n NULLIFY(&x,false); [line 14]\n APPLY_ABSTRACTION; [line 14]\n " shape="box"] + + + 22 -> 21 ; +21 [label="21: Exit A_setX: \n " color=yellow style=filled] + + +20 [label="20: Start A_setX: (generated)\nFormals: self:class A * x:int \nLocals: \n DECLARE_LOCALS(&return); [line 14]\n " color=yellow style=filled] - 23 -> 30 ; + 20 -> 22 ; 19 [label="19: Return Stmt \n n$13=*&self:class A * [line 14]\n n$14=*n$13._x:int [line 14]\n *&return:int =n$14 [line 14]\n REMOVE_TEMPS(n$13,n$14); [line 14]\n NULLIFY(&self,false); [line 14]\n APPLY_ABSTRACTION; [line 14]\n " shape="box"] @@ -40,56 +61,56 @@ digraph iCFG { 17 -> 19 ; -16 [label="16: BinaryOperatorStmt: Assign \n n$11=_fun___objc_alloc_no_fail(sizeof(class A ):unsigned long ) [line 41]\n n$9=_fun_A_init(n$11:class A *) virtual [line 41]\n *&#GB$A_trans_SI_sharedInstance:struct objc_object *=n$9 [line 41]\n REMOVE_TEMPS(n$9,n$11); [line 41]\n APPLY_ABSTRACTION; [line 41]\n " shape="box"] +16 [label="16: BinaryOperatorStmt: Assign \n n$11=_fun___objc_alloc_no_fail(sizeof(class A ):unsigned long ) [line 37]\n n$9=_fun_A_init(n$11:class A *) virtual [line 37]\n *&#GB$A_trans_sharedInstance:struct objc_object *=n$9 [line 37]\n REMOVE_TEMPS(n$9,n$11); [line 37]\n APPLY_ABSTRACTION; [line 37]\n " shape="box"] 16 -> 15 ; -15 [label="15: Exit __objc_anonymous_block_A_trans_SI______2 \n " color=yellow style=filled] +15 [label="15: Exit __objc_anonymous_block_A_trans______2 \n " color=yellow style=filled] -14 [label="14: Start __objc_anonymous_block_A_trans_SI______2\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 40]\n " color=yellow style=filled] +14 [label="14: Start __objc_anonymous_block_A_trans______2\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 36]\n " color=yellow style=filled] 14 -> 16 ; -13 [label="13: DeclStmt \n DECLARE_LOCALS(&__objc_anonymous_block_A_trans_SI______2); [line 40]\n n$12=_fun___objc_alloc_no_fail(sizeof(class __objc_anonymous_block_A_trans_SI______2 ):unsigned long ) [line 40]\n *&__objc_anonymous_block_A_trans_SI______2:class __objc_anonymous_block_A_trans_SI______2 =n$12 [line 40]\n *&dummy_block:_fn_ (*)=(_fun___objc_anonymous_block_A_trans_SI______2) [line 40]\n REMOVE_TEMPS(n$12); [line 40]\n " shape="box"] +13 [label="13: DeclStmt \n DECLARE_LOCALS(&__objc_anonymous_block_A_trans______2); [line 36]\n n$12=_fun___objc_alloc_no_fail(sizeof(class __objc_anonymous_block_A_trans______2 ):unsigned long ) [line 36]\n *&__objc_anonymous_block_A_trans______2:class __objc_anonymous_block_A_trans______2 =n$12 [line 36]\n *&dummy_block:_fn_ (*)=(_fun___objc_anonymous_block_A_trans______2) [line 36]\n REMOVE_TEMPS(n$12); [line 36]\n " shape="box"] 13 -> 12 ; -12 [label="12: Call n$8 \n n$8=*&dummy_block:_fn_ (*) [line 43]\n n$8() [line 43]\n REMOVE_TEMPS(n$8); [line 43]\n NULLIFY(&dummy_block,false); [line 43]\n " shape="box"] +12 [label="12: Call n$8 \n n$8=*&dummy_block:_fn_ (*) [line 39]\n n$8() [line 39]\n REMOVE_TEMPS(n$8); [line 39]\n NULLIFY(&dummy_block,false); [line 39]\n " shape="box"] 12 -> 11 ; -11 [label="11: Return Stmt \n n$7=*&#GB$A_trans_SI_sharedInstance:struct objc_object * [line 44]\n *&return:struct objc_object *=n$7 [line 44]\n REMOVE_TEMPS(n$7); [line 44]\n NULLIFY(&__objc_anonymous_block_A_trans_SI______2,true); [line 44]\n APPLY_ABSTRACTION; [line 44]\n " shape="box"] +11 [label="11: Return Stmt \n n$7=*&#GB$A_trans_sharedInstance:struct objc_object * [line 40]\n *&return:struct objc_object *=n$7 [line 40]\n REMOVE_TEMPS(n$7); [line 40]\n NULLIFY(&__objc_anonymous_block_A_trans______2,true); [line 40]\n APPLY_ABSTRACTION; [line 40]\n " shape="box"] 11 -> 10 ; -10 [label="10: Exit A_trans_SI \n " color=yellow style=filled] +10 [label="10: Exit A_trans \n " color=yellow style=filled] -9 [label="9: Start A_trans_SI\nFormals: \nLocals: dummy_block:_fn_ (*) \n DECLARE_LOCALS(&return,&dummy_block); [line 36]\n NULLIFY(&dummy_block,false); [line 36]\n " color=yellow style=filled] +9 [label="9: Start A_trans\nFormals: \nLocals: dummy_block:_fn_ (*) \n DECLARE_LOCALS(&return,&dummy_block); [line 33]\n NULLIFY(&dummy_block,false); [line 33]\n " color=yellow style=filled] 9 -> 13 ; -8 [label="8: BinaryOperatorStmt: Assign \n n$5=_fun___objc_alloc_no_fail(sizeof(class A ):unsigned long ) [line 29]\n n$3=_fun_A_init(n$5:class A *) virtual [line 29]\n *&#GB$A_sharedInstance_sharedInstance:struct objc_object *=n$3 [line 29]\n REMOVE_TEMPS(n$3,n$5); [line 29]\n APPLY_ABSTRACTION; [line 29]\n " shape="box"] +8 [label="8: BinaryOperatorStmt: Assign \n n$5=_fun___objc_alloc_no_fail(sizeof(class A ):unsigned long ) [line 28]\n n$3=_fun_A_init(n$5:class A *) virtual [line 28]\n *&#GB$A_sharedInstance_sharedInstance:struct objc_object *=n$3 [line 28]\n REMOVE_TEMPS(n$3,n$5); [line 28]\n APPLY_ABSTRACTION; [line 28]\n " shape="box"] 8 -> 7 ; 7 [label="7: Exit __objc_anonymous_block_A_sharedInstance______1 \n " color=yellow style=filled] -6 [label="6: Start __objc_anonymous_block_A_sharedInstance______1\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 28]\n " color=yellow style=filled] +6 [label="6: Start __objc_anonymous_block_A_sharedInstance______1\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 27]\n " color=yellow style=filled] 6 -> 8 ; -5 [label="5: DeclStmt \n DECLARE_LOCALS(&infer___objc_anonymous_block_A_sharedInstance______1); [line 30]\n DECLARE_LOCALS(&__objc_anonymous_block_A_sharedInstance______1); [line 28]\n n$6=_fun___objc_alloc_no_fail(sizeof(class __objc_anonymous_block_A_sharedInstance______1 ):unsigned long ) [line 28]\n *&__objc_anonymous_block_A_sharedInstance______1:class __objc_anonymous_block_A_sharedInstance______1 =n$6 [line 28]\n *&infer___objc_anonymous_block_A_sharedInstance______1:_fn_ (*)=(_fun___objc_anonymous_block_A_sharedInstance______1) [line 30]\n REMOVE_TEMPS(n$6); [line 30]\n " shape="box"] +5 [label="5: DeclStmt \n DECLARE_LOCALS(&infer___objc_anonymous_block_A_sharedInstance______1); [line 29]\n DECLARE_LOCALS(&__objc_anonymous_block_A_sharedInstance______1); [line 27]\n n$6=_fun___objc_alloc_no_fail(sizeof(class __objc_anonymous_block_A_sharedInstance______1 ):unsigned long ) [line 27]\n *&__objc_anonymous_block_A_sharedInstance______1:class __objc_anonymous_block_A_sharedInstance______1 =n$6 [line 27]\n *&infer___objc_anonymous_block_A_sharedInstance______1:_fn_ (*)=(_fun___objc_anonymous_block_A_sharedInstance______1) [line 29]\n REMOVE_TEMPS(n$6); [line 29]\n " shape="box"] 5 -> 4 ; -4 [label="4: Call n$1 \n n$1=*&infer___objc_anonymous_block_A_sharedInstance______1:_fn_ (*) [line 30]\n n$2=n$1() [line 30]\n REMOVE_TEMPS(n$1,n$2); [line 30]\n NULLIFY(&infer___objc_anonymous_block_A_sharedInstance______1,true); [line 30]\n " shape="box"] +4 [label="4: Call n$1 \n n$1=*&infer___objc_anonymous_block_A_sharedInstance______1:_fn_ (*) [line 29]\n n$2=n$1() [line 29]\n REMOVE_TEMPS(n$1,n$2); [line 29]\n NULLIFY(&infer___objc_anonymous_block_A_sharedInstance______1,true); [line 29]\n " shape="box"] 4 -> 3 ; -3 [label="3: Return Stmt \n n$0=*&#GB$A_sharedInstance_sharedInstance:struct objc_object * [line 32]\n *&return:struct objc_object *=n$0 [line 32]\n REMOVE_TEMPS(n$0); [line 32]\n NULLIFY(&__objc_anonymous_block_A_sharedInstance______1,true); [line 32]\n APPLY_ABSTRACTION; [line 32]\n " shape="box"] +3 [label="3: Return Stmt \n n$0=*&#GB$A_sharedInstance_sharedInstance:struct objc_object * [line 30]\n *&return:struct objc_object *=n$0 [line 30]\n REMOVE_TEMPS(n$0); [line 30]\n NULLIFY(&__objc_anonymous_block_A_sharedInstance______1,true); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="box"] 3 -> 2 ; diff --git a/infer/tests/codetoanalyze/objc/frontend/block/dispatch.m b/infer/tests/codetoanalyze/objc/frontend/block/dispatch.m index e42f62956..1aeb85e52 100644 --- a/infer/tests/codetoanalyze/objc/frontend/block/dispatch.m +++ b/infer/tests/codetoanalyze/objc/frontend/block/dispatch.m @@ -24,32 +24,28 @@ { static dispatch_once_t once; static id sharedInstance; - dispatch_once(&once, - ^{ - sharedInstance = [[self alloc] init]; - }); - + dispatch_once(&once, ^{ + sharedInstance = [[self alloc] init]; + }); return sharedInstance; } - -+ (instancetype)trans_SI ++ (instancetype)trans { static id sharedInstance; - - void (^dummy_block)()=^{ - sharedInstance = [[self alloc] init]; - }; + void (^dummy_block)() = ^{ + sharedInstance = [[self alloc] init]; + }; dummy_block(); return sharedInstance; - } @end int main () { - A *b =[A sharedInstance]; - b.x=17; - return 0; + A *b = [A sharedInstance]; + int *p = 0; + if (b == 0) return *p; + else return 0; } diff --git a/infer/tests/endtoend/objc/BlockDispatchTest.java b/infer/tests/endtoend/objc/BlockDispatchTest.java new file mode 100644 index 000000000..82ea9afb7 --- /dev/null +++ b/infer/tests/endtoend/objc/BlockDispatchTest.java @@ -0,0 +1,63 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package endtoend.objc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static utils.matchers.ResultContainsErrorInMethod.contains; +import static utils.matchers.ResultContainsExactly.containsExactly; + +import com.google.common.collect.ImmutableList; + +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +import java.io.IOException; + +import utils.DebuggableTemporaryFolder; +import utils.InferException; +import utils.InferResults; +import utils.InferRunner; + +public class BlockDispatchTest { + + public static final String FILE = "infer/tests/codetoanalyze/objc/frontend/block/dispatch.m"; + + private static ImmutableList inferCmd; + + public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE"; + + @ClassRule + public static DebuggableTemporaryFolder folder = + new DebuggableTemporaryFolder(); + + @BeforeClass + public static void runInfer() throws InterruptedException, IOException { + inferCmd = InferRunner.createiOSInferCommandWithMLBuckets(folder, FILE, "cf", false); + } + + @Test + public void whenInferRunsOnMThenNPEIsFound() + throws InterruptedException, IOException, InferException { + InferResults inferResults = InferRunner.runInferObjC(inferCmd); + String[] procedures = { + }; + assertThat( + "Results should contain the expected null pointer exception", + inferResults, + containsExactly( + NULL_DEREFERENCE, //No NPEs are found. If the call to dispatch_once didn't work + // we would get an NPE. + FILE, + procedures + ) + ); + } +}