Skip to content

Commit

Permalink
Fix non-deterministic to_bin (#20)
Browse files Browse the repository at this point in the history
* Identical filters should serialize to identical binraries

* Fix non-deterministic to_bin

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.
  • Loading branch information
JayKickliter authored Jan 23, 2021
1 parent db02d1f commit 8f875bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 22 additions & 1 deletion c_src/xor_filter_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include "erl_nif.h"
#include "ewok.h"

#define malloc(size) enif_alloc(size)
// This forward declaration is needed because we're using this
// function to override xorfilter's use of malloc.
void * xor_nif_zalloc(size_t size);
#define malloc(size) xor_nif_zalloc(size)
#define free(size) enif_free(size)
#include "xorfilter.h"

Expand Down Expand Up @@ -46,6 +49,24 @@ pack_le_u64(uint8_t * dst, uint64_t val) {
dst[7] = (val >> 56) & 0xff;
}

// Allocates 'size' zeroized bytes from the VM.
//
// Erlang does not provide a malloc like function which returns
// zeroized memory. That's not usually a problem, as you can always
// call memset after allocation memory. However, we're overriding the
// xorfilter's use malloc by redefining it before including
// xorfilter.h, leaving us no other option than to the VMs allocator
// and memset into this function.
void *
xor_nif_zalloc(size_t size)
{
void * mem = enif_alloc(size);
if (mem) {
memset(mem, 0, size);
}
return mem;
}

void
destroy_exor_t_resource(ErlNifEnv* env, void* obj)
{
Expand Down
10 changes: 8 additions & 2 deletions test/exor_filter_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ xor8_serialization() ->
?assertEqual(false, xor8:contain(BinFilter, "test4")),
Filter2 = xor8:from_bin(BinFilter),
?assertEqual(true, xor8:contain(Filter2, "test1")),
?assertEqual(false, xor8:contain(Filter2, "test4")).
?assertEqual(false, xor8:contain(Filter2, "test4")),
Filter3 = xor8:new(["test1", "test2", "test3"]),
BinFilter2 = xor8:to_bin(Filter3),
?assertEqual(BinFilter, BinFilter2).

xor8_incremental_builder() ->
Filter0 = xor8:new_empty(),
Expand Down Expand Up @@ -403,7 +406,10 @@ xor16_serialization() ->
?assertEqual(false, xor16:contain(BinFilter, "test4")),
Filter2 = xor16:from_bin(BinFilter),
?assertEqual(true, xor16:contain(Filter2, "test1")),
?assertEqual(false, xor16:contain(Filter2, "test4")).
?assertEqual(false, xor16:contain(Filter2, "test4")),
Filter3 = xor16:new(["test1", "test2", "test3"]),
BinFilter2 = xor16:to_bin(Filter3),
?assertEqual(BinFilter, BinFilter2).

xor16_incremental_builder() ->
Filter0 = xor16:new_empty(),
Expand Down

0 comments on commit 8f875bc

Please sign in to comment.