<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/126629>126629</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`#embed` doesn't support reading from devices
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
zygoloid
</td>
</tr>
</table>
<pre>
Clang's `#embed` implementation doesn't behave properly when applied to a device:
```c++
int per_build_entropy[] = {
#embed "/dev/random" limit(1024)
};
```
Instead it treats devices as being empty -- in the above case, this leads to the array being initialized with zeroes! This is probably because we're using the `st_size` field produced by `stat`, but that only lists the size of the file if applied to a regular file:
> `st_size`
> > This field gives the size of the file (if it is a regular file or a symbolic link) in bytes. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.
While we try to [detect this case](https://github.com/llvm/llvm-project/blob/3c6d1dd362009e0aebd91c5197d40f8ce59fcff4/llvm/lib/Support/MemoryBuffer.cpp#L493) and use stream IO instead of mmap, we only do so when we weren't given a `MapSize`, which in this case we presumably will be due to the `limit(1024)`. And the code that handles the case where we have no `MapSize` does *not* handle the case where we're opening a device rather than a regular file. Perhaps `shouldUseMmap` should return `false` unless it's operating on a regular file?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVEGL4zgT_TXKpeggy4kdH3xId09g4Gu-hZllj41slWPtypKRygnuX7-UnTDdWfawYEiw_Oo9vXpVOiV79oi12D-L_etGT9SHWH_M5-CCNZsmmLl-cdqfhSoTiEIKlePQoBGFBDuMDgf0pMkGDyZg8kKVBA32-oIwxjBidDNce_Sgx9FZNEABNBi82BZFfhTyyFWXpxXqmR95tJ5gxPjeTNaZd_QUwzivGkHkryBK_uquBYRSQp0MXoQ6Re1NGIRS4OxgSahDJtVOqIoB5avInz9TCnn87hOhNmAJKKKmdBOXQCdo0Poz4DDSDE9PYD1Qj6CbcEFodUKhXoB6m8ChNonvtpzHqOcb1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