Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What Is ABI?

What Is ABI?

What Is ABI Stability
What Is ABI
Exercises: Let's Break ABI

Kishikawa Katsumi

November 16, 2019
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. Today I talk, • What Is ABI Stability • What

    Is ABI • Exercises: Let's Break ABI
  2. What Is ABI Stability? • ABI stability enables OS vendors

    to embed a Swift standard library and runtime that is compatible with applications built with older or newer versions of Swift. This would remove the need for apps to distribute their own copy of these libraries on those platforms. It also allows for better decoupling of tools and better integration into the OS. https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md Swift ABI Stability Manifesto - What Does ABI Stability Enable?
  3. What Is ABI Stability? • An app built with one

    version of the Swift compiler will be able to talk to a library built with another version. https://swift.org/blog/abi-stability-and-more/ ABI Stability and More
  4. What Is ABI? • At runtime, Swift program binaries interact

    with other libraries and components through an ABI. ABI is Application Binary Interface, or the specification to which independently compiled binary entities must conform to be linked together and executed. https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md Swift ABI Stability Manifesto
  5. What Is ABI? • API: Application Programming Interface ‣ Source

    code level interface for your library • ABI: Application Binary Interface ‣ Machine instruction level interface for your library
  6. What Is ABI? • API: Application Programming Interface ‣ Source

    code level interface for your library ‣ How we interface with your library before compilation • ABI: Application Binary Interface ‣ Machine instruction level interface for your library ‣ How we interface with your library after compilation
  7. Components of the Swift ABI • Data Layout: a defined

    in-memory layout for instances of type • Type Metadata: a set of defined APIs for querying the metadata of a type • Mangling: any name in source code might not be globally unique, a unique name is produced through a technique called name mangling • Calling Convention: functions must know how to call each other • Runtime: dynamic casting, reference counting, reflection, etc • Standard Library: defines many common types, structures, and operations on these
  8. Exercises: Let's Break ABI import Foundation public struct Logger {

    let prefix: String public init() { prefix = "" } public func log(level: LogLevel, _ message: String) { print("\(prefix) [\(level)] \(message)") } }
  9. Exercises: Let's Break ABI import Foundation let logger = Logger()

    logger.log(level: .info, "Hello Swift!") // => [INFO] Hello Swift!
  10. Exercises: Let's Break ABI Logger.swift: swift -frontend -g -Onone -c

    \ -primary-file src/Logger.swift src/main.swift \ -module-name main -o bin/Logger.o \ -emit-module -emit-module-path bin/Logger~partial.swiftmodule \ -sdk $(shell xcrun --sdk macosx --show-sdk-path) \ -enable-library-evolution main.swift: swift -frontend -g -Onone -c \ -primary-file src/main.swift src/Logger.swift \ -module-name main -o bin/main.o \ -emit-module -emit-module-path bin/main~partial.swiftmodule \ -sdk $(shell xcrun --sdk macosx --show-sdk-path) \ -enable-library-evolution link: swiftc $(wildcard bin/*.o) -o bin/main clean: $(shell rm -r bin/*)
  11. Exercises: Let's Break ABI $ make Logger.swift # Logger.swiftΛίϯύΠϧ $

    make main.swift # main.swiftΛίϯύΠϧ $ make link # Logger.oͱmain.oΛϦϯΫ
  12. Exercises: Let's Break ABI • Link object files generated from

    different versions of the compiler • Explore cases that source code compatible but binary incompatible ‣ Add a default parameter to a public function ‣ Add a function to a struct ‣ Add a stored property to a struct
  13. Summary • ABI binary level interface between libraries • ABI

    stability is binary level compatibility • ABI incompatible is where code that has already been compiled against version 1 will no longer work with version 2
  14. References • ABI Stability and More
 https://swift.org/blog/abi-stability-and-more/ • Swift ABI

    Stability Manifesto
 https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md • Plan for module stability
 https://forums.swift.org/t/plan-for-module-stability/14551 • Swift Intermediate Language (SIL)
 https://github.com/apple/swift/blob/master/docs/SIL.rst • How Swift Achieved Dynamic Linking Where Rust Couldn't
 https://gankra.github.io/blah/swift-abi/