Skip to content

ffi: accept the full declaration form in dlsym() - #530

Merged
jow- merged 1 commit into
ucode-lang:masterfrom
Cypih:fix/ffi-dlsym-declaration
Nov 30, 2026
Merged

jow- merged 1 commit into
ucode-lang:masterfrom
Cypih:fix/ffi-dlsym-declaration

Conversation

@Cypih

@Cypih Cypih commented Nov 30, 2026 •

Copy link
Copy Markdown

uc_clib_dlsym() parses the documented full declaration form with CPARSE_MODE_ABSTRACT only, which rejects a named declarator, so ffi.C.dlsym('int abs(int)') fails.
The change adds CPARSE_MODE_DIRECT, as in wrap().
Line 2 of the documented example now calls getenv() through wrap() instead of reading its code as a pointer.

Limits: only function declarations work (the documentation now says so); variable and constant declarations still fail. The parser keeps the name of a failed non-function declaration on the shared type, so a later unnamed declaration can report it (dlsym('int labs'), then dlsym('signed int') gives "missing declaration for symbol 'labs'"); master does the same through wrap(), and this change leaves it.

Test: the new 17_lib_ffi/37_dlsym_declaration fails on master and passes with the change; the other tests give the same results.

Not tested: musl, 32-bit targets, valgrind.

Closes #528

The documentation of CLib#dlsym() names a full C declaration as the
second form of its argument. uc_clib_dlsym() parses such a string with
CPARSE_MODE_ABSTRACT, and in that mode the parser rejects a declarator
with a name. So each declaration with a name fails with "failed to parse
C declaration", also the example of the documentation.

Add CPARSE_MODE_DIRECT, which uc_clib_resolve_common() also uses for
wrap(). Keep CPARSE_MODE_ABSTRACT, so that a declaration without a name
still gets the error "declaration does not define a named symbol".

The form works for function declarations only; a variable or a constant
declaration still fails, as before. So the documentation now names the
form a full function declaration.

The second line of the example read the code of getenv() as a char
pointer through deref(), which ends with SIGSEGV. Call the symbol through
wrap() instead, the documented way for a function pointer from dlsym().

Fixes: 23d5561 ("ffi: add LibFFI-based foreign function interface module")
Fixes: ucode-lang#528
Signed-off-by: Piotr Kubik <piotr@kubik.pl>
@jow-
jow- merged commit dcfb42f into ucode-lang:master Nov 30, 2026
2 checks passed
@jow-

jow- commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Merged, thanks!

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.

ffi: CLib#dlsym() rejects the documented full-declaration form

2 participants