encryptogroup / ABY

ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
GNU Lesser General Public License v3.0
463 stars 132 forks source link

Responsibility for the deallocation of share objects #15

Open lenerd opened 7 years ago

lenerd commented 7 years ago

Each time a gate is created via the Put...Gate, e.g. BooleanCircuit::PutXORGate(share*, share*), a share object is created.

share* BooleanCircuit::PutXORGate(share* ina, share* inb) {
    return new boolshare(PutXORGate(ina->get_wires(), inb->get_wires()), this);
}

Following the examples and building a circuit like this results in memory leaks.

            s_f = circ->PutANDGate(s_b, s_c);
            s_tmp = circ->PutANDGate(s_b, s_d);
            s_f = circ->PutXORGate(s_f, s_tmp);

The pointer s_f to the object allocated by PutANDGate is overwritten in the third line and the corresponding object remains in memory without being used ever again.

If the user is responsible for the deallocation of the share objects, it should be noted in the developer guide and done in the examples applications.

A different (and in my eyes more pleasant) solution would be something like this:

using share_p = std::shared_ptr<share>;
share_p BooleanCircuit::PutXORGate(share_p ina, share_p inb) {
    return std::make_shared<boolshare>(PutXORGate(ina->get_wires(), inb->get_wires()), this);
}

The created boolshare gets destroyed as soon as all copies of the std::shared_ptr are gone.