Skip to content

Is it really safe to read pointers from get after the txn has finished? #2

@msackman

Description

@msackman

Just been reading the code. In your c_src drv, in get, you create a txn and do the get and then abort the txn. The pointers to data returned from the get are then copied to the ErlNifBinary which are then sent back to erlang-land.

It's not clear from the mdb docs, but I'd be surprised if those pointers are safe after the txn has been aborted. Having read various bits about how mdb works, once the txn has been finished, there's nothing to stop someone else coming in and modifying those locations, and given how mdb reuses pages, it could well end up with utterly different key-value pairs in those locations.

I think you really have to expose the whole txn api within erlang and if the values really need to exist outside the scope of the txn then you're going to have to copy. I think - I could be wrong though.

I also have some concerns about memory management of those binaries. The value locations should not be freed by erlang when the binary is GC'd as they're pointers straight into the mmap used by mdb. Instead they should just be forgotten about. I think you'd have to use enif_make_resource_binary for that so you can specify a noop dtor. Again I could be wrong - I'm curious as to whether you've considered these issues and found they are safe as you've written them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions