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

Fix non-deterministic to_bin #20

Merged
merged 2 commits into from
Jan 23, 2021
Merged

Conversation

JayKickliter
Copy link
Contributor

@JayKickliter JayKickliter commented Jan 22, 2021

Calling to_bin on identical filters returns non-equal binaries. It appears to be related to the use of uninitialized memory in the xorfilter library itself. Because we're already replacing its use of malloc with enif_alloc, let's override it to use a custom function that wraps enif_alloc with memset.

Curiously, xor8 doesn’t appear to be affected.

Example code

A = lists:foldl(fun(_, Acc) -> [crypto:strong_rand_bytes(16) | Acc] end,[],lists:seq(1, 10)),
{Filter1, _} = xor16:new(A, default_hash),
{BinFilter1, _} = xor16:to_bin({Filter1, default_hash}),
{Filter2, _} = xor16:new(A, default_hash),
{BinFilter2, _} = xor16:to_bin({Filter2, default_hash}),

Current binary contents:

7> rp(BinFilter1).
<<193,92,2,137,236,45,10,145,14,0,0,0,0,0,0,0,0,0,160,201,
  139,127,150,146,64,142,160,201,139,127,0,0,0,0,0,0,0,0,
  0,0,86,102,0,0,0,0,187,230,3,0,0,0,112,108,3,0,0,0,191,
  125,0,0,0,0,18,203,25,56,194,0,195,0,226,0,140,236,6,1,
  4,0,226,52,11,0,12,0,14,0,196,31,177,132,21,0,23,0,24,0,
  27,0>>
ok
8> rp(BinFilter2).
<<193,92,2,137,236,45,10,145,14,0,0,0,0,0,0,0,0,0,160,201,
  139,127,214,144,0,0,0,0,0,0,0,0,64,142,160,201,139,127,
  0,0,88,102,0,0,0,0,187,230,1,0,0,0,8,108,3,0,0,0,179,
  125,0,0,0,0,30,203,12,56,130,2,105,116,1,0,204,98,0,0,0,
  0,162,186,0,0,0,0,0,0,196,31,177,132,0,0,0,0,0,0,0,0>>
ok

This patch:

7> rp(BinFilter1).
<<193,92,2,137,236,45,10,145,14,0,0,0,0,0,0,0,142,156,0,0,
  223,235,45,151,0,0,0,0,0,0,0,0,0,0,98,50,114,214,246,
  176,130,223,0,0,0,0,126,89,18,6,0,0,223,173,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0>>
ok
8> rp(BinFilter2).
<<193,92,2,137,236,45,10,145,14,0,0,0,0,0,0,0,142,156,0,0,
  223,235,45,151,0,0,0,0,0,0,0,0,0,0,98,50,114,214,246,
  176,130,223,0,0,0,0,126,89,18,6,0,0,223,173,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0>>
ok

Calling to_bin on identical filters returns non-equal binaries. It
_appears_ to related to use of uninitialzed memeory in the xorfilter
library itself. Because we're already replacing its use of malloc with
enif_alloc, let's override it to use a custom function that wraps
enif_alloc with memset.
@mpope9
Copy link
Owner

mpope9 commented Jan 23, 2021

Nice, love the tests! I'd prefer bzero, but this looks fine. Thanks again!

@mpope9 mpope9 merged commit 8f875bc into mpope9:master Jan 23, 2021
@JayKickliter
Copy link
Contributor Author

JayKickliter commented Jan 23, 2021

I'd prefer bzero

Me too, but it’s not standard and not available everywhere.

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.

2 participants