Skip to content

Add guarded COM borrowed-copy transactions for Audio, WIC and MF - #146

Merged
leileizhang (lei9444) merged 5 commits into
feature/com-native-completionfrom
feature/com-borrowed-buffers
Sep 9, 2026
Merged

leileizhang (lei9444) merged 5 commits into
feature/com-native-completionfrom
feature/com-borrowed-buffers

Conversation

@lei9444

@lei9444 leileizhang (lei9444) commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Stack and scope

PR3 of the four-PR roadmap, based on feature/com-native-completion / #145. #147 remains stacked on this branch. The activation target registry migration from #144 is preserved without expanding its target set.

This adds synchronous owned-copy transactions, not native-backed JavaScript views or caller-asserted pointer lifetimes.

Receiver Generated API
IAudioRenderClient writeFramesCopy(data), writeSilence(frames)
IAudioCaptureClient readPacketCopy()
IWICBitmap readLockedBgra8Copy(rect)
IMFMediaBuffer readCopy(), replaceCopy(data)

Audio render/capture and linear MF are copy-only facades, not complete native-interface projections. WIC retains its complete methods and gains the read-copy operation. Complete coverage remains 5,697 / 7,929.

Plan-driven execution

The follow-up refactor replaces the original four family-specific execution flows, rather than merely relocating expected(kind).

  • A native-independent dynwinrt-com-contracts crate contains one packaged registry of interface identities, exact IID/slot/ABI evidence, positional scalar units, acquisition/finalizer relationships, and typed operation recipes.
  • Codegen selects and validates that data against the configured WinMD. Runtime admission requires an exact packaged record plus an independently validated typed graph. Application-supplied lookalike records are not authorization.
  • Named bindings and before / acquire / after steps drive actual native calls. Frame, linear-byte and row extents drive checked bounds and copying. Transfer, native length commit, normal/abort cleanup, and result mappings are executed from the plan.
  • Public operations are thin selectors over the common executor. There is no production family dispatch or per-interface SDK adapter, and renderers only serialize validated projection decisions.

The graph proof rejects an unrelated method substituted for cleanup, frame counts bound to native flag parameters, unavailable abort inputs, duplicate or dangling bindings, and mismatched owner lifetimes. Negative tests exercise this proof independently of whole-record admission.

Test-only records use different IIDs, slots, call names and argument orders for linear, frame-writer, frame-reader and row-copy interfaces. They perform actual byte copies and full/zero/owner cleanup through the same executor; misleading display labels cannot select an algorithm. Test registry injection is absent from production and JS test-hooks builds.

Preserved safety boundaries

Canonical-identity sidecars, observed successful Audio initialization/GetService provenance, owner-thread gates, callback suppression, exactly-once cleanup and poisoning remain intact. Audio byte sizes still come only from the format used by actual successful initialization, never caller block alignment or GetMixFormat.

Capture preserves empty/silent/timestamp-error handling. WIC uses a READ lock, native BGRA8 dimensions/stride/count, bounded pixel-row copies and owner Release, not an invented Unlock. Linear MF checks both native lengths, commits replacement length, and Unlocks after successful acquisition.

This does not make ownership or provenance user-declarable. New ownership models still require reviewed semantic support. No raw borrow addresses, escaping native buffers, general cross-apartment dispatch or new production interfaces are introduced. The 64 MiB cap, unsupported exclusive-event render and unsupported MF2D/plane/pitch cases remain unchanged; audio capture testing uses controlled fakes, not real recording.

The WinRT model, generated WinRT APIs and npm root surface are unchanged. A policy-free, fallible pre-dispatch hook in the private lowering backend permits the COM argument check described below; ordinary WinRT calls supply the existing no-op behavior. Build integration adds the shared crate and its validation coverage.

Shared COM argument admission

Ordinary invocation now checks managed COM arguments as well as the receiver. The common boundary rejects wrong-apartment, busy and poisoned references before argument conversion, and checks again after native argument coercion/QI, before entering the target vtable method. Rejection leaves tracked dispatch markers unset and preserves call-local cleanup.

Counted interface arrays, supported interface-bearing VARIANT/SAFEARRAY/DISPPARAMS containers, and private WinRT-backed array/typed-struct bookkeeping retain shared context slots. Containers built while idle cannot discard later poison by outliving their original wrappers or by extracting an alias. Unbound WinRT bookkeeping stays thread-safe and unaffiliated until first COM use; its ordinary cross-thread destruction does not leak deferred state.

This is not an IMFSample::AddBuffer special case. The generated media E2E uses a stock in-memory IMFSample and controlled IMFMediaBuffer fixture: a healthy carrier is accepted, while poisoned original/alias inputs are rejected and the sample's buffer count remains unchanged. Standard identity handling and deterministic release remain available; no blanket busy state is imposed on ordinary healthy calls.

Evidence and regeneration

The same 26 exact selectors, fingerprints and Microsoft citations are retained for Win32Metadata 71.0.14-preview, SHA256 B64EE4818A7ED9F9D135038D58C51BD08369184D4D5ED428F20E9DE55DF8121D.

Borrowed descriptors are version 2; the COM file-ownership manifest is version 5. Delete old generated COM bindings and fully regenerate all selected roots with matching runtime/codegen. There is no silent migration or mixed-version support. Unsafe support schema 12 and evidence IDs remain unchanged.

All seven public receiver declarations remain byte-identical to the pre-refactor baseline. Copy-only facades are still excluded from complete-interface counts.

Validation

  • Pure graph validation: 10 tests on x64 and 10 on live i686.
  • Actual copy transactions and reordered ABI fixtures: 24 tests on x64 and 24 on live i686, including real stock-Windows WIC and linear MF objects.
  • Final metadata/evidence and public-declaration regression: 8 tests, including comparisons of all seven receiver declarations.
  • Matching final-source generated JS borrowed-copy/completion pipelines: 8 + 10 scenarios; their generated TypeScript checks also pass.
  • Stock generated E2E: 42 Python, 38 TypeScript and 17 Classic COM scenarios; strict Python mypy passes. Python dependencies and the rebuilt binding were installed only into a worktree-local venv.
  • Final i686 JS binding compile, package/type checks and unchanged WinRT snapshots pass.
  • All seven final census artifacts are byte-identical to the pre-refactor baseline, including the 5,697/7,929 complete-safe count.
  • Shared argument admission adds 12 focused runtime regressions, executed on live i686, covering pre-existing aliases/containers, later poisoning, argument/receiver changes during QI, apartment rejection before COM refcount actions, and normal unbound WinRT cross-thread lifetime.
  • Rejected fill-array dispatch hooks are covered on x64 and live i686; output allocations are RAII-owned before the hook can fail.

An unrelated serial combination of existing dynamic-sink, metadata and native-call tests hits an access violation in coerces_object_array_elements_to_the_expected_interface. A fresh build of unchanged parent 2bad1ce reproduces the same failure at the same test. Isolated suites pass; no unrelated native-call code or thresholds were changed to hide it. The earlier WinUI timing limitation remains documented separately.

Add metadata-validated owned-copy plans for Audio, WIC BGRA8 and linear
Media Foundation buffers. Observe real successful Audio initialization and
GetService calls, retain canonical identity provenance across aliases, and
finalize native borrows exactly once with thread and poison guards.

Preserve WIC's existing complete surface, keep three copy-only facades out
of complete-interface counts, require full regeneration, and add exact
metadata evidence, native fixtures, live x64/i686 tests and regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
Keep target eligibility and existing borrowed-copy contracts unchanged while consuming named activation evidence.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
Centralize reviewed ABI and lifecycle evidence in a shared contracts crate. Execute typed bindings, bounds, copies and finalizers through one plan-driven runtime while preserving public APIs and supported interfaces.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
Retain shared apartment and lifecycle contexts across interface aliases and supported containers. Recheck receivers and inputs after native coercion, preserve deferred WinRT lifetime behavior, and reject poisoned buffer inputs before IMFSample can retain them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
Preserve existing arity/shape dispatch for distinguishable groups. Emit stable AtSlot names for previously unsupported ordinary overloads without guessing ABI behavior, retain each validated method plan, reject alias collisions, and update exact coverage evidence.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
@lei9444
leileizhang (lei9444) merged commit 318762d into feature/com-native-completion Sep 9, 2026
1 check passed
@lei9444
leileizhang (lei9444) deleted the feature/com-borrowed-buffers branch September 9, 2026 01:45
leileizhang (lei9444) added a commit that referenced this pull request Sep 9, 2026
* Add metadata-driven native COM one-shot completion

Generate bounded audio activation promises with validated WinMD contracts,
libffi export/result plans, a native-only FTM signal sink, and owner-local
result collection. Preserve synchronous JS callback thread restrictions.
Cover publication, HRESULTs, ownership, teardown, bounds, and WinRT regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Add guarded COM borrowed-copy transactions for Audio, WIC and MF (#146)

* Add guarded COM borrowed-copy transactions

Add metadata-validated owned-copy plans for Audio, WIC BGRA8 and linear
Media Foundation buffers. Observe real successful Audio initialization and
GetService calls, retain canonical identity provenance across aliases, and
finalize native borrows exactly once with thread and poison guards.

Preserve WIC's existing complete surface, keep three copy-only facades out
of complete-interface counts, require full regeneration, and add exact
metadata evidence, native fixtures, live x64/i686 tests and regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Refactor COM borrowed copies into typed execution recipes

Centralize reviewed ABI and lifecycle evidence in a shared contracts crate. Execute typed bindings, bounds, copies and finalizers through one plan-driven runtime while preserving public APIs and supported interfaces.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Guard managed COM arguments before native dispatch

Retain shared apartment and lifecycle contexts across interface aliases and supported containers. Recheck receivers and inputs after native coercion, preserve deferred WinRT lifetime behavior, and reject poisoned buffer inputs before IMFSample can retain them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Project ambiguous COM overloads as explicit slot-named methods (#147)

Preserve existing arity/shape dispatch for distinguishable groups. Emit stable AtSlot names for previously unsupported ordinary overloads without guessing ABI behavior, retain each validated method plan, reject alias collisions, and update exact coverage evidence.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
leileizhang (lei9444) added a commit that referenced this pull request Sep 9, 2026
* Add safe COM projection and endpoint activation

Provide non-consuming COM projectAs with registered safe descriptors, restricted in-process IMMDevice activation with native NULL parameters, and exact GetId ownership. Preserve the WinRT root and dynamic architecture; require complete COM regeneration with manifest v3.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Move endpoint activation targets into a cited contract registry

Preserve the five existing target IIDs, in-process context, native NULL parameters and owned result contract. Validate named evidence records and lower dynamic-length IID lists without changing generated bindings or coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Add metadata-driven native COM one-shot completion (PR2/4) (#145)

* Add metadata-driven native COM one-shot completion

Generate bounded audio activation promises with validated WinMD contracts,
libffi export/result plans, a native-only FTM signal sink, and owner-local
result collection. Preserve synchronous JS callback thread restrictions.
Cover publication, HRESULTs, ownership, teardown, bounds, and WinRT regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Add guarded COM borrowed-copy transactions for Audio, WIC and MF (#146)

* Add guarded COM borrowed-copy transactions

Add metadata-validated owned-copy plans for Audio, WIC BGRA8 and linear
Media Foundation buffers. Observe real successful Audio initialization and
GetService calls, retain canonical identity provenance across aliases, and
finalize native borrows exactly once with thread and poison guards.

Preserve WIC's existing complete surface, keep three copy-only facades out
of complete-interface counts, require full regeneration, and add exact
metadata evidence, native fixtures, live x64/i686 tests and regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Refactor COM borrowed copies into typed execution recipes

Centralize reviewed ABI and lifecycle evidence in a shared contracts crate. Execute typed bindings, bounds, copies and finalizers through one plan-driven runtime while preserving public APIs and supported interfaces.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Guard managed COM arguments before native dispatch

Retain shared apartment and lifecycle contexts across interface aliases and supported containers. Recheck receivers and inputs after native coercion, preserve deferred WinRT lifetime behavior, and reject poisoned buffer inputs before IMFSample can retain them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Project ambiguous COM overloads as explicit slot-named methods (#147)

Preserve existing arity/shape dispatch for distinguishable groups. Emit stable AtSlot names for previously unsupported ordinary overloads without guessing ABI behavior, retain each validated method plan, reject alias collisions, and update exact coverage evidence.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

* Make COM declaration tests independent of built JS artifacts

Share the isolated COM runtime declaration fixture between overload and sink tests. Keep strict TypeScript and negative assertions while removing the coverage-time dependency on bindings/js/dist.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9ef7fa55-eb2a-4360-bd17-05feae50fadb
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