Skip to content

Export only the C API entrypoint from the loadable extension - #167

Closed
Robinho-MR wants to merge 1 commit into
duckdb:mainfrom
Robinho-MR:fix-capi-exported-symbols
Closed

Robinho-MR wants to merge 1 commit into
duckdb:mainfrom
Robinho-MR:fix-capi-exported-symbols

Conversation

@Robinho-MR

Copy link
Copy Markdown
Contributor

Since the port to the C API (C_STRUCT_UNSTABLE), the loadable avro extension exports all of its symbols. That includes avro-c and the bundled jansson. The published v2.0.1 binary exports 1336 symbols on linux_amd64, 75 of them json_*. The C++ ABI v2.0.0 binary exported 12. DuckDB's build doesn't restrict exports for C API extensions yet (# TODO strip all symbols except the capi init in extension_build_tools.cmake).

On Linux, a symbol that the host executable defines takes precedence over one in a dlopen-ed library. When DuckDB runs inside Postgres (pg_duckdb), jansson's internal call to json_object() binds to Postgres's exported SQL function json_object(fcinfo) instead. It then crashes in pg_detoast_datum while avro parses an Avro file's schema, for example on the first iceberg_scan.

This PR links the loadable extension so it exports only avro_init_c_api_v2:

  • Linux: a version script, { global: avro_init_c_api_v2; local: *; };
  • macOS: -exported_symbol,_avro_init_c_api_v2, the same flag DuckDB uses for C++ extensions
  • Wasm and Windows are unchanged. They already export only EXPORTED_FUNCTIONS and dllexport symbols, respectively.

Testing (linux_arm64, reldebug)

  • nm -D --defined-only avro.duckdb_extension: 1298 symbols, json_object among them, before; only avro_init_c_api_v2 after.
  • Repro without Postgres: LD_PRELOAD a library that defines an aborting json_object, then LOAD the extension and read_avro(...) an Iceberg manifest. Before: host json_object called (symbol clash), SIGABRT. After: the query returns normally.
  • unittest "test/*": all 32 test files pass.
  • I couldn't test macOS locally. CI covers it.

Since the move to the C API, the loadable extension exported all of its
symbols, including those of avro-c and its bundled jansson (1298 on
linux_arm64). On Linux a symbol defined by the host executable takes
precedence over one in a dlopen-ed library, so when DuckDB runs inside
Postgres (pg_duckdb), jansson's internal call to json_object() bound to
Postgres' SQL function json_object(fcinfo) and crashed while parsing an
Avro schema.

Link the loadable extension with a version script (Linux) or an
exported-symbols list (macOS) that exports only avro_init_c_api_v2.
@Mytherin

Copy link
Copy Markdown
Member

Superseded by duckdb/duckdb#29057

@Mytherin Mytherin closed this Nov 19, 2026
PeterVanHolland pushed a commit to PeterVanHolland/duckdb that referenced this pull request Nov 19, 2026
…kdb#29057)

C++ ABI loadable extensions are linked so that they export only
`<name>_duckdb_cpp_init`. For C API extensions (`C_STRUCT` /
`C_STRUCT_UNSTABLE`) this was still a TODO in
`build_loadable_extension_directory` (`# TODO strip all symbols except
the capi init`), so they exported every symbol of the libraries they
bundle. The v2.0.1 avro extension, which moved to the C API, exports
1336 symbols on linux_amd64, including jansson's `json_*` functions. The
v2.0.0 C++ ABI build exported 12.

On Linux, a symbol that the host executable defines takes precedence
over one in a `dlopen`-ed library. When DuckDB runs inside Postgres
(pg_duckdb), jansson's internal call to `json_object()` binds to
Postgres's exported SQL function `json_object(fcinfo)` instead. It then
crashes in `pg_detoast_datum` while avro parses an Avro schema, for
example on the first `iceberg_scan`.

This PR exports only the C API entrypoint (`<name>_init_c_api` or
`<name>_init_c_api_v2`, from `capi_entrypoint_symbol`):
- Linux and other GNU/Clang platforms: a version script, `{ global:
<entrypoint>; local: *; };`
- macOS: `-exported_symbol,_<entrypoint>`, as for C++ extensions
- Wasm, Windows and z/OS are unchanged. Wasm already exports only
`EXPORTED_FUNCTIONS`, and Windows only `dllexport` symbols.

Because the release builds use this file, this also fixes avro at its
current pin, with no avro bump needed. duckdb/duckdb-avro#167 makes the
same change in avro's own CMake.

### Testing (linux_arm64, reldebug, `BUILD_EXTENSIONS=demo_capi` +
`.github/config/extensions/avro.cmake`)
- `nm -D --defined-only`: avro now exports only `avro_init_c_api_v2`,
and `demo_capi` only `demo_capi_init_c_api`. C++ extensions are
unchanged.
- Repro without Postgres: `LD_PRELOAD` a library that defines an
aborting `json_object`, then `LOAD` avro and `read_avro(...)` an Iceberg
manifest. Before: `host json_object called (symbol clash)`, SIGABRT.
After: the query returns normally.
- `LOAD demo_capi; SELECT add_numbers_together(40, 2)` returns 42.
- avro's `test/*` with `--test-dir`: 105 test cases pass. `*capi*`: 38
pass. `test/extension/*`: 29 pass.
- I couldn't test macOS locally.
eraycep pushed a commit to eraycep/duckdb that referenced this pull request Nov 19, 2026
C++ ABI loadable extensions are linked so that they export only
`<name>_duckdb_cpp_init`. For C API extensions (`C_STRUCT` /
`C_STRUCT_UNSTABLE`) this was still a TODO in
`build_loadable_extension_directory` (`# TODO strip all symbols except
the capi init`), so they exported every symbol of the libraries they
bundle. The v2.0.1 avro extension, which moved to the C API, exports
1336 symbols on linux_amd64, including jansson's `json_*` functions. The
v2.0.0 C++ ABI build exported 12.

On Linux, a symbol that the host executable defines takes precedence
over one in a `dlopen`-ed library. When DuckDB runs inside Postgres
(pg_duckdb), jansson's internal call to `json_object()` binds to
Postgres's exported SQL function `json_object(fcinfo)` instead. It then
crashes in `pg_detoast_datum` while avro parses an Avro schema, for
example on the first `iceberg_scan`.

This PR (the `main` counterpart of duckdb#29057) exports only the
C API entrypoint (`<name>_init_c_api` or `<name>_init_c_api_v2`, from
`capi_entrypoint_symbol`):
- Linux and other GNU/Clang platforms: a version script, `{ global:
<entrypoint>; local: *; };`
- macOS: `-exported_symbol,_<entrypoint>`, as for C++ extensions
- Wasm, Windows and z/OS are unchanged. Wasm already exports only
`EXPORTED_FUNCTIONS`, and Windows only `dllexport` symbols.

Because the release builds use this file, this also fixes avro at its
current pin, with no avro bump needed. duckdb/duckdb-avro#167 makes the
same change in avro's own CMake.

### Testing (linux_arm64, reldebug, `BUILD_EXTENSIONS=demo_capi` +
`.github/config/extensions/avro.cmake`)
- `nm -D --defined-only`: avro now exports only `avro_init_c_api_v2`,
and `demo_capi` only `demo_capi_init_c_api`. C++ extensions are
unchanged.
- Repro without Postgres: `LD_PRELOAD` a library that defines an
aborting `json_object`, then `LOAD` avro and `read_avro(...)` an Iceberg
manifest. Before: `host json_object called (symbol clash)`, SIGABRT.
After: the query returns normally.
- `LOAD demo_capi; SELECT add_numbers_together(40, 2)` returns 42.
- avro's `test/*` with `--test-dir`: 105 test cases pass. `*capi*`: 38
pass. `test/extension/*`: 29 pass.
- I couldn't test macOS locally.
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.

2 participants