connectedhomeip icon indicating copy to clipboard operation
connectedhomeip copied to clipboard

Platform::MemoryInit pretends to be threadsafe but is not

Open bzbarsky-apple opened this issue 2 years ago • 2 comments

Problem

MemoryInit has an atomic counter that it uses to ensure MemoryAllocatorInit is only called once if multiple MemoryInit calls happen. But you can easily get a race as follows:

  1. Thread 1 calls MemoryInit, increments counter, loses timeslice before calling MemoryAllocatorInit.
  2. Thread 2 calls MemoryInit, counter is nonzero, MemoryInit returns.
  3. Thread 2 does Platform::MemoryAlloc before things are properly initialized.

Proposed Solution

Either properly synchronize MemoryInit (with a lock around the "increment the counter and call MemoryAllocatorInit" bits), or just use a non-atomic counter and clearly document that synchronization is the consumer's problem.

bzbarsky-apple avatar May 20 '22 15:05 bzbarsky-apple

Reopening, since #18854 was reverted.

bzbarsky-apple avatar Jun 03 '22 15:06 bzbarsky-apple

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Dec 11 '22 18:12 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Jun 25 '23 17:06 stale[bot]