Skip to content

fix(rdp): rebuild GDI in place on failed resize and lock CLIPRDR data for remote file downloads - #14

Open
heartacker wants to merge 4 commits into
AtomUICommunity:masterfrom
heartacker:fix2
Open

heartacker wants to merge 4 commits into
AtomUICommunity:masterfrom
heartacker:fix2

Conversation

@heartacker

Copy link
Copy Markdown
Contributor

Summary

Two native CxRdpBridge fixes. Note on stacking: the first two commits in this branch are byte-identical (same SHAs) to the two commits in #13 (fix/ssh-resize-and-datagrid-keys). This branch is deliberately stacked on them, so once #13 lands this PR automatically reduces to the two RDP commits below — either PR can merge first.

1. Stop frame callbacks reading a freed GDI buffer on resize (native/CxRdpBridge/cxrdp_bridge.cpp)

Reproducible Access Violation during RDP resolution switches (seen 11-27 and 11-29): FreeRDP 3.32.2's gdi_init_primary assigns primary_buffer before its final allocations, and its fail_hwnd path frees the bitmap without clearing the cached pointer. After a failed gdi_resize the cached value dangles at freed data while gdi->width/height already hold the new size, so push_frame passed every null check and handed freed memory to CopyDirtyRectFromNative.

  • push_frame now publishes gdi->primary->bitmap->data — the bitmap owns the buffer and can only be freed together with gdi->primary — turning every broken state into a skipped frame instead of a crash.
  • cx_desktop_resize rebuilds the GDI in place when gdi_resize fails destructively (unbind_gfx_pipeline -> gdi_init at the settings size -> bind_gfx_pipeline, because gfx->custom still points at the broken struct) and only aborts the session via freerdp_abort_connect when the rebuild itself fails.
  • cx_begin_paint keeps a null-guarded primary->hdc->hwnd->invalid chain for the same broken state.
  • Documents the buffer-ownership rule in AGENTS.md so the pattern is not reintroduced.

2. Lock server clipboard data when downloading remote files (native/CxRdpBridge/cxrdp_bridge.cpp)

rdpclip answers FILECONTENTS requests with CB_RESPONSE_FAIL once both sides negotiated CB_CAN_LOCK_CLIPDATA, so a client must hold a lock and reference its clipDataId in every file-contents request — exactly what mstsc and FreeRDP's FUSE clients do. The bridge never sent CB_LOCK_CLIPDATA and skipped FILECONTENTS_SIZE when the descriptor already carried FD_FILESIZE, so every remote-to-local file download died on the first range request, silently (the FAIL path returned without logging).

  • Store the server capability flags (generalFlags) and lock the remote file list before pulling descriptors; unlock on completion, failure, clipboard change and teardown.
  • Reference the clipDataId in every FILECONTENTS request.
  • Always open each file stream with FILECONTENTS_SIZE first (MS-RDPECLIP 3.1.5.4.5 calls the size a prerequisite, and rdpclip associates the server-side handle with the streamId it first sees).
  • Log failed file-contents requests instead of swallowing them.

Verification

  • dotnet build CxShell.csproj: 0 warnings, 0 errors
  • dotnet test CxShell.Tests/CxShell.Tests.csproj: 793 passed, 0 failed
  • Live Windows RDP server: remote clipboard file downloads now complete (a bisect run without the clipDataId still failed, confirming it is the decisive part); local-to-remote paste upload still works
  • The GDI fix removes the Access Violation during resolution switches (the 11-27 / 11-29 repros)

zhangruochu and others added 4 commits November 30, 2026 17:34
Reaching into SSH.NET's private _channel field and invoking SendWindowChangeRequest by reflection broke whenever the library reshaped its internals, and reflection is unavailable under NativeAOT. ShellStream exposes ChangeWindowSize publicly, so call it directly.
RebuildKeyMap runs on every selection query and scanned the item type with up to eight GetProperty calls per row, so selection cost grew with the number of properties rather than the number of rows. Resolve the nested and direct key properties once per type and reuse them, keeping the existing key format of type:property:value:instance-hash.

Adds coverage for the nested, direct and no-key fallbacks.
Reproducible Access Violation during RDP resolution switches (11-27
and 11-29): FreeRDP 3.32.2 gdi_init_primary assigns primary_buffer
before its final allocations, and its fail_hwnd path frees the bitmap
without clearing the cached pointer. After a failed gdi_resize the
cached value dangles at freed data while gdi->width/height already
carry the new size, so push_frame passed every null check and
CopyDirtyRectFromNative read freed memory.

- push_frame now publishes gdi->primary->bitmap->data (the bitmap
  owns the buffer; it can only be freed together with gdi->primary)
- cx_desktop_resize rebuilds the GDI in place when gdi_resize fails
  destructively (unbind/rebind the EGFX pipeline around gdi_init,
  abort the session only when the rebuild fails)
- cx_begin_paint guards the primary->hdc->hwnd->invalid chain
rdpclip answers FILECONTENTS requests with CB_RESPONSE_FAIL once both
sides negotiated CB_CAN_LOCK_CLIPDATA, so a client must hold a lock and
reference its clipDataId in every file contents request - exactly what
mstsc and FreeRDP FUSE clients do. The bridge never sent
CB_LOCK_CLIPDATA and skipped FILECONTENTS_SIZE when the descriptor
already carried FD_FILESIZE, so every remote-to-local file download
died on the first range request, silently (the FAIL path returned
without logging).

- Store the server capability flags and lock the remote file list
  before pulling descriptors; unlock on completion, failure, clipboard
  change and teardown.
- Reference the clipDataId in every FILECONTENTS request.
- Always open each file stream with FILECONTENTS_SIZE first
  (MS-RDPECLIP 3.1.5.4.5 calls the size a prerequisite).
- Log failed file contents requests instead of swallowing them.

Verified against a live Windows RDP server: remote clipboard file
downloads now complete (a bisect run without clipDataId still failed,
confirming it is the decisive part); local-to-remote paste upload
still works.
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