This is an archive of the discontinued LLVM Phabricator instance.

[libc] Maintain proper alignment for the hermetic tests malloc
ClosedPublic

Authored by jhuber6 on May 4 2023, 9:23 AM.

Details

Summary

We use a bump pointer to implement malloc for the hermetic tests.
Currently, we bump the pointer up by any amount. This means that calling
malloc(1) will misalign the buffer so any following malloc(8)
accesses will not be aligned. This causes problems in architectures
which require alignment.

Diff Detail

Event Timeline

jhuber6 created this revision.May 4 2023, 9:23 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 4 2023, 9:23 AM
jhuber6 requested review of this revision.May 4 2023, 9:23 AM
sivachandra added inline comments.May 4 2023, 9:55 AM
libc/test/UnitTest/HermeticTestUtils.cpp
68

Is it 8 that we want or is it alignof(uintptr_t)?

jhuber6 added inline comments.May 4 2023, 9:56 AM
libc/test/UnitTest/HermeticTestUtils.cpp
68

Eight is just a good number. I think to be consistent with most implementations of malloc it's alignof(long double) or something. But I could change it to use that number since it's clearer to say "We want alignment for at least a 64-bit type"

jhuber6 updated this revision to Diff 519546.May 4 2023, 9:57 AM

Changing magic number

sivachandra accepted this revision.May 4 2023, 10:51 AM

Using alignof(long double) is the best I think. Feel free to change to it.

libc/test/UnitTest/HermeticTestUtils.cpp
68

Nit: If you want to do long double, you should use a convenience var:

constexpr size_t ALIGNMENT = aligngof(long double);
This revision is now accepted and ready to land.May 4 2023, 10:51 AM

Using alignof(long double) is the best I think. Feel free to change to it.

Was somewhat concerned using long double since it would double the wasted space for most tests. I wonder if it's possible to run asan or ubsan on the hermetic tests to catch stuff like this.