2

Does Objective-C Method Swizzling affect code in other process?

For example, I do the method swizzling on the -[NSArray count] in my app. Will the code in other processes be affected by that method swizzling?

AFAIU method resolution should be within the process. But I'm not sure about the code from system frameworks. Do they have their own method resolution shared between processes or they have the method resolution per process

1 Answers1

2

No. Think about the security holes if it did.....

Btw: why in he world would you want to swizzle ‘count’?

In general, sizzling is bad. Fragile, a maintenance nightmare, and a good way to get rejected from the store.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • 1
    I concern about the security issue on this and I don't want to swizzle `count`. I just curious about the Objective-C runtime and came up with that count as an easy to follow example. :) – Pitiphong Phongpattranont Feb 07 '18 at 16:24
  • Thank you. Do you have more in-depth information about this? For example, when the system framework is loaded into a process, how the Objective-C runtime does method resolution within that process? Does it maintain the dispatch table within the process? – Pitiphong Phongpattranont Feb 07 '18 at 16:30
  • 2
    Every process on the system is, effectively, running with its own copy of user space memory. Much of that memory is shared-- either readonly (executable code and other static data) or copy-on-write (rare-- basically, you touch a byte, you get a copy of the page of data that no other process can see)-- so, yeah, every process has its own method tables. When you swizzle, you're mucking with your process's method tables in memory that only your process can see. – bbum Feb 07 '18 at 20:34
  • Thank you. That explanation help me understand how it works under the hood. I've been wondering about this for years :) – Pitiphong Phongpattranont Feb 08 '18 at 09:01
  • 1
    Also I'm a huge fan of yours. Thank you for your contribution here :) – Pitiphong Phongpattranont Feb 08 '18 at 09:02
  • 2
    Happy to help! You might find this overview interesting. Note that iOS *mostly* works as this describes. https://developer.apple.com/library/content/documentation/Darwin/Conceptual/KernelProgramming/vm/vm.html – bbum Feb 08 '18 at 20:35