SlideShare a Scribd company logo
Live-updating Swift
code
Bartosz Polaczyk
Senior iOS Developer
Bartosz Polaczyk
Agenda
• Demo

• How does Injection work?

• Swift limitations
Demo
Injection for Xcode®
Runtime
Live code injection ObjC
Runtime
Live code injection ObjC
Injection
Bundle
Runtime
Live code injection ObjC
Injection
Bundle
Method swizzling
let selectorToReplace = #selector(methodName)
let originalMethod = class_getInstanceMethod(UIView.self, selectorToReplace)
let swizzledMethod = class_getInstanceMethod(OtherClass.self, selectorToReplace)
method_exchangeImplementations(originalMethod, swizzledMethod)
Method swizzling
let selectorToReplace = #selector(methodName)
let originalMethod = class_getInstanceMethod(UIView.self, selectorTo
let swizzledMethod = class_getInstanceMethod(OtherClass.self, select
method_exchangeImplementations(originalMethod, swizzledMethod)
Runtime
Live code injection ObjC
Injection
Bundle
Runtime
Live code injection ObjC
Injection
Bundle
Runtime
Live code injection ObjC
Dispatching in Swift
Dispatching in Swift
Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
pushq %rbp
movq %rsp, %rbp
pushq %r13
subq $0x38, %rsp
movq %r12, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
pushq %rbp
movq %rsp, %rbp
pushq %r13
subq $0x38, %rsp
movq %r12, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
pushq %rbp
movq %rsp, %rbp
pushq %r14
subq $0x38, %rsp
movq %r11, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
pushq %rbp
movq %rsp, %rbp
pushq %r13
subq $0x38, %rsp
movq %r12, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
pushq %rbp
movq %rsp, %rbp
pushq %r14
subq $0x38, %rsp
movq %r11, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
Swift vtable copy
// if swift language class, copy vtable
struct _in_objc_class *newclass = (struct _in_objc_class *)INJECTION_BRIDGE(void *)newClass;
if ( (uintptr_t)newclass->internal & 0x1 ) {
struct _in_objc_class *oldclass = (struct _in_objc_class *)INJECTION_BRIDGE(void *)oldClass;
size_t bytes = oldclass->mdsize - offsetof(struct _in_objc_class, dispatch) - 2*sizeof(IMP);
memcpy( oldclass->dispatch, newclass->dispatch, bytes );
}
Swift vtable copy
// if swift language class, copy vtable
struct _in_objc_class *newclass = (struct _in_ob
if ( (uintptr_t)newclass->internal & 0x1 ) {
struct _in_objc_class *oldclass = (struct _i
size_t bytes = oldclass->mdsize - offsetof(s
memcpy( oldclass->dispatch, newclass->dispat
}
Static dispatch
class
struct
Static dispatch
class
struct
Static dispatch
class
struct
InjectionForXcode Swift
limitations
InjectionForXcode Swift
limitations
• classes inherit from NSObject
InjectionForXcode Swift
limitations
• classes inherit from NSObject
• non-final classes
InjectionForXcode Swift
limitations
• classes inherit from NSObject
• non-final classes
• non-final methods
InjectionForXcode Swift
limitations
• classes inherit from NSObject
• non-final classes
• non-final methods
• no structs/enums, no extensions
InjectionForXcode Swift
limitations
• classes inherit from NSObject
• non-final classes
• non-final methods
• no structs/enums, no extensions
• no global functions
InjectionForXcode Swift
limitations
• classes inherit from NSObject
• non-final classes
• non-final methods
• no structs/enums, no extensions
• no global functions
• no methods/properties added
but…
Live Updating Swift Code
InjectionTDD
Demo
InjectionTDD
InjectionTDD
• Live unit tests
InjectionTDD
• Live unit tests
• Zero configuration
InjectionTDD
• Live unit tests
• Zero configuration
• CocoaPods or .framework integration
InjectionTDD
• Live unit tests
• Zero configuration
• CocoaPods or .framework integration
• Standard Xcode interface
InjectionTDD
• Live unit tests
• Zero configuration
• CocoaPods or .framework integration
• Standard Xcode interface
• Swift only
build.log
build.log
build.logbuild.log
lldb
simulator
build.log
build.log
build.logbuild.log
lldb
simulator
build.log
build.log
build.logbuild.log
lldb
simulator
1
lldb
build.log
build.log
build.logbuild.log
lldb
simulator
1
lldb
TCP
socket
2
build.log
build.log
build.logbuild.log
lldb
simulator
TCP
socket
2
build.log
build.log
build.logbuild.log
lldb
simulator
3
TCP
socket
2
build.log
build.log
build.logbuild.log
lldb
simulator
TCP
socket
2
build.log
build.log
build.logbuild.log
lldb
simulator
TCP
socket
2
4
build.log
build.log
build.logbuild.log
lldb
simulator
TCP
socket
2
build.log
build.log
build.logbuild.log
lldb
simulator
TCP
socket
2
5
swiftc/swift
InjectionBundle
swiftc/swift
file1.o
file2.o
file3.o
InjectionBundle
swiftc/swift
file1.o
file2.o
file3.o
InjectionBundle
class
struct
class
struct
class
struct
file .swiftdeps
file .swiftdeps
## Swift dependencies file v0 ###
provides-top-level:
- "TrackingClientType"
provides-nominal:
- "7Library18TrackingClientTypeP"
provides-member:
- ["7Library18TrackingClientTypeP", ""]
- ["7Library18TrackingClientTypeP", "track"]
provides-dynamic-lookup:
depends-top-level:
- !private "!"
- !private "!="
...
- !private ["13ReactiveSwift14SignalProducerV", "uniqueValues"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "collect"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "concat"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "demoteErrors"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "filter"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "ksr_delay"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "map"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "take"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "uncollect"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "uniqueValues"]
- !private ["13ReactiveSwift14SignalProtocolP", "combineLatest"]
- !private ["13ReactiveSwift14SignalProtocolP", "filter"]
- !private ["13ReactiveSwift14SignalProtocolP", "flatMap"]
- !private ["13ReactiveSwift14SignalProtocolP", "ignoreValues"]
...
file .swiftdeps
- "TrackingClientType"
provides-nominal:
- "7Library18TrackingClientTypeP"
provides-member:
- ["7Library18TrackingClientTypeP", ""]
- ["7Library18TrackingClientTypeP", "track"]
provides-dynamic-lookup:
depends-top-level:
- !private "!"
- !private "!="
...
- !private ["13ReactiveSwift14SignalProducerV", "uniqueVal
- !private ["13ReactiveSwift22SignalProducerProtocolP", "c
- !private ["13ReactiveSwift22SignalProducerProtocolP", "c
- !private ["13ReactiveSwift22SignalProducerProtocolP", "d
- !private ["13ReactiveSwift22SignalProducerProtocolP", "f
- !private ["13ReactiveSwift22SignalProducerProtocolP", "k
- !private ["13ReactiveSwift22SignalProducerProtocolP", "m
InjectionTDD limitations
InjectionTDD limitations
• No Objective-C
InjectionTDD limitations
• No Objective-C
• No static libraries used
InjectionTDD limitations
• No Objective-C
• No static libraries used
• Host application
InjectionTDD limitations
• No Objective-C
• No static libraries used
• Host application
• ⌘+U, if new file added
Installation
• Download Injection For Xcode with TDD

• correct xcode-select -p
• Integrate InjectionTDD to your project

• e.g. pod 'InjectionTDD', '~> 0.4’
• ⌘+U
Optionally: install Xcode
breakpoints
Optionally: install Xcode
breakpoints
curl https://codeload.github.com/polac24/InjectionTDD/tar.gz/master | tar -xz --strip=3
InjectionTDD-master/scripts/update_breakpoints_source/
./update_breakpoints_source.sh
Summary
• Injection For Xcode for UI

• InjectionTDD for unit tests

• tests on ⌘+S

• Swift

• minimal configuration

• standard Xcode interface
References
• InjectionTDD: https://github.com/polac24/InjectionTDD

• Download Injection: http://johnholdsworth.com/
injection.html

• Method Dispatch in Swift: https://www.raizlabs.com/dev/
2016/12/swift-method-dispatch/

• Exploring Swift Memory Layout • Mike Ash: https://
www.youtube.com/watch?v=ERYNyrfXjlg
Q&A

More Related Content

PPTX
Mastering Java Bytecode With ASM - 33rd degree, 2012
PPT
Mastering Java ByteCode
PPTX
A topology of memory leaks on the JVM
PDF
sizeof(Object): how much memory objects take on JVMs and when this may matter
PPT
Building a java tracer
PDF
Server1
KEY
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
PDF
Обзор фреймворка Twisted
Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java ByteCode
A topology of memory leaks on the JVM
sizeof(Object): how much memory objects take on JVMs and when this may matter
Building a java tracer
Server1
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Обзор фреймворка Twisted

What's hot (20)

PPTX
An introduction to JVM performance
PDF
Native code in Android applications
PPTX
Down to Stack Traces, up from Heap Dumps
PDF
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
PPTX
Monitoring distributed (micro-)services
PPTX
Do we need Unsafe in Java?
PDF
Apache Commons - Don\'t re-invent the wheel
PDF
Voxxed Days Vilnius 2015 - Having fun with Javassist
PDF
Building fast interpreters in Rust
PDF
Oredev 2015 - Taming Java Agents
PDF
Notes for xx_use_serialgc
PDF
Everything you wanted to know about Stack Traces and Heap Dumps
PDF
Riga Dev Day 2016 - Having fun with Javassist
PDF
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
PDF
Sonar rules in action with walkmod
PDF
Rust ⇋ JavaScript
PPT
PPTX
PPTX
Jdk 7 4-forkjoin
PDF
Антон Нонко, Классические строки в C++
An introduction to JVM performance
Native code in Android applications
Down to Stack Traces, up from Heap Dumps
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Monitoring distributed (micro-)services
Do we need Unsafe in Java?
Apache Commons - Don\'t re-invent the wheel
Voxxed Days Vilnius 2015 - Having fun with Javassist
Building fast interpreters in Rust
Oredev 2015 - Taming Java Agents
Notes for xx_use_serialgc
Everything you wanted to know about Stack Traces and Heap Dumps
Riga Dev Day 2016 - Having fun with Javassist
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Sonar rules in action with walkmod
Rust ⇋ JavaScript
Jdk 7 4-forkjoin
Антон Нонко, Классические строки в C++
Ad

Similar to Live Updating Swift Code (20)

PDF
SV-ios-objc-to-swift
PDF
iOS 14 Programming Fundamentals with Swift Covers iOS 14 Xcode 12 and Swift 5...
PDF
Download Full iOS 14 Programming Fundamentals with Swift Covers iOS 14 Xcode ...
PDF
Protocol-Oriented Programming in Swift
PDF
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
PDF
The Swift Compiler and Standard Library
PDF
Swift - Under the Hood
PDF
From android/java to swift (3)
PDF
Swift rocks! #1
PDF
Swift2 smalltalk osxdev
PDF
Swift Programming
PDF
Swift Introduction
PPTX
Best practices iOS meetup - pmd
PDF
Swift, swiftly
PDF
Introduction to Swift
PDF
InterConnect: Server Side Swift for Java Developers
PDF
Denis Lebedev, Swift
PDF
Objective-C to Swift - Swift Cloud Workshop 3
PDF
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
PDF
Swift 2 Under the Hood - Gotober 2015
SV-ios-objc-to-swift
iOS 14 Programming Fundamentals with Swift Covers iOS 14 Xcode 12 and Swift 5...
Download Full iOS 14 Programming Fundamentals with Swift Covers iOS 14 Xcode ...
Protocol-Oriented Programming in Swift
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
The Swift Compiler and Standard Library
Swift - Under the Hood
From android/java to swift (3)
Swift rocks! #1
Swift2 smalltalk osxdev
Swift Programming
Swift Introduction
Best practices iOS meetup - pmd
Swift, swiftly
Introduction to Swift
InterConnect: Server Side Swift for Java Developers
Denis Lebedev, Swift
Objective-C to Swift - Swift Cloud Workshop 3
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Swift 2 Under the Hood - Gotober 2015
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
cuic standard and advanced reporting.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
Teaching material agriculture food technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Spectral efficient network and resource selection model in 5G networks
cuic standard and advanced reporting.pdf
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
CIFDAQ's Market Insight: SEC Turns Pro Crypto
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Understanding_Digital_Forensics_Presentation.pptx

Live Updating Swift Code