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

Breaking KISS principle for the win

Breaking KISS principle for the win

DEV_AT_WORK mobile meetup

Petro Korienev

October 22, 2016
Tweet

More Decks by Petro Korienev

Other Decks in Programming

Transcript

  1. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    Name: Petro Korienev Role: iOS Team Lead Company: Sigma Software Things I care about:
  2. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    KISS stays for 1. Keep it short and simple 2. Keep it simple and straightforward 3. Keep it simple, stupid
  3. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    enum Decision { case DontGiveAFuck() case Panic() case KeepItSimple() case Fuckup() }
  4. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    switch (decision) { case .Fuckup(let ): // ... }
  5. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    Given: UIWebView-based browser application for iOS When: Regression testing run Then: Found blocker issue: google accounts authentication (mail, drive etc.) is not persisted between app launches on iOS 9 Problem statement
  6. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    (you as! Engineer). workWith(“UIWebView-based browser") == .Fuckup
  7. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • UIWebView stores cookies in .binarycookies format on disk • Somehow it’s started to fail to read written cookies • [[NSHTTPCookieStorage sharedCookieStorage] allCookies] is nil Investigation
  8. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • We’ve adopted on-the-fly proprietary disk encryption solution • “on-the-fly” means “magic” • We’ve broken cookies storing mechanism The root cause
  9. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    func useMagic() -> Result { if !self.understandsMagic() { return .Fuckup } else { return .Win } }
  10. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    With great power comes great responsibility
  11. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • Encryption library hooks UNIX file-system functions. • 200-line function with comparisons to string literals, magic numbers, system imports constants etc. • around 70-80 hooked unix implementations • And todo 17 Jan 2003 Deep investigation
  12. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • System calls atomic write to cookies file • File is written to temp • File is moved to original • Appropriate encryption applied • Hooked move is called on iOS 8 and not on iOS 9 Result
  13. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • AdHoc for encryption solution - ignore “_tmp_” pattern • Initial vector for encryption is the same for temp/original • Profit Solution
  14. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    func breakTheRules(principle: EngineeringPrinciples) -> Result { switch principle { case .SOLID: return rand() % 2 == 0 ? .Win : .Fuckup case .YAGNI: return rand() % 2 == 0 ? .Win : .Fuckup case .KISS : return .Fuckup } }
  15. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • Get the hook working on iOS 9 The next iteration
  16. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    • XCode->Debug->Always show disassembly • symbolic breakpoints on UNIX-functions • debug symbol stubs • compare across different versions of iOS Tools
  17. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    If you see a previous document, you are most-likely doing something wrong.
  18. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    func RTFM(m: String) -> Result { if m.containsString("ARM Instruction set") { return .Fuckup } else { return .Win } }
  19. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    It didn’t work for this case neither. But trying it first i could save a time. And for sure I could find it myself. The library works exactly as the needed hook code
  20. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    func useGoogle(use : Bool) -> Result { return use ? .Win : .Fuckup }
  21. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    Ages of debugging on device, a week spent on implementing this for ARM architecture, offset calculations, reading Mach-O spec and i got the stubs working on iOS 9 I’ve written the solution
  22. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    func testOn(target: TestTarget) -> Result { return target == .Device ? .Win : .Fuckup }
  23. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    I was inspired by my awkward solution. I’ve reversed a big bunch of a format. I forgot about initial issue with login, cookies, etc... Once upon a time...
  24. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    func tryWithoutDebugger(without: Bool) -> Result { return without ? .Win : .Fuckup }
  25. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    (you as! Engineer).workWith("UIWebView-based browser") == .Fuckup (you as! Engineer).useMagic() == .Fuckup (you as! Engineer).breakTheRules(.KISS) == .Fuckup (you as! Engineer).RTFM("ARM Instruction set") == .Fuckup (you as! Engineer).useGoogle(false) == .Fuckup (you as! Engineer).testOn(.Simulator) == .Fuckup (you as! Engineer).tryWithoutDebugger(false) == .Fuckup Summary
  26. DEV_AT_WORK Petro Korienev Breaking KISS principle for the win e

    Me: https://twitter.com/soxjke https://github.com/soxjke https://www.facebook.com/soxjke https://stackoverflow.com/users/2392973