gradle / native-platform

Java bindings for various native APIs
Apache License 2.0
117 stars 39 forks source link

Unhandled failure case for GetFileInformationByHandleEx when STD handles are pipes #43

Open lacasseio opened 4 years ago

lacasseio commented 4 years ago

This Gradle issue expose an interesting system configuration which cause GetFileInformationByHandleEx to fail with ERROR_INVALID_FUNCTION under a scenario where the STD handles are pipes on, what appears to be, very specific driver usage. Unfortunately, the reproduction case is still unknown but I will document what have been found.

I could only successfully creating STD handles as pipe when running on mintty with Cygwin 3. However, even with this setup the GetFileInformationByHandleEx function succeeded properly. The most plausible answer for the API call to return invalid function error is the underlying driver doesn't handle the IRP sent to it or it simply fail for some other reason which cause the invalid function error to be returned. Assuming this is correct, the possible scenarios could be where custom drivers would be used which involves containers, some sort of virtualization, network drives or specialized hardware. It seems the Gradle errors is flaky which makes it harder to narrow the issue further.

A better alternative could be to use NtQueryObject API instead. I can't confirmed yet if the API goes through the driver. The feeling is it's a Kernel call that return information from the object store.

lacasseio commented 4 years ago

This answer seems to be quite comprehensive on the difference of both NtQueryObject and GetFileInformationByHandleEx. I also quickly went through the Object Manager explanation in the Windows Internal book and it seems like my feeling was correct. The header of the object should contains the name and should be owned by the Kernel so no IRP to the underlying driver hence no ERROR_INVALID_FUNCTION should be returned.

lacasseio commented 4 years ago

The issue may be caused by Powershell, see https://github.com/gradle/gradle/issues/11205#issuecomment-557617924.