Skip to content

Importing a -shared module's export declare C binding crashes #368

Closed

Description

@ASDAlexander77

Importing a function that a -shared module binds to a C runtime function with export declare crashes the importing program.

// export_declare_linkname.ts - built with -shared
namespace C {
    @linkname("strlen")          // @dllname("strlen") behaves the same way
    export declare function len(s: string): index;
}
// import_declare_linkname.ts
import './export_declare_linkname'

function main() {
    assert(C.len("hello") == 5);
    print("done.");
}

test-runner -shared import_declare_linkname.ts export_declare_linkname.ts compiles both files, then the program faults when it calls C.len.

Cause

  • The importer re-prints the declaration from __decls as @dllimport @dllname("strlen").
  • At startup it looks strlen up in the loaded tslang libraries (tslang_search_for_address_of_symbol).
  • The exporting DLL never contains strlen: nothing in that module calls it, so the unused declaration is dropped.
  • The lookup returns null, and the call jumps through it.

This is older than @linkname: the @dllname spelling crashes the same way on a 2026-09-23 build.

Test

test-compile-shared-export-import-declare-linkname (added in #366) is registered DISABLED until this is fixed. Its static-link twin, test-compile-export-import-declare-linkname, passes.

Possible fixes:

  • the importer resolves a declared binding against the process's own symbols, not the tslang library's;
  • or the exporting module forwards the symbol.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions