Issue 126629
Summary `#embed` doesn't support reading from devices
Labels
Assignees
Reporter zygoloid
    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?
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to