<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/86282>86282</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[libc] revisit `trivial-auto-var-init=pattern`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
SchrodingerZhu
</td>
</tr>
</table>
<pre>
We have enabled `trivial-auto-var-init=pattern` to improve security around uninitialized variables with automatic storage duration. See https://github.com/llvm/llvm-project/pull/78776.
Normally, compiler is start enough to omit pattern filling if the variable is initialized properly after its creation. However, we might want to revisit some situations to see if `[[clang::uninitialized]]` is needed.
Consider common a situation in internal usage of `syscall` wrappers. We may create a template trivial object on stack as buffer and then pass its address to the syscall interface as `inout` argument.
```
LinuxStatFs result;
// On 32-bit platforms, original fstatfs cannot handle large file systems.
// In such cases, SYS_fstatfs64 is defined and should be used.
#ifdef SYS_fstatfs64
int ret = syscall_impl<int>(SYS_fstatfs64, fd, sizeof(result), &result);
#else
int ret = syscall_impl<int>(SYS_fstatfs, fd, &result);
#endif
if (ret < 0) {
libc_errno = -ret;
return cpp::nullopt;
}
return result;
```
The compiler does not know that the syscall is to populate the fields of the struct. Hence, to be safe, classify it as a possible use before initialization. As a result, one may see the following in the generated code:
![image](https://github.com/llvm/llvm-project/assets/20108837/9df7fc91-0305-462f-a636-696eb17b8302)
For relative large local variable, it may be worthy to bare in mind that we are using pattern filling for trival auto variables. Adding `[[clang::uninitialized]]` will improve the code if we are certain that `asm/syscall` will initialize the variable properly.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVluv4jgS_jXmpQQKDoTwwMO5LOqVVrsPZ6XW7kurElcSTzt2ZFdg6F8_KgPnHFoaaXqkCHKx6_LVV18ZU7K9Jzqo7bPavi5w5iHEw1s7xGCs7yn-f5gXTTCXw1eCAU8E5LFxZEBVBUd7suiWOHNYnjA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