-
Notifications
You must be signed in to change notification settings - Fork 225
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,13 @@ package roaring | |
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sys/unix" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
"runtime/debug" | ||
"testing" | ||
) | ||
|
||
|
@@ -126,3 +131,90 @@ func TestFrozenFormat(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
type frozenTestCase struct { | ||
name string | ||
useMMapped bool | ||
useFrozen bool | ||
shouldPanic bool | ||
} | ||
|
||
func TestFrozenCases(t *testing.T) { | ||
cases := []frozenTestCase{ | ||
{name: "in-memory-frozen", useMMapped: false, | ||
useFrozen: true, | ||
shouldPanic: false}, | ||
{name: "mmap-frozen", useMMapped: true, | ||
useFrozen: true, | ||
// THIS SHOULD NOT BE PANIC/FAULTING | ||
shouldPanic: true}, | ||
{name: "in-memory", useMMapped: false, | ||
useFrozen: false, | ||
shouldPanic: false}, | ||
{name: "mmap", useMMapped: true, | ||
useFrozen: false, | ||
shouldPanic: false}, | ||
} | ||
for _, testCase := range cases { | ||
testFrozenCase(t, testCase) | ||
} | ||
} | ||
func testFrozenCase(t *testing.T, testCase frozenTestCase) { | ||
|
||
startingBitmap := NewBitmap() | ||
startingBitmap.highlowcontainer.appendContainer(0, &runContainer16{iv: []interval16{{0, 0}, | ||
{2, 5}}}, false) | ||
|
||
primary := getBitmap(t, testCase, startingBitmap) | ||
assert.True(t, startingBitmap.Equals(primary)) | ||
clone := primary.Clone() | ||
primary.Xor(clone) | ||
res := primary.GetCardinality() | ||
assert.Equal(t, res, uint64(0)) | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I would do
I am not sure why you want to mutate the frozen view. |
||
}) | ||
} else { | ||
primary.Xor(clone) | ||
} | ||
} | ||
|
||
func getBitmap(t *testing.T, testCase frozenTestCase, startingBitmap *Bitmap) *Bitmap { | ||
receiver := NewBitmap() | ||
if testCase.useFrozen { | ||
frozenBytes, err := startingBitmap.Freeze() | ||
require.NoError(t, err) | ||
if testCase.useMMapped { | ||
require.NoError(t, receiver.FrozenView(getBytesAsMMap(t, frozenBytes))) | ||
return receiver | ||
} | ||
require.NoError(t, receiver.FrozenView(frozenBytes)) | ||
return receiver | ||
} | ||
nonFrozenBytes, err := startingBitmap.ToBytes() | ||
require.NoError(t, err) | ||
|
||
if testCase.useMMapped { | ||
_, err = receiver.FromBuffer(getBytesAsMMap(t, nonFrozenBytes)) | ||
require.NoError(t, err) | ||
return receiver | ||
} | ||
_, err = receiver.FromBuffer(nonFrozenBytes) | ||
require.NoError(t, err) | ||
return receiver | ||
} | ||
|
||
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 commentThe 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. |
||
file, err := os.OpenFile("data", os.O_RDONLY, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file is read-only. |
||
require.NoError(t, err) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. There is a ressource leak around it, surely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bytes has to be immutable, it cannot be written to. |
||
unix.PROT_READ, unix.MAP_PRIVATE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
require.NoError(t, err) | ||
return bytes | ||
} |
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