dyld won't read large closure from closured

Number:rdar://40522089 Date Originated:May 24 2018, 5:31 PM
Status:open Resolved:
Product:macOS + SDK Product Version:10.13.4 (17E202)
Classification:Serious bug Reproducible:always
`callClosureDaemon()` uses single `read` instruction to receive payload from `closured`. According to `man 2 read`:
> It is not an error if this number is smaller than the number of bytes requested (...)
`read` should be invoked in loop until it returns 0 – that is until whole payload was received from `closured`. Currently `callClosureDaemon` fails because `read` receives smaller number of bytes than expected.

Steps to Reproduce:
1. Make sure `/usr/libexec/closured` is present (I compiled it from https://opensource.apple.com/tarballs/dyld/dyld-519.2.2.tar.gz).
3. Enable `sEnableClosures` by setting DYLD_USE_CLOSURES=1 (I used lldb to set sEnableClosures to 1).
4. Launch an app with lots of frameworks (so the resulting closure is very big).

Expected Results:
At 4. App should be launched using the closure from closured.

Actual Results:
At.4. App launch fallbacks to dyld2 with log message:
`dyld: error reading buffer header from closured, amount=65528, errno=0`

macOS 10.13.4 (17E202)

MacBook Pro (Retina, 13-inch, Early 2015)


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!