Files
bun-src/patches/boringssl/require-memory-hooks.patch
2026-08-27 21:09:14 +00:00

82 lines
2.2 KiB
Diff

--- a/crypto/mem.cc
+++ b/crypto/mem.cc
@@ -100,9 +100,20 @@
// primitives used must tolerate every other synchronization primitive linked
// into the process, including pthreads locks. Failing to meet these constraints
// may result in deadlocks, crashes, or memory corruption.
+#if defined(BORINGSSL_REQUIRE_MEMORY_HOOKS)
+// Bun: the embedder guarantees these three are always defined. WEAK_SYMBOL_FUNC
+// is gated on __ELF__, so on Mach-O and COFF it would expand to a static null
+// pointer and the hook path would be folded away at compile time.
+extern "C" {
+void *OPENSSL_memory_alloc(size_t size);
+void OPENSSL_memory_free(void *ptr);
+size_t OPENSSL_memory_get_size(void *ptr);
+}
+#else
WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size))
WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr))
WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr))
+#endif
#if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
static StaticMutex malloc_failure_lock;
@@ -186,6 +197,15 @@
goto err;
}
+#if defined(BORINGSSL_REQUIRE_MEMORY_HOOKS)
+ {
+ void *ptr2 = OPENSSL_memory_alloc(size);
+ if (ptr2 == nullptr && size != 0) {
+ goto err;
+ }
+ return ptr2;
+ }
+#else
if (OPENSSL_memory_alloc != nullptr) {
assert(OPENSSL_memory_free != nullptr);
assert(OPENSSL_memory_get_size != nullptr);
@@ -195,6 +215,7 @@
}
return ptr2;
}
+#endif
if (size + OPENSSL_MALLOC_PREFIX < size) {
goto err;
@@ -238,10 +259,15 @@
return;
}
+#if defined(BORINGSSL_REQUIRE_MEMORY_HOOKS)
+ OPENSSL_memory_free(orig_ptr);
+ return;
+#else
if (OPENSSL_memory_free != nullptr) {
OPENSSL_memory_free(orig_ptr);
return;
}
+#endif
void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
__asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
@@ -268,6 +294,9 @@
}
size_t old_size;
+#if defined(BORINGSSL_REQUIRE_MEMORY_HOOKS)
+ old_size = OPENSSL_memory_get_size(orig_ptr);
+#else
if (OPENSSL_memory_get_size != nullptr) {
old_size = OPENSSL_memory_get_size(orig_ptr);
} else {
@@ -276,6 +305,7 @@
old_size = *(size_t *)ptr;
__asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
}
+#endif
void *ret = OPENSSL_malloc(new_size);
if (ret == nullptr) {