Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arena may produce misaligned values #16

Closed
MortenLohne opened this issue Mar 5, 2024 · 1 comment · Fixed by #17
Closed

Arena may produce misaligned values #16

MortenLohne opened this issue Mar 5, 2024 · 1 comment · Fixed by #17

Comments

@MortenLohne
Copy link

Mixing and matching multiple types in an arena may produce misaligned values, like an int at an odd byte:

func TestSlabArenaMultipleTypes(t *testing.T) {
	arena := NewSlabArena(8182, 1) // 8KB

	var b *byte = New[byte](arena)
	var p *int = New[int](arena)

	require.Equal(t, *b, byte(0))
	require.Equal(t, int(uintptr(unsafe.Pointer(p))%unsafe.Alignof(int(0))), 0)
}

The Go spec requires all values to be aligned correctly. For example, this test fails if run with go test -race:

func TestSlabArenaMultipleTypes(t *testing.T) {
	arena := NewSlabArena(8182, 1) // 8KB

	var b *byte = New[byte](arena)
	var p **int = New[*int](arena)

	require.Equal(t, *b, byte(0))
	require.True(t, *p == nil)
}

In theory, even a monotype arena can produce unaligned values, because the go spec does not guarantee that the underlying []byte is 8-byte aligned in the first place.

ortuman added a commit that referenced this issue Mar 6, 2024
ref: #16

Signed-off-by: Miguel Ángel Ortuño <[email protected]>
ortuman added a commit that referenced this issue Mar 6, 2024
ref: #16

Signed-off-by: Miguel Ángel Ortuño <[email protected]>
ortuman added a commit that referenced this issue Mar 6, 2024
ref: #16

Signed-off-by: Miguel Ángel Ortuño <[email protected]>
ortuman added a commit that referenced this issue Mar 6, 2024
ref: #16

Signed-off-by: Miguel Ángel Ortuño <[email protected]>
ortuman added a commit that referenced this issue Mar 6, 2024
ref: #16

Signed-off-by: Miguel Ángel Ortuño <[email protected]>
ortuman added a commit that referenced this issue Mar 6, 2024
ref: #16

Signed-off-by: Miguel Ángel Ortuño <[email protected]>
@ortuman
Copy link
Owner

ortuman commented Mar 6, 2024

Thank you for reporting the issue @MortenLohne.

I was aware that allocated pointers were not aligned, and I had planned to change this. However I simply assumed it could negatively affect performance, but it never occurred to me that with -race enabled this condition could cause a panic (TIL).

Anyway, I've already worked on a fix, and after merging #17 , this condition should not occur again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants