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

Example test for Frozen Bitmap fault when using Frozen Bitmaps as regular/mutable bitmaps with a read-only buffer #362

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jacksonrnewhouse
Copy link

This is a test that should not pass but does, as it asserts a panic exactly when FrozenBitmaps and MMap is used. @a392n328 helped me diagnose these issues.

}

func getBytesAsMMap(t *testing.T, startingBytes []byte) []byte {
os.WriteFile("data", startingBytes, 0777)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely, there is some needed error handling here?

Note that os.WriteFile is go 1.16. I think you want ioutil.WriteFile for portability.

primary := getBitmap(t, testCase, startingBitmap)
assert.True(t, startingBitmap.Equals(primary))
clone := primary.Clone()
primary.Xor(clone)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are modifying, in-place, the frozen view.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider doing

I would do

newprimary := Xor(primary, clone)

if testCase.shouldPanic {
assert.Panics(t, func() {
debug.SetPanicOnFault(true)
primary.Xor(clone)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are, again, modifying the frozen view.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do

newprimary := Xor(primary, clone)

I am not sure why you want to mutate the frozen view.

fileinfo, err := file.Stat()
require.NoError(t, err)

bytes, err := unix.Mmap(int(file.Fd()), 0, int(fileinfo.Size()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a ressource leak around it, surely.

@lemire lemire changed the title Example test for Frozen Bitmap fault Example test for Frozen Bitmap fault when using Frozen Bitmaps as regular/mutable bitmaps Jun 25, 2022
@lemire lemire requested a review from Oppen June 25, 2022 14:33
@lemire
Copy link
Member

lemire commented Jun 25, 2022

@Oppen Could you take a look?

Users are reminded that the frozen support in roaring is experimental. It has minimal testing, and there are no tests with mutation over frozen bitmaps. The frozen format was designed for immutable bitmaps (that you just access without modifying). Using frozen bitmaps as regular bitmaps is an extension.

@lemire lemire marked this pull request as draft June 25, 2022 14:41
@lemire
Copy link
Member

lemire commented Jun 25, 2022

This PR cannot be merged. I converted it to a draft.

require.NoError(t, err)

bytes, err := unix.Mmap(int(file.Fd()), 0, int(fileinfo.Size()),
unix.PROT_READ, unix.MAP_PRIVATE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PROT_READ means that the data can be read, it cannot be written to.

@lemire lemire changed the title Example test for Frozen Bitmap fault when using Frozen Bitmaps as regular/mutable bitmaps Example test for Frozen Bitmap fault when using Frozen Bitmaps as regular/mutable bitmaps with a read-only buffer Jun 25, 2022
@lemire
Copy link
Member

lemire commented Jun 25, 2022

@jacksonrnewhouse How sure are you that the byte array you get from the memory map is never written to when you mutate the bitmap? Your byte[] has to be read-only.


func getBytesAsMMap(t *testing.T, startingBytes []byte) []byte {
os.WriteFile("data", startingBytes, 0777)
file, err := os.OpenFile("data", os.O_RDONLY, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is read-only.

fileinfo, err := file.Stat()
require.NoError(t, err)

bytes, err := unix.Mmap(int(file.Fd()), 0, int(fileinfo.Size()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes has to be immutable, it cannot be written to.

@Oppen
Copy link
Contributor

Oppen commented Jun 25, 2022

As @lemire pointed out, frozen bitmaps are not meant to be modified. The name attempts to make that clear. It could maybe handle that better or behave as CoW, but that's arguably a bad idea, as you'd potentially be duplicating memory use and inducing a leak. The idea of frozen bitmaps is to have an optimized version of read only bitmaps (in particular one that is compatible with CRoaring).

I'll look into it with more detail at night.

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 this pull request may close these issues.

None yet

3 participants