marian-nmt / marian-dev

Fast Neural Machine Translation in C++ - development repository
https://marian-nmt.github.io
Other
257 stars 126 forks source link

intrusive_ptr not threadsafe #991

Open kpu opened 1 year ago

kpu commented 1 year ago

The reference counter is a bare size_t https://github.com/marian-nmt/marian-dev/blob/3daf4ee2906583dfc86e4f6986b40f843e7e3a3c/src/common/intrusive_ptr.h#L15-L16 and the pointer type is a bare pointer: https://github.com/marian-nmt/marian-dev/blob/3daf4ee2906583dfc86e4f6986b40f843e7e3a3c/src/common/intrusive_ptr.h#L137

If two threads destroy their references simultaneously, then both enter this function: https://github.com/marian-nmt/marian-dev/blob/3daf4ee2906583dfc86e4f6986b40f843e7e3a3c/src/common/intrusive_ptr.h#L23-L28 It's possible both threads will read x != 0 and x->references_ == 2 then commit 1 back and leak the object.

Aside: this line doesn't do anything since it's in a function that takes type* x https://github.com/marian-nmt/marian-dev/blob/3daf4ee2906583dfc86e4f6986b40f843e7e3a3c/src/common/intrusive_ptr.h#L26