Skip to content

[TSan] Change TSan inout accesses to use __tsan_external_write callback #11032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -1210,11 +1210,11 @@ FUNCTION(GetErrorValue, swift_getErrorValue, C_CC,
ARGS(ErrorPtrTy, Int8PtrPtrTy, OpenedErrorTriplePtrTy),
ATTRS(NoUnwind))

// void __tsan_write1(void *addr);
// void __tsan_external_write(void *addr, void *caller_pc, void *tag);
// This is a Thread Sanitizer instrumentation entry point in compiler-rt.
FUNCTION(TSanInoutAccess, __tsan_write1, C_CC,
FUNCTION(TSanInoutAccess, __tsan_external_write, C_CC,
RETURNS(VoidTy),
ARGS(Int8PtrTy),
ARGS(Int8PtrTy, Int8PtrTy, Int8PtrTy),
ATTRS(NoUnwind))

FUNCTION(GetKeyPath, swift_getKeyPath, C_CC,
Expand Down
13 changes: 12 additions & 1 deletion lib/IRGen/IRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,18 @@ void IRGenFunction::emitTSanInoutAccessCall(llvm::Value *address) {
llvm::Function *fn = cast<llvm::Function>(IGM.getTSanInoutAccessFn());

llvm::Value *castAddress = Builder.CreateBitCast(address, IGM.Int8PtrTy);
Builder.CreateCall(fn, {castAddress});

// Passing 0 as the caller PC causes compiler-rt to get our PC.
llvm::Value *callerPC = llvm::ConstantPointerNull::get(IGM.Int8PtrTy);

// A magic number agreed upon with compiler-rt to indicate a modifying
// access.
const unsigned kExternalTagSwiftModifyingAccess = 0x1;
llvm::Value *tagValue =
llvm::ConstantInt::get(IGM.SizeTy, kExternalTagSwiftModifyingAccess);
llvm::Value *castTag = Builder.CreateIntToPtr(tagValue, IGM.Int8PtrTy);

Builder.CreateCall(fn, {castAddress, callerPC, castTag});
}


Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/tsan_instrumentation.sil
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sil @_T020tsan_instrumentation11inoutAccessyyF : $@convention(thin) () -> () {
bb0:
%0 = global_addr @_T020tsan_instrumentation1gSiv : $*Int
%1 = builtin "tsanInoutAccess"(%0 : $*Int) : $()
// CHECK: call void @__tsan_write1(i8* bitcast ([[GLOBAL]]* @_T020tsan_instrumentation1gSiv to i8*))
// CHECK: call void @__tsan_external_write(i8* bitcast ([[GLOBAL]]* @_T020tsan_instrumentation1gSiv to i8*), i8* null, i8* inttoptr ({{(i32|i64)}} 1 to i8*))

%2 = tuple ()
return %2 : $()
Expand Down
42 changes: 21 additions & 21 deletions test/Sanitizers/tsan-inout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,31 @@ testRace(name: "GlobalStructMutatingMethod",
thread: { _ = globalForGlobalStructMutatingMethod.read() },
thread: { globalForGlobalStructMutatingMethod.mutate() } )
// CHECK-LABEL: Running GlobalStructMutatingMethod
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is global

var globalForGlobalStructDifferentStoredPropertiesInout = UninstrumentedStruct()
testRace(name: "GlobalStructDifferentStoredPropertiesInout",
thread: { uninstrumentedTakesInout(&globalForGlobalStructDifferentStoredPropertiesInout.storedProperty1) },
thread: { uninstrumentedTakesInout(&globalForGlobalStructDifferentStoredPropertiesInout.storedProperty2) } )
// CHECK-LABEL: Running GlobalStructDifferentStoredPropertiesInout
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is global

var globalForGlobalStructSameStoredPropertyInout = UninstrumentedStruct()
testRace(name: "GlobalStructSameStoredPropertyInout",
thread: { uninstrumentedTakesInout(&globalForGlobalStructSameStoredPropertyInout.storedProperty1) },
thread: { uninstrumentedTakesInout(&globalForGlobalStructSameStoredPropertyInout.storedProperty1) } )
// CHECK-LABEL: Running GlobalStructSameStoredPropertyInout
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race


var globalForGlobalStructSubscriptDifferentIndexesInout = UninstrumentedStruct()
testRace(name: "GlobalStructSubscriptDifferentIndexesInout",
thread: { uninstrumentedTakesInout(&globalForGlobalStructSubscriptDifferentIndexesInout[0]) },
thread: { uninstrumentedTakesInout(&globalForGlobalStructSubscriptDifferentIndexesInout[1]) } )
// CHECK-LABEL: Running GlobalStructSubscriptDifferentIndexes
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is global


Expand All @@ -109,45 +109,45 @@ testRace(name: "GlobalStructSubscriptDifferentIndexesGetSet",
thread: { _ = globalForGlobalStructSubscriptDifferentIndexesGetSet[0] },
thread: { globalForGlobalStructSubscriptDifferentIndexesGetSet[1] = 12 } )
// CHECK-LABEL: Running GlobalStructSubscriptDifferentIndexesGetSet
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is global

var globalForGlobalClassGeneralMethods = UninstrumentedClass()
testRace(name: "GlobalClassGeneralMethods",
thread: { _ = globalForGlobalClassGeneralMethods.read() },
thread: { globalForGlobalClassGeneralMethods.mutate() } )
// CHECK-LABEL: Running GlobalClassGeneralMethods
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race

var globalForGlobalClassDifferentStoredPropertiesInout = UninstrumentedClass()
testRace(name: "GlobalClassDifferentStoredPropertiesInout",
thread: { uninstrumentedTakesInout(&globalForGlobalClassDifferentStoredPropertiesInout.storedProperty1) },
thread: { uninstrumentedTakesInout(&globalForGlobalClassDifferentStoredPropertiesInout.storedProperty2) } )
// CHECK-LABEL: Running GlobalClassDifferentStoredPropertiesInout
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race

var globalForGlobalClassSubscriptDifferentIndexesInout = UninstrumentedClass()
testRace(name: "GlobalClassSubscriptDifferentIndexesInout",
thread: { uninstrumentedTakesInout(&globalForGlobalClassSubscriptDifferentIndexesInout[0]) },
thread: { uninstrumentedTakesInout(&globalForGlobalClassSubscriptDifferentIndexesInout[1]) } )
// CHECK-LABEL: Running GlobalClassSubscriptDifferentIndexesInout
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race


var globalForGlobalClassSameStoredPropertyInout = UninstrumentedClass()
testRace(name: "GlobalClassSameStoredPropertyInout",
thread: { uninstrumentedTakesInout(&globalForGlobalClassSameStoredPropertyInout.storedProperty1) },
thread: { uninstrumentedTakesInout(&globalForGlobalClassSameStoredPropertyInout.storedProperty1) } )
// CHECK-LABEL: Running GlobalClassSameStoredPropertyInout
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is heap block

// These access a global declared in the TSanUninstrumented module
testRace(name: "InoutAccessToStoredGlobalInUninstrumentedModule",
thread: { uninstrumentedTakesInout(&storedGlobalInUninstrumentedModule1) },
thread: { uninstrumentedTakesInout(&storedGlobalInUninstrumentedModule1) } )
// CHECK-LABEL: Running InoutAccessToStoredGlobalInUninstrumentedModule
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is global

// These access a global declared in the TSanUninstrumented module.
Expand All @@ -165,14 +165,14 @@ testRace(name: "InoutAccessToComputedGlobalInUninstrumentedModule",
thread: { uninstrumentedTakesInout(&computedGlobalInUninstrumentedModule1) },
thread: { uninstrumentedTakesInout(&computedGlobalInUninstrumentedModule1) } )
// CHECK-LABEL: Running InoutAccessToComputedGlobalInUninstrumentedModule
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race

// These access a computed global declared in the TSanUninstrumented module
testRace(name: "ReadAndWriteToComputedGlobalInUninstrumentedModule",
thread: { computedGlobalInUninstrumentedModule2 = 7 },
thread: { _ = computedGlobalInUninstrumentedModule2 } )
// CHECK-LABEL: Running ReadAndWriteToComputedGlobalInUninstrumentedModule
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race



Expand All @@ -183,7 +183,7 @@ testRace(name: "GlobalUninstrumentedClassStoredPropertyMutatingMethod",
thread: { _ = globalForGlobalUninstrumentedClassStoredPropertyMutatingMethod.storedStructProperty.read() },
thread: { globalForGlobalUninstrumentedClassStoredPropertyMutatingMethod.storedStructProperty.mutate() } )
// CHECK-LABEL: Running GlobalUninstrumentedClassStoredPropertyMutatingMethod
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race

// Note: TSan doesn't see a race above because it doesn't see any load on the
// read side because the getter for the class property is not instrumented.
Expand All @@ -194,7 +194,7 @@ testRace(name: "GlobalUninstrumentedClassStoredPropertyInout",
thread: { uninstrumentedTakesInout(&globalForGlobalUninstrumentedClassStoredPropertyInout.storedStructProperty.storedProperty1) },
thread: { uninstrumentedTakesInout(&globalForGlobalUninstrumentedClassStoredPropertyInout.storedStructProperty.storedProperty2) } )
// CHECK-LABEL: Running GlobalUninstrumentedClassStoredPropertyInout
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is heap block

// Note: TSan sees the race above because the inout instrumentation adds an
Expand All @@ -206,7 +206,7 @@ testRace(name: "GlobalUninstrumentedClassComputedPropertyInout",
thread: { uninstrumentedTakesInout(&globalForGlobalUninstrumentedClassComputedPropertyInout.computedStructProperty.storedProperty1) },
thread: { uninstrumentedTakesInout(&globalForGlobalUninstrumentedClassComputedPropertyInout.computedStructProperty.storedProperty1) } )
// CHECK-LABEL: Running GlobalUninstrumentedClassComputedPropertyInout
// CHECK-NO: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race

// In the above the write in instrumented code is to the value buffer allocated
// at the call site so there is no data race if the getter and setters themselves
Expand All @@ -219,7 +219,7 @@ testRace(name: "GlobalInstrumentedClassStoredPropertyMutatingMethod",
thread: { _ = globalForGlobalInstrumentedClassStoredPropertyMutatingMethod.storedStructProperty.read() },
thread: { globalForGlobalInstrumentedClassStoredPropertyMutatingMethod.storedStructProperty.mutate() } )
// CHECK-LABEL: Running GlobalInstrumentedClassStoredPropertyMutatingMethod
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is heap block
//
// TSan does see this above race because the getter and materializeForSet is instrumented
Expand Down Expand Up @@ -250,7 +250,7 @@ func runCapturedLocalStructMutatingMethod() {
thread: { l.mutate() } )
}
// CHECK-LABEL: Running CapturedLocalStructMutatingMethod
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is heap block


Expand All @@ -261,7 +261,7 @@ func runCapturedLocalStructDifferentStoredPropertiesInout() {
thread: { uninstrumentedTakesInout(&l.storedProperty2) } )
}
// CHECK-LABEL: Running CapturedLocalStructDifferentStoredPropertiesInout
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is heap block


Expand All @@ -272,7 +272,7 @@ func runCapturedLocalClassGeneralMethods() {
thread: { l.mutate() } )
}
// CHECK-LABEL: Running CapturedLocalClassGeneralMethods
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race


func runCapturedLocalDifferentStoredPropertiesInout() {
Expand All @@ -282,7 +282,7 @@ func runCapturedLocalDifferentStoredPropertiesInout() {
thread: { uninstrumentedTakesInout(&l.storedProperty2) } )
}
// CHECK-LABEL: Running CapturedLocalClassDifferentStoredPropertiesInout
// CHECK-NOT: ThreadSanitizer: data race
// CHECK-NOT: ThreadSanitizer: {{.*}} race

func runCapturedLocalSameStoredPropertyInout() {
let l = UninstrumentedClass()
Expand All @@ -291,7 +291,7 @@ func runCapturedLocalSameStoredPropertyInout() {
thread: { uninstrumentedTakesInout(&l.storedProperty1) } )
}
// CHECK-LABEL: Running CapturedLocalClassSameStoredPropertyInout
// CHECK: ThreadSanitizer: data race
// CHECK: ThreadSanitizer: Swift access race
// CHECK: Location is heap block

runLocalTests()