<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62264>62264</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
MemorySanitizer missing `use-of-uninitialized-value` error when uninitialized memory read through `uin32_t*`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sergey-pashaev
</td>
</tr>
</table>
<pre>
```c++
// -Wall -Werror -Wextra -fsanitize=memory -fsanitize-memory-track-origins=2
// x86-64 clang 16.0.0
#include <cassert>
#include <memory>
// 1 - uint32_t
// 0 - float
#define USE_UINT 0
int main() {
// Allocate uninitialized memory enough for two floats.
const size_t float_size = sizeof(float);
char* buffer = new char[2 * float_size];
// Write bool & float.
float magic = 42.0f;
reinterpret_cast<bool*> (buffer + (0 * float_size))[0] = true;
reinterpret_cast<float*>(buffer + (1 * float_size))[0] = magic;
assert(sizeof(float) == sizeof(uint32_t));
// Find magic float constant in memory.
for (int i = 0; i < 2; ++i) {
#if USE_UINT
// Read as uint32_t - memory sanitizer is silent.
uint32_t* u = reinterpret_cast<uint32_t*>(buffer + (i * float_size));
if (*u == *(reinterpret_cast<uint32_t*>(&magic))) {
*u = 0u;
break;
}
#else
// Read as float - memory sanitizer is loud.
float* f = reinterpret_cast<float*>(buffer + (i * float_size));
if (*f == magic) {
*f = 0.0f;
break;
}
#endif
}
return 0;
}
```
```
==1==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x55f095238b13 (/app/output.s+0xa8b13)
#1 0x7f5380914082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee)
#2 0x55f0951af29d (/app/output.s+0x1f29d)
Uninitialized value was created by a heap allocation
#0 0x55f095237769 (/app/output.s+0xa7769)
#1 0x55f0952386b2 (/app/output.s+0xa86b2)
#2 0x7f5380914082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee)
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/app/output.s+0xa8b13)
```
https://godbolt.org/z/sx9Maadn8
For some reason msan thinks that it's okay to read uninitialized memory through `uint32_t` pointer, and not okay through `float` pointer. I expect it should report use-of-uninitialized-value in both cases, since when we write bool at the beginning of buffer - we write 1 byte, not 4. And when we try to read this memory as 4 bytes we should get error no matter which type is used (`uint32_t` or `float`).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEl0tzozgQgH-NfOnCJQTIcPDBjsdbOWQOM5tK7SkloDHaYMklicTJr98SD79iZ2ZOS1EhqNUPfd2NZGGt3CjEOUmWJFlNROtqbeYWzQbfg52wtcDXSa7L9znhtL8Lwpb-pitCF4StCVtD8CSaBoInNEYb_9w7IyCorFDSyQ8k0WqLW23eT8aCfiRwRhQvgTZyI5Ul0YqdWd6nPOAxFI1QGwj5lE7pIB9mRVIVTVsikOiuENaicST6dk3a-zsKT92EEEArlYvYszsTUAigarQ4jEYlVlIhPP789vx4__1vOItHKgdbIRVhKWEZkNnACQBgsLhoGl0Ih9Aq6UmIRn5gCQMfVLrd1FBpA-5N967t9Gik0Mo6sPIDn10vffYvQKJVN6orwtI-YJaR6MR9UQtD2ALytqrQdAoK3_rhZMnAy44GSbI6aH9awpORDiHXugHCeK91EmP3DluxkUXnJmZTWp3FYlAqh2Zn0D0XwjoS3XlrhC1I9A0IS8cg2dK_0cvgWObvZElJsupcONPirzwMVLyLTx7CX3rolnMFyVByLP1E36udpeVQYL3xm3jXUpUDvh5ll3ShHEg11Mkpbe1XkfrKk12klETL7t87YP7fvl_lWT36zqgONXw0dhLFDxQlCHtoCwjGGh072IC0YGWD6jT7_jqudAFtF9SVfJxMupYSeT0lp0n2l6yga7ZFO_L29lj6Ow4J431ae9OXDXsE0hsH2n5y76_coHj5JCGz1YE1NhYH6QXdPsHX0Ta6LS_AjjUM1S2qX1X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