CodeGenFunction::EmitObjCForCollectionStmt currently emits lifetime markers for the loop variable in an inconsistent way: lifetime.start is emitted before the loop is entered, but lifetime.end is emitted inside the loop:
; entry block %u = alloca %1*, align 8 %1 = bitcast %1** %u to i8* call void @llvm.lifetime.start(i64 8, i8* %1) #5 ... ; loop body ... %14 = bitcast %1** %u to i8* call void @llvm.lifetime.end(i64 8, i8* %14) #5 ... br i1 %21, ... ; loop ; loop ended ret void
AddressSanitizer uses these markers to track out-of-scope accesses to local variables, and we get false positives in Obj-C foreach loops (in the 2nd iteration of the loop). The markers of the loop variable need to be either both inside the loop (so that we poison and unpoison the variable in each iteration), or both outside. This patch implements the "both inside" approach and makes EmitObjCForCollectionStmt emit:
; entry block %u = alloca %1*, align 8 ... ; loop body %12 = bitcast %1** %u to i8* call void @llvm.lifetime.start(i64 8, i8* %12) #5 ... %14 = bitcast %1** %u to i8* call void @llvm.lifetime.end(i64 8, i8* %14) #5 br label %15 ; loop ended ret void
The test fixups are only changing the order of allocas. There's some related discussion at https://reviews.llvm.org/D18618.