Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions patches/boringssl/require-memory-hooks.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--- 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) {
7 changes: 7 additions & 0 deletions scripts/build/deps/boringssl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export const boringssl: Dependency = {
commit: BORINGSSL_COMMIT,
}),

// Upstream mem.cc gates OPENSSL_memory_* weak-symbol overrides on __ELF__;
// on Mach-O/COFF the hooks compile to static nullptr and OPENSSL_malloc goes
// straight to libc. Declare them as plain externs so lib.rs binds everywhere.
patches: ["patches/boringssl/require-memory-hooks.patch"],

build: cfg => {
// win-x64 uses NASM-syntax .asm; everything else (including win-aarch64)
// uses gas .S that clang assembles.
Expand All @@ -48,6 +53,8 @@ export const boringssl: Dependency = {
includes: ["include"],
defines: {
BORINGSSL_IMPLEMENTATION: true,
// See `patches:` above. Bun always defines the three hooks.
BORINGSSL_REQUIRE_MEMORY_HOOKS: true,
Comment thread
robobun marked this conversation as resolved.
Resolved
...(cfg.windows && {
_HAS_EXCEPTIONS: 0,
WIN32_LEAN_AND_MEAN: true,
Expand Down
12 changes: 0 additions & 12 deletions scripts/build/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,18 +964,6 @@ export const linkerFlags: Flag[] = [
when: c => c.windows && c.winsysroot !== undefined,
desc: "Windows cross-compile: MSVC CRT + Windows SDK library search root (xwin splat)",
},
{
// BoringSSL's allocator hooks (defined in src/boringssl/lib.rs, routing
// OPENSSL_malloc to mimalloc) are referenced by BoringSSL only as COFF
// weak externals, which never pull the defining archive member on their
// own — so on Windows the hooks silently stayed unbound and every
// BoringSSL allocation (TLS handshakes, parsed certificates) landed on
// the CRT's NT heap instead of mimalloc. Force the symbols in so the
// weak externals bind, like they already do on ELF/Mach-O.
flag: ["/INCLUDE:OPENSSL_memory_alloc", "/INCLUDE:OPENSSL_memory_free", "/INCLUDE:OPENSSL_memory_get_size"],
when: c => c.windows,
desc: "Bind BoringSSL's mimalloc-backed allocator hooks (COFF weak externals don't pull archive members)",
},
{
flag: ["/STACK:0x1200000,0x200000", "/errorlimit:0"],
when: c => c.windows,
Expand Down
Loading