Skip to content

out-of-bounds access when masking ushort #22381

Description

@rainers

Just happened to get a "write access violation" in dmd when accessing a bitfield that boiled down to accessing data beyond the end of an object that might not be in mapped memory.

struct BitField
{
    void clearBit()
    {
         bitFields &= ~(1u << 0u);
    }
    ushort bitFields;
}

void mask(ushort* p)
{
    *p &= 0xfffe;
}

Both functions generate dword accesses (from run.dlang.io):

text.void onlineapp.BitField.clearBit()	segment
	assume	CS:.text.void onlineapp.BitField.clearBit()
void onlineapp.BitField.clearBit():
		push	RBP
		mov	RBP,RSP
		sub	RSP,010h
		mov	-8[RBP],RDI
		mov	RAX,-8[RBP]
		and	dword ptr [RAX],0FFFFFFFEh
		leave
		ret
.text.void onlineapp.BitField.clearBit()	ends

.text.void onlineapp.mask(ushort*)	segment
	assume	CS:.text.void onlineapp.mask(ushort*)
void onlineapp.mask(ushort*):
		push	RBP
		mov	RBP,RSP
		sub	RSP,010h
		mov	-8[RBP],RDI
		mov	RAX,-8[RBP]
		and	dword ptr [RAX],0FFFFFFFEh
		leave
		ret

Tested with dmd 2.111 and 2.112.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compiler:Backendglue code, optimizer, code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions