Skip to content

Undefined Behavior: Missalinged memory access (UndefinedBehaviorSanitizer) #19

@dehorsley

Description

@dehorsley

Hi Andreas, great to see the project on GitHub. Thanks for all your hard work over the years. Tack så mycket!

In summary, LLVM's undefined behavior detector flags certain AS memory access (std::string in particular but also others) as unaligned, at least on ARM64. It appears to be genuine UB.

It looks like this issue was reported a few years ago on the forums but without a convenient reproduction. Below are some steps to reproduce that perhaps could be worked into a GitHub runner.

Setup

OS: macOS 15.6.1
Arch: ARM64
Compiler: AppleClang 17.0.0.17000319
AngelScript version: 2.38 / 2.39 WIP 5921c1b

Steps to reproduce

Build and AS executable with UBSan enabled. For example, feature test suite:

cd angelscript/sdk/tests/test_feature/projects/cmake
mkdir build && cd build                                                         
cmake .. -GNinja -DCMAKE_CXX_FLAGS='-fsanitize=undefined -fno-sanitize=vptr'
cmake --build .
cd ../../../bin
./test_feature

(NB: I had to disable vptr sanitizer to get it to compile)

Issue

Running the program produces a lot of misaligned memory hits, e.g. a 4-byte aligned std::string:

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior angelscript/sdk/tests/test_feature/source/test_thiscall_as_method.cpp:169:62
angelscript/sdk/tests/test_feature/source/test_thiscall_as_method.cpp:182:62: runtime error: constructor call on misaligned address 0x00010600afcc for type 'std::string', which requires 8 byte alignment
0x00010600afcc: note: pointer points here
  4e 00 00 00 00 e8 94 40  68 e2 41 03 e8 af 00 06  01 00 00 00 02 00 00 00  f8 af 00 06 01 00 00 00

Impact

If I understand correctly, this shouldn't lead to corruption on x86 and most ARM architectures, but may cause performance issues. It could also lead to incorrect programs because the compiler can do what it likes when hitting UB, though I suspect it's less catastrophic than other UB given AngelScript's long history!

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions