Skip to content

UBSan reports an error and incorrect alignment when global new returns an offset pointer #51035

@belkiss

Description

@belkiss
Bugzilla Link 51693
Version 12.0
OS All
CC @devincoughlin,@jkorous-apple,@vedantk

Extended Description

Hello!

I found that ubsan will report an incorrect alignment for a type in case it is allocated with the global operator new (without alignment), if we have it return an offset ptr.

I wrote a small repro: https://godbolt.org/z/n8Yh8eoaE

The type is aligned on 8 bytes (verified by static_assert on its alignof), but ubsan reports: "constructor call on misaligned address 0x000002af8fd8 for type 'Param', which requires 16 byte alignment".

Now I suppose changing the ptr returned by new that way breaks the STDCPP_DEFAULT_NEW_ALIGNMENT, but in the specs in [basic.stc.dynamic.allocation] it says for the non-aligned, non array new: "Otherwise, the storage is aligned for any object that does not have new-extended alignment and is of the requested size", which is pretty vague.

I would either expect to get an error message to indicate that break, or nothing, because in the end the pointer returned by new is 8 bytes aligned, and matches the 8 bytes alignment requirement of the type.

I think the issue comes from this line:

result.getPointer(), allocType, result.getAlignment(),

Instead of the allocator alignment result.getAlignment(), it should be the type alignment allocAlign. I've tried it, and ran the tests, the error goes away and the tests pass.

Open to ideas :)
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillacompiler-rt:ubsanUndefined behavior sanitizer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions