SlideShare une entreprise Scribd logo
1  sur  69
Télécharger pour lire hors ligne
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…
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

Contenu connexe

Tendances

Native code in Android applications
Native code in Android applicationsNative code in Android applications
Native code in Android applications
Dmitry Matyukhin
 
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerabilityCsw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
CanSecWest
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
knight1128
 

Tendances (20)

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

Similaire à Live Updating Swift Code

JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
Charles Nutter
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systems
Serge Stinckwich
 

Similaire à Live Updating Swift Code (20)

Message in a bottle
Message in a bottleMessage in a bottle
Message in a bottle
 
Tierney bq207
Tierney bq207Tierney bq207
Tierney bq207
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Objective-C Runtime overview
Objective-C Runtime overviewObjective-C Runtime overview
Objective-C Runtime overview
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systems
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real world
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agents
 
Migrating Objective-C to Swift
Migrating Objective-C to SwiftMigrating Objective-C to Swift
Migrating Objective-C to Swift
 
Supercharging reflective libraries with InvokeDynamic
Supercharging reflective libraries with InvokeDynamicSupercharging reflective libraries with InvokeDynamic
Supercharging reflective libraries with InvokeDynamic
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build system
 

Dernier

Dernier (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Live Updating Swift Code