Invariants that Free must uphold for the soundness of various unsafe code include:
Item
Description
compatible
It must be safe to pass the result of any thin::Alloc or thin::Realloc function, implemented on the same type, to any thin::Free function (once - twice would be an unsound double free.)
exceptions
System allocators typically use extern "C" FFI, which is not safe to unwind exceptions through. Avoid C++ or SEH exceptions in favor of error codes or fatal exceptions.
threads
Allocators are typically implicitly Send+Sync, which means the underlying FFI calls must be thread safe too.
⚠️ thread-unsafe stdlibs existed once upon a time. I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
✔️ HeapFree returns FALSE/0 on error per docs / lack of HEAP_GENERATE_EXCEPTIONS. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
✔️ HeapFree returns FALSE/0 on error per docs / lack of HEAP_GENERATE_EXCEPTIONS. Non-unwinding fatalish heap corruption exceptions will only occur after previous undefined behavior.
threads
✔️ HEAP_NO_SERIALIZE is used, making this not thread safe. However, we enforce HeapNoSerialize : !Send + !Sync as validated by AssertNotSendSync.
✔️ free throws no exceptions (it’s C) and returns no errors (C89 § 7.20.3.2 ¶ 3)
threads
⚠️ thread-unsafe stdlibs existed once upon a time. I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
✔️ ::operator delete(void*) is throw() and returns no errors (C++03 § 18.4.1.1 ¶ 10) / noexcept (C++11+)
threads
⚠️ thread-unsafe stdlibs existed once upon a time. I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
✔️ ::operator delete[](void*) is throw() and returns no errors (C++03 § 18.4.1.2 ¶ 9) / noexcept (C++11+)
threads
⚠️ thread-unsafe stdlibs existed once upon a time. I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
✔️ VirtualFree returns FALSE/0 on error per docs, page structures live outside of process memory where they’re “incorruptable”
threads
⚠️ As everything builds upon Virtual*, and Microsoft isn’t a bunch of dummies, VirtualFreeshould should be thread safe, although it’s poorly documented