<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61401>61401</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Windows] File IDs aren't properly unique in some file share mounts
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            platform:windows
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mstorsjo
      </td>
    </tr>
</table>

<pre>
    CC @tru @aganea @zmodem @nico and others who might know something about Windows.

In https://github.com/mstorsjo/llvm-mingw/issues/327 I got a report of various intermittent random failures with using a LLVM based toolchain. After some digging, we found out that the issues are related to using file share mounts.

Observations:
- Folders shared from the user host with MS Remote Desktop (tested from macOS) show the issue
- Folders shared from the host system with VirtualBox allegedly also show the issue (as reported by the original poster)
- Folders shared from the host system with VMWare Fusion don't show the problem
- Folders mounted from a Samba server don't show the problem

The problem appears in different forms, depending on whether the source files or the toolchain itself (or both) are located on the file share. The problems are nondeterministic.

In the case of linking, the symptom is that lld fails to resolve a lot of symbols, e.g. like this:
```
ld.lld: error: undefined symbol: __set_app_type
>>> referenced by ../crt/crtexe.c:0
>>> z:/llvm-mingw-x86_64/x86_64-w64-mingw32/lib/crt2.o:(pre_c_init)

ld.lld: error: undefined symbol: __p__fmode
>>> referenced by ../crt/crtexe.c:119
>>> z:/llvm-mingw-x86_64/x86_64-w64-mingw32/lib/crt2.o:(pre_c_init)

ld.lld: error: undefined symbol: __p__commode
>>> referenced by ../crt/crtexe.c:120
>>> z:/llvm-mingw-x86_64/x86_64-w64-mingw32/lib/crt2.o:(pre_c_init)

ld.lld: error: undefined symbol: _setargv
>>> referenced by ../crt/crtexe.c:125
>>> z:/llvm-mingw-x86_64/x86_64-w64-mingw32/lib/crt2.o:(pre_c_init)
```
It behaves as if lld simply is ignoring a number of the input files.

After some digging, I've pinpointed that the issue is with file IDs. Within LLVM, `llvm::sys::fs::UniqueID`, https://github.com/llvm/llvm-project/blob/llvmorg-17-init/llvm/include/llvm/Support/FileSystem/UniqueID.h, is fairly central. This is built on the Windows File ID concept, see https://learn.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information:
> The identifier (low and high parts) and the volume serial number uniquely identify a file on a single computer. To determine whether two open handles represent the same file, combine the identifier and the volume serial number for each file and compare them.
> [...]
> The identifier that is stored in the nFileIndexHigh and nFileIndexLow members is called the file ID. Support for file IDs is file system-specific. File IDs are not guaranteed to be unique over time, because file systems are free to reuse them. In some cases, the file ID for a file can change over time.

In the case of LLD, it uses the `UniqueID`s to deduplicate input files. In that case, I'm not sure if it's solely a performance optimization or whether it would have a functional effect too. In other cases throughout LLVM, I'm sure the `UniqueID`s are fairly functionally essential.

To observe the issue, I've made a standalone test program that iterates over a directory and grabs the file unique IDs and inspects them for uniqueness, see https://gist.github.com/mstorsjo/74ea7b1769ae7043e0b784310a9a0d1e. Run the tool like e.g. `uniqueid.exe z:\directory`, where `z:` is a problematic file share mount. On a correctly working mount, it prints unique file IDs for all files and concludes e.g. `Iterated over 918 unique files, found 0 duplicates`, while on a problematic one, it prints e.g. `Iterated over 54 unique files, found 864 duplicates`.

In https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle, it is said that:
> Depending on the underlying network features of the operating system and the type of server connected to, the GetFileInformationByHandle function may fail, return partial information, or full information for the given file.

> You can compare the VolumeSerialNumber and FileIndex members returned in the [BY_HANDLE_FILE_INFORMATION](https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-by_handle_file_information) structure to determine if two paths map to the same target; for example, you can compare two file paths and determine if they map to the same directory.

It doesn't say how to know if it actually returned partial information, and whether the FileIndex members can be known bogus.

What's the best way forward wrt this, given how central `UniqueID`s are within LLVM? Just conclude that the tools are incompatible with file share mounts that don't fulfill this file system contract? It's perhaps not so surprising that the VirtualBox mounts fail in this way, but a bit more surprising that Microsoft's own Remote Desktop mounts expose the same issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWFlv28jS_TX0S8GERO0PfrCt6Bt9cBJgnDvBPAlNskj2pNnN2120rPn1F1VNbc4ymFzgYoBAobnU1qfqnG4Vgq4t4l0ye0hm6xvVU-P8XRvI-fCHu8ldebh7fIRkOiLf83-qVhYVX_3ZuhJbvrK6cKBsCY4a9AH2jYNW1w3BF-v2EFyL1Ghbg8pdT_BZ29LtQ5qM1snoPv5uLTREXUgm90m2SbJNranp87RwbZJtjvEk2caYl_a21bbeJ9lGh9BjSLLNJFvAFmpHoMBj5zyBq-BFee36ANoS-lYToSXwypauhUpp03sMsNfUQB8kPHh6-u095CpgCeScKRqlbQr3FaGXNKDUda1tnWSPsEeoXM9Z9wTUKP5BiCGB8ggejSKxNNivtEEIDT9rXW_pugQf84D-RZF2VsogN29h40zJRZXvSqi8a8VRH9BD4wLFDN4_w6_YOkJYY_hCroMkWxIGOn7TquLjc5KtIDRufw71r9yIh3AIhG109Jv21Cvz4F5BGYM1luYAygT3xjD7V2FYDSwhP8gz53WtrTLQuUDok2z19yN4_5lLuOmDdhZKZ5NsQWfvnXe5wfatWan40a6CZ9XmCrji6P_CRvz9dL4NqutQeQYWlLqq0DOwKufbwMAosUNb8oI7C_sGuSnEanC9L1BgEMDFeyeYgaaApuKyOQ-5o4YXixM1rhAcOStfnFGUwkVQEXPW2RIF7VYH0sXbJmMDhQrI7WG0_TJgWaI7tB25FnSIaDamlC4JDGCPwZkXBAXGSW-FQ5s7I_liWqdg9BcEavQZusl8NPyTP02ZGlMmk3tA753ni96WWGmL5WCN7-12AWmnum5Hh25AZzJ5F_-BRyl2EfGUpkm2KTzFX3zFtEgm96O3H_0Zh8p5cty-Lue7-TTJNvHidj-fxieTjF_UebSYpU4-XXYed8VOW00nvP7NpLrdruJ5-TMJjcerf2hKhWt_Oqnsn7hOAUn5-uXnEpr9bxK6bqstQY6NemHOCaAradug284cuJN1bZ2P3Gb7NkfPvSsz2nY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