parcel-bundler / watcher

👀 A native C++ Node module for querying and subscribing to filesystem events
MIT License
656 stars 45 forks source link

Fix use after free errors #177

Closed yamadapc closed 3 months ago

yamadapc commented 3 months ago

Fixes a few cases where use after free could happen, and one where it can be easily reproduced.

When using the FSEvents back-end, both the Watcher and a State pointers are passed as part of the context parameter to a C style callback.

Both of these pointers may be free-ed before the callback is called before this commit.

After this commit, these two pointers, as well as all most others in the codebase are replaced with shared_ptr.

This is a lazy fix and goes against C++ core guidelines - "F.7". (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-smart)

A secondary pass would use reference counting only in the shared ownership cases. For most back-ends this shared ownership happens on: error types, subscription maps/sets and state structs. (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-owner)

yamadapc commented 3 months ago

I still need to:

While it is possible that use after free happens on other back-ends, main bug I can reproduce is related to macOS fsevents callback.

yamadapc commented 3 months ago

@devongovett I get errors on the wasm/index.mjs file, due to missing syscall_open definition. Do you know how that file was generated?