Skip to content

Namespace-wrap vendored mbedtls to avoid clashes with statically-linked duckdb - #830

Merged
adsharma merged 1 commit into
mainfrom
fix/mbedtls-namespace-wrap
Aug 22, 2026
Merged

Namespace-wrap vendored mbedtls to avoid clashes with statically-linked duckdb#830
adsharma merged 1 commit into
mainfrom
fix/mbedtls-namespace-wrap

Conversation

@adsharma

Copy link
Copy Markdown
Contributor

Problem

GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs' failed at the final link with duplicate symbol errors: both our third_party/mbedtls and Homebrew's libduckdb_static.a bundle mbedtls with plain, unprefixed mbedtls_* symbols, and both copies get pulled into the shell link.

Fix

Wrap our vendored mbedtls in namespace lbug_mbedtls so its symbols can never collide with another statically-linked mbedtls copy:

  • third_party/ports/mbedtls/do-patch.py (new): port patch script following the zstd port pattern. Renames .c -> .cpp, strips the dead extern "C" guards (the copy is compiled as C++ everywhere, and C linkage is exactly what forces plain symbol names), and wraps top-level code segments in balanced namespace lbug_mbedtls { } pairs. Segmentation tracks preprocessor conditional context and brace depth per line, so sibling conditional branches (MBEDTLS_AES_ALT vs MBEDTLS_SELF_TEST) and mid-declaration conditionals (the lone static + conditional-attribute idiom in bignum.cpp) stay balanced. Also absorbs the previously-missing format_c_to_cpp.py step, making the port runnable again.
  • ports/mbedtls/Makefile: invoke do-patch.py during build.
  • third_party/mbedtls: vendored sources regenerated via the script (57 files; diff is only namespace pairs and extern "C" removals).
  • extension submodule bump: requires Link duckdb_generated_extension_loader when linking duckdb statically extensions#63 (linking libduckdb_generated_extension_loader.a + bundled extension archives when linking duckdb statically).

Public headers re-expose names via using namespace lbug_mbedtls, so in-tree consumers (src/common/sha256.cpp, extension/httpfs/src/crypto.cpp) are unchanged.

Verification

GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs' completes: the link succeeds, libmbedtls.a exports zero plain _mbedtls_* symbols (222 namespaced ones), and the shell passes a smoke query.

Both ladybug's third_party/mbedtls and a statically-linked duckdb
(libduckdb_static.a) bundle mbedtls with plain, unprefixed mbedtls_*
symbols. When both end up on one link line (e.g. GEN=Ninja make shell
EXTENSION_STATIC_LINK_LIST='httpfs'), the linker reports ~19 duplicate
symbol errors for _mbedtls_cipher_*.

Fix by wrapping our vendored copy in namespace lbug_mbedtls:

- third_party/ports/mbedtls/do-patch.py (new): port patch script,
  following the zstd port pattern. It renames .c -> .cpp, strips the
  now-dead extern "C" guards (the copy is compiled as C++ everywhere),
  and wraps top-level code segments in balanced namespace pairs.
  Segmentation tracks preprocessor conditional context and brace depth
  per line, so sibling conditional branches (MBEDTLS_AES_ALT vs
  MBEDTLS_SELF_TEST) and mid-declaration conditionals (lone 'static'
  followed by a conditional attribute block) stay balanced. It also
  absorbs the previously-missing format_c_to_cpp.py step, making the
  port runnable again.
- ports/mbedtls/Makefile: invoke do-patch.py during build.
- third_party/mbedtls: vendored sources regenerated via the script
  (57 files; only namespace pairs and extern "C" removals).

Public headers re-expose names via 'using namespace lbug_mbedtls', so
in-tree consumers (src/common/sha256.cpp,
extension/httpfs/src/crypto.cpp) are unchanged.

Requires LadybugDB/extensions#63 (submodule bump included here).
Verified with GEN=Ninja make shell EXTENSION_STATIC_LINK_LIST='httpfs':
the link succeeds, libmbedtls.a exports zero plain _mbedtls_* symbols,
and the shell passes a smoke query.
@adsharma
adsharma force-pushed the fix/mbedtls-namespace-wrap branch from 3356a5a to 8432982 Compare August 22, 2026 17:09
@adsharma
adsharma merged commit 850f260 into main Aug 22, 2026
3 checks passed
@adsharma
adsharma deleted the fix/mbedtls-namespace-wrap branch August 22, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant