-
Notifications
You must be signed in to change notification settings - Fork 224
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
base: master
Are you sure you want to change the base?
Conversation
} | ||
|
||
func getBytesAsMMap(t *testing.T, startingBytes []byte) []byte { | ||
os.WriteFile("data", startingBytes, 0777) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()), |
There was a problem hiding this comment.
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.
@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. |
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) |
There was a problem hiding this comment.
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.
@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 |
|
||
func getBytesAsMMap(t *testing.T, startingBytes []byte) []byte { | ||
os.WriteFile("data", startingBytes, 0777) | ||
file, err := os.OpenFile("data", os.O_RDONLY, 0) |
There was a problem hiding this comment.
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()), |
There was a problem hiding this comment.
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.
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. |
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.