Thread Sanitizer and Static Analysis

Similar documents
Advanced Debugging and the Address Sanitizer

Finding Bugs Using Xcode Runtime Tools

Understanding Undefined Behavior

Using and Extending the Xcode Source Editor

What s New in Xcode App Signing

Localizing with Xcode 6

Quick Interaction Techniques for watchos

Creating Complications with ClockKit Session 209

Building Watch Apps #WWDC15. Featured. Session 108. Neil Desai watchos Engineer

Building Faster in Xcode

Optimizing for Bugs Fixed

Implementing UI Designs in Interface Builder

Introducing On Demand Resources

What s New in SpriteKit

ITP 342 Advanced Mobile App Dev. Memory

Announcement. Final Project Proposal Presentations and Updates

Introducing the Modern WebKit API

Localizing with Xcode 9

What s New in Testing

Mastering UIKit on tvos

What s New in LLDB. Debug your way to fame and glory #WWDC15. Developer Tools. Session 402

Profiling in Depth. Do you know where your code is? Session 412. Kris Markel Performance Tools Engineer Chad Woolf Performance Tools Engineer

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

Getting the Most Out of HealthKit

Enhancing your apps for the next dimension of touch

Optimizing Swift Performance Session 409

Power, Performance, and Diagnostics

Extending Your Apps with SiriKit

WatchKit In-Depth, Part 2

Mastering Drag and Drop

What s New in the LLVM Compiler. Chris Lattner LLVM Chief Architect

Using Grouped Notifications

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

Accessibility on OS X

Monetize and Promote Your App with iad

Introducing Swift Playgrounds

imessage Apps and Stickers, Part 2

Enabling Your App for CarPlay

Data Delivery with Drag and Drop

Advanced Memory Analysis with Instruments. Daniel Delwood Performance Tools Engineer

New features in AddressSanitizer. LLVM developer meeting Nov 7, 2013 Alexey Samsonov, Kostya Serebryany

Mastering Xcode for iphone OS Development Part 2. Marc Verstaen Sr. Manager, iphone Tools

What s New in watchos

New Ways to Work with Workouts

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

CloudKit Tips And Tricks

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

What s New in Foundation for Swift Session 207

Improving your Existing Apps with Swift

Introducing the Contacts Framework

Advances in AVFoundation Playback

What's New in Core Spotlight

Swift API Design Guidelines

Accessibility on ios. Developing for everyone. Frameworks #WWDC14. Session 210 Clare Kasemset ios Accessibility

Working with Metal Overview

Fast dynamic program analysis Race detection. Konstantin Serebryany May

Building for Voice with Siri Shortcuts

Localization Best Practices on tvos

Getting to Know Swift Package Manager

New UIKit Support for International User Interfaces

Verification & Validation of Open Source

Dynamic code analysis tools

What's New in UIKit Dynamics and Visual Effects Session 229

Mobile Application Development

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

Seamless Linking to Your App

Vulcan: Hardware Support for Detecting Sequential Consistency Violations Dynamically

Mastering Xcode for iphone OS Development Part 1. Todd Fernandez Sr. Manager, IDEs

Getting the Most out of Playgrounds in Xcode

Identifying Memory Corruption Bugs with Compiler Instrumentations. 이병영 ( 조지아공과대학교

ios Accessibility Developing for everyone Session 201 Ian Fisch ios Accessibility

What s New in Audio. Media #WWDC17. Akshatha Nagesh, AudioEngine-eer Béla Balázs, Audio Artisan Torrey Holbrook Walker, Audio/MIDI Black Ops

What s New in Core Data?

What s New in Core Bluetooth

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Mysteries of Auto Layout, Part 1

ios: Objective-C Primer

Modern User Interaction on ios

Introducing the Photos Frameworks

Advances in TVMLKit. App Frameworks #WWDC17. Trevor Cortez, Localization Engineer Parry Panesar, tvos Engineer Jeremy Foo, tvos Engineer

Designing for Apple Watch

Introduction to Siri Shortcuts

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

What s New in Swift #WWDC18. Ted Kremenek, Languages & Runtimes Manager Slava Pestov, Swift Compiler Engineer

Media and Gaming Accessibility

App Extension Best Practices

Introducing Password AutoFill for Apps

Integrating Apps and Content with AR Quick Look

Grigore Rosu Founder, President and CEO Professor of Computer Science, University of Illinois

ios Application Development Course Details

Writing Energy Efficient Apps

What s New in MapKit. App Frameworks #WWDC17. Fredrik Olsson

Your Apps and the Future of macos Security

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

What s New in Energy Debugging

C/C++ toolchain. Static and dynamic code analysis. Karel Kubíček. Masaryk University. Brno, Czech Republic

Xcode Tricks. ios App Development Fall 2010 Lecture 13

Designing Great Apple Watch Experiences

Transcription:

Developer Tools #WWDC16 Thread Sanitizer and Static Analysis Help with finding bugs in your code Session 412 Anna Zaks Manager, Program Analysis Team Devin Coughlin Engineer, Program Analysis Team 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

What Is This Talk About? Runtime Sanitizers Static Analyzer

Runtime Sanitizers

Sanitizers Find bugs at run time Similar to Valgrind Low overhead Work with Swift 3 and C/C++/Objective-C Integrated into Xcode IDE

Address Sanitizer (ASan) Introduced last year Finds memory corruption issues Effective at finding critical bugs Now has full support for Swift

Threading Issues Hard to consistently reproduce Difficult to debug Lead to unpredictable results

Thread Sanitizer (TSan) NEW

Thread Sanitizer (TSan) NEW Use of uninitialized mutexes Thread leaks (missing pthread_join) Unsafe calls in signal handlers (ex: malloc) Unlock from wrong thread Data races

Demo Thread Sanitizer in Xcode

Thread Sanitizer (TSan) in Xcode 1. Edit Scheme Diagnostics tab 2. Enable Thread Sanitizer checkbox 3. Build and Run 4. View all of the generated runtime issues 5. Can choose to break on every issue

TSan Build Flow TSan dylib clang swift

Usage from Command-Line Compile and link with TSan $ clang -fsanitize=thread source.c -o executable $ swiftc -sanitize=thread source.swift -o executable $ xcodebuild -enablethreadsanitizer YES Stop after the first error $ TSAN_OPTIONS=halt_on_error=1./executable

Platform Support for TSan 64-bit macos 64-bit ios and tvos simulators No device support No watchos support

Fixing Data Races

Data Race Multiple threads access the same memory without synchronization At least one access is a write May end up with any value or even memory corruption!

Reasons for Data Races Can indicate a problem with the structure of your program Often means that synchronization is missing

Data Race Example var data: Int? = nil func producer() { // More code here. data = 42 } func consumer() { // More code here. print(data) }

Data Race Example var data: Int? = nil func producer() { // More code here. data = 42 dataisavailable = true } func consumer() { // More code here. while!dataisavailable { usleep(1000) } print(data) } Order is not guaranteed

Data Race Example var data: Int? = nil func producer() { // More code here. serialdispatchqueue.async { data = 42 } } func consumer() { // More code here. serialdispatchqueue.sync { print(data) } }

Data Race in Lazy Initialization Code Singleton *getsingleton() { static Singleton *sharedinstance = nil; if (sharedinstance == nil) { } sharedinstance = [[Singleton alloc] init]; } return sharedinstance; Both threads could be setting the value at the same time

Data Race in Lazy Initialization Code Singleton *getsingleton() { static Singleton *sharedinstance = nil; if (sharedinstance == nil) { Singleton *localobject = [[Singleton alloc] init]; } // Only assign if sharedinstance is still nil. atomic_compare_and_set(&sharedinstance, nil, localobject); return sharedinstance; } Use-after-free in ARC and object is leaked on a race in MRR Unsynchronized read

Data Race in Lazy Initialization Code Singleton *getsingleton() { static dispatch_once_t predicate; static Singleton *sharedinstance = nil; dispatch_once(&predicate, ^{ }); sharedinstance = [[self alloc] init]; } return sharedinstance;

Lazy Initialization in Swift Global variables var sharedinstance = Singleton() func getsingleton() -> Singleton { return sharedinstance } Class constants class Singleton { static let sharedinstance = Singleton() }

Choosing the Right Synchronization API 1. Use GCD Dispatch racy accesses to the same serial queue 2. Use pthread API, NSLock pthread_mutex_lock() to synchronize accesses 3. New os_unfair_lock (use instead of OSSpinLock) 4. Atomic operations Concurrent Programming With GCD in Swift 3 Pacific Heights Friday 4:00PM

There is No Such Thing as a Benign Race On some architectures (ex., x86) reads and writes are atomic But even a benign race is undefined behavior in C May cause issues with new compilers or architectures

Behind the Scenes

Compiler Instruments Memory Accesses *p = a ; RecordAndCheckWrite(p); *p = a ; For every access: Records the information about that access Checks if that access participates in a race

TSan Another Record Shadow Information Shadow State Cell Tracks About is Evicted Up Memory to 4 Accesses Thread 1 Thread 2 Thread 3 Thread 4 Th1 Th2 Th3 Th4 : 0xb009 th1_1 th1_2 th3_1 th2_1 th3_2 th4_1 a : Write Read Application memory: 8 bytes Shadow state: Up to four 8 byte objects

Detecting a Race Every thread stores (in thread local): Thread 1 Thread 2 Thread 3 Thread s own timestamp th1_0 th1_2 th2_22 th2_0 th3_55 th3_0 th2_0 th1_0 th1_0 The timestamps for other threads that th3_0 th3_0 th2_0 establish the points of synchronization Timestamps are incremented on every memory access

Thread 1 th1_2 th2_0 th3_0 Thread 2 th2_22 th1_0 th3_0 Thread 3 th3_55 th1_0 th2_0

Detecting Thread 12 3 Writes: a Race : Check for Races Thread 1 Thread 2 Thread 3 th1_2 th1_3 th2_0 th3_0 th2_22 th2_23 th1_0 th3_0 th3_56 th3_55 th1_0 th2_0 vector clocks th1_3 th1_3 th2_23 lock 0xb009 b c a Th2 Th3 Th1 : th2_23 th3_56 th1_3 : Write

Thread Sanitizer Timing does not matter Can detect races even if they did not manifest during the particular run The more run time coverage the better Run all your tests with TSan!

Thread Sanitizer

Finding Bugs with Static Analysis

Find Bugs Without Running Code Does not require running code (unlike sanitizers) Great at catching hard to reproduce edge-case bugs Supported only for C, C++, and Objective-C

NEW NEW NEW Localizability Instance Cleanup Nullability

Missing Localizability

Startling for Users

Demo Clang Static Analyzer in Xcode

Clang Static Analyzer in Xcode 1. Product > Analyze or Product > Analyze SingleFile.m 2. View in Issue Navigator 3. Explore Issue Path

Find Missing Localizability Find unlocalized user-facing string: [button settitle:@ Cancel ]; Find missing localization context comment: NSString *t = NSLocalizedString(@ Cancel, nil);

Enable Checks in Build Settings

Checking -dealloc in Manual Retain/Release

Do Not Release assign Properties Release of synthesized ivar in -dealloc is over-release: @property(assign) id delegate; -(void)dealloc { [_delegate release]; } [super dealloc];

Do Release retain/copy Properties Leak if no release of ivar for retain/copy property in -dealloc: @property(assign) id delegate; @property(copy) NSString *title; -(void)dealloc { } [super dealloc];

Update to Automated Reference Counting

Nullability Violations

Nullability Annotations Indicate whether a value is expected to be nil: @interface CLLocation : NSObject @property(readonly, nonnull) NSDate *timestamp; @property(readonly, nullable) CLFloor *floor;

Why Annotate Nullability? New programming model communicates expectation to callers Violation can cause crashes or other unexpected behavior Swift enforces model in type system with optionals class CLLocation : NSObject { public var timestamp: NSDate { get } public var floor: CLFloor? { get } }

Finding Nullability Violations Particularly useful in mixed Swift/Objective-C projects Logical problem in code Incorrect annotations

Violation: Branching with nil Default - (nonnull NSString *)shortdescription { NSString *name = nil; if (self.cityname) name = self.cityname; if (self.countryname) name = self.countryname; } return name;

Violation: Branching with nil Default - (nonnull NSString *)shortdescription { NSString *name = NSLocalizedString(@ Earth, @ The planet ); if (self.cityname) name = self.cityname; if (self.countryname) name = self.countryname; } return name;

Violation: Incorrect Annotation NS_ASSUME_NONNULL_BEGIN @property(readonly) PressureData *pressure; NS_ASSUME_NONNULL_END - (PressureData *)pressure { if ([self hasbarometer]) return [self measurepressure]; } return nil;

Violation: Incorrect Annotation NS_ASSUME_NONNULL_BEGIN @property(readonly, nullable) PressureData *pressure; NS_ASSUME_NONNULL_END - (PressureData *)pressure { if ([self hasbarometer]) return [self measurepressure]; } return nil;

Nullability of Your API is a Contract Do not change just to silence the analyzer Do carefully consider nullability of API

Suppress with Cast Return nil defensively for backwards compatibility: - (NSString * _Nonnull)stringAtIndex:(int) index { if (index < 0 index >= _count) return (NSString * _Nonnull)nil; }

Static Analyzer

Wrapping Up

These Tools Find Real Bugs! Address Sanitizer and Thread Sanitizer Clang Static Analyzer Use on your code!

More Information https://developer.apple.com/wwdc16/412

Related Sessions Internationalization Best Practices Mission Tuesday 9:00AM Visual Debugging with Xcode Presidio Wednesday 4:00PM Debugging Tips and Tricks Pacific Heights Friday 1:40PM Using Time Profiler in Instruments Nob Hill Friday 3:00PM Concurrent Programming with GCD in Swift 3 Pacific Heights Friday 4:00PM

Labs Thread Sanitizer, Static Analysis, and LLVM Compiler Lab Developer Tools Lab B Thursday 12:00PM Thread Sanitizer, Static Analysis, and LLVM Compiler Lab Developer Tools Lab C Friday 3:00PM LLVM Compiler, Objective-C, and C++ Lab Developer Tools Lab C Friday 4:30PM GCD Lab Frameworks Lab D Friday 5:00PM