DYLD3 Closure Rebuilding Issue

Originator:max.ovtsin
Number:rdar://FB7527953 Date Originated:09.01.2020
Status:Open Resolved:
Product:iOS Product Version:iOS 13.x
Classification:Performance Issue Reproducible:Yes
 
DYLD3 Closure Rebuilding Issue

As a file name of the closure, dyld3 uses SHA256 hash which is based on 3 variables:

- Main executable file's inode number.
- Main executable file's modification time.
- iOS boot-time in microseconds. 

While using two first perfectly makes sense, the last one can cause some issues.
I can reproduce such a situation when boot-time slightly drifts in microseconds without any visible reason.

So as a result, when boot-time is changed, dyld will rebuild the same closure that was before but with a different name. It causes some degradation in pre-main time and bloats the content of the tmp folder where dyld stores the closures.

In vast majority cases, the bug was preceded by JetsamEvent and in all cases, iPhone wasn't connected to the external source of power, so it can be related to sleep mode as well.
The issue is reproducible on iPhone7 (iPhone9,3) and iPhone8 (iPhone10,4), where both of them on the latest iOS version 13.3 (17C54).

So it seems the issue affects the huge number of devices on iOS 13, and while the closure itself already contains `boot-uuid` to detect the system reboots, it's not clear why such an unreliable value as boot-time is used as a parameter.

The piece of code used to obtain iOS boot-time:

https://github.com/maxovtsin/BootTimeChecker/

```
timeval_t kern_boottime() {
  struct timeval boottime;
  int mib[2] = {CTL_KERN, KERN_BOOTTIME};
  size_t size = sizeof(boottime);
  if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
    return boottime;
  }
  return (timeval_t){ .tv_sec = 0, .tv_usec = 0 };
}
```

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!