Skip to content

build(win): share session code via rsession-shared.dll between launcher stubs - #18070

Merged
kevinushey merged 2 commits into
mainfrom
feature/win-rsession-shared-dll
Jun 26, 2026
Merged

kevinushey merged 2 commits into
mainfrom
feature/win-rsession-shared-dll

Conversation

@kevinushey

@kevinushey kevinushey commented Jun 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

On Windows we ship two R session executables, rsession.exe and rsession-utf8.exe. They are identical code with different embedded manifests (rsession-utf8.exe declares a UTF-8 activeCodePage for UCRT builds of R 4.2.0+, selected at runtime via R_RUNTIME=='ucrt'). Until now each executable statically linked its own full copy of the (large) session code, so we shipped two big binaries.

This builds the session code once into rsession.dll and turns the two executables into thin launcher stubs that load it. We now ship one copy of the heavy code plus two small stubs.

Why this is safe

The process active code page is fixed at startup from the launching executable's manifest and applies process-wide -- including to code running in a DLL it loads. So a per-manifest stub is all that needs to differ between the two; the shared DLL inherits the correct (legacy or UTF-8) code page from whichever stub launched it.

This was verified empirically on a Windows VM: a single test DLL reported GetACP() == 1252 when loaded by a stub with no UTF-8 manifest and 65001 when loaded by an otherwise-identical stub carrying the UTF-8 manifest.

What changed

  • SessionMain.cpp: the int main(...) body becomes RSESSION_MAIN_API int rsessionMain(...) (no logic changes).
  • SessionMain.hpp: adds the RSESSION_MAIN_API export/import macro and the rsessionMain() declaration.
  • SessionMainStub.cpp (new): thin int main() that forwards to rsessionMain(). Compiled into each stub on Windows, and into the single executable on other platforms.
  • CMakeLists.txt (Windows): the old rsession-objects OBJECT library becomes a SHARED library shipped as rsession.dll (holding SESSION_LIBRARIES + LIBR_LIBRARIES); rsession.exe / rsession-utf8.exe become stubs that link it and keep their existing .rc/manifest. The CMake target stays named rsession-shared and its import lib / .pdb are named rsession-shared.* so they do not collide with the rsession.exe stub's own .lib/.pdb. The DLL (and its PDB) are installed alongside. Non-Windows link configuration is unchanged (just fenced under NOT WIN32).

Notes

  • macOS/Linux are unaffected: still a single rsession exporting _rd_* via rsession.exports. On Windows the two rd_* debug helpers (rd_eval / rd_traceback, declared RS_EXPORT == __declspec(dllexport)) were previously exported from rsession.exe; they are now exported from rsession.dll instead. They remain exported and debugger-discoverable in the loaded module, so this is behavior-preserving; the only newly added export is rsessionMain (so the stub can call into the DLL).
  • rsession --run-tests still works (the stub forwards argv to rsessionMain in the DLL).
  • The runtime DLL ships as rsession.dll; its import lib and .pdb are kept under the rsession-shared name (via ARCHIVE_OUTPUT_NAME / PDB_NAME) so they don't collide with the rsession.exe stub's .lib/.pdb.
  • Executable names are intentionally unchanged. Longer term, when R < 4.2 (MSVCRT) support is dropped, the rsession-utf8 target and the R_RUNTIME=='ucrt' launcher branch can be removed and a single UTF-8 rsession.exe shipped.

Validation

Windows build/packaging is exercised by CI. Worth confirming there: rsession.dll is colocated with the stubs for dev/run-in-place launches, the installer picks up the new DLL, and the 32-bit SessionWin32 build (no UTF-8 variant) packages its DLL + stub.

Developer-only / build change; no user-facing behavior change, so no NEWS.md entry.

…er stubs

On Windows the rsession.exe and rsession-utf8.exe binaries were identical
code with different embedded manifests, each statically linking its own full
copy of the session code. Build the code once into rsession-shared.dll and
make the two executables thin launcher stubs that load it, so we ship a single
copy of the (large) session code instead of two.

The executables differ only in the manifest embedded via their .rc file; the
active code page is fixed at process startup from the launching exe's manifest
and applies process-wide, including to code in the DLL, so the per-manifest
stubs are sufficient. Non-Windows builds are unchanged apart from moving main()
into SessionMainStub.cpp.
@kevinushey
kevinushey requested a review from gtritchie June 25, 2026 21:57
@kevinushey
kevinushey marked this pull request as ready for review June 25, 2026 21:57
@gtritchie

Copy link
Copy Markdown
Member

Code review findings

Actionable items from an automated multi-agent review (code quality + comment accuracy). The build mechanism itself was verified sound: CMake wiring, the main -> rsessionMain rename, DLL discovery/colocation, the 32-bit path, and the active-code-page reasoning all check out. No critical issues.

Important

  1. src/cpp/session/include/session/SessionMain.hpp:26 — the comment says "applies process-wide, including to code running in rsession.dll", but the artifact is named rsession-shared.dll (CMakeLists.txt:614, OUTPUT_NAME "rsession-shared"); there is no rsession.dll (the rsession target is the .exe stub). This also contradicts line 20 of the same comment block, which correctly says rsession-shared.dll. Fix: change to rsession-shared.dll (or just "the DLL", matching the phrasing in SessionMainStub.cpp:25 and CMakeLists.txt:605).

Suggestions

  1. src/cpp/session/SessionMainStub.cpp:4 — copyright header reads Copyright (C) 2022 by Posit Software, PBC on a newly created file. Repo convention puts the creation year on new files (other 2025-2026 additions under src/cpp use the current year). Fix: use 2026.

  2. PR description / rationale only — the claim "nothing on Windows exports symbols today, so exporting only rsessionMain is behavior-preserving" is inaccurate: rd_eval/rd_traceback are declared RS_EXPORT (__declspec(dllexport) on Windows, see SessionDebugging.cpp:147,165 and core/Macros.hpp). They are exported today from rsession.exe and will instead be exported from rsession-shared.dll. This is not a functional break — they remain exported and discoverable by a debugger in the loaded DLL — but the rationale should say the two rd_* debug exports move from the exe to the DLL rather than asserting nothing is exported.

  3. (Optional) CMakeLists.txt:598-606, SessionMain.hpp:19-31, SessionMainStub.cpp:18-28 — the same explanatory paragraph is near-duplicated across three files; finding 1 is a direct symptom of one copy drifting out of sync. Each cross-references SessionMain.hpp, so consider trimming the CMake and stub comments to a one-line summary plus the cross-reference and letting SessionMain.hpp hold the canonical explanation.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Some minor comment tweaks to consider, and wrong copyright year on a new file.

@github-actions

github-actions Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

🍎 macOS Desktop E2E · ARM64

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
404 0 16 0
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · macos-26 ARM64 · commit 9acdea9e46e55d119ff1ef6c511e906f9f6d57ad · Run #196

@gtritchie gtritchie added this to the 2026.07 (Pacific Dogwood) milestone Jun 25, 2026
@github-actions

github-actions Bot commented Jun 26, 2026 •

Copy link
Copy Markdown

🪟 Windows Desktop E2E · Server 2025 x64

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
409 0 11 2
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · Windows Server 2025 x64 · commit 9acdea9e46e55d119ff1ef6c511e906f9f6d57ad · Run #196

@kevinushey
kevinushey merged commit cc8959d into main Jun 26, 2026
14 checks passed
@kevinushey
kevinushey deleted the feature/win-rsession-shared-dll branch June 26, 2026 02:05
gtritchie added a commit that referenced this pull request Aug 11, 2026
#18070 moved the bulk of the session code out of rsession.exe and
rsession-utf8.exe into a shared library, leaving the two executables as
thin launcher stubs. The Sign Executables stage was not updated, so the
signed stubs load an unsigned DLL holding essentially all of the session
implementation -- which WDAC, AppLocker publisher rules, and some EDR
configurations evaluate as a loaded module.

Add both build-tree copies of the DLL to %EXECUTABLES%: the x64 one next
to the stubs, and the x86 one that the multiarch build installs into
src/cpp/session/x86 alongside x86/rsession.exe. Signing already runs
before the Package stage, so the installer picks up the signed DLLs.

Addresses #18512
gtritchie added a commit that referenced this pull request Aug 11, 2026
The multiarch Windows Desktop build installs the 32-bit session into
<build>/src/cpp/session/x86, and the 64-bit build had an install rule
that collected that directory into resources/app/bin/x86 at package
time. That rule sat inside an "install winpty on windows" block and was
deleted with it in 04db3f5, so since 2026.06.0 the shipped installer
has resources/app/bin/x86 with only the x86 VC runtime DLLs -- no
rsession.exe, and since #18070 no rsession.dll. With a 32-bit R,
session-launcher.ts finds no x86/rsession.exe and falls back to the
64-bit session, which cannot load a 32-bit R.dll.

Restore the rule under a comment that names its actual source, and keep
the file(MAKE_DIRECTORY) call: install(DIRECTORY) errors on a missing
source directory, and non-multiarch builds never create it.

Addresses #18515
gtritchie added a commit that referenced this pull request Aug 14, 2026
Backport of #18516 (06473dc) to Yellow Yarrow. This branch carries the
rsession-shared library from #18070, so rsession.exe and rsession-utf8.exe are
thin launcher stubs and rsession.dll holds essentially all of the session
implementation -- but %EXECUTABLES% in the Sign Executables stage still named
only the .exe files, so the shipped DLL was unsigned.

Add both build-tree copies of the DLL: the x64 one next to the stubs, and the
x86 one that the multiarch build installs into src/cpp/session/x86 alongside
x86/rsession.exe. Signing runs before the Package stage, so the installer picks
up the signed DLLs.

The NEWS entry goes in version/news/os/NEWS-2026.08.1-yellow-yarrow.md, since
NEWS.md is not carried on this branch.

Addresses #18557
gtritchie added a commit that referenced this pull request Aug 14, 2026
Backport of #18519 (9e0e8e9) to Yellow Yarrow. The multiarch Windows
Desktop build installs the 32-bit session into <build>/src/cpp/session/x86,
and the 64-bit build had an install rule that collected that directory into
resources/app/bin/x86 at package time. That rule sat inside an "install winpty
on windows" block and was deleted with it in 04db3f5 (#17814), so the
shipped installer has resources/app/bin/x86 with only the x86 VC runtime DLLs
-- no rsession.exe, and since #18070 no rsession.dll. With a 32-bit R,
session-launcher.ts finds no x86/rsession.exe and falls back to the 64-bit
session, which cannot load a 32-bit R.dll.

Restore the rule under a comment that names its actual source, and keep the
file(MAKE_DIRECTORY) call: install(DIRECTORY) errors on a missing source
directory, and non-multiarch builds never create it.

This is what makes the x86 binaries signed by 999de99 (#18562) actually
reach the installer.

The NEWS entry goes in version/news/os/NEWS-2026.08.1-yellow-yarrow.md, since
NEWS.md is not carried on this branch.

Addresses #18558
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