From 1371ca96e9688316969a60821486f18c7767e3ac Mon Sep 17 00:00:00 2001 From: Dulma Rodriguez Date: Fri, 23 Oct 2015 09:04:12 -0700 Subject: [PATCH] Added endtoend test that shows that we can release objects inside a block Reviewed By: akotulski Differential Revision: D2570345 fb-gh-sync-id: d96cdc8 --- .../objc/frontend/block/block_release.dot | 38 ++++++------ .../objc/frontend/block/block_release.m | 11 ++-- .../tests/endtoend/objc/BlockReleaseTest.java | 60 +++++++++++++++++++ 3 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 infer/tests/endtoend/objc/BlockReleaseTest.java diff --git a/infer/tests/codetoanalyze/objc/frontend/block/block_release.dot b/infer/tests/codetoanalyze/objc/frontend/block/block_release.dot index 759184603..48111ac53 100644 --- a/infer/tests/codetoanalyze/objc/frontend/block/block_release.dot +++ b/infer/tests/codetoanalyze/objc/frontend/block/block_release.dot @@ -1,33 +1,29 @@ digraph iCFG { -19 [label="19: BinaryOperatorStmt: Assign \n *&#GB$g:int =7 [line 24]\n " shape="box"] - - - 19 -> 18 ; -18 [label="18: DeclStmt \n *&z:int =3 [line 26]\n " shape="box"] +18 [label="18: DeclStmt \n *&z:int =3 [line 23]\n " shape="box"] 18 -> 17 ; -17 [label="17: DeclStmt \n n$12=_fun_CGBitmapContextCreate(0:void *,0:unsigned long ,0:unsigned long ,8:unsigned long ,0:unsigned long ,0:struct CGColorSpace *,0:unsigned int ) [line 27]\n *&context:struct CGContext *=n$12 [line 27]\n REMOVE_TEMPS(n$12); [line 27]\n " shape="box"] +17 [label="17: DeclStmt \n n$12=_fun_CGBitmapContextCreate(0:void *,0:unsigned long ,0:unsigned long ,8:unsigned long ,0:unsigned long ,0:struct CGColorSpace *,0:unsigned int ) [line 24]\n *&context:struct CGContext *=n$12 [line 24]\n REMOVE_TEMPS(n$12); [line 24]\n " shape="box"] 17 -> 16 ; -16 [label="16: DeclStmt \n n$10=*&context:struct CGContext * [line 28]\n n$11=_fun_CGBitmapContextCreateImage(n$10:struct CGContext *) [line 28]\n *&newImage:struct CGImage *=n$11 [line 28]\n REMOVE_TEMPS(n$10,n$11); [line 28]\n " shape="box"] +16 [label="16: DeclStmt \n n$10=*&context:struct CGContext * [line 25]\n n$11=_fun_CGBitmapContextCreateImage(n$10:struct CGContext *) [line 25]\n *&newImage:struct CGImage *=n$11 [line 25]\n REMOVE_TEMPS(n$10,n$11); [line 25]\n " shape="box"] 16 -> 15 ; -15 [label="15: BinaryOperatorStmt: Assign \n DECLARE_LOCALS(&__objc_anonymous_block_My_manager_my_mehtod______1); [line 29]\n n$8=_fun___objc_alloc_no_fail(sizeof(class __objc_anonymous_block_My_manager_my_mehtod______1 ):unsigned long ) [line 29]\n *&__objc_anonymous_block_My_manager_my_mehtod______1:class __objc_anonymous_block_My_manager_my_mehtod______1 =n$8 [line 29]\n n$9=*&newImage:struct CGImage * [line 29]\n *n$8.newImage:struct CGImage *=n$9 [line 29]\n n$5=*&newImage:struct CGImage * [line 29]\n *&b:_fn_ (*)=(_fun___objc_anonymous_block_My_manager_my_mehtod______1,n$5) [line 29]\n REMOVE_TEMPS(n$8,n$9,n$5); [line 29]\n " shape="box"] +15 [label="15: BinaryOperatorStmt: Assign \n DECLARE_LOCALS(&__objc_anonymous_block_My_manager_m______1); [line 26]\n n$8=_fun___objc_alloc_no_fail(sizeof(class __objc_anonymous_block_My_manager_m______1 ):unsigned long ) [line 26]\n *&__objc_anonymous_block_My_manager_m______1:class __objc_anonymous_block_My_manager_m______1 =n$8 [line 26]\n n$9=*&newImage:struct CGImage * [line 26]\n *n$8.newImage:struct CGImage *=n$9 [line 26]\n n$5=*&newImage:struct CGImage * [line 26]\n *&b:_fn_ (*)=(_fun___objc_anonymous_block_My_manager_m______1,n$5) [line 26]\n REMOVE_TEMPS(n$8,n$9,n$5); [line 26]\n " shape="box"] 15 -> 8 ; -14 [label="14: Call _fun_CGImageRelease \n n$7=*&newImage:struct CGImage * [line 30]\n _fun_CGImageRelease(n$7:struct CGImage *) [line 30]\n REMOVE_TEMPS(n$7); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="box"] +14 [label="14: Call _fun_CGImageRelease \n n$7=*&newImage:struct CGImage * [line 27]\n _fun_CGImageRelease(n$7:struct CGImage *) [line 27]\n REMOVE_TEMPS(n$7); [line 27]\n APPLY_ABSTRACTION; [line 27]\n " shape="box"] 14 -> 11 ; -13 [label="13: Prune (false branch) \n n$6=*&newImage:struct CGImage * [line 30]\n PRUNE((n$6 == 0), false); [line 30]\n REMOVE_TEMPS(n$6); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="invhouse"] +13 [label="13: Prune (false branch) \n n$6=*&newImage:struct CGImage * [line 27]\n PRUNE((n$6 == 0), false); [line 27]\n REMOVE_TEMPS(n$6); [line 27]\n APPLY_ABSTRACTION; [line 27]\n " shape="invhouse"] 13 -> 11 ; -12 [label="12: Prune (true branch) \n n$6=*&newImage:struct CGImage * [line 30]\n PRUNE((n$6 != 0), true); [line 30]\n REMOVE_TEMPS(n$6); [line 30]\n " shape="invhouse"] +12 [label="12: Prune (true branch) \n n$6=*&newImage:struct CGImage * [line 27]\n PRUNE((n$6 != 0), true); [line 27]\n REMOVE_TEMPS(n$6); [line 27]\n " shape="invhouse"] 12 -> 14 ; @@ -35,28 +31,28 @@ digraph iCFG { 11 -> 10 ; -10 [label="10: Exit __objc_anonymous_block_My_manager_my_mehtod______1 \n " color=yellow style=filled] +10 [label="10: Exit __objc_anonymous_block_My_manager_m______1 \n " color=yellow style=filled] -9 [label="9: Start __objc_anonymous_block_My_manager_my_mehtod______1\nFormals: newImage:struct CGImage * a:int \nLocals: \nCaptured: newImage:struct CGImage * \n DECLARE_LOCALS(&return); [line 29]\n NULLIFY(&a,false); [line 29]\n " color=yellow style=filled] +9 [label="9: Start __objc_anonymous_block_My_manager_m______1\nFormals: newImage:struct CGImage * a:int \nLocals: \nCaptured: newImage:struct CGImage * \n DECLARE_LOCALS(&return); [line 26]\n NULLIFY(&a,false); [line 26]\n " color=yellow style=filled] 9 -> 12 ; 9 -> 13 ; -8 [label="8: Call n$3 \n n$3=*&b:_fn_ (*) [line 32]\n n$4=*&z:int [line 32]\n n$3(n$4:int ) [line 32]\n REMOVE_TEMPS(n$3,n$4); [line 32]\n NULLIFY(&b,false); [line 32]\n " shape="box"] +8 [label="8: Call n$3 \n n$3=*&b:_fn_ (*) [line 29]\n n$4=*&z:int [line 29]\n n$3(n$4:int ) [line 29]\n REMOVE_TEMPS(n$3,n$4); [line 29]\n NULLIFY(&b,false); [line 29]\n " shape="box"] 8 -> 5 ; 8 -> 6 ; -7 [label="7: Call _fun_CGContextRelease \n n$2=*&context:struct CGContext * [line 33]\n _fun_CGContextRelease(n$2:struct CGContext *) [line 33]\n REMOVE_TEMPS(n$2); [line 33]\n NULLIFY(&context,false); [line 33]\n APPLY_ABSTRACTION; [line 33]\n " shape="box"] +7 [label="7: Call _fun_CGContextRelease \n n$2=*&context:struct CGContext * [line 30]\n _fun_CGContextRelease(n$2:struct CGContext *) [line 30]\n REMOVE_TEMPS(n$2); [line 30]\n NULLIFY(&context,false); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="box"] 7 -> 4 ; -6 [label="6: Prune (false branch) \n n$1=*&context:struct CGContext * [line 33]\n PRUNE((n$1 == 0), false); [line 33]\n REMOVE_TEMPS(n$1); [line 33]\n APPLY_ABSTRACTION; [line 33]\n " shape="invhouse"] +6 [label="6: Prune (false branch) \n n$1=*&context:struct CGContext * [line 30]\n PRUNE((n$1 == 0), false); [line 30]\n REMOVE_TEMPS(n$1); [line 30]\n APPLY_ABSTRACTION; [line 30]\n " shape="invhouse"] 6 -> 4 ; -5 [label="5: Prune (true branch) \n n$1=*&context:struct CGContext * [line 33]\n PRUNE((n$1 != 0), true); [line 33]\n REMOVE_TEMPS(n$1); [line 33]\n " shape="invhouse"] +5 [label="5: Prune (true branch) \n n$1=*&context:struct CGContext * [line 30]\n PRUNE((n$1 != 0), true); [line 30]\n REMOVE_TEMPS(n$1); [line 30]\n " shape="invhouse"] 5 -> 7 ; @@ -64,15 +60,15 @@ digraph iCFG { 4 -> 3 ; -3 [label="3: Return Stmt \n NULLIFY(&context,false); [line 34]\n n$0=*&z:int [line 34]\n *&return:int =n$0 [line 34]\n REMOVE_TEMPS(n$0); [line 34]\n NULLIFY(&__objc_anonymous_block_My_manager_my_mehtod______1,true); [line 34]\n NULLIFY(&z,false); [line 34]\n APPLY_ABSTRACTION; [line 34]\n " shape="box"] +3 [label="3: Return Stmt \n NULLIFY(&context,false); [line 31]\n n$0=*&z:int [line 31]\n *&return:int =n$0 [line 31]\n REMOVE_TEMPS(n$0); [line 31]\n NULLIFY(&__objc_anonymous_block_My_manager_m______1,true); [line 31]\n NULLIFY(&z,false); [line 31]\n APPLY_ABSTRACTION; [line 31]\n " shape="box"] 3 -> 2 ; -2 [label="2: Exit My_manager_my_mehtod \n " color=yellow style=filled] +2 [label="2: Exit My_manager_m \n " color=yellow style=filled] -1 [label="1: Start My_manager_my_mehtod\nFormals: self:class My_manager *\nLocals: newImage:struct CGImage * context:struct CGContext * z:int b:_fn_ (*) \n DECLARE_LOCALS(&return,&newImage,&context,&z,&b); [line 22]\n NULLIFY(&b,false); [line 22]\n NULLIFY(&context,false); [line 22]\n NULLIFY(&self,false); [line 22]\n NULLIFY(&z,false); [line 22]\n " color=yellow style=filled] +1 [label="1: Start My_manager_m\nFormals: self:class My_manager *\nLocals: newImage:struct CGImage * context:struct CGContext * z:int b:_fn_ (*) \n DECLARE_LOCALS(&return,&newImage,&context,&z,&b); [line 20]\n NULLIFY(&b,false); [line 20]\n NULLIFY(&context,false); [line 20]\n NULLIFY(&self,false); [line 20]\n NULLIFY(&z,false); [line 20]\n " color=yellow style=filled] - 1 -> 19 ; + 1 -> 18 ; } diff --git a/infer/tests/codetoanalyze/objc/frontend/block/block_release.m b/infer/tests/codetoanalyze/objc/frontend/block/block_release.m index 3c517d13a..4e0eea04e 100644 --- a/infer/tests/codetoanalyze/objc/frontend/block/block_release.m +++ b/infer/tests/codetoanalyze/objc/frontend/block/block_release.m @@ -10,8 +10,6 @@ #import #import -int g; - @interface My_manager :NSObject - (int) my_mehtod; @@ -19,14 +17,13 @@ int g; @implementation My_manager -- (int) my_mehtod +- (int) m { - g=7; void (^b)(int a); - int z=3; - CGContextRef context = CGBitmapContextCreate(NULL,0, 0, 8, 0, 0, 0); + int z = 3; + CGContextRef context = CGBitmapContextCreate(NULL, 0, 0, 8, 0, 0, 0); CGImageRef newImage = CGBitmapContextCreateImage(context); - b=^(int a){ + b = ^(int a) { if (newImage) CGImageRelease(newImage); }; b(z); diff --git a/infer/tests/endtoend/objc/BlockReleaseTest.java b/infer/tests/endtoend/objc/BlockReleaseTest.java new file mode 100644 index 000000000..d09f44326 --- /dev/null +++ b/infer/tests/endtoend/objc/BlockReleaseTest.java @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2013 - 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.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 BlockReleaseTest { + + public static final String FILE = "infer/tests/codetoanalyze/objc/frontend/block/block_release.m"; + + private static ImmutableList inferCmd; + + public static final String MEMORY_LEAK = "MEMORY_LEAK"; + + @ClassRule + public static DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder(); + + @BeforeClass + public static void runInfer() throws InterruptedException, IOException { + inferCmd = InferRunner.createObjCInferCommand(folder, FILE); + } + + @Test + public void whenInferRunsOnMnThenMemoryLeakIsNotFound() + throws InterruptedException, IOException, InferException { + InferResults inferResults = InferRunner.runInferObjC(inferCmd); + String[] procedures = { // no memory leak found + }; + assertThat( + "Results should contain the expected memory leak", + inferResults, + containsExactly( + MEMORY_LEAK, + FILE, + procedures + ) + ); + } +}