morganstanley / hobbes

A language and an embedded JIT compiler
http://hobbes.readthedocs.io/
Apache License 2.0
1.16k stars 105 forks source link

UBSan reports few misaligned addresses #415

Closed mo-xiaoming closed 3 years ago

mo-xiaoming commented 3 years ago

Running hobbes-test with UBSan, got errors like

../include/hobbes/mc/encode.H:1625:53: runtime error: store to misaligned address 0x7ffcf203b3f2 for type 'const void *', which requires 8 byte alignment
0x7ffcf203b3f2: note: pointer points here
../lib/hobbes/lang/type.C:2365:9: runtime error: load of misaligned address 0x607000e849ab for type 'const int', which requires 4 byte alignment
0x607000e849ab: note: pointer points here
.
.
.
  1. placement new has to ensure buffer is properly aligned
  2. reinterpret_cast<T*>(p) has to ensure p points to a legit T object, if T is not a byte-like type. Until we have std::bitcast in C++20, any casts in C++ won't start an object lift time, so this cast simply casts a memory space to a non-existing object. And reinterpret_cast has the alignment issue as well