1. Jun 03, 2020
  2. Jun 02, 2020
  3. May 20, 2020
  4. May 19, 2020
  5. May 18, 2020
  6. May 17, 2020
    • Anna Henningsen's avatar
      buffer,n-api: release external buffers from BackingStore callback · 24faa37a
      Anna Henningsen authored
      Release `Buffer` and `ArrayBuffer` instances that were created through
      our addon APIs and have finalizers attached to them only after V8 has
      called the deleter callback passed to the `BackingStore`, instead of
      relying on our own GC callback(s).
      
      This fixes the following race condition:
      
      1. Addon code allocates pointer P via `malloc`.
      2. P is passed into `napi_create_external_buffer` with a finalization
         callback which calls `free(P)`. P is inserted into V8’s global array
         buffer table for tracking.
      3. The finalization callback is executed on GC. P is freed and returned
         to the allocator. P is not yet removed from V8’s global array
         buffer table. (!)
      4. Addon code attempts to allocate memory once again. The allocator
         returns P, as it is now available.
      5. P is passed into `napi_create_external_buffer`. P still has not been
         removed from the v8 global array buffer table.
      6. The world ends with `Check failed: result.second`.
      
      Since our API contract is to call the finalizer on the JS thread on
      which the `ArrayBuffer` was created, but V8 may call the `BackingStore`
      deleter callback on another thread, fixing this requires posting
      a task back to the JS thread.
      
      Refs: https://github.com/nodejs/node/issues/32463#issuecomment-625877175
      Fixes: https://github.com/nodejs/node/issues/32463
      
      PR-URL: https://github.com/nodejs/node/pull/33321
      
      
      Reviewed-By: default avatarJames M Snell <jasnell@gmail.com>
      24faa37a