Advanced Debugging and the Address Sanitizer

Similar documents
Thread Sanitizer and Static Analysis

Implementing UI Designs in Interface Builder

Using and Extending the Xcode Source Editor

Finding Bugs Using Xcode Runtime Tools

What s New in Xcode App Signing

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

Understanding Undefined Behavior

Dynamic code analysis tools

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

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

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

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

Programming in C. Lecture 9: Tooling. Dr Neel Krishnaswami. Michaelmas Term

Fast dynamic program analysis Race detection. Konstantin Serebryany May

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

Improving your Existing Apps with Swift

Advanced Memory Analysis with Instruments. Daniel Delwood Performance Tools Engineer

Optimizing Swift Performance Session 409

Xcode Tricks. ios App Development Fall 2010 Lecture 13

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

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

Power, Performance, and Diagnostics

Bruce Merry. IOI Training Dec 2013

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

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

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

Accessibility on OS X

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Monetize and Promote Your App with iad

CS2141 Software Development using C/C++ Debugging

Introducing the Modern WebKit API

Adapting to the New UI of OS X Yosemite

Buffer overflow background

CS Week 8 Lab Assignment 3. Teaching Assistant Henrique Potter

Introducing On Demand Resources

Debugging and Profiling

Cling: A Memory Allocator to Mitigate Dangling Pointers. Periklis Akritidis

Copyright 2015 MathEmbedded Ltd.r. Finding security vulnerabilities by fuzzing and dynamic code analysis

Praktische Aspekte der Informatik

Seamless Linking to Your App

TI2725-C, C programming lab, course

What s New in CloudKit

Introducing the Photos Frameworks

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

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

Working with Metal Overview

DAY 3. CS3600, Northeastern University. Alan Mislove

Designing Great Apple Watch Experiences

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

What s New in Testing

Scientific Programming in C IX. Debugging

18-600: Recitation #3

What s New in SpriteKit

Introducing Swift Playgrounds

Eliminate Memory Errors to Improve Program Stability

Extending Your Apps with SiriKit

What s New in Energy Debugging

Memory Analysis tools

Runtime Defenses against Memory Corruption

Limitations of the stack

IDE for medical device software development. Hyun-Do Lee, Field Application Engineer

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

o Code, executable, and process o Main memory vs. virtual memory

Storyboards and Controllers on OS X

Mastering UIKit on tvos

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

UniSan: Proactive Kernel Memory Initialization to Eliminate Data Leakages

Using Memory Analysis to Create Leaner, Faster, More Reliable Embedded Systems

Localizing with Xcode 6

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CS 241 Data Organization Binary Trees

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Mysteries of Auto Layout, Part 1

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

CSE 565 Computer Security Fall 2018

6.S096: Introduction to C/C++

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

WatchKit In-Depth, Part 2

Facing the challenges of. New Approaches To Debugging Complex Codes! Ed Hinkel, Sales Engineer Rogue Wave Software

CS 322 Operating Systems Practice Midterm Questions

Making things work as expected

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Use Dynamic Analysis Tools on Linux

J2EE Development Best Practices: Improving Code Quality

KOTLIN/NATIVE + CLANG, TRAVEL NOTES NIKOLAY IGOTTI, JETBRAINS

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Designing for Apple Watch

System Assertions. Your Submissions. Oral Exams FEBRUARY FEBRUARY FEBRUARY

C Programming Basics II

Working With Metal Advanced

A program execution is memory safe so long as memory access errors never occur:

ECE 598 Advanced Operating Systems Lecture 12

Apple Watch Design Tips and Tricks

XMEM. Extended C/C++ Dynamic Memory Control and Debug Library. XMEM Manual. Version 1.7 / September 2010

COSC Software Engineering. Lecture 16: Managing Memory Managers

Debugging. Erwan Demairy Dream

valgrind overview: runtime memory checker and a bit more What can we do with it?

CSCI 2212: Intermediate Programming / C Storage Class and Dynamic Allocation

Lecture 14 Notes. Brent Edmunds

Transcription:

Developer Tools #WWDC15 Advanced Debugging and the Address Sanitizer Finding your undocumented features Session 413 Mike Swingler Xcode UI Infrastructure Anna Zaks LLVM Program Analysis 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Overview

Overview View Debugger

Overview View Debugger Advanced Breakpoint Actions

Overview View Debugger Advanced Breakpoint Actions Address Sanitizer

Overview View Debugger Advanced Breakpoint Actions Address Sanitizer

Demo View Debugger and Advanced Breakpoints Mike Swingler Xcode UI Infrastructure

Summary

Summary View Debugger Focus on troublesome views Visualize your constraints

Summary View Debugger Focus on troublesome views Visualize your constraints Advanced Breakpoint Actions Catch exceptions at throw, print message Print expressions without adding clutter

Summary View Debugger Focus on troublesome views Visualize your constraints Advanced Breakpoint Actions Catch exceptions at throw, print message Print expressions without adding clutter Address Sanitizer

Address Sanitizer Anna Zaks LLVM Program Analysis

Memory Corruption

Memory Corruption

Memory Corruption Is Hard to Debug Hard to consistently reproduce The source of error is often far from its manifestation

Language Memory Safety

Language Memory Safety Less error prone Swift Objective-C Automatic Reference Counting

Language Memory Safety Less error prone Swift Objective-C Automatic Reference Counting More susceptible to memory issues Direct memory manipulation Code that interoperates with C/C++

Language Memory Safety Less error prone Swift Objective-C Automatic Reference Counting More susceptible to memory issues Direct memory manipulation Code that interoperates with C/C++

What Is Address Sanitizer? Similar to Guard Malloc and Valgrind Finds memory corruption at run time Less overhead Integrated into Debug Navigator Works on OS X, ios (simulator and device)

Analyze Memory Corruption Use after free Heap buffer overflow Stack buffer overflow Global variable overflow Overflows in C++ containers Use after return

Analyze Memory Corruption Use after free Heap buffer overflow Stack buffer overflow Global variable overflow Overflows in C++ containers Use after return

Demo Using Address Sanitizer from Xcode Anna Zaks LLVM Program Analysis

Demo Recap 1. Edit Scheme Diagnostics tab 2. Enable Address Sanitizer checkbox 3. Build and Run

When to Use Address Sanitizer Investigating memory corruption Manual testing Continuous integration

Continuous Integration Enable Sanitization in your non-performance tests In Xcode 1. Edit Scheme Test Diagnostics tab 2. Enable Address Sanitizer checkbox 3. Build and Test

Continuous Integration Enable Sanitization in your non-performance tests In Xcode 1. Edit Scheme Test Diagnostics tab 2. Enable Address Sanitizer checkbox 3. Build and Test Command Line $ xcodebuild -scheme "Jogr" test -enableaddresssanitizer YES

Compiler Optimization Level None [-O0] is recommended Fast [-O1] is supported Higher optimization is not supported

Under the Hood How Address Sanitizer works

How Address Sanitizer Works

How Address Sanitizer Works clang

How Address Sanitizer Works clang

How Address Sanitizer Works clang -fsanitize=address

How Address Sanitizer Works clang -fsanitize=address

How Address Sanitizer Works asan dylib clang -fsanitize=address

Shadow Mapping

Shadow Mapping Process memory Allocated objects

Shadow Mapping Process memory Shadow memory

Shadow Mapping Process memory Shadow memory Redzones

Shadow Mapping if (IsPoisoned(p)) *p = 0xb00; Crash(); *p = 0xb00;

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory p IsPoisoned(p)

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory p IsPoisoned(p)

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory p IsPoisoned(p) 0xb00

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory p IsPoisoned(p)

Shadow Mapping if (IsPoisoned(p)) Crash(); *p = 0xb00; Process memory Shadow memory p IsPoisoned(p)

Shadow Mapping 0x7fffffffffff IsPoisoned needs to be fast 1/8 of the address space 0x200000000000 mmap d at launch 0x1fffffffffff Shadow Region 0x100000000000 0x0fffffffffff 0x000000000000

Shadow Mapping 0x7fffffffffff IsPoisoned needs to be fast 1/8 of the address space 0x200000000000 mmap d at launch 0x1fffffffffff Shadow Region bool IsPoisoned(Addr) { Shadow = Addr >> 3 + Offset 0x100000000000 return (*Shadow)!= 0 0x0fffffffffff } 0x000000000000

Default Malloc Implementation

Default Malloc Implementation allocations

Default Malloc Implementation 1 2 3 4 5 allocations

Custom Malloc Implementation 1 2 3 4 5

Custom Malloc Implementation 1 2 3 4 5 1 2 3 4 5 Valid Poisoned

Custom Malloc Implementation 2 3 4 5 1 2 3 4 5 Valid Poisoned

Custom Malloc Implementation 2 3 4 5 2 3 4 5 Valid Poisoned

Custom Malloc Implementation

Custom Malloc Implementation Inserts poisoned red zones around allocations Heap underflows/overflows

Custom Malloc Implementation Inserts poisoned red zones around allocations Heap underflows/overflows Delays reuse of freed memory Use-after-free, double free

Custom Malloc Implementation Inserts poisoned red zones around allocations Heap underflows/overflows Delays reuse of freed memory Use-after-free, double free Collects stack traces for allocations and frees Comprehensive error reports

Compiler Instrumentation of the Stack void foo() { char buffer[16]; int number; buffer } buffer[16] = \0 ; number

Compiler Instrumentation of the Stack void foo() { char buffer[16]; int number; if (IsPoisoned(&buffer[16])) Crash(); buffer } buffer[16] = \0 ; number

Compiler Instrumentation of Globals int array[] = {1, 2, 3}; void foo() { } int x = array[3];

Compiler Instrumentation of Globals char poisoned_redzone1[16]; int array[] = {1, 2, 3}; char poisoned_redzone2[16]; void foo() { if (IsPoisoned(&array[3])) Crash(); int x = array[3]; }

Catching C++ Container Overflows std::vector<t> v; v.begin() v.end() v.begin() + v.capacity()

Catching C++ Container Overflows std::vector<t> v; v.begin() v.end() v.begin() + v.capacity()

Catching C++ Container Overflows std::vector<t> v; v.begin() v.end() v.begin() + v.capacity() std::vector<int> V(8); V.resize(5); return V.data()[5];

Catching C++ Container Overflows std::vector<t> v; v.begin() v.end() v.begin() + v.capacity() std::vector<int> V(8); V.resize(5); return V.data()[5]; container-overflow

Runtime Function Interposition

Runtime Function Interposition Wraps memcpy, memset, strcpy, strlen, fwrite, printf, getline, Extended with extra memory checks These checks work even in non-instrumented code

Runtime Function Interposition Wraps memcpy, memset, strcpy, strlen, fwrite, printf, getline, Extended with extra memory checks These checks work even in non-instrumented code wrap_memcpy(dest, src, n) { ASSERT_MEMORY_READABLE(src, n) ASSERT_MEMORY_WRITABLE(dest, n) return orig_memcpy(dest, src, n) }

Small Performance Overhead

Small Performance Overhead CPU slowdown usually between 2x 5x

Small Performance Overhead CPU slowdown usually between 2x 5x Memory overhead 2x 3x

Small Performance Overhead CPU slowdown usually between 2x 5x Memory overhead 2x 3x

Address Sanitizer

Complementary Tools Guard Malloc Finds heap overruns and use-after-free Adds guard pages before and after allocations Does not require recompilation Supported on OS X and in ios simulator Misses some bugs that Address Sanitizer finds

Complementary Tools NSZombie Catches Objective-C object over-releases Replaces deallocated objects with zombie objects that trap Enable Zombie Objects in Xcode Zombies Instrument

Complementary Tools Malloc Scribble Helps detecting uninitialized variables Fills allocated memory with 0xAA Fills deallocated memory with 0x55

Complementary Tools Leaks Instrument Helps detecting leaks Retain cycles Abandoned memory

Summary

Summary View Debugger

Summary View Debugger Advanced Breakpoint Actions

Summary View Debugger Advanced Breakpoint Actions Address Sanitizer

More Information Documentation Xcode Debugging http://developer.apple.com/support/technical/debugging/ Address Sanitizer http://clang.llvm.org/docs/addresssanitizer.html Apple Developer Forums developer.apple.com/forums Stefan Lesser Developer Technologies Evangelist slesser@apple.com

Related Sessions What s New in LLDB Nob Hill Tuesday 2:30PM UI Testing in Xcode Nob Hill Wednesday 11:00AM Implementing UI Designs in Interface Builder Pacific Hights Wednesday 1:30PM Continuous Integration and Code Coverage in Xcode Presidio Thursday 10:00AM Profiling in Depth Mission Thursday 3:30PM

Labs Instruments and Debugging Developer Tools Lab B Friday 9:00AM Xcode Open Hours Developer Tools Lab B Friday 1:00PM