Skip to content

Commit ec4868a

Browse files
dulinrileyfacebook-github-bot
authored andcommitted
Fix lifetime issue of PyBundledModule bytes object (#1108)
Summary: Pull Request resolved: #1108 The PyBundledModule stores a raw pointer into a buffer, but the problem is that there's no guarantee that the input `py::bytes` object is still alive later when the PyBundledModule is used, leading to an ASAN error. Fix this by storing a refcounted `py::bytes` object instead of a raw pointer in the mirrored C++ object. This will prevent the refcount from going to zero while the PyBundledModule object is still alive. Reviewed By: JacobSzwejbka Differential Revision: D50670477 fbshipit-source-id: 3de4f5c2eee125a2d9552bc206d2091bf4879e84
1 parent 66fdef6 commit ec4868a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

extension/pybindings/pybindings.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,16 @@ struct PyBundledModule final {
268268
explicit PyBundledModule(
269269
const py::bytes& buffer,
270270
uint32_t bundled_input_pool_size)
271-
: bundled_program_ptr_(
272-
static_cast<const void*>((buffer.cast<std::string_view>().data()))),
271+
: bundled_program_ptr_(buffer),
273272
program_ptr_(static_cast<const void*>(
274-
bundled_program_flatbuffer::GetBundledProgram(bundled_program_ptr_)
273+
bundled_program_flatbuffer::GetBundledProgram(
274+
get_bundled_program_ptr())
275275
->program()
276276
->data())),
277-
program_len_(
278-
bundled_program_flatbuffer::GetBundledProgram(bundled_program_ptr_)
279-
->program()
280-
->size()),
277+
program_len_(bundled_program_flatbuffer::GetBundledProgram(
278+
get_bundled_program_ptr())
279+
->program()
280+
->size()),
281281
bundled_input_allocator_(
282282
{bundled_input_pool_size, new uint8_t[bundled_input_pool_size]}) {}
283283

@@ -291,7 +291,7 @@ struct PyBundledModule final {
291291
return bundled_input_allocator_;
292292
}
293293
const void* get_bundled_program_ptr() {
294-
return bundled_program_ptr_;
294+
return bundled_program_ptr_.cast<std::string_view>().data();
295295
}
296296

297297
const void* get_program_ptr() {
@@ -303,7 +303,9 @@ struct PyBundledModule final {
303303
}
304304

305305
private:
306-
const void* bundled_program_ptr_;
306+
// Store the bytes object instead of a raw pointer so that this module will
307+
// keep the bytes alive.
308+
const py::bytes bundled_program_ptr_;
307309
const void* program_ptr_;
308310
size_t program_len_;
309311
MemoryAllocator bundled_input_allocator_;

0 commit comments

Comments
 (0)