Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Windows installer: emit the native path helper instead of compiling it (#10540)

Daniel Han95feb6979e79
diff --git a/.github/scripts/Watch-ForCompiler.ps1 b/.github/scripts/Watch-ForCompiler.ps1
new file mode 100644
index 00000000000..2a3ea7cfa10
--- /dev/null
+++ b/.github/scripts/Watch-ForCompiler.ps1
@@ -0,0 +1,269 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+<#
+.SYNOPSIS
+Run a script block and report whether a C# compiler ran, or a DLL landed in a
+temporary directory, while it did.
+
+.DESCRIPTION
+Two independent detectors, because either one alone can be defeated by something
+that is not the code under test:
+
+ 1. Security log event 4688, process creation. Authoritative about what ran, and
+ it sees a compiler spawned through any depth of child process, which is the
+ case a text search of our own scripts cannot reach. Needs auditing enabled
+ by the caller; the workflow does that and verifies it took.
+
+ 2. New *.dll, *.cmdline, *.rsp and *.?.cs files under every temporary directory
+ in play. This is the artefact half of the same shape, and it survives the
+ case where auditing is silently overridden by a machine policy. csc.exe
+ writes the source and the response file next to the assembly, so the
+ supporting names are watched too: an assembly written and cleaned up leaves
+ the job with nothing to show, and a .cmdline left behind still names it.
+
+Both are reported. A caller decides what to do with them; the positive control
+requires both to fire, and the real measurement requires neither to.
+
+TEMP is read from the environment rather than assumed, and the machine-wide
+C:\Windows\Temp is watched alongside it, because a compile launched from a
+service or an elevated child does not write where this process would.
+#>
+
+Set-StrictMode -Version Latest
+
+$script:CompilerNames = @('csc.exe', 'vbc.exe', 'cvtres.exe', 'jsc.exe')
+
+function Get-StudioTempRoots {
+ <#
+ .SYNOPSIS
+ Every directory a compile could write its intermediates to, de-duplicated.
+ #>
+ $roots = @($env:TEMP, $env:TMP, "$env:SystemRoot\Temp", "$env:LOCALAPPDATA\Temp")
+ $seen = New-Object 'System.Collections.Generic.HashSet[string]' ([StringComparer]::OrdinalIgnoreCase)
+ $result = @()
+ foreach ($root in $roots) {
+ if ([string]::IsNullOrWhiteSpace($root)) { continue }
+ if (-not (Test-Path -LiteralPath $root)) { continue }
+ $full = (Resolve-Path -LiteralPath $root).ProviderPath
+ if ($seen.Add($full)) { $result += $full }
+ }
+ return $result
+}
+
+function Get-StudioTempArtifacts {
+ <#
+ .SYNOPSIS
+ Compiler intermediates and assemblies currently sitting in the temp roots.
+ #>
+ $patterns = @('*.dll', '*.cmdline', '*.rsp', '*.cs', '*.err', '*.out')
+ $found = @()
+ foreach ($root in (Get-StudioTempRoots)) {
+ foreach ($pattern in $patterns) {
+ # Recurse: PowerShell compiles into a per-invocation subdirectory, not
+ # into the root, so a non-recursive listing sees none of this.
+ $found += Get-ChildItem -LiteralPath $root -Filter $pattern -File -Recurse `
+ -Force -ErrorAction SilentlyContinue |
+ Select-Object -ExpandProperty FullName
+ }
+ }
+ # Comma-wrapped: PowerShell unrolls an empty array to nothing on return, and the
+ # caller casts this into a HashSet whose two-argument constructor rejects null. On a
+ # clean runner with no matching temp files that killed the watcher before it ran even
+ # the positive control.
+ return ,[string[]]$found
+}
+
+function Get-StudioProcessImageName {
+ <#
+ .SYNOPSIS
+ The image a 4688 record says was created, from the record's own field.
+ .DESCRIPTION
+ NewProcessName, read out of the event XML by name rather than by position, since
+ the position is a property of the schema and this has to keep meaning the same
+ thing if the schema gains a field. Nothing else in the record is consulted: the
+ rendered message also contains the command line, so matching the message scores
+ `cmd.exe /c echo csc.exe` as a compiler, which is a false report of the one thing
+ this job exists to detect.
+ #>
+ param([Parameter(Mandatory = $true)]$Event)
+
+ try {
+ $xml = [xml]$Event.ToXml()
+ foreach ($field in $xml.Event.EventData.Data) {
+ if ($field.Name -eq 'NewProcessName') { return [string]$field.'#text' }
+ }
+ } catch { }
+ return ''
+}
+
+function Select-StudioCompilerHits {
+ <#
+ .SYNOPSIS
+ The records among $Events whose created image is a compiler.
+ .DESCRIPTION
+ Separate from the query so it can be exercised without a Security log, which is
+ the only way to test the classification anywhere but a Windows runner that has
+ just compiled something.
+ #>
+ param([Parameter(Mandatory = $true)][AllowEmptyCollection()][array]$Events)
+
+ $hits = @()
+ foreach ($record in $Events) {
+ $image = Get-StudioProcessImageName -Event $record
+ if ([string]::IsNullOrEmpty($image)) { continue }
+ # Split explicitly rather than through [System.IO.Path]::GetFileName, which splits on
+ # the separators of the HOST it runs on: under a Linux pwsh, which is where the
+ # classification is tested, a backslash is an ordinary character and the whole path
+ # came back as the leaf. The records are always Windows paths whatever reads them.
+ $leaf = ($image -split '[\\/]')[-1]
+ foreach ($name in $script:CompilerNames) {
+ if ($leaf -eq $name) {
+ $rendered = ''
+ try { $rendered = [string]$record.Message } catch { }
+ $hits += ("{0:o} {1} :: {2}" -f $record.TimeCreated, $image,
+ ($rendered -replace '\s+', ' '))
+ break
+ }
+ }
+ }
+ return ,[string[]]$hits
+}
+
+function Get-StudioCompilerEvents {
+ <#
+ .SYNOPSIS
+ 4688 records naming a compiler image, created at or after $Since.
+ .PARAMETER Since
+ The instant the measured action began. Taken before the action rather than
+ filtering afterwards by a fixed window, so a slow installer cannot outrun it.
+ .PARAMETER Until
+ The instant it ended. Both ends are needed: a runner is a shared machine, and an
+ unrelated service starting a compiler after the action would otherwise be scored
+ against it.
+
+ This window is the honest bound rather than the process tree the workflow prose
+ describes. 4688 carries the creator's pid, but a compile can be several processes
+ deep and the intermediate pids have exited by the time this reads the log, so an
+ ancestry walk is not reconstructable after the fact. The window is what can be
+ measured; the positive control is what proves it measures anything.
+ #>
+ param(
+ [Parameter(Mandatory = $true)][datetime]$Since,
+ [Parameter(Mandatory = $true)][datetime]$Until
+ )
+
+ $events = @()
+ try {
+ # Bounded at both ends. Open-ended, a compiler started by something else while the
+ # recursive temp scan was still running counted against the action that had
+ # already finished.
+ $events = Get-WinEvent -FilterHashtable @{
+ LogName = 'Security'
+ Id = 4688
+ StartTime = $Since
+ EndTime = $Until
+ } -ErrorAction Stop
+ } catch [System.Exception] {
+ # No matching events is an exception from Get-WinEvent, not an empty set, and on a
+ # clean run that is the expected result. A log this cannot READ throws the same way,
+ # and swallowing both made an unreadable Security log indistinguishable from a clean
+ # measurement: the job would print "no compiler" having seen nothing at all. The
+ # positive control does not cover this, because it runs in an earlier step and says
+ # nothing about whether the log was still readable during the two measurements.
+ #
+ # So the two are separated structurally rather than by matching the message text,
+ # which is localised: ask the log for any one record at all. If that succeeds the log
+ # is readable and the filter genuinely matched nothing; if it fails too, this
+ # measurement is void and the caller must not report it as clean.
+ # Bound before the probe below, whose own catch rebinds $_.
+ $reason = $_.Exception.Message
+ $readable = $false
+ try {
+ $null = Get-WinEvent -LogName 'Security' -MaxEvents 1 -ErrorAction Stop
+ $readable = $true
+ } catch { }
+ if (-not $readable) {
+ throw ("the Security log could not be read, so this run measured nothing. " +
+ "Treat it as void rather than as clean. Underlying error: $reason")
+ }
+ return ,[string[]]@()
+ }
+
+ return Select-StudioCompilerHits -Events @($events)
+}
+
+function Invoke-WithCompilerWatch {
+ <#
+ .SYNOPSIS
+ Run $Action and return what the two detectors saw while it ran.
+ .OUTPUTS
+ A hashtable with Compilers and TempLibraries, each an array of strings, plus
+ the exit state of the action. Evidence is written under $EvidenceRoot whether
+ the action succeeded or not, since a failed installer is exactly when the
+ trace is wanted.
+ #>
+ param(
+ [Parameter(Mandatory = $true)][string]$Name,
+ [Parameter(Mandatory = $true)][scriptblock]$Action,
+ [Parameter(Mandatory = $true)][string]$EvidenceRoot
+ )
+
+ New-Item -ItemType Directory -Force -Path $EvidenceRoot | Out-Null
+
+ # Baseline first, THEN open the window. The sweep walks every temp root
+ # recursively and can take seconds on a runner with a large temp tree, and a
+ # csc.exe the machine started during that walk predates the action: counting it
+ # fails the positive control or the installer measurement for something neither
+ # one did. Same reasoning as the $until below, which is taken before the second
+ # sweep for the same reason.
+ $before = New-Object 'System.Collections.Generic.HashSet[string]' (
+ [string[]](Get-StudioTempArtifacts), [StringComparer]::OrdinalIgnoreCase)
+ # A second back, so a process created in the same tick as the timestamp is not
+ # filtered out by a strictly-later comparison inside Get-WinEvent. Stated exactly,
+ # since the sweep above runs before this: the window still reaches one second into
+ # the tail of that sweep, so a compiler the machine started in that last second is
+ # counted. The trade is deliberate and it errs towards a false alarm, which fails a
+ # measurement loudly, rather than towards dropping a real compile.
+ $since = (Get-Date).AddSeconds(-1)
+
+ $failure = $null
+ try {
+ # Piped to Out-Host, not left on the success stream. The installer action tees its
+ # log, and every one of those lines would otherwise be emitted as function output
+ # ahead of the result hashtable, so the caller's $seen became an object array and
+ # $seen.Compilers failed under Set-StrictMode against the string elements rather
+ # than reporting the measurement.
+ & $Action | Out-Host
+ } catch {
+ # Recorded and re-thrown by the caller if it cares. The detectors still
+ # report, because "the installer died AND spawned a compiler" is a more
+ # useful message than either half alone.
+ $failure = $_
+ }
+
+ # Closed before the temp sweep, not after it: the sweep walks every temp root
+ # recursively and can take seconds, and anything the machine starts during that walk
+ # belongs to nobody's measurement.
+ $until = Get-Date
+ $compilers = @(Get-StudioCompilerEvents -Since $since -Until $until)
+
+ $after = Get-StudioTempArtifacts
+ $newArtifacts = @($after | Where-Object { -not $before.Contains($_) })
+ $newLibraries = @($newArtifacts | Where-Object { $_ -match '\.(dll|cmdline|rsp)$' })
+
+ $stem = Join-Path $EvidenceRoot $Name
+ $compilers | Out-File -FilePath "$stem-compilers.txt" -Encoding utf8
+ $newArtifacts | Out-File -FilePath "$stem-temp-artifacts.txt" -Encoding utf8
+ if ($failure) {
+ $failure | Out-String | Out-File -FilePath "$stem-error.txt" -Encoding utf8
+ }
+
+ if ($failure) { throw $failure }
+
+ return @{
+ Compilers = $compilers
+ TempLibraries = $newLibraries
+ TempArtifacts = $newArtifacts
+ }
+}
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.yml
index f43c3ec26f9..8f844bb8691 100644
--- a/.github/workflows/cross-platform-parity-ci.yml
+++ b/.github/workflows/cross-platform-parity-ci.yml
@@ -65,6 +65,10 @@ on:
- 'studio/install_*.py'
- 'studio/backend/requirements/single-env/patch_metadata.py'
- 'tests/python/test_cross_platform_parity.py'
+ # This is the only lane that executes it, and the Windows smoke workflow it moved
+ # from deliberately skips it, so without this line a PR editing only the test
+ # would never run it.
+ - 'tests/studio/test_native_path_resolver_degrades.ps1'
- 'tests/python/test_windows_python_venv_hardening.py'
- 'tests/python/test_windows_setup_output_encoding.py'
- 'tests/python/test_pwsh_runner_encoding.py'
@@ -132,6 +136,10 @@ on:
- 'studio/install_*.py'
- 'studio/backend/requirements/single-env/patch_metadata.py'
- 'tests/python/test_cross_platform_parity.py'
+ # This is the only lane that executes it, and the Windows smoke workflow it moved
+ # from deliberately skips it, so without this line a PR editing only the test
+ # would never run it.
+ - 'tests/studio/test_native_path_resolver_degrades.ps1'
- 'tests/python/test_windows_python_venv_hardening.py'
- 'tests/python/test_windows_setup_output_encoding.py'
- 'tests/python/test_pwsh_runner_encoding.py'
@@ -276,6 +284,14 @@ jobs:
- name: Application Control CLI fallback tests
shell: pwsh
run: pwsh -NoProfile -File tests/studio/test_application_control_cli_fallback.ps1
+ # Linux on purpose, and the only lane that can run it. Reflection emit succeeds on
+ # this engine so the types build exactly as they do on 5.1, while kernel32 and
+ # shell32 do not resolve, so every call through them fails the way it does on a
+ # Windows host whose antivirus took the helper away. That is the build side and the
+ # call side in one run, and it is not reproducible on a healthy Windows runner.
+ - name: Native path resolver degradation tests
+ if: runner.os == 'Linux'
+ run: pwsh -NoProfile -File tests/studio/test_native_path_resolver_degrades.ps1
- name: POSIX rollback lifecycle tests
if: runner.os == 'Linux'
run: sh tests/sh/test_install_rollback_lifecycle.sh
diff --git a/.github/workflows/studio-windows-inference-smoke.yml b/.github/workflows/studio-windows-inference-smoke.yml
index 8b8d66d188c..16e86b23388 100644
--- a/.github/workflows/studio-windows-inference-smoke.yml
+++ b/.github/workflows/studio-windows-inference-smoke.yml
@@ -135,6 +135,12 @@ jobs:
if ($LASTEXITCODE) { exit $LASTEXITCODE }
pwsh -NoProfile -File tests/studio/test_path_probe_access_denied.ps1
if ($LASTEXITCODE) { exit $LASTEXITCODE }
+ # test_native_path_resolver_degrades.ps1 is deliberately NOT here. Its whole
+ # premise is a host where the emit succeeds and kernel32 does not resolve, so
+ # it asserts that the native rung answers null and the ladder reports the
+ # result inexact. On a healthy Windows runner those calls SUCCEED and every one
+ # of those assertions is false. It runs on the Linux leg of
+ # cross-platform-parity-ci.yml, which is the host it describes.
# uninstall.ps1: native uninstall must keep the shared unsloth.ico while a
# WSL shortcut still references it (dual install), else that shortcut blanks.
diff --git a/.github/workflows/windows-no-compiler-ci.yml b/.github/workflows/windows-no-compiler-ci.yml
new file mode 100644
index 00000000000..1354a01cc6f
--- /dev/null
+++ b/.github/workflows/windows-no-compiler-ci.yml
@@ -0,0 +1,267 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+# The behavioural half of "the installer does not run a compiler".
+#
+# tests/studio/test_installer_av_shapes.py reads the scripts and fails on an
+# Add-Type that is not -AssemblyName. That is a text check, and text checks are
+# blind to the two ways this regresses in practice: a compile reached through a
+# module, a dot-sourced file or a generated here-string, and a compile that some
+# dependency performs on our behalf while our own process tree is what a scanner
+# scores. Bitdefender flagged the chain, not the bytes:
+#
+# unsloth-studio.exe -> powershell.exe -> csc.exe -> %TEMP%\<random>.dll
+#
+# so the assertion that matters is about processes, not source. This job turns on
+# 4688 process-creation auditing with command lines, runs the real installer, and
+# fails if csc.exe, vbc.exe or cvtres.exe ever appears, or if a DLL lands in a
+# temporary directory while the installer is running.
+#
+# The positive control is not optional and runs FIRST. A detector that sees
+# nothing is indistinguishable from a clean run, and every part of this is
+# something that can silently fail to apply: auditpol can be overridden by a
+# machine policy, the Security log can be full, command-line capture can be off.
+# So the job deliberately compiles one throwaway type with Add-Type and REQUIRES
+# both signals to fire. If they do not, the job fails as a broken detector and
+# never reaches the real measurement.
+#
+# Scope, stated because a green run here does not mean more than it means: this
+# proves nothing about a machine where App Control or Smart App Control is
+# enforcing, since a hosted runner has neither. It proves that the shipped
+# install path spawns no compiler and drops no temporary DLL, which is the shape
+# that was reported.
+#
+# Which revision of studio/setup.ps1 each leg measures is also part of that scope.
+# The installer delegates setup to the managed CLI, so a default run executes the
+# setup.ps1 inside the released package, not the one in the checkout. The shell leg
+# is therefore a measurement of the release; the desktop leg passes --local, which
+# overlays the checkout as an editable install, and refuses to report a result
+# unless the overlay is in the log.
+
+name: Windows No Compiler CI
+
+on:
+ pull_request:
+ paths:
+ - 'install.ps1'
+ - 'studio/setup.ps1'
+ - 'scripts/uninstall.ps1'
+ - 'studio/src-tauri/src/install.rs'
+ - 'studio/src-tauri/src/process.rs'
+ - 'tests/studio/test_installer_av_shapes.py'
+ # The watcher implements both detectors. A PR touching only it could disable the
+ # detection without ever running the positive control that would catch that.
+ - '.github/scripts/Watch-ForCompiler.ps1'
+ - '.github/workflows/windows-no-compiler-ci.yml'
+ push:
+ branches: [main]
+ workflow_dispatch:
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.ref == 'refs/heads/main' && github.sha || '' }}
+ cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
+
+permissions:
+ contents: read
+
+jobs:
+ no-compiler-under-the-installer:
+ name: the installer spawns no C# compiler
+ runs-on: windows-latest
+ timeout-minutes: 60
+
+ env:
+ # Outside any user profile, for the same reason the Application Control leg
+ # pins it: the paths under test must not depend on whose profile ran.
+ UNSLOTH_STUDIO_HOME: 'C:\Users\Public\no-compiler-home'
+ UNSLOTH_EVIDENCE: 'C:\Users\Public\no-compiler-evidence'
+
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+
+ - name: Enable process creation auditing with command lines
+ shell: powershell
+ run: |
+ $ErrorActionPreference = 'Stop'
+ New-Item -ItemType Directory -Force -Path $env:UNSLOTH_EVIDENCE | Out-Null
+
+ # 4688 with the command line, which is what distinguishes "csc.exe ran"
+ # from "csc.exe ran for us". Both are needed: the subcategory produces
+ # the event, the policy value fills in the command line.
+ auditpol /set /subcategory:"Process Creation" /success:enable | Out-Null
+ $auditKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit'
+ New-Item -Path $auditKey -Force | Out-Null
+ New-ItemProperty -Path $auditKey -Name 'ProcessCreationIncludeCmdLine_Enabled' `
+ -Value 1 -PropertyType DWord -Force | Out-Null
+
+ # A Security log large enough that the installer's own noise cannot roll
+ # the window under test off the end.
+ wevtutil sl Security /ms:268435456
+
+ $state = auditpol /get /subcategory:"Process Creation"
+ $state | Tee-Object -FilePath "$env:UNSLOTH_EVIDENCE\auditpol.txt"
+ # Joined to one string, and matched on the subcategory line rather than the bare
+ # word. auditpol returns an ARRAY of lines, and -notmatch on an array is a filter,
+ # not a test: it returns the elements that do not match, so on a runner where
+ # auditing turned on correctly it still returned the three header lines, and a
+ # non-empty array is truthy. This threw on every healthy machine.
+ if (($state | Out-String) -notmatch 'Process Creation\s+Success') {
+ throw "process creation auditing did not turn on; this job cannot measure anything"
+ }
+
+ - name: Positive control -- the detector must see a real compile
+ shell: powershell
+ run: |
+ $ErrorActionPreference = 'Stop'
+ . "$env:GITHUB_WORKSPACE\.github\scripts\Watch-ForCompiler.ps1"
+
+ $probe = {
+ # One throwaway type, compiled the way the installer used to. This is
+ # the only Add-Type in this repository's CI and it exists to prove the
+ # measurement works.
+ Add-Type -TypeDefinition 'public static class UnslothCompilerProbe { public static int N() { return 1; } }'
+ [UnslothCompilerProbe]::N() | Out-Null
+ }
+ $seen = Invoke-WithCompilerWatch -Name 'positive-control' -Action $probe `
+ -EvidenceRoot $env:UNSLOTH_EVIDENCE
+
+ if (-not $seen.Compilers) {
+ throw ("the positive control compiled a type and no compiler process was recorded. " +
+ "The detector is broken, so a clean result later would mean nothing.")
+ }
+ if (-not $seen.TempLibraries) {
+ throw ("the positive control compiled a type and no temporary DLL was recorded. " +
+ "The temporary-directory half of the detector is broken.")
+ }
+ Write-Host ("detector verified: {0} compiler process(es), {1} temporary DLL(s)" -f `
+ $seen.Compilers.Count, $seen.TempLibraries.Count)
+
+ - name: Require the installer to run without a compiler
+ shell: powershell
+ run: |
+ $ErrorActionPreference = 'Stop'
+ . "$env:GITHUB_WORKSPACE\.github\scripts\Watch-ForCompiler.ps1"
+
+ $install = {
+ # Spawned the way the desktop spawns it, not dot-sourced: the shape
+ # under test is a child powershell.exe, and a compile started by the
+ # installer's own children is exactly what a dot-source would hide
+ # from the 4688 ancestry. --no-torch for time; the native
+ # declarations are made before any of that. Not --tauri, which
+ # refuses UNSLOTH_STUDIO_HOME, and the pinned home is what keeps
+ # every step reading one tree.
+ New-Item -ItemType Directory -Force -Path logs | Out-Null
+ & powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
+ -File "$env:GITHUB_WORKSPACE\install.ps1" --no-torch *>&1 |
+ Tee-Object -FilePath "$env:UNSLOTH_EVIDENCE\install.log"
+ $exit = $LASTEXITCODE
+ $global:LASTEXITCODE = 0
+ if ($exit -ne 0) { throw "install.ps1 exited $exit" }
+ }
+ $seen = Invoke-WithCompilerWatch -Name 'installer' -Action $install `
+ -EvidenceRoot $env:UNSLOTH_EVIDENCE
+
+ if ($seen.Compilers) {
+ $seen.Compilers | ForEach-Object { Write-Host "COMPILER: $_" }
+ throw ("the installer spawned {0} compiler process(es). This is the chain that was " +
+ "blocked in the field: a windowless PowerShell launching csc.exe and dropping " +
+ "a DLL in TEMP. Declare native methods with New-StudioEmittedNativeType." -f `
+ $seen.Compilers.Count)
+ }
+ if ($seen.TempLibraries) {
+ $seen.TempLibraries | ForEach-Object { Write-Host "TEMP DLL: $_" }
+ throw ("the installer left {0} DLL(s) in a temporary directory. Even without a " +
+ "compiler process this is the artefact half of the same shape." -f `
+ $seen.TempLibraries.Count)
+ }
+ Write-Host "no compiler, no temporary DLL"
+
+ - name: Require the desktop mode to run without a compiler
+ shell: powershell
+ run: |
+ $ErrorActionPreference = 'Stop'
+ . "$env:GITHUB_WORKSPACE\.github\scripts\Watch-ForCompiler.ps1"
+
+ # --tauri is what the launcher actually passes: install.rs initialises its
+ # argument list with it unconditionally. The step above runs the shell mode
+ # because that is the one that honours a pinned UNSLOTH_STUDIO_HOME, and the
+ # desktop mode refuses it; running only that left a compile reached through a
+ # Tauri-only branch invisible to a job whose whole claim is the desktop shape.
+ # So both modes run, and the home is unpinned here rather than the mode changed.
+ #
+ # --local, which is the only thing that makes this leg measure the setup.ps1 in
+ # THIS checkout. Without it the installer installs the released unsloth and
+ # delegates setup to the copy inside that release (install.ps1 hands the work to
+ # the managed CLI), so a PR touching only studio/setup.ps1 triggered this workflow
+ # and then measured a different revision of the file it changed. --local overlays
+ # the checkout as an editable install, so the delegated `studio setup` runs the
+ # bytes under review. The gate below is what proves the overlay took; a run that
+ # fell back to the release is void here, not clean.
+ $desktop = {
+ Remove-Item Env:\UNSLOTH_STUDIO_HOME -ErrorAction SilentlyContinue
+ & powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass `
+ -File "$env:GITHUB_WORKSPACE\install.ps1" --tauri --local --no-torch *>&1 |
+ Tee-Object -FilePath "$env:UNSLOTH_EVIDENCE\install-tauri.log"
+ $exit = $LASTEXITCODE
+ $global:LASTEXITCODE = 0
+ # Not gated on exit 0. A desktop-mode install can decline for reasons that
+ # have nothing to do with this measurement, and the question here is only
+ # whether a compiler ran on the way.
+ Write-Host "install.ps1 --tauri exited $exit"
+ # It IS gated on having got as far as the work. A run that stopped at
+ # argument validation spawns nothing at all, so both detectors come back
+ # empty and this step prints "no compiler" having measured nothing. The
+ # STEP line is emitted after the install lock is taken, which is where the
+ # native declarations happen, so seeing it means the branch under test ran.
+ $log = ""
+ if (Test-Path "$env:UNSLOTH_EVIDENCE\install-tauri.log") {
+ $log = Get-Content -Raw "$env:UNSLOTH_EVIDENCE\install-tauri.log"
+ }
+ if ($log -notmatch 'Checking system dependencies') {
+ throw ("the desktop-mode install stopped before it reached any install " +
+ "work (exit $exit), so this measurement is void rather than clean.")
+ }
+ # And that the checkout is what setup ran from. This line is printed by the
+ # installer's --local overlay step; without it the delegated setup came out of
+ # the released package and this leg measured someone else's file.
+ if ($log -notmatch 'overlaying local repo') {
+ throw ("the local overlay never ran (exit $exit), so the setup.ps1 that " +
+ "executed was not the one in this checkout. Void, not clean.")
+ }
+ # Which revision, recorded rather than asserted, so the artifact says what was
+ # measured instead of leaving it to be inferred from the commit.
+ $setup = Join-Path $env:GITHUB_WORKSPACE 'studio\setup.ps1'
+ "path=$setup" | Out-File "$env:UNSLOTH_EVIDENCE\setup-measured.txt" -Encoding utf8
+ "sha256=$((Get-FileHash -LiteralPath $setup -Algorithm SHA256).Hash)" |
+ Out-File "$env:UNSLOTH_EVIDENCE\setup-measured.txt" -Encoding utf8 -Append
+ }
+ $seen = Invoke-WithCompilerWatch -Name 'installer-tauri' -Action $desktop `
+ -EvidenceRoot $env:UNSLOTH_EVIDENCE
+
+ if ($seen.Compilers) {
+ $seen.Compilers | ForEach-Object { Write-Host "COMPILER: $_" }
+ throw "the desktop-mode installer spawned $($seen.Compilers.Count) compiler process(es)."
+ }
+ if ($seen.TempLibraries) {
+ $seen.TempLibraries | ForEach-Object { Write-Host "TEMP DLL: $_" }
+ throw "the desktop-mode installer left $($seen.TempLibraries.Count) DLL(s) in a temporary directory."
+ }
+ Write-Host "desktop mode: no compiler, no temporary DLL"
+
+ - name: Upload evidence
+ if: always()
+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
+ with:
+ name: windows-no-compiler
+ path: 'C:\Users\Public\no-compiler-evidence'
+ if-no-files-found: warn
+
+ - name: Restore auditing
+ if: always()
+ shell: powershell
+ run: |
+ auditpol /set /subcategory:"Process Creation" /success:disable | Out-Null
+ $auditKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit'
+ Remove-ItemProperty -Path $auditKey -Name 'ProcessCreationIncludeCmdLine_Enabled' `
+ -ErrorAction SilentlyContinue
+ exit 0
diff --git a/.github/workflows/workflow-trigger-lint.yml b/.github/workflows/workflow-trigger-lint.yml
index 2d687f2fe11..97251cc7942 100644
--- a/.github/workflows/workflow-trigger-lint.yml
+++ b/.github/workflows/workflow-trigger-lint.yml
@@ -147,6 +147,7 @@ jobs:
tests/studio/test_indicator_browsers_run_in_parallel.py \
tests/studio/test_inference_smoke_http_diagnostics.py \
tests/studio/test_install_phase_timing.py \
+ tests/studio/test_installer_av_shapes.py \
tests/studio/test_mac_bundled_job_phases.py \
tests/studio/test_mac_host_offload_optin.py \
tests/studio/test_settings_smoke_covers_every_tab.py \
diff --git a/install.ps1 b/install.ps1
index 63dd9481921..75b008c1fa5 100644
--- a/install.ps1
+++ b/install.ps1
@@ -346,9 +346,9 @@ function Install-UnslothStudio {
# Windows picks the temp directory from TMP, then TEMP, then the profile, and
# never checks it exists or is writable. The desktop app passes on whatever it
# inherited (studio/src-tauri/src/install.rs sets neither); one report had it at
- # C:\Windows\TEMP, where the source Add-Type had just written was gone by the
- # time csc.exe opened it (issue #9140). The Python, uv and VC++ downloads stage
- # through it too. Probe it once and, if it cannot hold a file, point BOTH
+ # C:\Windows\TEMP, unwritable, which used to break the install outright because
+ # the native helper was compiled through it (issue #9140). Nothing compiles now,
+ # but the Python, uv and VC++ downloads still stage through it. Probe it once and, if it cannot hold a file, point BOTH
# variables at a directory we own: every child process and every
# [System.IO.Path]::GetTempPath() call reads the process environment block.
function Test-StudioDirectoryUsable {
@@ -830,14 +830,319 @@ function Install-UnslothStudio {
try { $defaultProfile = [Environment]::GetFolderPath("UserProfile") } catch {}
$tauriProfile = if ($defaultProfile) { $defaultProfile } else { $env:USERPROFILE }
- # GetFinalPathNameByHandleW is the only exact answer: it follows junctions,
- # symlinks and SUBST drives, expands 8.3 aliases and reports the on-disk
- # spelling, none of which GetFullPath does. It costs a C# compile, and 5.1 (the
- # interpreter the desktop app spawns) compiles by writing the source to %TEMP%
- # and running csc.exe. When that directory is unusable, or a scanner eats the
- # source, Add-Type throws CS2001, which used to abort a first launch as "Could
- # not create the Unsloth install lock" (issue #9140). Try once, retry with a
- # %TEMP% we own, cache the answer (callers resolve dozens of paths), then let
+ # Every native declaration in this script goes through here, and none of them
+ # goes through Add-Type.
+ #
+ # Add-Type on Windows PowerShell 5.1 (the interpreter the desktop app spawns)
+ # has no in-process compiler: it writes C# to %TEMP% and runs csc.exe to get a
+ # DLL back. That is true of -TypeDefinition and of -MemberDefinition alike.
+ # Bitdefender blocks the DLL that produces, and the verdict lands on the
+ # generated file rather than on anything shipped: a windowless PowerShell,
+ # spawned by a GUI binary, launching a compiler and writing executable content
+ # to %TEMP% is a dropper's shape, whatever the code says. It also used to fail
+ # outright with CS2001 when %TEMP% was unusable (issue #9140).
+ #
+ # Reflection emit builds the same interop stubs in memory. No compiler
+ # process, no source on disk, no DLL, nothing in %TEMP%, and the resulting
+ # assembly reports an empty Location. Available on .NET Framework 4 and on
+ # .NET 5+, so 5.1 and 7 take the same path.
+ #
+ # Throws on failure rather than reporting it: each caller wants a different
+ # answer to "the native side is unavailable", and the two that are cosmetic
+ # must not print the resolver's warning.
+ #
+ # Test-StudioCanDefineNativeTypes is the gate every caller checks first, and it
+ # is a gate rather than a try/catch because one of the two conditions is not
+ # catchable. App Control's Dynamic Code Security (policy option 19) always
+ # blocks loading unsigned assemblies built with System.Reflection.Emit, and
+ # Microsoft documents the parent process as usually stopped or crashing rather
+ # than raising, so nothing below could recover from it:
+ # learn.microsoft.com/en-us/windows/security/application-security/application-control/app-control-for-business/design/appcontrol-and-dotnet
+ # A machine enforcing user-mode code integrity puts PowerShell in Constrained
+ # Language, which is the first check and the one that fires in practice; the
+ # Device Guard probe is the belt to that pair of braces, for a policy that
+ # somehow left the language mode alone. When one IS active, the answer comes from
+ # a child process that tries the emit, not from a guess about which options the
+ # policy set.
+ $script:StudioCanDefineNativeTypes = $null
+ # Why the last probe answered as it did, so a caller can tell "the child ran and
+ # said no" from "the child never got to answer". Those are the same boolean and
+ # they are not the same fact: one is a policy, the other is a process that failed
+ # to start, was killed at the deadline, or lost its output.
+ $script:StudioEmitProbeOutcome = $null
+ function Test-StudioCanDefineNativeTypes {
+ if ($null -ne $script:StudioCanDefineNativeTypes) { return $script:StudioCanDefineNativeTypes }
+ $languageMode = "FullLanguage"
+ try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}
+ if ($languageMode -ne "FullLanguage") {
+ $script:StudioCanDefineNativeTypes = $false
+ return $false
+ }
+ # Three outcomes, not two. A status that was READ and says 0 is the only one that
+ # skips the probe: a query that threw, returned nothing, or returned an object
+ # without the property is UNKNOWN, and unknown used to be treated as unrestricted.
+ # That is the wrong direction here. The case it lets through is option 19 enforced
+ # on a host whose CIM query happens to fail, and the cost of being wrong there is a
+ # stopped installer, while the cost of probing unnecessarily is one short-lived
+ # process.
+ $known = $false
+ $active = $false
+ try {
+ # Bounded, with what that bound actually covers stated rather than implied:
+ # -OperationTimeoutSec limits the CIM operation on a responsive target. It does
+ # not interrupt DCOM connection setup and it does not override a provider that
+ # has wedged, where the server's own timeout wins. It is worth having for the
+ # slow case and it is not a hang guard. The installer already depends on CIM
+ # for adapter and process queries, so this query does not introduce that
+ # exposure; the child probe below is the part that carries a real deadline.
+ $guard = Get-CimInstance -Namespace "root\Microsoft\Windows\DeviceGuard" `
+ -ClassName "Win32_DeviceGuard" -OperationTimeoutSec 10 -ErrorAction Stop
+ # 0 off, 1 audit, 2 enforced. A null property is not a zero.
+ if ($guard -and $null -ne $guard.UsermodeCodeIntegrityPolicyEnforcementStatus) {
+ $known = $true
+ if ([int]$guard.UsermodeCodeIntegrityPolicyEnforcementStatus -ne 0) {
+ $active = $true
+ }
+ }
+ } catch {}
+ if ($known -and -not $active) {
+ $script:StudioCanDefineNativeTypes = $true
+ return $true
+ }
+ # A policy is active, or its state could not be read, and WHICH policy decides
+ # this. Option 19 Dynamic Code
+ # Security always blocks unsigned System.Reflection.Emit assemblies and has no
+ # audit mode on Windows 10 or Windows 11 before 24H2, where it is enforced even
+ # in an audit policy; an audit policy WITHOUT that option emits perfectly well.
+ # Win32_DeviceGuard does not report the option bit, so either guess costs a
+ # population: refusing everything sends every audit-mode machine down the lexical
+ # path, and allowing everything risks the process. Ask the machine instead.
+ $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess
+ # One retry, and only when the first attempt never reached an answer. The compiled
+ # version this replaces also tried twice before caching a negative, and without
+ # that a single transient process failure is indistinguishable from a policy: it
+ # is cached for the whole run and sends the installer down the lexical path, where
+ # two unequal roots compare as unknown and a second lock gets taken. A child that
+ # RAN and said no is not retried, so a genuinely blocked machine still pays for
+ # one probe.
+ if (-not $script:StudioCanDefineNativeTypes -and
+ $script:StudioEmitProbeOutcome -eq "indeterminate") {
+ $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess
+ }
+ return $script:StudioCanDefineNativeTypes
+ }
+
+ # The same emit, in a process that is allowed to die. Microsoft documents a blocked
+ # dynamic load as usually stopping or crashing the parent, which is precisely why
+ # this is not attempted in-process: a child that vanishes is an answer, and the same
+ # event here would be the installer vanishing. Silence is refusal, so a probe that
+ # cannot be spawned at all lands on the lexical path rather than on optimism.
+ function Test-StudioEmitInChildProcess {
+ # HostPath is for the tests, which have no policy to trigger the real path and cannot
+ # shadow $PSHOME, since it is read-only. Production never passes it.
+ param([string]$HostPath)
+ # Until something here establishes otherwise. Every exit below either leaves this
+ # alone or says what it learned.
+ $script:StudioEmitProbeOutcome = "indeterminate"
+ $probe = @'
+try {
+ $name = New-Object System.Reflection.AssemblyName 'UnslothStudioEmitProbe'
+ $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run
+ $assembly = $null
+ try { $assembly = [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly($name, $access) }
+ catch { $assembly = [AppDomain]::CurrentDomain.DefineDynamicAssembly($name, $access) }
+ $module = $assembly.DefineDynamicModule('UnslothStudioEmitProbe')
+ $builder = $module.DefineType('UnslothStudioEmitProbe', 'Public, Class, AutoClass, AnsiClass, BeforeFieldInit')
+ $method = $builder.DefinePInvokeMethod('CloseHandle', 'kernel32.dll', 'CloseHandle',
+ 'Public, Static, HideBySig, PinvokeImpl',
+ [System.Reflection.CallingConventions]::Standard, [bool], @([IntPtr]),
+ [System.Runtime.InteropServices.CallingConvention]::Winapi,
+ [System.Runtime.InteropServices.CharSet]::Ansi)
+ $method.SetImplementationFlags(
+ $method.GetMethodImplementationFlags() -bor [System.Reflection.MethodImplAttributes]::PreserveSig)
+ $null = $builder.CreateType()
+} catch {}
+# Outside the try, because CreateType can publish the type and then throw on the way
+# back, and a published type works. The parent recovers from exactly that; a check
+# inside the try answered no for a machine that had just succeeded.
+# One line, and no closing brace in column 0: this body sits inside a here-string that
+# starts at column 0 in both entrypoints, and the tests extract a function by finding the
+# first line that is exactly its closing brace. A block here ends the extraction early.
+if ('UnslothStudioEmitProbe' -as [type]) { Write-Output ('STUDIO_EMIT_OK ' + [string]$ExecutionContext.SessionState.LanguageMode); exit 0 }
+exit 1
+'@
+ # This host, not a guessed one: a 5.1 answer does not carry to pwsh or the other
+ # way, and the emit that matters is the one this interpreter will make. Both
+ # spellings of the leaf, so the function is the same one a non-Windows lane can
+ # execute end to end rather than a Windows-only path nothing tests.
+ $hostExe = $HostPath
+ if (-not $hostExe) {
+ try {
+ $leaves = if ($PSVersionTable.PSEdition -eq "Core") { @("pwsh.exe", "pwsh") }
+ else { @("powershell.exe", "powershell") }
+ foreach ($leaf in $leaves) {
+ $candidate = Join-Path $PSHOME $leaf
+ if (Test-Path -LiteralPath $candidate) { $hostExe = $candidate; break }
+ }
+ } catch {}
+ }
+ if (-not $hostExe) { return $false }
+ # Through a Process object rather than the call operator, for a deadline. The call
+ # operator waits for the child forever, and "forever" is reachable: a security
+ # product inspecting a freshly spawned interpreter, a wedged runtime start, a child
+ # that blocks on shutdown. A probe that exists to keep the installer alive must not
+ # be the thing that hangs it.
+ #
+ # BOTH streams are redirected and drained asynchronously. Draining is what stops a
+ # chatty child filling a pipe and deadlocking against the wait. Redirecting stderr as
+ # well is the difference between a probe that is invisible and one that can write
+ # into the installer's own stderr, which the desktop app reads, and which anything
+ # the child spawns would inherit and hold open.
+ #
+ # The body is embedded in double quotes here, which is safe for exactly one reason:
+ # it contains none, asserted by a test. See the note above.
+ $info = New-Object System.Diagnostics.ProcessStartInfo
+ $info.FileName = $hostExe
+ $info.Arguments = "-NoProfile -NonInteractive -Command `"$probe`""
+ $info.UseShellExecute = $false
+ $info.RedirectStandardOutput = $true
+ $info.RedirectStandardError = $true
+ $info.CreateNoWindow = $true
+ $child = $null
+ try {
+ $child = [System.Diagnostics.Process]::Start($info)
+ $reader = $child.StandardOutput.ReadToEndAsync()
+ $null = $child.StandardError.ReadToEndAsync()
+ if (-not $child.WaitForExit(20000)) {
+ try { $child.Kill() } catch {}
+ return $false
+ }
+ # Exit code AND an exact record. A marker followed by a crash is a crash: the
+ # question is whether this machine can emit and live, and a child that printed
+ # and then died has answered no. FullLanguage because an approved script can run
+ # in FullLanguage while a fresh inline command does not, and a child restricted
+ # differently from its parent has measured a different machine.
+ if ($child.ExitCode -ne 0) {
+ $script:StudioEmitProbeOutcome = "blocked"
+ return $false
+ }
+ $lines = ($reader.GetAwaiter().GetResult() -split "`r?`n")
+ foreach ($line in $lines) {
+ if ($line.Trim() -eq "STUDIO_EMIT_OK FullLanguage") {
+ $script:StudioEmitProbeOutcome = "ok"
+ return $true
+ }
+ # Emitted, but in a language mode this parent is not in. The child measured a
+ # different machine, which is an answer rather than a missed one.
+ if ($line.Trim() -like "STUDIO_EMIT_OK *") {
+ $script:StudioEmitProbeOutcome = "blocked"
+ return $false
+ }
+ }
+ # Exit 0 with no marker at all: the child cannot have got past the emit and then
+ # reported nothing, so its output was lost rather than negative.
+ return $false
+ } catch {
+ return $false
+ } finally {
+ if ($child) {
+ # The read end goes first. A killed child can leave a grandchild holding the
+ # write end of that pipe, and the pending async read then keeps this process
+ # alive past the deadline it just enforced.
+ try { $child.StandardOutput.Close() } catch {}
+ try { $child.StandardError.Close() } catch {}
+ try { $child.Dispose() } catch {}
+ }
+ }
+ }
+
+ function New-StudioDynamicAssembly {
+ <#
+ Both spellings of "define a dynamic assembly", because the two PowerShell
+ hosts that run this file are on different runtimes. The static
+ AssemblyBuilder::DefineDynamicAssembly is documented for .NET Framework
+ 4.5 through 4.8.1 as well as .NET Core, so Windows PowerShell 5.1 should
+ take the first branch. It is tried rather than assumed because nothing
+ here can test a .NET Framework host, and getting it wrong is not a visible
+ error: the catch would cache the resolver as unavailable and every desktop
+ install would silently use the lexical fallback, which can assign
+ different mutex names to a long path and its 8.3 alias.
+
+ AppDomain.CurrentDomain.DefineDynamicAssembly is the .NET Framework
+ spelling and is absent on .NET Core, so it is the fallback rather than the
+ first try; pwsh would fail on it.
+ #>
+ param([Parameter(Mandatory = $true)][System.Reflection.AssemblyName]$AssemblyName)
+ $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run
+ try {
+ return [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly(
+ $AssemblyName, $access)
+ } catch [System.Management.Automation.MethodException] {
+ return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)
+ } catch [System.Management.Automation.RuntimeException] {
+ # A missing static surfaces as a RuntimeException on some hosts rather
+ # than a MethodException. Both mean "no such method here", and a real
+ # emit failure (Dynamic Code Security, Constrained Language) throws
+ # from the AppDomain call too, so the caller still sees it.
+ return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)
+ }
+ }
+
+ function New-StudioEmittedNativeType {
+ param(
+ [Parameter(Mandatory = $true)][string]$TypeName,
+ # @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]
+ # Args = @([IntPtr]) }
+ [Parameter(Mandatory = $true)][object[]]$Imports
+ )
+ $assemblyName = New-Object System.Reflection.AssemblyName $TypeName
+ $assembly = New-StudioDynamicAssembly -AssemblyName $assemblyName
+ $module = $assembly.DefineDynamicModule($TypeName)
+ $builder = $module.DefineType(
+ $TypeName, "Public, Class, AutoClass, AnsiClass, BeforeFieldInit")
+
+ $winapi = [System.Runtime.InteropServices.CallingConvention]::Winapi
+ # Per import, because CharSet is not decoration: it selects name mangling
+ # as well as marshalling. Unicode probes <Name>W before <Name>, Ansi probes
+ # <Name> before <Name>A. Every import here names an export that exists
+ # exactly as written, so both orders arrive; declaring the one the C# these
+ # replace declared keeps the metadata honest and puts the export that does
+ # exist first. Unicode is the default, since the calls carrying text are
+ # the ones already spelled W.
+ $unicode = [System.Runtime.InteropServices.CharSet]::Unicode
+ $ansi = [System.Runtime.InteropServices.CharSet]::Ansi
+ $standard = [System.Reflection.CallingConventions]::Standard
+ $attributes = "Public, Static, HideBySig, PinvokeImpl"
+ $preserveSig = [System.Reflection.MethodImplAttributes]::PreserveSig
+
+ foreach ($import in $Imports) {
+ $charSet = if ($import.ContainsKey("Ansi") -and $import.Ansi) { $ansi } else { $unicode }
+ $method = $builder.DefinePInvokeMethod(
+ $import.Name, $import.Library, $import.Name, $attributes,
+ $standard, $import.Return, $import.Args, $winapi, $charSet)
+ $method.SetImplementationFlags(
+ $method.GetMethodImplementationFlags() -bor $preserveSig)
+ # `out uint` in the C# this replaces, and DefinePInvokeMethod has no way to say
+ # so: a by-ref type alone emits `ref`, which is In and Out unset. The value is
+ # blittable and every caller initialises it first, so the marshaller pins and
+ # writes back either way, but the metadata is what a reader and any future
+ # marshalling change go by, so it says what the declaration said.
+ # ContainsKey, not a bare property read: most imports have no Out key and reading a
+ # missing one is fatal under a caller's Set-StrictMode. setup.ps1 sets none of its own.
+ if ($import.ContainsKey("Out")) {
+ foreach ($position in @($import.Out)) {
+ if ($position) { $null = $method.DefineParameter($position, "Out", $null) }
+ }
+ }
+ }
+ $null = $builder.CreateType()
+ return $null -ne ($TypeName -as [type])
+ }
+
+ # GetFinalPathNameByHandleW is the only exact answer for a path: it follows
+ # junctions, symlinks and SUBST drives, expands 8.3 aliases and reports the
+ # on-disk spelling, none of which GetFullPath does. Cache the answer, since
+ # callers resolve dozens of paths, and where it is unavailable let
# Get-StudioLexicalPath carry the run.
$script:StudioFinalPathNativeState = $null
# Reset with the rest: under `irm | iex` these are the caller's own.
@@ -847,172 +1152,146 @@ function Install-UnslothStudio {
param([string]$Reason)
if ($script:StudioFinalPathWarned) { return }
$script:StudioFinalPathWarned = $true
+ # "installation is unaffected" is what this used to promise, and it is a
+ # promise this line is not in a position to keep: the same security software
+ # that can stop a type being defined can also be acting on the rest of the
+ # run. What is true is the narrower thing, that the installer has a way to
+ # continue without it.
Write-StudioLine "[WARN] Could not load the native path resolver ($Reason)." -ForegroundColor Yellow
- Write-StudioLine " Continuing with the PowerShell resolver; installation is unaffected." -ForegroundColor Yellow
+ Write-StudioLine " Continuing with the PowerShell resolver, which cannot recover a path's" -ForegroundColor Yellow
+ Write-StudioLine " stored casing or expand an 8.3 name, so paths are compared as written." -ForegroundColor Yellow
}
function Initialize-StudioFinalPathNativeType {
- if ("UnslothStudioFinalPathV2" -as [type]) {
+ if ("UnslothStudioFinalPathV3" -as [type]) {
$script:StudioFinalPathNativeState = $true
return $true
}
if ($null -ne $script:StudioFinalPathNativeState) { return $script:StudioFinalPathNativeState }
- # Constrained Language Mode forbids Add-Type, so compiling would only produce
- # a second, less honest error.
- $languageMode = "FullLanguage"
- try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}
- if ($languageMode -ne "FullLanguage") {
+ # Constrained Language Mode forbids defining types at all, by emit as by
+ # Add-Type, so compiling would only produce a second, less honest error.
+ if (-not (Test-StudioCanDefineNativeTypes)) {
$script:StudioFinalPathNativeState = $false
- Write-StudioFinalPathDegraded -Reason "PowerShell is in $languageMode"
+ $languageMode = "FullLanguage"
+ try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}
+ # Three reasons, because the gate now has three ways to say no and only one of
+ # them is a policy. Claiming code integrity for a probe that could not be
+ # spawned, or that was killed at its deadline, is a wrong answer written into
+ # the log a support request is built from: it names a machine setting the user
+ # would then go looking for and not find.
+ $reason = if ($languageMode -ne "FullLanguage") { "PowerShell is in $languageMode" }
+ elseif ($script:StudioEmitProbeOutcome -eq "blocked") {
+ "this host enforces user-mode code integrity"
+ } else { "a probe process could not confirm native type support" }
+ Write-StudioFinalPathDegraded -Reason $reason
return $false
}
- Initialize-StudioTempEnvironment
- $source = @'
-using System;
-using System.ComponentModel;
-using System.Runtime.InteropServices;
-using System.Text;
-using Microsoft.Win32.SafeHandles;
-
-public static class UnslothStudioFinalPathV2
-{
- private const uint FileShareRead = 0x00000001;
- private const uint FileShareWrite = 0x00000002;
- private const uint FileShareDelete = 0x00000004;
- private const uint OpenExisting = 3;
- private const uint FileFlagBackupSemantics = 0x02000000;
-
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- private static extern SafeFileHandle CreateFileW(
- string fileName,
- uint desiredAccess,
- uint shareMode,
- IntPtr securityAttributes,
- uint creationDisposition,
- uint flagsAndAttributes,
- IntPtr templateFile);
-
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- private static extern uint GetFinalPathNameByHandleW(
- SafeFileHandle file,
- StringBuilder path,
- uint pathLength,
- uint flags);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr OpenProcess(
- uint desiredAccess,
- bool inheritHandle,
- int processId);
-
- [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- private static extern bool QueryFullProcessImageNameW(
- IntPtr process,
- uint flags,
- StringBuilder path,
- ref uint pathLength);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool CloseHandle(IntPtr handle);
-
- public static string Resolve(string path)
- {
- using (SafeFileHandle handle = CreateFileW(
- path,
- 0,
- FileShareRead | FileShareWrite | FileShareDelete,
- IntPtr.Zero,
- OpenExisting,
- FileFlagBackupSemantics,
- IntPtr.Zero))
- {
- if (handle.IsInvalid)
- throw new Win32Exception(Marshal.GetLastWin32Error());
-
- StringBuilder buffer = new StringBuilder(512);
- uint length = GetFinalPathNameByHandleW(
- handle, buffer, (uint)buffer.Capacity, 0);
- if (length == 0)
- throw new Win32Exception(Marshal.GetLastWin32Error());
- if (length >= buffer.Capacity)
- {
- buffer = new StringBuilder((int)length + 1);
- length = GetFinalPathNameByHandleW(
- handle, buffer, (uint)buffer.Capacity, 0);
- if (length == 0)
- throw new Win32Exception(Marshal.GetLastWin32Error());
- }
- if (length >= buffer.Capacity)
- throw new InvalidOperationException("Final path exceeded the allocated buffer");
- return buffer.ToString();
- }
- }
-
- public static string GetProcessImagePath(int processId)
- {
- const uint ProcessQueryLimitedInformation = 0x1000;
- IntPtr process = OpenProcess(ProcessQueryLimitedInformation, false, processId);
- if (process == IntPtr.Zero)
- {
- return null;
- }
- try
- {
- StringBuilder path = new StringBuilder(32768);
- uint pathLength = (uint)path.Capacity;
- return QueryFullProcessImageNameW(process, 0, path, ref pathLength)
- ? path.ToString()
- : null;
- }
- finally
- {
- CloseHandle(process);
- }
- }
-}
-'@
- $firstError = $null
+ # Everything below the capability check is unchanged, so a host where the
+ # emit fails degrades exactly as a host that could not compile already
+ # did, which is a path the installer has always taken and still completes
+ # on.
+ #
+ # DefinePInvokeMethod cannot ask for SetLastError, so nothing below reads
+ # GetLastWin32Error. The C# it replaces threw a Win32Exception the callers
+ # only ever turned back into "use the lexical answer", so returning null
+ # loses a code nothing acted on.
try {
- Add-Type -TypeDefinition $source -ErrorAction Stop
+ $null = New-StudioEmittedNativeType -TypeName "UnslothStudioFinalPathV3" -Imports @(
+ @{ Name = "CreateFileW"; Library = "kernel32.dll"; Return = [IntPtr]
+ Args = @([string], [uint32], [uint32], [IntPtr], [uint32], [uint32], [IntPtr]) },
+ @{ Name = "GetFinalPathNameByHandleW"; Library = "kernel32.dll"; Return = [uint32]
+ Args = @([IntPtr], [System.Text.StringBuilder], [uint32], [uint32]) },
+ @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr])
+ Ansi = $true }
+ )
} catch {
- $firstError = $_.Exception.Message
- }
- # A compile that reports failure can still have loaded the type, and the same
- # name cannot be defined twice in one session.
- if ("UnslothStudioFinalPathV2" -as [type]) {
- $script:StudioFinalPathNativeState = $true
- return $true
- }
- $private = New-StudioPrivateTempDirectory
- if ($private) {
- $hadTmp = ($null -ne $env:TMP)
- $previousTmp = $env:TMP
- $hadTemp = ($null -ne $env:TEMP)
- $previousTemp = $env:TEMP
- try {
- # Both, because GetTempPath reads TMP first.
- $env:TMP = $private
- $env:TEMP = $private
- try { Add-Type -TypeDefinition $source -ErrorAction Stop } catch {}
- } finally {
- if ($hadTmp) { $env:TMP = $previousTmp } else { Remove-Item Env:\TMP -ErrorAction SilentlyContinue }
- if ($hadTemp) { $env:TEMP = $previousTemp } else { Remove-Item Env:\TEMP -ErrorAction SilentlyContinue }
- # Only now: deleting while csc.exe still holds it is the race being
- # worked around.
- Remove-Item -LiteralPath $private -Recurse -Force -ErrorAction SilentlyContinue
+ # A throw does not always mean nothing was defined: CreateType can
+ # publish the type and then fail on the way back, and the compiled
+ # version this replaces checked for exactly that before giving up. If
+ # the type is there it is usable, so ask before caching the negative.
+ if ("UnslothStudioFinalPathV3" -as [type]) {
+ $script:StudioFinalPathNativeState = $true
+ return $true
}
+ $script:StudioFinalPathNativeState = $false
+ Write-StudioFinalPathDegraded -Reason (($_.Exception.Message -split "`r?`n")[0].Trim())
+ return $false
}
- if ("UnslothStudioFinalPathV2" -as [type]) {
+ if ("UnslothStudioFinalPathV3" -as [type]) {
$script:StudioFinalPathNativeState = $true
return $true
}
$script:StudioFinalPathNativeState = $false
- # First line of the compiler output, not the whole C# dump it echoes after.
- $reason = if ($firstError) { ($firstError -split "`r?`n")[0].Trim() } else { "compilation failed" }
- Write-StudioFinalPathDegraded -Reason $reason
+ Write-StudioFinalPathDegraded -Reason "the native path resolver could not be defined"
return $false
}
+ # GetFinalPathNameByHandleW is the only exact answer: it follows junctions,
+ # symlinks and SUBST drives, expands 8.3 aliases and reports the on-disk
+ # spelling, none of which GetFullPath does.
+ #
+ # This is what the compiled Resolve() did, moved out of C# so the imports
+ # above are all the native code there is. Same flags, same two-pass buffer
+ # growth. Null rather than an exception when the handle or the call fails:
+ # every caller already treats "no exact answer" as "use the lexical one".
+ function Get-StudioNativeFinalPath {
+ param([Parameter(Mandatory = $true)][string]$Path)
+
+ $invalidHandle = [IntPtr](-1)
+ $fileShareAll = [uint32]7 # READ | WRITE | DELETE
+ $openExisting = [uint32]3
+ $backupSemantics = [uint32]0x02000000 # required to open a DIRECTORY
+
+ # Every native call is guarded, not just the ones that can fail usefully.
+ # An emitted stub binds its import on first CALL, not when the type is
+ # defined, so a host missing the export raises here rather than above, and
+ # the caller must see the same "no exact answer" it sees from a handle it
+ # could not open. The callers already treat null that way; throwing past
+ # them would be a new failure mode this change has no business inventing.
+ # Acquisition sits INSIDE the region that closes the handle. The C# this
+ # replaces returned a SafeFileHandle, whose finalizer was a backstop if the
+ # pipeline stopped between the open and the using block; an IntPtr has
+ # none, so the open and the finally are one region instead. What is left
+ # is the instant between the native return and the assignment, which no
+ # arrangement of PowerShell can close. It costs nothing here: the handle is
+ # opened with desired access 0 and FILE_SHARE_READ|WRITE|DELETE, so even a
+ # leaked one blocks no other opener, and it dies with the process.
+ $handle = $invalidHandle
+ try {
+ try {
+ $handle = [UnslothStudioFinalPathV3]::CreateFileW(
+ $Path, [uint32]0, $fileShareAll, [IntPtr]::Zero,
+ $openExisting, $backupSemantics, [IntPtr]::Zero)
+ } catch {
+ return $null
+ }
+ if ($handle -eq $invalidHandle -or $handle -eq [IntPtr]::Zero) { return $null }
+ $buffer = New-Object System.Text.StringBuilder 512
+ $length = [UnslothStudioFinalPathV3]::GetFinalPathNameByHandleW(
+ $handle, $buffer, [uint32]$buffer.Capacity, [uint32]0)
+ if ($length -eq 0) { return $null }
+ if ($length -ge $buffer.Capacity) {
+ $buffer = New-Object System.Text.StringBuilder ([int]$length + 1)
+ $length = [UnslothStudioFinalPathV3]::GetFinalPathNameByHandleW(
+ $handle, $buffer, [uint32]$buffer.Capacity, [uint32]0)
+ if ($length -eq 0) { return $null }
+ }
+ # Still short is the only answer worth trusting.
+ if ($length -ge $buffer.Capacity) { return $null }
+ return $buffer.ToString()
+ } catch {
+ return $null
+ } finally {
+ # Guarded now that the open is inside this region: the failure paths
+ # reach here with the sentinel, and closing it is a call Windows has no
+ # reason to be asked to make.
+ if ($handle -ne $invalidHandle -and $handle -ne [IntPtr]::Zero) {
+ try { [void][UnslothStudioFinalPathV3]::CloseHandle($handle) } catch {}
+ }
+ }
+ }
+
function Resolve-StudioLinkTarget {
param([Parameter(Mandatory = $true)][string]$Path)
$item = $null
@@ -1189,15 +1468,16 @@ public static class UnslothStudioFinalPathV2
$resolved = $null
if (Initialize-StudioFinalPathNativeType) {
try {
- $resolved = [UnslothStudioFinalPathV2]::Resolve($existingPath)
+ $resolved = Get-StudioNativeFinalPath -Path $existingPath
+ if ([string]::IsNullOrWhiteSpace($resolved)) { throw "no exact answer" }
$exact = $true
} catch {
- # The helper COMPILED and still could not answer: a path renamed
+ # The helper was DEFINED and still could not answer: a path renamed
# between the Test-Path walk and CreateFileW, an access denial on a
# component, a volume with no drive letter. Falling back keeps the
# install alive, and Exact = $false already makes the runtime lock
# fail closed, but nothing said so out loud: the degraded warning
- # below only fires when the compile itself failed. An operator was
+ # below only fires when the type itself could not be built. An operator was
# left with a silently inexact identity on a host that looks fine.
$resolved = $null
if (-not $script:StudioNativeResolveWarned) {
@@ -1264,9 +1544,12 @@ public static class UnslothStudioFinalPathV2
Write-StudioLine " The desktop app uses the Windows profile .unsloth\studio root." -ForegroundColor Red
Write-StudioLine " Run install.ps1 without --tauri for custom-root shell installs," -ForegroundColor Yellow
Write-StudioLine " or unset the env var for default desktop installs." -ForegroundColor Yellow
- # Resolving the roots above can redirect TMP/TEMP, and this throw is well
- # before the lock try/finally. Under `irm | iex` those variables are the
- # caller's own and would stay pointed at an installer-owned directory.
+ # Belt and braces: TMP/TEMP are only redirected from
+ # Initialize-StudioTempEnvironment, which now runs after this throw, so
+ # there is nothing to restore here today. The call returns immediately
+ # when nothing was overridden, and under `irm | iex` those variables are
+ # the caller's own, so the day anything above starts redirecting them
+ # again this stays correct instead of silently leaking.
Restore-StudioTempEnvironment
throw "$envOverrideVar is not supported with --tauri."
}
@@ -1528,23 +1811,43 @@ public static class UnslothStudioFinalPathV2
function Enable-StudioVirtualTerminal {
if ($env:NO_COLOR) { return $false }
# A redirected stdout is not a console and GetConsoleMode fails on a non-console handle,
- # so the block below could only return $false anyway. Answer without Add-Type, which runs
- # csc.exe and drops source in %TEMP%. install.rs spawns us with a pipe, so this is the path
- # the compile was on.
+ # so the block below could only return $false anyway. install.rs spawns us with a pipe,
+ # so that is the path the desktop app is on.
if ($script:StudioStdoutRedirected) { return $false }
+ # Emitted rather than compiled, for the reason New-StudioEmittedNativeType
+ # gives. -MemberDefinition runs csc.exe just as -TypeDefinition does, and
+ # the guard above only keeps the desktop app off it; the console path,
+ # including `irm | iex`, reached the compiler here every run. Colour is
+ # cosmetic, so failure is still just a plain banner.
+ # The same gate the resolver uses: colour is not worth a risk that cannot be
+ # caught, and a plain banner is the answer either way.
+ # The published type first, the gate only if there is nothing published. A type
+ # this session already emitted is proof that emit works here, and the compiled
+ # version checked for it in the same order; asking a child instead means one
+ # failed probe throws away a console helper that is already loaded and usable.
+ if (-not ("StudioVTNative" -as [type]) -and -not (Test-StudioCanDefineNativeTypes)) {
+ return $false
+ }
try {
- if (-not ("StudioVT.Native" -as [type])) {
- Add-Type -Namespace StudioVT -Name Native -MemberDefinition @'
-[DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int nStdHandle);
-[DllImport("kernel32.dll")] public static extern bool GetConsoleMode(IntPtr h, out uint m);
-[DllImport("kernel32.dll")] public static extern bool SetConsoleMode(IntPtr h, uint m);
-'@ -ErrorAction Stop
- }
- $h = [StudioVT.Native]::GetStdHandle(-11)
+ if (-not ("StudioVTNative" -as [type])) {
+ $null = New-StudioEmittedNativeType -TypeName "StudioVTNative" -Imports @(
+ @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]
+ Args = @([int])
+ Ansi = $true },
+ @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32].MakeByRefType())
+ Ansi = $true
+ Out = @(2) },
+ @{ Name = "SetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32])
+ Ansi = $true }
+ )
+ }
+ $h = [StudioVTNative]::GetStdHandle(-11)
[uint32]$mode = 0
- if (-not [StudioVT.Native]::GetConsoleMode($h, [ref]$mode)) { return $false }
+ if (-not [StudioVTNative]::GetConsoleMode($h, [ref]$mode)) { return $false }
$mode = $mode -bor 0x0004
- return [StudioVT.Native]::SetConsoleMode($h, $mode)
+ return [StudioVTNative]::SetConsoleMode($h, $mode)
} catch {
return $false
}
@@ -1578,8 +1881,11 @@ public static class UnslothStudioFinalPathV2
}
Write-StudioLine ""
- # Here so its warning lands under the banner. A no-op the second time: a --tauri
- # run with a custom root reaches it first via Initialize-StudioFinalPathNativeType.
+ # Here so its warning lands under the banner. The only caller now: the native
+ # path resolver used to reach it first, because compiling wrote to %TEMP%, and
+ # it no longer writes anything anywhere. Nothing between the banner and here
+ # touches the temporary directory, so the fix-up still lands before the first
+ # user of it.
Initialize-StudioTempEnvironment
# ── Helper: refresh PATH from registry (deduplicating entries) ──
@@ -2873,13 +3179,28 @@ exit 0
substep "Created Unsloth Studio shortcut"
# Per-item SHChangeNotify: the global broadcast misses a rewritten same-name .lnk.
try {
- Add-Type -Namespace UnslothShell -Name IconRefresh -MemberDefinition '[System.Runtime.InteropServices.DllImport("shell32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] public static extern void SHChangeNotify(int eventId, uint flags, string item1, System.IntPtr item2);' -ErrorAction SilentlyContinue
+ # Emitted, not compiled: -MemberDefinition runs csc.exe too.
+ # Tauri returns before it gets here, so this one was only
+ # ever on the console path, but that path deserves the same
+ # treatment. A failure here leaves stale icons, nothing more,
+ # and the enclosing catch already absorbed that.
+ # Same order as everywhere else: a published type settles it, and
+ # only an absent one asks the gate.
+ if (-not ("UnslothShellIconRefresh" -as [type])) {
+ if (-not (Test-StudioCanDefineNativeTypes)) {
+ throw "native types are unavailable on this host"
+ }
+ $null = New-StudioEmittedNativeType -TypeName "UnslothShellIconRefresh" -Imports @(
+ @{ Name = "SHChangeNotify"; Library = "shell32.dll"; Return = [System.Void]
+ Args = @([int], [uint32], [string], [IntPtr]) }
+ )
+ }
# SHCNE_UPDATEITEM (0x00002000) + SHCNF_PATHW (0x0005) per shortcut
foreach ($scPath in $createdShortcutPaths) {
- try { [UnslothShell.IconRefresh]::SHChangeNotify(0x00002000, 0x0005, $scPath, [System.IntPtr]::Zero) } catch {}
+ try { [UnslothShellIconRefresh]::SHChangeNotify(0x00002000, 0x0005, $scPath, [System.IntPtr]::Zero) } catch {}
}
# SHCNE_ASSOCCHANGED (0x08000000) global refresh (belt-and-suspenders)
- [UnslothShell.IconRefresh]::SHChangeNotify(0x08000000, 0, $null, [System.IntPtr]::Zero)
+ [UnslothShellIconRefresh]::SHChangeNotify(0x08000000, 0, $null, [System.IntPtr]::Zero)
} catch {}
if ($firstInstall -or $iconChanged) {
try { & "$env:SystemRoot\System32\ie4uinit.exe" -ClearIconCache 2>$null } catch {}
@@ -3262,19 +3583,99 @@ exit 0
}
# QueryFullProcessImageNameW answers for processes whose MainModule is not
- # readable here, but needs the compiled helper. Without a fallback a host that
- # cannot compile would find NO running processes and overwrite a venv Unsloth has
- # open, so the ladder ends at Win32_Process. Every rung reports a real executable
- # image; a command line or working directory mentioning the path is never proof.
+ # readable here: PROCESS_QUERY_LIMITED_INFORMATION is granted where the
+ # PROCESS_VM_READ that MainModule needs is refused, and it asks nothing of WMI.
+ # Without it a host can find NO running processes and overwrite a venv Unsloth
+ # has open, so the ladder still ends at Get-Process and Win32_Process. Every
+ # rung reports a real executable image; a command line or working directory
+ # mentioning the path is never proof.
+ #
+ # Emitted rather than compiled, like every other native declaration here. The
+ # ladder itself is unchanged from before that switch.
+ $script:StudioProcessImageNativeState = $null
+ function Initialize-StudioProcessImageNativeType {
+ if ("UnslothStudioProcessImageV1" -as [type]) {
+ $script:StudioProcessImageNativeState = $true
+ return $true
+ }
+ if ($null -ne $script:StudioProcessImageNativeState) { return $script:StudioProcessImageNativeState }
+ # A type this session already emitted outranks any probe. The compiled version
+ # could not disagree with itself here, because one type carried the path helper
+ # and this one; two types can, and the way they do is a session that emitted the
+ # path type, was interrupted, and came back to a probe that now fails. Asking a
+ # child whether emit works, in a process where emit demonstrably worked, is a
+ # question with an answer already on the table.
+ $alreadyEmitted = $null -ne ("UnslothStudioFinalPathV3" -as [type])
+ if (-not $alreadyEmitted -and -not (Test-StudioCanDefineNativeTypes)) {
+ $script:StudioProcessImageNativeState = $false
+ return $false
+ }
+ try {
+ $null = New-StudioEmittedNativeType -TypeName "UnslothStudioProcessImageV1" -Imports @(
+ @{ Name = "OpenProcess"; Library = "kernel32.dll"; Return = [IntPtr]
+ Args = @([uint32], [bool], [int])
+ Ansi = $true },
+ @{ Name = "QueryFullProcessImageNameW"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32], [System.Text.StringBuilder], [uint32].MakeByRefType()) },
+ @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr])
+ Ansi = $true }
+ )
+ } catch {
+ # Same reason as the path helper: a throw on the way out of CreateType
+ # can still leave the type published, and a published type works.
+ if ("UnslothStudioProcessImageV1" -as [type]) {
+ $script:StudioProcessImageNativeState = $true
+ return $true
+ }
+ $script:StudioProcessImageNativeState = $false
+ return $false
+ }
+ $script:StudioProcessImageNativeState = $null -ne ("UnslothStudioProcessImageV1" -as [type])
+ return $script:StudioProcessImageNativeState
+ }
+
+ # The body of the compiled GetProcessImagePath, moved out of C# so the imports
+ # above are all the native code there is. Same flags, same buffer. Null rather
+ # than an exception on any failure, which is what the compiled one returned and
+ # what the caller below skips on.
+ function Get-StudioNativeProcessImagePath {
+ param([Parameter(Mandatory = $true)][int]$ProcessId)
+ $queryLimitedInformation = [uint32]0x1000
+ # Same shape as Get-StudioNativeFinalPath: the open is inside the region
+ # that closes it, because an IntPtr has no finalizer to fall back on.
+ $handle = [IntPtr]::Zero
+ try {
+ try {
+ $handle = [UnslothStudioProcessImageV1]::OpenProcess(
+ $queryLimitedInformation, $false, $ProcessId)
+ } catch {
+ return $null
+ }
+ if ($handle -eq [IntPtr]::Zero) { return $null }
+ $buffer = New-Object System.Text.StringBuilder 32768
+ [uint32]$length = $buffer.Capacity
+ if (-not [UnslothStudioProcessImageV1]::QueryFullProcessImageNameW(
+ $handle, [uint32]0, $buffer, [ref]$length)) {
+ return $null
+ }
+ return $buffer.ToString()
+ } catch {
+ return $null
+ } finally {
+ if ($handle -ne [IntPtr]::Zero) {
+ try { [void][UnslothStudioProcessImageV1]::CloseHandle($handle) } catch {}
+ }
+ }
+ }
+
$script:StudioProcessImageTable = $null
$script:StudioProcessImageWarned = $false
function Get-StudioProcessImagePath {
param([Parameter(Mandatory = $true)][int]$ProcessId)
- if (Initialize-StudioFinalPathNativeType) {
- try {
- $native = [UnslothStudioFinalPathV2]::GetProcessImagePath($ProcessId)
- if (-not [string]::IsNullOrWhiteSpace($native)) { return $native }
- } catch {}
+ if (Initialize-StudioProcessImageNativeType) {
+ $native = Get-StudioNativeProcessImagePath -ProcessId $ProcessId
+ if (-not [string]::IsNullOrWhiteSpace($native)) { return $native }
return $null
}
if (-not $script:StudioProcessImageWarned) {
@@ -3322,7 +3723,7 @@ exit 0
# D:\env\python.exe matched a protected C:\env, aborting a legitimate install
# as "still in use". The alias it was written for, SUBST, is folded in
# Get-StudioLexicalPath instead. A volume reached by GUID still cannot be
- # matched to the same volume by drive letter without the compiler.
+ # matched to the same volume by drive letter without the native resolver.
# Block only confirmed executable identities: a command line or working
# directory that merely mentions the path is not proof of an open file.
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index f4e466643ad..7d5872b62ea 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -1591,24 +1591,301 @@ function Ensure-VCRedist {
# ─────────────────────────────────────────────
$Rule = [string]::new([char]0x2500, 52)
+# Native declarations are emitted, never compiled. Add-Type on Windows PowerShell 5.1 has no
+# in-process compiler: -TypeDefinition and -MemberDefinition alike write C# to %TEMP% and run
+# csc.exe to get a DLL back, and behavioural antivirus blocks the result, because a windowless
+# PowerShell launching a compiler and writing executable content to %TEMP% is a dropper's shape
+# whatever the code says. Reflection emit builds the same stub in memory: no compiler process, no
+# source on disk, no DLL, and an assembly whose Location is empty. install.ps1 carries the same
+# helper, and for the same reason.
+# See install.ps1: App Control's Dynamic Code Security always blocks loading unsigned
+# assemblies built with System.Reflection.Emit, and Microsoft documents the parent process as
+# usually stopped or crashing rather than raising, so this has to be a gate and not a catch.
+$script:StudioCanDefineNativeTypes = $null
+# Why the last probe answered as it did, so a caller can tell "the child ran and
+# said no" from "the child never got to answer". Those are the same boolean and
+# they are not the same fact: one is a policy, the other is a process that failed
+# to start, was killed at the deadline, or lost its output.
+$script:StudioEmitProbeOutcome = $null
+function Test-StudioCanDefineNativeTypes {
+ if ($null -ne $script:StudioCanDefineNativeTypes) { return $script:StudioCanDefineNativeTypes }
+ $languageMode = "FullLanguage"
+ try { $languageMode = [string]$ExecutionContext.SessionState.LanguageMode } catch {}
+ if ($languageMode -ne "FullLanguage") {
+ $script:StudioCanDefineNativeTypes = $false
+ return $false
+ }
+ # Read-and-zero is the only outcome that skips the probe. A query that threw, returned
+ # nothing, or returned an object without the property is UNKNOWN, and treating unknown as
+ # unrestricted lets option 19 through on a host whose CIM query failed. install.ps1
+ # carries the full note.
+ $known = $false
+ $active = $false
+ try {
+ # Bounded, with what that bound actually covers stated rather than implied:
+ # -OperationTimeoutSec limits the CIM operation on a responsive target. It does
+ # not interrupt DCOM connection setup and it does not override a provider that
+ # has wedged, where the server's own timeout wins. It is worth having for the
+ # slow case and it is not a hang guard. The child probe below is the part that
+ # carries a real deadline.
+ $guard = Get-CimInstance -Namespace "root\Microsoft\Windows\DeviceGuard" `
+ -ClassName "Win32_DeviceGuard" -OperationTimeoutSec 10 -ErrorAction Stop
+ # 0 off, 1 audit, 2 enforced. A null property is not a zero.
+ if ($guard -and $null -ne $guard.UsermodeCodeIntegrityPolicyEnforcementStatus) {
+ $known = $true
+ if ([int]$guard.UsermodeCodeIntegrityPolicyEnforcementStatus -ne 0) {
+ $active = $true
+ }
+ }
+ } catch {}
+ if ($known -and -not $active) {
+ $script:StudioCanDefineNativeTypes = $true
+ return $true
+ }
+ # Which policy decides this, and Win32_DeviceGuard does not say. Option 19 Dynamic Code
+ # Security always blocks unsigned System.Reflection.Emit assemblies and is enforced even in
+ # an audit policy before Windows 11 24H2, while an audit policy without it emits fine. So a
+ # child process tries it. Same reasoning as install.ps1, which carries the full note.
+ $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess
+ # One retry, and only when the first attempt never reached an answer. The compiled
+ # version this replaces also tried twice before caching a negative, and without
+ # that a single transient process failure is indistinguishable from a policy: it
+ # is cached for the whole run and sends the installer down the lexical path, where
+ # two unequal roots compare as unknown and a second lock gets taken. A child that
+ # RAN and said no is not retried, so a genuinely blocked machine still pays for
+ # one probe.
+ if (-not $script:StudioCanDefineNativeTypes -and
+ $script:StudioEmitProbeOutcome -eq "indeterminate") {
+ $script:StudioCanDefineNativeTypes = Test-StudioEmitInChildProcess
+ }
+ return $script:StudioCanDefineNativeTypes
+}
+
+# The same emit, in a process that is allowed to die. A blocked dynamic load usually stops the
+# parent, so this is asked in a child; silence is refusal.
+function Test-StudioEmitInChildProcess {
+ # HostPath is for the tests, which have no policy to trigger the real path and cannot
+ # shadow $PSHOME, since it is read-only. Production never passes it.
+ param([string]$HostPath)
+ # Until something here establishes otherwise. Every exit below either leaves this
+ # alone or says what it learned.
+ $script:StudioEmitProbeOutcome = "indeterminate"
+ $probe = @'
+try {
+ $name = New-Object System.Reflection.AssemblyName 'UnslothStudioEmitProbe'
+ $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run
+ $assembly = $null
+ try { $assembly = [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly($name, $access) }
+ catch { $assembly = [AppDomain]::CurrentDomain.DefineDynamicAssembly($name, $access) }
+ $module = $assembly.DefineDynamicModule('UnslothStudioEmitProbe')
+ $builder = $module.DefineType('UnslothStudioEmitProbe', 'Public, Class, AutoClass, AnsiClass, BeforeFieldInit')
+ $method = $builder.DefinePInvokeMethod('CloseHandle', 'kernel32.dll', 'CloseHandle',
+ 'Public, Static, HideBySig, PinvokeImpl',
+ [System.Reflection.CallingConventions]::Standard, [bool], @([IntPtr]),
+ [System.Runtime.InteropServices.CallingConvention]::Winapi,
+ [System.Runtime.InteropServices.CharSet]::Ansi)
+ $method.SetImplementationFlags(
+ $method.GetMethodImplementationFlags() -bor [System.Reflection.MethodImplAttributes]::PreserveSig)
+ $null = $builder.CreateType()
+} catch {}
+# Outside the try, because CreateType can publish the type and then throw on the way
+# back, and a published type works. The parent recovers from exactly that; a check
+# inside the try answered no for a machine that had just succeeded.
+# One line, and no closing brace in column 0: this body sits inside a here-string that
+# starts at column 0 in both entrypoints, and the tests extract a function by finding the
+# first line that is exactly its closing brace. A block here ends the extraction early.
+if ('UnslothStudioEmitProbe' -as [type]) { Write-Output ('STUDIO_EMIT_OK ' + [string]$ExecutionContext.SessionState.LanguageMode); exit 0 }
+exit 1
+'@
+ # This host, not a guessed one: a 5.1 answer does not carry to pwsh or the other
+ # way, and the emit that matters is the one this interpreter will make. Both
+ # spellings of the leaf, so the function is the same one a non-Windows lane can
+ # execute end to end rather than a Windows-only path nothing tests.
+ $hostExe = $HostPath
+ if (-not $hostExe) {
+ try {
+ $leaves = if ($PSVersionTable.PSEdition -eq "Core") { @("pwsh.exe", "pwsh") }
+ else { @("powershell.exe", "powershell") }
+ foreach ($leaf in $leaves) {
+ $candidate = Join-Path $PSHOME $leaf
+ if (Test-Path -LiteralPath $candidate) { $hostExe = $candidate; break }
+ }
+ } catch {}
+ }
+ if (-not $hostExe) { return $false }
+ # Through a Process object rather than the call operator, for a deadline. The call
+ # operator waits for the child forever, and "forever" is reachable: a security
+ # product inspecting a freshly spawned interpreter, a wedged runtime start, a child
+ # that blocks on shutdown. A probe that exists to keep the installer alive must not
+ # be the thing that hangs it.
+ #
+ # BOTH streams are redirected and drained asynchronously. Draining is what stops a
+ # chatty child filling a pipe and deadlocking against the wait. Redirecting stderr as
+ # well is the difference between a probe that is invisible and one that can write
+ # into the installer's own stderr, which the desktop app reads, and which anything
+ # the child spawns would inherit and hold open.
+ #
+ $info = New-Object System.Diagnostics.ProcessStartInfo
+ $info.FileName = $hostExe
+ $info.Arguments = "-NoProfile -NonInteractive -Command `"$probe`""
+ $info.UseShellExecute = $false
+ $info.RedirectStandardOutput = $true
+ $info.RedirectStandardError = $true
+ $info.CreateNoWindow = $true
+ $child = $null
+ try {
+ $child = [System.Diagnostics.Process]::Start($info)
+ $reader = $child.StandardOutput.ReadToEndAsync()
+ $null = $child.StandardError.ReadToEndAsync()
+ if (-not $child.WaitForExit(20000)) {
+ try { $child.Kill() } catch {}
+ return $false
+ }
+ # Exit code AND an exact record. A marker followed by a crash is a crash: the
+ # question is whether this machine can emit and live, and a child that printed
+ # and then died has answered no. FullLanguage because an approved script can run
+ # in FullLanguage while a fresh inline command does not, and a child restricted
+ # differently from its parent has measured a different machine.
+ if ($child.ExitCode -ne 0) {
+ $script:StudioEmitProbeOutcome = "blocked"
+ return $false
+ }
+ $lines = ($reader.GetAwaiter().GetResult() -split "`r?`n")
+ foreach ($line in $lines) {
+ if ($line.Trim() -eq "STUDIO_EMIT_OK FullLanguage") {
+ $script:StudioEmitProbeOutcome = "ok"
+ return $true
+ }
+ # Emitted, but in a language mode this parent is not in. The child measured a
+ # different machine, which is an answer rather than a missed one.
+ if ($line.Trim() -like "STUDIO_EMIT_OK *") {
+ $script:StudioEmitProbeOutcome = "blocked"
+ return $false
+ }
+ }
+ # Exit 0 with no marker at all: the child cannot have got past the emit and then
+ # reported nothing, so its output was lost rather than negative.
+ return $false
+ } catch {
+ return $false
+ } finally {
+ if ($child) {
+ # The read end goes first. A killed child can leave a grandchild holding the
+ # write end of that pipe, and the pending async read then keeps this process
+ # alive past the deadline it just enforced.
+ try { $child.StandardOutput.Close() } catch {}
+ try { $child.StandardError.Close() } catch {}
+ try { $child.Dispose() } catch {}
+ }
+ }
+}
+
+function New-StudioDynamicAssembly {
+ <#
+ Both spellings of "define a dynamic assembly", because the two PowerShell hosts that
+ run this file are on different runtimes. The static
+ AssemblyBuilder::DefineDynamicAssembly is documented for .NET Framework 4.5 through
+ 4.8.1 as well as .NET Core, so Windows PowerShell 5.1 should take the first branch. It
+ is tried rather than assumed because nothing here can test a .NET Framework host, and
+ getting it wrong is not a visible error: the catch would cache the thunk as unavailable
+ and every install would silently lose it.
+
+ AppDomain.CurrentDomain.DefineDynamicAssembly is the .NET Framework spelling and is
+ absent on .NET Core, so it is the fallback rather than the first try.
+ #>
+ param([Parameter(Mandatory = $true)][System.Reflection.AssemblyName]$AssemblyName)
+ $access = [System.Reflection.Emit.AssemblyBuilderAccess]::Run
+ try {
+ return [System.Reflection.Emit.AssemblyBuilder]::DefineDynamicAssembly(
+ $AssemblyName, $access)
+ } catch [System.Management.Automation.MethodException] {
+ return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)
+ } catch [System.Management.Automation.RuntimeException] {
+ # A missing static surfaces as a RuntimeException on some hosts rather than a
+ # MethodException. Both mean "no such method here", and a real emit failure throws
+ # from the AppDomain call too, so the caller still sees it.
+ return [AppDomain]::CurrentDomain.DefineDynamicAssembly($AssemblyName, $access)
+ }
+}
+
+function New-StudioEmittedNativeType {
+ param(
+ [Parameter(Mandatory = $true)][string]$TypeName,
+ [Parameter(Mandatory = $true)][object[]]$Imports
+ )
+ $assemblyName = New-Object System.Reflection.AssemblyName $TypeName
+ $assembly = New-StudioDynamicAssembly -AssemblyName $assemblyName
+ $module = $assembly.DefineDynamicModule($TypeName)
+ $builder = $module.DefineType(
+ $TypeName, "Public, Class, AutoClass, AnsiClass, BeforeFieldInit")
+
+ $winapi = [System.Runtime.InteropServices.CallingConvention]::Winapi
+ # Per import, because CharSet selects name mangling as well as marshalling: Unicode probes
+ # <Name>W before <Name>, Ansi probes <Name> before <Name>A. Declaring the one the C# this
+ # replaces declared keeps the metadata honest and puts the export that does exist first.
+ $unicode = [System.Runtime.InteropServices.CharSet]::Unicode
+ $ansi = [System.Runtime.InteropServices.CharSet]::Ansi
+ $standard = [System.Reflection.CallingConventions]::Standard
+ $attributes = "Public, Static, HideBySig, PinvokeImpl"
+ $preserveSig = [System.Reflection.MethodImplAttributes]::PreserveSig
+
+ foreach ($import in $Imports) {
+ $charSet = if ($import.ContainsKey("Ansi") -and $import.Ansi) { $ansi } else { $unicode }
+ $method = $builder.DefinePInvokeMethod(
+ $import.Name, $import.Library, $import.Name, $attributes,
+ $standard, $import.Return, $import.Args, $winapi, $charSet)
+ $method.SetImplementationFlags(
+ $method.GetMethodImplementationFlags() -bor $preserveSig)
+ # `out uint` in the C# this replaces, and DefinePInvokeMethod has no way to say
+ # so: a by-ref type alone emits `ref`, which is In and Out unset. The value is
+ # blittable and every caller initialises it first, so the marshaller pins and
+ # writes back either way, but the metadata is what a reader and any future
+ # marshalling change go by, so it says what the declaration said.
+ # ContainsKey, not a bare property read: most imports have no Out key and reading a
+ # missing one is fatal under a caller's Set-StrictMode. setup.ps1 sets none of its own.
+ if ($import.ContainsKey("Out")) {
+ foreach ($position in @($import.Out)) {
+ if ($position) { $null = $method.DefineParameter($position, "Out", $null) }
+ }
+ }
+ }
+ $null = $builder.CreateType()
+ return $null -ne ($TypeName -as [type])
+}
+
function Enable-StudioVirtualTerminal {
if ($env:NO_COLOR) { return $false }
# A redirected stdout is not a console and GetConsoleMode fails on a non-console handle, so the
- # block below could only return $false anyway. Answer without Add-Type, which runs csc.exe and
- # drops source in %TEMP%. The CLI and the desktop app both pipe us, so this is the path the
- # compile was on.
+ # block below could only return $false anyway. The CLI and the desktop app both pipe us, so
+ # that is the path they are on.
if ($script:StudioStdoutRedirected) { return $false }
+ # The published type first, the gate only if there is nothing published. A type
+ # this session already emitted is proof that emit works here, and the compiled
+ # version checked for it in the same order; asking a child instead means one
+ # failed probe throws away a console helper that is already loaded and usable.
+ if (-not ("StudioVTNative" -as [type]) -and -not (Test-StudioCanDefineNativeTypes)) {
+ return $false
+ }
try {
- Add-Type -Namespace StudioVT -Name Native -MemberDefinition @'
-[DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int nStdHandle);
-[DllImport("kernel32.dll")] public static extern bool GetConsoleMode(IntPtr h, out uint m);
-[DllImport("kernel32.dll")] public static extern bool SetConsoleMode(IntPtr h, uint m);
-'@ -ErrorAction Stop
- $h = [StudioVT.Native]::GetStdHandle(-11)
+ if (-not ("StudioVTNative" -as [type])) {
+ $null = New-StudioEmittedNativeType -TypeName "StudioVTNative" -Imports @(
+ @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]
+ Args = @([int])
+ Ansi = $true },
+ @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32].MakeByRefType())
+ Ansi = $true
+ Out = @(2) },
+ @{ Name = "SetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32])
+ Ansi = $true }
+ )
+ }
+ $h = [StudioVTNative]::GetStdHandle(-11)
[uint32]$mode = 0
- if (-not [StudioVT.Native]::GetConsoleMode($h, [ref]$mode)) { return $false }
+ if (-not [StudioVTNative]::GetConsoleMode($h, [ref]$mode)) { return $false }
$mode = $mode -bor 0x0004
- return [StudioVT.Native]::SetConsoleMode($h, $mode)
+ return [StudioVTNative]::SetConsoleMode($h, $mode)
} catch {
return $false
}
diff --git a/tests/python/test_windows_installer_concurrency_guard.py b/tests/python/test_windows_installer_concurrency_guard.py
index 7e4ba220bb8..184de5aa638 100644
--- a/tests/python/test_windows_installer_concurrency_guard.py
+++ b/tests/python/test_windows_installer_concurrency_guard.py
@@ -43,7 +43,10 @@ def _run_powershell(shell: str, script: str, env: dict[str, str]) -> str:
Path(name).write_text(script, encoding = "utf-8-sig")
result = subprocess.run(
[shell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", name],
- check = True,
+ # check = False, then asserted below with the output attached. With check = True the
+ # only thing a failing run reports is "exit status 1" and the PowerShell error that
+ # caused it is discarded, which on a CI runner is the whole diagnosis gone.
+ check = False,
capture_output = True,
text = True,
# utf-8 with replacement: cp1252 cannot decode what PowerShell writes.
@@ -57,6 +60,10 @@ def _run_powershell(shell: str, script: str, env: dict[str, str]) -> str:
os.unlink(name)
except OSError:
pass
+ assert result.returncode == 0, (
+ f"{shell} exited {result.returncode}\n"
+ f"--- stdout ---\n{result.stdout}\n--- stderr ---\n{result.stderr}"
+ )
return result.stdout.strip()
@@ -76,7 +83,12 @@ _FINAL_PATH_CHAIN = (
"New-StudioPrivateTempDirectory",
"Initialize-StudioTempEnvironment",
"Write-StudioFinalPathDegraded",
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
"Initialize-StudioFinalPathNativeType",
+ "Get-StudioNativeFinalPath",
"Resolve-StudioLinkTarget",
"Get-StudioSubstTarget",
"Get-StudioLexicalPath",
@@ -107,6 +119,11 @@ def _mutex_helpers(source: str) -> str:
"Initialize-StudioTempEnvironment",
"Write-StudioFinalPathDegraded",
"Initialize-StudioFinalPathNativeType",
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
+ "Get-StudioNativeFinalPath",
"Resolve-StudioLinkTarget",
"Get-StudioSubstTarget",
"Get-StudioLexicalPath",
@@ -139,12 +156,19 @@ def _process_helpers(source: str) -> str:
"Initialize-StudioTempEnvironment",
"Write-StudioFinalPathDegraded",
"Initialize-StudioFinalPathNativeType",
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
+ "Get-StudioNativeFinalPath",
"Resolve-StudioLinkTarget",
"Get-StudioSubstTarget",
"Get-StudioLexicalPath",
"Resolve-StudioFinalPathInfo",
"Get-StudioFinalPath",
"Test-StudioProtectedPathMatch",
+ "Initialize-StudioProcessImageNativeType",
+ "Get-StudioNativeProcessImagePath",
"Get-StudioProcessImagePath",
"Get-RunningStudioVenvProcesses",
)
@@ -346,9 +370,9 @@ public static class UnslothStudioFinalPath
}}
'@
{final_path_helper}
-Get-StudioFinalPath -Path $env:SystemRoot | Out-Null
-Write-Output ([bool]("UnslothStudioFinalPathV2" -as [type]))
-Write-Output ([bool]([UnslothStudioFinalPathV2]::GetProcessImagePath($PID)))
+$resolved = Get-StudioFinalPath -Path $env:SystemRoot
+Write-Output ([bool]("UnslothStudioFinalPathV3" -as [type]))
+Write-Output ([bool]($resolved -and (Test-Path -LiteralPath $resolved)))
"""
assert _run_powershell(shell, script, os.environ.copy()).splitlines() == ["True", "True"]
@@ -1067,8 +1091,8 @@ def test_tauri_start_install_rejects_backend_conflicts_before_spawn():
@pytest.mark.parametrize(
"helpers",
- [_mutex_helpers, _process_helpers],
- ids = ["mutex", "process"],
+ [_mutex_helpers, _process_helpers, _final_path_helpers],
+ ids = ["mutex", "process", "final-path"],
)
def test_the_extracted_helpers_can_call_everything_they_call(helpers):
"""Every installer function these harnesses reach must be in the harness: the scripts run under
diff --git a/tests/python/test_windows_installer_addtype_fallback.py b/tests/python/test_windows_installer_native_type_fallback.py
similarity index 62%
rename from tests/python/test_windows_installer_addtype_fallback.py
rename to tests/python/test_windows_installer_native_type_fallback.py
index dbe7bd8cc1e..1493691f51b 100644
--- a/tests/python/test_windows_installer_addtype_fallback.py
+++ b/tests/python/test_windows_installer_native_type_fallback.py
@@ -1,19 +1,28 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-"""The installer must survive a host where Add-Type cannot compile.
-
-install.ps1 resolves path identity through a C# helper it compiles at runtime.
-Windows PowerShell 5.1 -- the interpreter studio/src-tauri/src/install.rs spawns
--- compiles by writing the source into %TEMP% and running csc.exe, so a %TEMP%
-that cannot hold a file (or a scanner that eats what was just written there)
-makes Add-Type throw CS2001. That exception used to travel up Get-StudioPathHash
-and end a first launch as "Could not create the Unsloth install lock" (#9140),
-with nothing about the message pointing at the compiler.
-
-These run under pwsh on any platform: the failure being guarded is that a thrown
-Add-Type aborts the install, and that is shell-independent. Windows PowerShell
-5.1's CodeDom behaviour itself is exercised by the Windows-only tests in
+"""The installer must survive a host where the native path helper cannot be built.
+
+install.ps1 resolves path identity through three kernel32 imports. It used to get
+them by compiling C#, and Windows PowerShell 5.1 -- the interpreter
+studio/src-tauri/src/install.rs spawns -- compiles by writing the source into
+%TEMP% and running csc.exe. A %TEMP% that could not hold a file made Add-Type
+throw CS2001, and that exception travelled up Get-StudioPathHash to end a first
+launch as "Could not create the Unsloth install lock" (#9140). Behavioural
+antivirus then blocked the DLL the compiler produced, which is a failure no
+retry can route around, so the imports are emitted with DefinePInvokeMethod now
+and no compiler runs at all.
+
+Both halves are still guarded here. That no compiler is reachable is the first
+half, and this file asserts it by sabotaging Add-Type and showing nothing changes.
+That a host where even the emit fails still installs is the second, and it is the
+same fallback the compile failure used to land on: a lexical answer, Exact false,
+and a lock that is still acquired.
+
+These run under pwsh on any platform. Emit succeeds here while kernel32 does not
+resolve, so the type builds exactly as it does on 5.1 and every call through it
+fails, which covers the build side and the call side separately. Windows
+PowerShell 5.1 behaviour itself is exercised by the Windows-only tests in
test_windows_installer_concurrency_guard.py.
"""
@@ -36,11 +45,18 @@ INSTALL_PS1 = REPO_ROOT / "install.ps1"
requires_pwsh = pytest.mark.skipif(shutil.which("pwsh") is None, reason = "PowerShell is unavailable")
-# Stands in for every Add-Type in install.ps1 that compiles C# rather than loading an assembly.
+# A compiler that always fails. install.ps1 no longer calls Add-Type at all, so this
+# now proves the absence rather than exercising a fallback: every test that installs
+# it must behave exactly as it does without it.
SABOTAGE = (
"""function Add-Type { throw "(0) : error CS2001: Source file 'a.0.cs' could not be found" }"""
)
+# What a host that cannot emit looks like. Constrained Language Mode and App
+# Control's Dynamic Code Security both land here, and Test-StudioCanDefineNativeTypes
+# is the gate they come through, so overriding it is the whole of "no native side".
+NO_NATIVE = """function Test-StudioCanDefineNativeTypes { return $false }"""
+
def _extract(pattern: str, source: str) -> str:
match = re.search(pattern, source, flags = re.DOTALL)
@@ -63,7 +79,12 @@ LOCK_CHAIN = (
"Initialize-StudioTempEnvironment",
"Restore-StudioTempEnvironment",
"Write-StudioFinalPathDegraded",
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
"Initialize-StudioFinalPathNativeType",
+ "Get-StudioNativeFinalPath",
"Resolve-StudioLinkTarget",
"Get-StudioSubstTarget",
"Get-StudioLexicalPath",
@@ -131,19 +152,26 @@ def _lines(result: subprocess.CompletedProcess, prefix: str) -> list[str]:
@requires_pwsh
-def test_install_lock_is_acquired_when_the_compiler_fails(tmp_path: Path):
- """The bug, exactly: a throwing Add-Type must not cost the install its lock."""
+def test_install_lock_is_acquired_when_the_native_helper_is_unavailable(tmp_path: Path):
+ """The bug from #9140, at the rung it lives on now.
+
+ It was a throwing Add-Type then and it is a host that cannot define a type at
+ all now, which is the same thing to every caller: no exact answer. The lock
+ must still be acquired, and the reason said once.
+ """
studio_home = tmp_path / "studio"
studio_home.mkdir()
result = _run_powershell(
_script(
f"""
+{NO_NATIVE}
$name = Get-StudioInstallMutexName -Path '{studio_home}'
Write-Output "NAME:$name"
$mutex = Enter-StudioInstallMutex -Path '{studio_home}'
Write-Output "LOCK:$($null -ne $mutex)"
Exit-StudioInstallMutex -Mutex $mutex
-"""
+""",
+ sabotage = False,
)
)
assert result.returncode == 0, result.stderr
@@ -151,14 +179,43 @@ Exit-StudioInstallMutex -Mutex $mutex
assert re.fullmatch(
r"NAME:Global\\UnslothStudioInstall-[0-9a-f]{64}", _lines(result, "NAME:")[0]
)
- # And it says why it degraded, once, without dumping the C# it tried to build.
+ # And it says why it degraded, once, without dumping anything it tried to build.
warnings = [line for line in result.stdout.splitlines() if "native path resolver" in line]
assert len(warnings) == 1
assert "using System" not in result.stdout
@requires_pwsh
-def test_the_compiler_is_not_retried_for_every_path(tmp_path: Path):
+def test_a_dead_compiler_is_not_something_the_installer_can_notice(tmp_path: Path):
+ """The other half, and the reason the file is named for a fallback it no longer needs.
+
+ install.ps1 reaches no compiler, so replacing Add-Type with one that always throws
+ has to change nothing at all: the lock is taken, the native side is built anyway,
+ and no degraded warning is printed. A failure here means a compile came back.
+ """
+ studio_home = tmp_path / "studio"
+ studio_home.mkdir()
+ result = _run_powershell(
+ _script(
+ f"""
+$mutex = Enter-StudioInstallMutex -Path '{studio_home}'
+Write-Output "LOCK:$($null -ne $mutex)"
+Exit-StudioInstallMutex -Mutex $mutex
+Write-Output "TYPE:$([bool]("UnslothStudioFinalPathV3" -as [type]))"
+"""
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "LOCK:") == ["LOCK:True"]
+ assert _lines(result, "TYPE:") == ["TYPE:True"]
+ assert not [line for line in result.stdout.splitlines() if "native path resolver" in line]
+
+
+@requires_pwsh
+def test_the_compiler_is_never_called_however_many_paths_are_resolved(tmp_path: Path):
+ """Zero, not "few". The count used to be two, one attempt and one retry under a
+ private %TEMP%, and both of those launched csc.exe, which is the chain behavioural
+ antivirus blocked."""
result = _run_powershell(
_script(
f"""
@@ -174,17 +231,44 @@ Write-Output "CALLS:$global:AddTypeCalls"
)
)
assert result.returncode == 0, result.stderr
- # One attempt, one retry with a private %TEMP%, then cached: the scan below resolves a path per running process.
- assert _lines(result, "CALLS:") == ["CALLS:2"]
+ assert _lines(result, "CALLS:") == ["CALLS:0"]
+
+
+@requires_pwsh
+def test_the_type_is_built_once_however_many_paths_are_resolved(tmp_path: Path):
+ """The caching the retry count used to stand in for. The install scan resolves a
+ path per running process, so building the type per call would be paid dozens of
+ times."""
+ result = _run_powershell(
+ _script(
+ f"""
+$global:Emits = 0
+# A scriptblock copy, not Get-Item function:. A FunctionInfo re-resolves by NAME when
+# it is invoked, so calling it from the replacement calls the replacement.
+$global:RealEmit = ${{function:New-StudioEmittedNativeType}}
+function New-StudioEmittedNativeType {{
+ param([string]$TypeName, [object[]]$Imports)
+ $global:Emits++
+ & $global:RealEmit -TypeName $TypeName -Imports $Imports
+}}
+foreach ($i in 1..5) {{ Get-StudioFinalPath -Path '{tmp_path}' | Out-Null }}
+Write-Output "EMITS:$global:Emits"
+""",
+ sabotage = False,
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "EMITS:") == ["EMITS:1"]
@requires_pwsh
-def test_the_private_temp_retry_recovers_the_native_helper(tmp_path: Path):
- """Surviving a dead compiler is the floor. The retry has to actually work.
+def test_the_native_helper_is_built_with_no_usable_temp_at_all(tmp_path: Path):
+ """What replaced the private-%TEMP% retry: nothing, because nothing needs %TEMP%.
- Add-Type is replaced by a stub that behaves the way 5.1's CodeDom does: it
- writes the source into %TEMP% and fails when that cannot hold a file, and it
- calls the real cmdlet once it can, so the type genuinely gets defined.
+ The retry existed because compiling wrote a source file there and a %TEMP% that
+ could not hold one failed the install. Emit writes nothing anywhere, so the same
+ unusable %TEMP% that used to need a retry is now simply irrelevant, and the
+ variables are handed back exactly as they arrived.
"""
blocker = tmp_path / "blocker"
blocker.write_text("not a directory", encoding = "utf-8")
@@ -201,25 +285,11 @@ def test_the_private_temp_retry_recovers_the_native_helper(tmp_path: Path):
result = _run_powershell(
_script(
f"""
-$script:RealAddType = Get-Command Add-Type -CommandType Cmdlet
-$global:Attempts = 0
-function Add-Type {{
- param([string]$TypeDefinition, [string]$ErrorAction)
- $global:Attempts++
- $probe = Join-Path $env:TMP ("codedom-" + [guid]::NewGuid().ToString('N').Substring(0, 8) + ".0.cs")
- try {{
- [System.IO.File]::WriteAllText($probe, $TypeDefinition)
- Remove-Item -LiteralPath $probe -Force -ErrorAction SilentlyContinue
- }} catch {{
- throw "(0) : error CS2001: Source file '$probe' could not be found"
- }}
- & $script:RealAddType -TypeDefinition $TypeDefinition -ErrorAction Stop
-}}
-# Skip the session-wide temp fix, so the RETRY is the only thing that can save it.
+# Skip the session-wide temp fix, so nothing but the emit itself can save this.
$script:StudioTempChecked = $true
$info = Resolve-StudioFinalPathInfo -Path '{studio_home}'
-Write-Output "ATTEMPTS:$global:Attempts"
-Write-Output "LOADED:$([bool]("UnslothStudioFinalPathV2" -as [type]))"
+Write-Output "LOADED:$([bool]("UnslothStudioFinalPathV3" -as [type]))"
+Write-Output "INMEMORY:$([string]::IsNullOrEmpty(("UnslothStudioFinalPathV3" -as [type]).Assembly.Location))"
Write-Output "PATH:$($info.Path)"
Write-Output "TMP:$env:TMP"
Write-Output "TEMP:$env:TEMP"
@@ -229,15 +299,15 @@ Write-Output "TEMP:$env:TEMP"
env = env,
)
assert result.returncode == 0, result.stderr
- assert _lines(result, "ATTEMPTS:") == ["ATTEMPTS:2"]
assert _lines(result, "LOADED:") == ["LOADED:True"]
+ assert _lines(result, "INMEMORY:") == ["INMEMORY:True"]
assert not [line for line in result.stdout.splitlines() if "native path resolver" in line]
- # Restored exactly, broken values and all: they are the caller's, not ours.
+ # Handed back exactly, broken values and all: they are the caller's, not ours.
assert _lines(result, "TMP:") == [f"TMP:{dead}"]
assert _lines(result, "TEMP:") == [f"TEMP:{dead}"]
assert list((local_app_data / "Unsloth Studio" / "temp").glob("ust-*")) == []
- # The P/Invoke targets kernel32, so off Windows the type loads but the CALL fails; it has to degrade to a usable
- # answer rather than throw.
+ # The imports target kernel32, so off Windows the type builds and the CALL fails; it
+ # has to degrade to a usable answer rather than throw.
assert _lines(result, "PATH:")[0].startswith("PATH:")
assert _lines(result, "PATH:") != ["PATH:"]
@@ -293,6 +363,11 @@ Write-Output "PATH:$(Get-StudioFinalPath -Path '{left}')"
@requires_pwsh
def test_unequal_paths_are_unknown_rather_than_different_when_inexact(tmp_path: Path):
+ """NO_NATIVE, not the Add-Type sabotage: the installer no longer calls Add-Type, so on a real
+ Windows host the emit succeeds, the answer is exact, and two different directories compare
+ $false. The case this test is about is the one with no exact answer, and the only way to that
+ case now is the capability gate.
+ """
left = tmp_path / "left"
right = tmp_path / "right"
left.mkdir()
@@ -300,6 +375,7 @@ def test_unequal_paths_are_unknown_rather_than_different_when_inexact(tmp_path:
result = _run_powershell(
_script(
f"""
+{NO_NATIVE}
$answer = Test-StudioPathEqual -Left '{left}' -Right '{right}'
Write-Output "NULL:$($null -eq $answer)"
"""
@@ -467,19 +543,23 @@ def test_link_targets_of_every_windows_powershell_5_1_shape(
assert _same_path(got[0][len("TARGET:[") : -1], expected)
-def _dead_pid() -> int:
+def _dead_pid(start: int = 4_000_000) -> int:
"""A PID that is not running, so a ust-<pid>- directory reads as abandoned."""
- for candidate in range(4_000_000, 4_000_400):
+ for candidate in range(start, start + 400):
try:
os.kill(candidate, 0)
except ProcessLookupError:
return candidate
except (OSError, PermissionError):
continue
- return 4_000_000
+ return start
_DEAD_PID = _dead_pid()
+# A second one, so a test can put two abandoned directories side by side. It cannot reuse
+# _DEAD_PID with a different hex spelling: NTFS and the default macOS filesystem are
+# case-insensitive, so the two names would be one directory and the second mkdir would raise.
+_OTHER_DEAD_PID = _dead_pid(_DEAD_PID + 1)
@requires_pwsh
@@ -1009,10 +1089,11 @@ def test_the_sweep_only_takes_directories_the_allocator_could_have_made(tmp_path
(ours / "scratch.bin").write_text("x", encoding = "utf-8")
keep = []
# Case-insensitively ours: Windows filenames are case-insensitive, so refusing the uppercase spelling would leak a
- # directory we created.
- upper = root / f"ust-{_DEAD_PID}-ABCDEF01"
+ # directory we created. A second dead PID, because on Windows and macOS the same PID with the other hex spelling is
+ # the same directory and the mkdir below would raise FileExistsError before the sweep ever ran.
+ upper = root / f"ust-{_OTHER_DEAD_PID}-ABCDEF01"
upper.mkdir()
- (upper / "owner.pid").write_text(str(_DEAD_PID), encoding = "utf-8")
+ (upper / "owner.pid").write_text(str(_OTHER_DEAD_PID), encoding = "utf-8")
for name in ("ust-legacy", "ust-user-cache", "ust-notapid-abcdef01", "ust-", "ust-12-abcdefg1"):
victim = root / name
victim.mkdir()
@@ -1050,17 +1131,28 @@ def test_a_native_resolver_that_throws_says_so_once(tmp_path: Path):
_script(
f"""
# The helper is "available" and throws anyway, which is what a rename between
-# the Test-Path walk and CreateFileW looks like.
+# the Test-Path walk and CreateFileW looks like. The stub has to be the type the
+# resolver actually calls, and it has to count its calls: a stub the resolver
+# never reaches produces the same outward fallback as one that threw, so without
+# the counter this test passes while proving nothing. Add-Type here is the test's
+# own scaffolding; install.ps1 does not call it.
function Initialize-StudioFinalPathNativeType {{ return $true }}
Add-Type -TypeDefinition @'
-public class UnslothStudioFinalPathV2 {{
- public static string Resolve(string path) {{ throw new System.Exception("access is denied"); }}
+public class UnslothStudioFinalPathV3 {{
+ public static int Calls = 0;
+ public static System.IntPtr CreateFileW(
+ string path, uint access, uint share, System.IntPtr security,
+ uint disposition, uint flags, System.IntPtr template) {{
+ Calls++;
+ throw new System.Exception("access is denied");
+ }}
}}
'@
foreach ($i in 1..3) {{ $null = Resolve-StudioFinalPathInfo -Path '{studio}' }}
$info = Resolve-StudioFinalPathInfo -Path '{studio}'
Write-Output "EXACT:$($info.Exact)"
Write-Output "PATH:$($info.Path)"
+Write-Output "CALLS:$([UnslothStudioFinalPathV3]::Calls)"
""",
sabotage = False,
)
@@ -1070,6 +1162,9 @@ Write-Output "PATH:$($info.Path)"
assert _lines(result, "PATH:")[0].endswith("studio")
warnings = [line for line in result.stdout.splitlines() if "native helper; continuing" in line]
assert len(warnings) == 1, warnings
+ # The throwing resolver was really reached, four times, rather than the whole thing
+ # falling back because the type was absent.
+ assert _lines(result, "CALLS:") == ["CALLS:4"]
@requires_pwsh
@@ -1610,3 +1705,588 @@ def test_the_private_temp_removal_only_takes_what_it_created(tmp_path: Path):
assert (temp / "somebody-elses").is_dir()
assert temp.is_dir()
assert (data / "studio.port").exists()
+
+
+SETUP_PS1 = REPO_ROOT / "studio" / "setup.ps1"
+
+
+def _one_function(source: str, name: str) -> str:
+ match = re.search(
+ rf"^(?P<indent>\s*)function {name} \{{.*?\n(?P=indent)\}}\n",
+ source,
+ flags = re.DOTALL | re.MULTILINE,
+ )
+ assert match is not None, f"{name} not found"
+ return match.group(0)
+
+
+def _gate(source: str) -> str:
+ """The capability gate plus the child probe it delegates to, from either script."""
+ return "\n".join(
+ _one_function(source, name)
+ for name in ("Test-StudioCanDefineNativeTypes", "Test-StudioEmitInChildProcess")
+ )
+
+
+# Status 0 is "no policy", and it answers without spawning anything. Anything else is a
+# policy whose OPTIONS decide the answer, and Win32_DeviceGuard does not report them:
+# option 19 Dynamic Code Security always blocks unsigned System.Reflection.Emit assemblies
+# and is enforced even in an audit policy before Windows 11 24H2, while an audit policy
+# without it emits perfectly well. So the gate asks a child process rather than guessing,
+# and what these assert is that it delegates rather than deciding.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
[email protected]("status", ["1", "2"])
[email protected]("probe,expected", [("$true", "True"), ("$false", "False")])
+def test_an_active_policy_is_decided_by_the_child_probe(
+ script: str, status: str, probe: str, expected: str
+):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _gate(source),
+ # Every parameter the real call passes has to bind, or the stub is skipped and
+ # the test measures the absent-provider path instead of the reported status.
+ "function Get-CimInstance {",
+ " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",
+ f" [pscustomobject]@{{ UsermodeCodeIntegrityPolicyEnforcementStatus = {status} }}",
+ "}",
+ "$script:ProbeCalls = 0",
+ f"function Test-StudioEmitInChildProcess {{ $script:ProbeCalls++; return {probe} }}",
+ 'Write-Output "CAN:$(Test-StudioCanDefineNativeTypes)"',
+ 'Write-Output "CALLS:$script:ProbeCalls"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "CAN:") == [f"CAN:{expected}"]
+ assert _lines(result, "CALLS:") == ["CALLS:1"]
+
+
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+def test_no_policy_answers_without_spawning_a_probe(script: str):
+ """The probe costs a process. A machine with no policy is the overwhelming majority and
+ must not pay for it."""
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _gate(source),
+ "function Get-CimInstance {",
+ " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",
+ " [pscustomobject]@{ UsermodeCodeIntegrityPolicyEnforcementStatus = 0 }",
+ "}",
+ "$script:ProbeCalls = 0",
+ "function Test-StudioEmitInChildProcess { $script:ProbeCalls++; return $false }",
+ 'Write-Output "CAN:$(Test-StudioCanDefineNativeTypes)"',
+ 'Write-Output "CALLS:$script:ProbeCalls"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "CAN:") == ["CAN:True"]
+ assert _lines(result, "CALLS:") == ["CALLS:0"]
+
+
+# The runtime test below catches this by executing it, but only on a host where emit
+# succeeds. This one is the invariant itself, and it is the one a future edit trips: adding
+# a single double quote to the probe body is enough, and nothing about the resulting failure
+# points back at the quote.
[email protected]("script", ["install", "setup"])
+def test_the_probe_body_carries_no_double_quote(script: str):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ body = re.search(r"\$probe = @'\n(.*?)\n'@", source, flags = re.DOTALL)
+ assert body is not None, "the probe here-string is gone"
+ assert '"' not in body.group(1), (
+ "the emit probe body gained a double quote. Windows PowerShell 5.1 appends a native "
+ "argument verbatim inside its own double quotes, so the first one here closes the "
+ "wrapper and the child runs something else entirely, silently answering no."
+ )
+
+
+# The acceptance rules, one child at a time. $PSHOME is an ordinary variable, so a fake host
+# under a temporary one lets each rule be exercised without a policy, a Windows box or luck.
+# Every case here is a way a real child can answer badly: it printed the marker and then died,
+# it ran in a language mode its parent did not, it printed something that merely CONTAINS the
+# marker, or it never returned at all.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+ "emits,code,expected,outcome,label",
+ [
+ ("STUDIO_EMIT_OK FullLanguage", 0, "True", "ok", "the good case"),
+ ("STUDIO_EMIT_OK FullLanguage", 23, "False", "blocked", "marker then a bad exit"),
+ (
+ "STUDIO_EMIT_OK ConstrainedLanguage",
+ 0,
+ "False",
+ "blocked",
+ "a child restricted differently",
+ ),
+ ("", 1, "False", "blocked", "a child that ran and refused"),
+ (
+ "NOT_STUDIO_EMIT_OK_FAILURE",
+ 0,
+ "False",
+ "indeterminate",
+ "a line that merely contains the marker",
+ ),
+ ("", 0, "False", "indeterminate", "silence"),
+ ],
+)
+def test_the_probe_only_accepts_a_clean_exact_answer(
+ script: str, emits: str, code: int, expected: str, outcome: str, label: str, tmp_path: Path
+):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ home = tmp_path / "fakehome"
+ home.mkdir()
+ fake = home / "pwsh"
+ fake.write_text(
+ "#!/bin/sh\n" + (f'echo "{emits}"\n' if emits else "") + f"exit {code}\n",
+ encoding = "utf-8",
+ )
+ fake.chmod(0o755)
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _one_function(source, "Test-StudioEmitInChildProcess"),
+ f"Write-Output \"ANSWER:$(Test-StudioEmitInChildProcess -HostPath '{fake}')\"",
+ 'Write-Output "OUTCOME:$script:StudioEmitProbeOutcome"',
+ ]
+ )
+ )
+ assert result.returncode == 0, f"{label}: {result.stderr}"
+ assert _lines(result, "ANSWER:") == [f"ANSWER:{expected}"], label
+ # And WHY, which is not the same fact as the boolean. A child that ran and refused is a
+ # machine that cannot emit; a child that never answered is a process that failed, and
+ # the gate retries only the second one and reports a different reason for it.
+ assert _lines(result, "OUTCOME:") == [f"OUTCOME:{outcome}"], label
+
+
+# One transient process failure used to be indistinguishable from a policy: cached for the
+# whole run, it sends the installer down the lexical path, where two unequal roots compare as
+# unknown and a second runtime lock is taken, which is how an unrelated install elsewhere on
+# the machine turns into "the managed Unsloth environment is busy". The compiled version this
+# replaces tried twice before caching a negative. A machine that really is blocked still pays
+# for exactly one probe, so the retry costs nothing where the answer was already real.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+ "outcome,calls",
+ [("indeterminate", "2"), ("blocked", "1")],
+ ids = ["never-answered-is-retried", "answered-no-is-not"],
+)
+def test_only_a_probe_that_never_answered_is_retried(script: str, outcome: str, calls: str):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ "$script:StudioCanDefineNativeTypes = $null",
+ "$script:StudioEmitProbeOutcome = $null",
+ "$script:ProbeCalls = 0",
+ # A stub, because the gate calls the real probe through $PSHOME, which is
+ # read-only and cannot be pointed at a fake host. What is under test here is
+ # the gate's retry rule, not the child.
+ "function Test-StudioEmitInChildProcess {",
+ " $script:ProbeCalls = $script:ProbeCalls + 1",
+ f' $script:StudioEmitProbeOutcome = "{outcome}"',
+ " return $false",
+ "}",
+ _one_function(source, "Test-StudioCanDefineNativeTypes"),
+ "$answer = Test-StudioCanDefineNativeTypes",
+ 'Write-Output "ANSWER:$answer"',
+ 'Write-Output "CALLS:$script:ProbeCalls"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr + result.stdout
+ assert _lines(result, "ANSWER:") == ["ANSWER:False"]
+ assert _lines(result, "CALLS:") == [f"CALLS:{calls}"]
+
+
+# The reason printed alongside the degradation. Naming a machine setting for what was really
+# a process that could not be spawned sends whoever reads the log looking for a policy that
+# is not there.
+@requires_pwsh
+ "outcome,expected",
+ [
+ ("blocked", "this host enforces user-mode code integrity"),
+ ("indeterminate", "a probe process could not confirm native type support"),
+ ],
+ ids = ["a-real-refusal", "a-probe-that-never-answered"],
+)
+def test_the_degradation_reason_says_what_was_actually_established(outcome: str, expected: str):
+ source = INSTALL_PS1.read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ "$script:StudioFinalPathNativeState = $null",
+ f'$script:StudioEmitProbeOutcome = "{outcome}"',
+ "function Test-StudioCanDefineNativeTypes { return $false }",
+ # Straight to the console, not Write-Output: the initializer's return value is
+ # discarded below, and that discards everything else on the success stream with
+ # it, including the warning this test is here to read.
+ "function Write-StudioLine { param($Message, $ForegroundColor)",
+ ' [Console]::Out.WriteLine("LINE:$Message") }',
+ _one_function(source, "Write-StudioFinalPathDegraded"),
+ _one_function(source, "Initialize-StudioFinalPathNativeType"),
+ "$null = Initialize-StudioFinalPathNativeType",
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr + result.stdout
+ warning = [line for line in _lines(result, "LINE:") if "native path resolver" in line]
+ assert warning, result.stdout
+ assert expected in warning[0], warning[0]
+
+
+# The compiled version carried the path helper and the process-image helper on ONE type, so
+# they could not disagree about whether this session can emit. Two types can: a session that
+# emitted the path type, was interrupted, and came back to a probe that now fails would keep
+# native path resolution and silently lose native process inspection, which is how a running
+# Studio stops being seen. A type already published here is stronger evidence than any child.
+@requires_pwsh
+def test_an_already_emitted_type_settles_it_without_asking_a_child():
+ source = INSTALL_PS1.read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ "$script:StudioProcessImageNativeState = $null",
+ "$script:GateCalls = 0",
+ "function Test-StudioCanDefineNativeTypes {",
+ " $script:GateCalls = $script:GateCalls + 1",
+ " return $false",
+ "}",
+ _one_function(source, "New-StudioDynamicAssembly"),
+ _one_function(source, "New-StudioEmittedNativeType"),
+ _one_function(source, "Initialize-StudioFinalPathNativeType"),
+ # The real path type, emitted the way a real run emits it, so what stands in
+ # for the interrupted session is the same evidence that session would leave.
+ "$script:StudioFinalPathNativeState = $null",
+ "function Write-StudioFinalPathDegraded { param($Reason) }",
+ "$null = New-StudioEmittedNativeType -TypeName 'UnslothStudioFinalPathV3' -Imports @(",
+ " @{ Name = 'CloseHandle'; Library = 'kernel32.dll'; Return = [bool]",
+ " Args = @([IntPtr]); Ansi = $true }",
+ ")",
+ "Write-Output \"PATHTYPE:$($null -ne ('UnslothStudioFinalPathV3' -as [type]))\"",
+ _one_function(source, "Initialize-StudioProcessImageNativeType"),
+ "$ok = Initialize-StudioProcessImageNativeType",
+ 'Write-Output "PROCESS:$ok"',
+ 'Write-Output "GATE:$script:GateCalls"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr + result.stdout
+ assert _lines(result, "PATHTYPE:") == ["PATHTYPE:True"], result.stdout
+ assert _lines(result, "PROCESS:") == ["PROCESS:True"], result.stdout
+ assert _lines(result, "GATE:") == ["GATE:0"], (
+ "the process-image helper asked a child whether emit works in a process that had "
+ "already emitted a type"
+ )
+
+
+# Same rule for the cosmetic helpers. The console one asked the gate before looking for its
+# type, so one failed probe threw away a console helper already loaded and working in this
+# very process. The compiled version checked the type first.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+def test_the_console_helper_keeps_a_type_it_already_has(script: str):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ "$env:NO_COLOR = $null",
+ "$script:StudioStdoutRedirected = $false",
+ "$script:GateCalls = 0",
+ "function Test-StudioCanDefineNativeTypes {",
+ " $script:GateCalls = $script:GateCalls + 1",
+ " return $false",
+ "}",
+ "function Get-StudioAnsi { param($Name) return '' }",
+ _one_function(source, "New-StudioDynamicAssembly"),
+ _one_function(source, "New-StudioEmittedNativeType"),
+ # Published exactly as a successful earlier call in the same session would
+ # have left it. The calls themselves fail off Windows and are caught; what
+ # is under test is whether the gate is consulted at all.
+ "$null = New-StudioEmittedNativeType -TypeName 'StudioVTNative' -Imports @(",
+ " @{ Name = 'GetStdHandle'; Library = 'kernel32.dll'; Return = [IntPtr]",
+ " Args = @([int]); Ansi = $true }",
+ ")",
+ _one_function(source, "Enable-StudioVirtualTerminal"),
+ "$null = Enable-StudioVirtualTerminal",
+ '[Console]::Out.WriteLine("GATE:$script:GateCalls")',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr + result.stdout
+ assert _lines(result, "GATE:") == [
+ "GATE:0"
+ ], "a published console type was discarded because a child probe said no"
+
+
+@requires_pwsh
+def test_a_child_that_never_returns_does_not_hang_the_installer(tmp_path: Path):
+ """The deadline. A probe that exists to keep the installer alive must not be the thing
+ that wedges it, and the call operator waits forever. Reachable in practice through a
+ security product inspecting a freshly spawned interpreter."""
+ source = INSTALL_PS1.read_text(encoding = "utf-8")
+ home = tmp_path / "fakehome"
+ home.mkdir()
+ fake = home / "pwsh"
+ fake.write_text("#!/bin/sh\nsleep 600\n", encoding = "utf-8")
+ fake.chmod(0o755)
+ # The wait is bounded in the script; this only has to outlast it.
+ started = time.monotonic()
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _one_function(source, "Test-StudioEmitInChildProcess"),
+ f"Write-Output \"ANSWER:$(Test-StudioEmitInChildProcess -HostPath '{fake}')\"",
+ ]
+ )
+ )
+ elapsed = time.monotonic() - started
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "ANSWER:") == ["ANSWER:False"]
+ assert elapsed < 60, f"the probe took {elapsed:.0f}s, so the deadline is not bounding it"
+
+
+# Legacy is not a curiosity: it is how Windows PowerShell 5.1 ALWAYS binds a native
+# command's arguments, and 5.1 is the interpreter studio/src-tauri/src/install.rs spawns.
+# It wraps the value in quotes and appends the body verbatim without escaping the quotes
+# inside it, so a probe passed with -Command arrives as
+# `if (UnslothStudioEmitProbe -as [type])`, a command lookup that throws into the probe's
+# own catch and answers "no emit here" on every 5.1 host. pwsh can be put into that exact
+# binder with $PSNativeCommandArgumentPassing, so the case is reachable from Linux.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
[email protected]("binding", ["Legacy", "Standard"])
+def test_the_child_probe_survives_the_5_1_argument_binder(script: str, binding: str):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ f'$PSNativeCommandArgumentPassing = "{binding}"',
+ _one_function(source, "Test-StudioEmitInChildProcess"),
+ "$answer = Test-StudioEmitInChildProcess",
+ 'Write-Output "TYPE:$($answer.GetType().Name)"',
+ 'Write-Output "ANSWER:$answer"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ # Boolean either way, since the gate calls this under ErrorActionPreference Stop.
+ assert _lines(result, "TYPE:") == ["TYPE:Boolean"]
+ # And True either way: emit works on this host, and the binder must not be able to
+ # turn a working host into a refusal.
+ assert _lines(result, "ANSWER:") == ["ANSWER:True"]
+
+
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+ "provider,label",
+ [
+ ('throw "no such namespace"', "the query throws"),
+ ("$null", "the query returns nothing"),
+ ("[pscustomobject]@{ Other = 1 }", "the object has no status property"),
+ ],
+)
+def test_an_unreadable_policy_asks_the_probe_rather_than_assuming(
+ script: str, provider: str, label: str
+):
+ """Unknown is not zero.
+
+ Treating an unreadable Device Guard as unrestricted lets exactly one case through: option
+ 19 enforced on a host whose CIM query happens to fail, where the documented outcome is a
+ stopped process. The cost of asking anyway is one short-lived child.
+ """
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _gate(source),
+ "function Get-CimInstance {",
+ " param([string]$Namespace, [string]$ClassName, [string]$ErrorAction)",
+ f" {provider}",
+ "}",
+ "$script:ProbeCalls = 0",
+ "function Test-StudioEmitInChildProcess { $script:ProbeCalls++; return $true }",
+ 'Write-Output "CAN:$(Test-StudioCanDefineNativeTypes)"',
+ 'Write-Output "CALLS:$script:ProbeCalls"',
+ ]
+ )
+ )
+ assert result.returncode == 0, f"{label}: {result.stderr}"
+ assert _lines(result, "CALLS:") == ["CALLS:1"], label
+ assert _lines(result, "CAN:") == ["CAN:True"], label
+
+
+# CharSet is not decoration on an emitted import: it picks the export the runtime looks for
+# first. Unicode asks for <Name>W, Ansi asks for <Name>. Every one of these names an export
+# that exists exactly as written, so the wrong charset still resolves, on the second probe;
+# what it stops being is an accurate description of the C# it replaced.
+@requires_pwsh
+ "method,charset",
+ [
+ ("CreateFileW", "Unicode"),
+ ("GetFinalPathNameByHandleW", "Unicode"),
+ ("CloseHandle", "Ansi"),
+ ],
+)
+def test_each_import_carries_the_charset_its_declaration_had(method: str, charset: str):
+ source = INSTALL_PS1.read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _helpers(
+ "Write-StudioLine",
+ "Write-StudioFinalPathDegraded",
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
+ "Initialize-StudioFinalPathNativeType",
+ ),
+ "$null = Initialize-StudioFinalPathNativeType",
+ f'$m = [UnslothStudioFinalPathV3].GetMethod("{method}")',
+ # The pseudo-custom attribute the runtime synthesises from the P/Invoke
+ # metadata, which is the metadata itself rather than a copy of the intent.
+ "$a = $m.GetCustomAttributes(",
+ " [System.Runtime.InteropServices.DllImportAttribute], $false)[0]",
+ 'Write-Output "CHARSET:$($a.CharSet)"',
+ 'Write-Output "ENTRY:$($a.EntryPoint)"',
+ 'Write-Output "LIB:$($a.Value)"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "CHARSET:") == [f"CHARSET:{charset}"]
+ assert _lines(result, "ENTRY:") == [f"ENTRY:{method}"]
+ assert _lines(result, "LIB:") == ["LIB:kernel32.dll"]
+
+
+# `out uint` in the C# this replaces. A by-ref type alone emits `ref`, which leaves both
+# In and Out unset, and DefinePInvokeMethod has no argument for it, so the emitter calls
+# DefineParameter. The value is blittable and the callers initialise it first, so writeback
+# works either way; what this pins is that the metadata says what the declaration said, and
+# that the Out key is wired through the emitter at all.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+def test_the_console_mode_parameter_is_still_declared_out(script: str):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ which = "StudioVTNative" if script == "setup" else "StudioVTNative"
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _one_function(source, "New-StudioDynamicAssembly"),
+ _one_function(source, "New-StudioEmittedNativeType"),
+ '$null = New-StudioEmittedNativeType -TypeName "UnslothOutParamProbe" -Imports @(',
+ ' @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]',
+ " Args = @([IntPtr], [uint32].MakeByRefType())",
+ " Ansi = $true",
+ " Out = @(2) }",
+ ")",
+ '$p = ([type]"UnslothOutParamProbe").GetMethod("GetConsoleMode").GetParameters()[1]',
+ 'Write-Output "OUT:$($p.IsOut)"',
+ 'Write-Output "BYREF:$($p.ParameterType.IsByRef)"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "OUT:") == ["OUT:True"]
+ assert _lines(result, "BYREF:") == ["BYREF:True"]
+ # And the real declaration in the script carries the key, not just the emitter's ability
+ # to honour it.
+ block = re.search(r'Name = "GetConsoleMode".*?\}', source, flags = re.DOTALL)
+ assert block is not None and "Out = @(2)" in block.group(
+ 0
+ ), "GetConsoleMode lost its Out position"
+
+
+# Almost every import declares no Out position, and reading a hashtable key that is not
+# there is fatal under Set-StrictMode -Version Latest, which raises PropertyNotFound rather
+# than returning $null. install.ps1 sets strict mode Off for itself, but setup.ps1 sets none
+# at all, so whatever the caller has is what runs, and a profile with strict mode on would
+# have taken out every emitted type. The emitter asks ContainsKey first, the way it already
+# does for the CharSet key, and this runs the whole emitter under the strictest setting.
+@requires_pwsh
[email protected]("script", ["install", "setup"])
+def test_an_import_without_an_out_position_survives_strict_mode(script: str):
+ source = (INSTALL_PS1 if script == "install" else SETUP_PS1).read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ "Set-StrictMode -Version Latest",
+ '$ErrorActionPreference = "Stop"',
+ _one_function(source, "New-StudioDynamicAssembly"),
+ _one_function(source, "New-StudioEmittedNativeType"),
+ # No Out key, and no Ansi key either: both reads have to be guarded.
+ '$ok = New-StudioEmittedNativeType -TypeName "UnslothStrictModeProbe" -Imports @(',
+ ' @{ Name = "CloseHandle"; Library = "kernel32.dll"; Return = [bool]',
+ " Args = @([IntPtr]) }",
+ ")",
+ 'Write-Output "OK:$ok"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr + result.stdout
+ assert _lines(result, "OK:") == ["OK:True"]
+
+
+@requires_pwsh
+def test_a_published_type_counts_even_when_creation_threw():
+ """CreateType can publish the type and then fail on the way back.
+
+ The compiled version asked whether the type had arrived anyway before it gave up, and
+ dropping that turned a recoverable failure into lexical resolution for the whole run.
+ """
+ source = INSTALL_PS1.read_text(encoding = "utf-8")
+ result = _run_powershell(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ _helpers(
+ "Write-StudioLine",
+ "Write-StudioFinalPathDegraded",
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
+ "Initialize-StudioFinalPathNativeType",
+ ),
+ # Publishes the type under the name the initializer wants, then throws, which
+ # is the shape being tested. Add-Type here is the test's own scaffolding, not
+ # the installer's: it is the shortest way to put a real type in the session.
+ "$real = ${function:New-StudioEmittedNativeType}",
+ "function New-StudioEmittedNativeType {",
+ " param([string]$TypeName, [object[]]$Imports)",
+ " Add-Type -TypeDefinition 'public static class UnslothStudioFinalPathV3 { }'",
+ ' throw "published, then failed"',
+ "}",
+ 'Write-Output "OK:$(Initialize-StudioFinalPathNativeType)"',
+ 'Write-Output "AGAIN:$(Initialize-StudioFinalPathNativeType)"',
+ ]
+ )
+ )
+ assert result.returncode == 0, result.stderr
+ assert _lines(result, "OK:") == ["OK:True"]
+ assert _lines(result, "AGAIN:") == ["AGAIN:True"]
+ # The degraded warning belongs to a run that really has no native side.
+ assert "Could not load the native path resolver" not in result.stdout
diff --git a/tests/python/test_windows_setup_output_encoding.py b/tests/python/test_windows_setup_output_encoding.py
index 1458b7978ae..8997d9e8c2e 100644
--- a/tests/python/test_windows_setup_output_encoding.py
+++ b/tests/python/test_windows_setup_output_encoding.py
@@ -595,7 +595,20 @@ def _console_less_probe(path: Path) -> str:
r"[ \t]*try \{ \$script:StudioStdoutRedirected = \[Console\]::IsOutputRedirected \} catch \{ \}",
)
parts += [redirect_probe or "$script:StudioStdoutRedirected = $false", ""]
- for name in ("Write-StudioLine", "Enable-StudioVirtualTerminal", "Get-StudioAnsi"):
+ # Enable-StudioVirtualTerminal's own helpers come first, because it calls them. They were
+ # added when the emitted-type path was extracted, and the reconstruction that this test
+ # builds to represent the predecessor calls them too: without them the probe dies with an
+ # unrecognised command, which reads as "the predecessor disagreed" when in truth it never
+ # ran. Anything the sliced functions call has to be sliced with them.
+ for name in (
+ "Test-StudioCanDefineNativeTypes",
+ "Test-StudioEmitInChildProcess",
+ "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
+ "Write-StudioLine",
+ "Enable-StudioVirtualTerminal",
+ "Get-StudioAnsi",
+ ):
if _function_match(masked, name):
lo, hi = _function_span(masked, name)
parts += [_dedent(source[lo:hi]), ""]
@@ -784,6 +797,10 @@ def test_vt_fast_path_decides_exactly_as_the_compile_did(path: Path) -> None:
assert new_code == old_code == 0, (
f"probe exit codes {new_code} (with the fast path) and {old_code} (without)"
f"{_explain(path, new_code, new_raw, new_err)}"
+ # Both sides. Reporting only the new run hid the whole failure once: the new run was
+ # the one that passed, and the reconstruction's stderr, which named the missing
+ # command, was never printed.
+ f"\n reconstructed predecessor stderr:\n{old_err}"
)
assert _vt_verdict(new_err) == _vt_verdict(old_err) == "False", (
f"a redirected stream cannot render VT: the fast path returned "
diff --git a/tests/studio/test_installer_av_shapes.py b/tests/studio/test_installer_av_shapes.py
index 3fa20748091..13bbd12cf51 100644
--- a/tests/studio/test_installer_av_shapes.py
+++ b/tests/studio/test_installer_av_shapes.py
@@ -12,6 +12,8 @@ lock at the bottom is the other half: hardening must not change what a user sees
"""
import re
+import shutil
+import subprocess
from pathlib import Path
import pytest
@@ -108,8 +110,10 @@ def test_a_hidden_window_never_pairs_with_a_bypassed_policy(name: str) -> None:
), f"{name}:{number} pairs a hidden window with a bypassed policy: {line.strip()}"
-# Every runtime-compiled P/Invoke left in the installers. Each costs a csc.exe compile and is scored, so a new entry
-# needs a reason; a PowerShell equivalent usually exists.
+# Every native import left in the installers, however it is declared. Both scripts define theirs through reflection
+# emit now, which costs no compile at all: install.ps1 the path resolver, the console thunk, the icon refresh and the
+# process-image lookup, studio/setup.ps1 the console thunk. A new entry still needs a reason, and a PowerShell
+# equivalent usually exists.
ALLOWED_PINVOKES = {
# Canonicalising linked ancestors of security-relevant paths.
# No PS 5.1 equivalent: ResolveLinkTarget is .NET 6+, and .Target misses a linked ancestor of a non-link leaf.
@@ -127,18 +131,21 @@ ALLOWED_PINVOKES = {
# Per-item Explorer icon refresh, standalone path only.
# ie4uinit.exe -show is the global broadcast, which alone does not recover a stale .lnk, so it is not a substitute.
"SHChangeNotify",
- # PID -> image path for the venv-holder check.
- # Win32_Process answers the same question, but test_windows_installer_concurrency_guard.py bans it and $process.Path
- # there: the races #7764 closed came from inferring "in use" from anything but a confirmed executable identity.
+ # Naming the image behind a pid, so a venv Unsloth still has open is not overwritten.
+ # PROCESS_QUERY_LIMITED_INFORMATION only, and it is the rung the others cannot replace: Process.Path goes through
+ # MainModule, which needs PROCESS_VM_READ and is refused across users and across bitness, and Win32_Process needs a
+ # working WMI service. Without this the scan can find nothing and let the install proceed over an open venv.
"OpenProcess",
"QueryFullProcessImageNameW",
+ # Closing the handles CreateFileW and OpenProcess opened.
"CloseHandle",
}
[email protected]("name", ALL_SCRIPTS)
-def test_no_new_native_imports(name: str) -> None:
- text = _text(name)
+# The two ways a native import can be declared. Add-Type takes C# and runs csc.exe; DefinePInvokeMethod builds the
+# same stub in memory. The second is invisible to a DllImport regex, so without this the inventory above would silently
+# stop covering install.ps1 the moment it stopped compiling.
+def _native_imports(text: str) -> set:
imported = set()
for match in re.finditer(
r"DllImport\(\"[^\"]+\"[^)]*\)\][^;{]*?extern\s+[\w.\[\]]+\s+(\w+)", text
@@ -147,7 +154,16 @@ def test_no_new_native_imports(name: str) -> None:
# install.ps1's multi-line declarations put the parameter list on later lines.
for match in re.finditer(r"extern\s+[\w.<>\[\]]+\s+(\w+)\s*\(", text):
imported.add(match.group(1))
- unexpected = imported - ALLOWED_PINVOKES
+ if "DefinePInvokeMethod" in text:
+ for match in re.finditer(r"@\{\s*Name\s*=\s*\"(\w+)\"", text):
+ imported.add(match.group(1))
+ return imported
+
+
[email protected]("name", ALL_SCRIPTS)
+def test_no_new_native_imports(name: str) -> None:
+ text = _text(name)
+ unexpected = _native_imports(text) - ALLOWED_PINVOKES
assert not unexpected, (
f"{name} imports {sorted(unexpected)} from native code. Prefer a PowerShell or .NET "
f"equivalent; if there genuinely is none, add it to ALLOWED_PINVOKES with the reason."
@@ -155,27 +171,31 @@ def test_no_new_native_imports(name: str) -> None:
@pytest.mark.parametrize("name", ("install.ps1", "studio/setup.ps1"))
-def test_virtual_terminal_answers_a_redirected_stream_without_compiling(name: str) -> None:
- """Add-Type runs the C# compiler, so the answer we already know must come first.
+def test_virtual_terminal_answers_a_redirected_stream_without_defining_a_type(name: str) -> None:
+ """The answer we already know must come first, before any native work at all.
Only the redirected case is decided early, and it is decided FALSE. A redirected stdout is
- not a console, GetConsoleMode fails on a non-console handle, and the compiled path could
+ not a console, GetConsoleMode fails on a non-console handle, and the native path could
only have returned false too. Anything that claimed VT here would put raw escape sequences
in the Unsloth log panel, which is a pipe.
+
+ This used to guard an Add-Type, back when the redirect check was the only thing keeping the
+ desktop app off csc.exe. Nothing here compiles now, so the ordering is no longer load-bearing
+ against a scanner; it is still the cheaper answer, and getting it wrong still corrupts the
+ log panel.
"""
text = _text(name)
start = text.index("function Enable-StudioVirtualTerminal")
- # The call, not the comment above it.
- call = re.compile(r"(?m)^[ \t]*Add-Type\b").search(text, start)
- assert call, f"{name} no longer compiles the console thunk; update this guard"
- compile_at = call.start()
+ call = re.compile(r"(?m)^[ \t]*\$null = New-StudioEmittedNativeType\b").search(text, start)
+ assert call, f"{name} no longer emits the console thunk; update this guard"
+ define_at = call.start()
fast_path = text.index("if ($script:StudioStdoutRedirected) { return $false }", start)
- assert fast_path < compile_at, (
- f"{name} compiles C# for colour before checking the stream: move the redirect guard "
- f"above Add-Type, or every install spawns csc.exe again."
+ assert fast_path < define_at, (
+ f"{name} builds the native console thunk before checking the stream: move the redirect "
+ f"guard above it, since a redirected stream can never render VT anyway."
)
- assert "$true" not in text[fast_path:compile_at], (
- f"{name} returns something other than $false before the compile. The early answer is "
+ assert "$true" not in text[fast_path:define_at], (
+ f"{name} returns something other than $false before the native work. The early answer is "
f"only sound because a redirected stream can never render VT."
)
@@ -214,3 +234,164 @@ def test_printed_remediation_survives_the_hardening(name: str) -> None:
if __name__ == "__main__":
raise SystemExit(pytest.main([__file__, "-v"]))
+
+
[email protected]("name", ("install.ps1", "studio/setup.ps1"))
+def test_the_installer_never_runs_the_c_sharp_compiler(name: str) -> None:
+ """The desktop app spawns Windows PowerShell 5.1, which compiles Add-Type by writing C# to
+ %TEMP% and running csc.exe. A GUI binary launching a windowless PowerShell that launches a
+ compiler and drops a DLL in %TEMP% is a dropper's shape whatever the code says, and it was
+ blocked in the field. Reflection emit builds the same stub in memory: no compiler process,
+ no source on disk, no DLL, nothing in %TEMP%.
+
+ Add-Type in full, not only -TypeDefinition: -MemberDefinition wraps its argument in a class
+ and compiles that, so it reaches csc.exe by the same road. -AssemblyName is the exception,
+ and the only one: it loads an assembly that already exists on disk and never reaches a
+ compiler. Both scripts, not only the bundled one: leaving a compile anywhere means the answer
+ to "does this run a compiler" depends on which entrypoint ran and whether an early return
+ happened to come first, and a guard that holds only conditionally is what let this reach the
+ field.
+ """
+ text = _text(name)
+ hits = re.findall(r"(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName).*", text)
+ assert not hits, (
+ f"{name} compiles C# again ({len(hits)} Add-Type call(s), first: {hits[0].strip()!r}). "
+ "Declare native methods with New-StudioEmittedNativeType instead; -MemberDefinition runs "
+ "csc.exe just as -TypeDefinition does."
+ )
+ assert (
+ "DefinePInvokeMethod" in text
+ ), f"{name} no longer emits its native imports; update this guard"
+ # The private-%TEMP% retry is gone with it. Redirecting TEMP to compile again after a block
+ # cannot beat a filter driver, and "blocked writing an executable to TEMP, change TEMP, write
+ # it again" is itself an evasion heuristic. Scoped to the resolver: Initialize-StudioTempEnvironment
+ # legitimately redirects an unusable inherited TEMP, and that is a different thing.
+ # Only install.ps1 has the path resolver; setup.ps1 emits the console thunk and nothing else.
+ if "function Initialize-StudioFinalPathNativeType" not in text:
+ return
+ start = text.index("function Initialize-StudioFinalPathNativeType")
+ body = text[start : text.index("\n function ", start + 1)]
+ assert (
+ "$env:TMP" not in body and "$env:TEMP" not in body
+ ), "the native resolver touches the temporary directory again; it should need nothing there"
+
+
+def test_a_ci_lane_fails_when_a_compiler_actually_runs() -> None:
+ """The behavioural half of the guard above.
+
+ Reading the scripts cannot see a compile reached through a module, a dot-sourced
+ file or a generated here-string, and it cannot see one a dependency performs while
+ our process tree is what a scanner scores. Bitdefender scored the chain, not the
+ bytes, so there has to be a lane that runs the installer and fails on the process.
+
+ The positive control is the part worth asserting from here: a detector that sees
+ nothing reads exactly like a clean run, and auditing can silently fail to apply. If
+ the lane ever loses the control, every later green result stops meaning anything.
+ """
+ workflow = REPO / ".github" / "workflows" / "windows-no-compiler-ci.yml"
+ assert workflow.is_file(), "the runtime guard lane is gone; the text check is alone again"
+ body = workflow.read_text(encoding = "utf-8")
+ assert "Positive control" in body, "the lane no longer proves its own detector works"
+ assert (
+ "Add-Type -TypeDefinition" in body
+ ), "the positive control must really compile something; a simulated one proves nothing"
+
+ watcher = REPO / ".github" / "scripts" / "Watch-ForCompiler.ps1"
+ assert watcher.is_file()
+ watcher_body = watcher.read_text(encoding = "utf-8")
+ for image in ("csc.exe", "vbc.exe", "cvtres.exe"):
+ assert image in watcher_body, f"the watcher no longer looks for {image}"
+ # 4688 is what sees a compiler spawned at any depth; the temp sweep is what
+ # survives auditing being overridden. Losing either leaves one detector.
+ assert "4688" in watcher_body
+ assert "*.cmdline" in watcher_body
+
+
+_WATCHER = REPO / ".github" / "scripts" / "Watch-ForCompiler.ps1"
+
+_FAKE_EVENTS = r"""
+function New-FakeEvent {
+ param([string]$Image, [string]$CommandLine)
+ $xml = "<Event><EventData>" +
+ "<Data Name='NewProcessName'>$Image</Data>" +
+ "<Data Name='CommandLine'>$CommandLine</Data>" +
+ "</EventData></Event>"
+ $record = [pscustomobject]@{
+ TimeCreated = [datetime]'2026-01-01T00:00:00Z'
+ Message = "New Process Name: $Image`nProcess Command Line: $CommandLine"
+ }
+ $body = [scriptblock]::Create("return @'`n$xml`n'@")
+ return ($record | Add-Member -MemberType ScriptMethod -Name ToXml -Value $body -PassThru)
+}
+"""
+
+
[email protected](shutil.which("pwsh") is None, reason = "needs PowerShell")
+ ("image", "command_line", "expected"),
+ [
+ (r"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe", "csc.exe /out:x.dll", 1),
+ (r"C:\Windows\System32\cmd.exe", "cmd.exe /c echo csc.exe", 0),
+ (r"C:\Windows\System32\cmd.exe", r"cmd.exe /c copy a.txt C:\csc.exe.log", 0),
+ (r"C:\Users\r\csc.exe.helper.exe", "whatever", 0),
+ ],
+ ids = ["a-real-compile", "a-command-line-mentioning-one", "a-path-argument", "a-similar-name"],
+)
+def test_the_watcher_scores_the_image_that_ran_not_the_words_in_the_message(
+ tmp_path, image: str, command_line: str, expected: int
+) -> None:
+ """4688 renders the command line into the message, so a message search is not a detector.
+
+ It scored `cmd.exe /c echo csc.exe` as a compile, and this job's whole output is a
+ yes or no about whether a compiler ran under the installer. The record names the
+ image it created in its own field; that is what gets read, and matched whole against
+ the leaf name rather than as a substring.
+ """
+ script = tmp_path / "probe.ps1"
+ script.write_text(
+ "\n".join(
+ [
+ '$ErrorActionPreference = "Stop"',
+ f'. "{_WATCHER}"',
+ _FAKE_EVENTS,
+ f"$e = New-FakeEvent -Image '{image}' -CommandLine '{command_line}'",
+ "$hits = Select-StudioCompilerHits -Events @($e)",
+ 'Write-Output "HITS:$($hits.Count)"',
+ ]
+ ),
+ encoding = "utf-8",
+ )
+ result = subprocess.run(
+ ["pwsh", "-NoProfile", "-NonInteractive", "-File", str(script)],
+ capture_output = True,
+ text = True,
+ timeout = 120,
+ )
+ assert result.returncode == 0, result.stderr + result.stdout
+ assert f"HITS:{expected}" in result.stdout, result.stdout
+
+
+def test_an_unreadable_security_log_is_void_rather_than_clean() -> None:
+ """Get-WinEvent throws both for "nothing matched" and for "could not read".
+
+ Swallowing both made a job that could not open the Security log print "no compiler"
+ and pass. The positive control does not cover it: it runs in an earlier step, and
+ says nothing about whether the log was still readable during the measurement.
+ """
+ body = _WATCHER.read_text(encoding = "utf-8")
+ assert (
+ "-MaxEvents 1" in body
+ ), "the watcher no longer distinguishes an empty result from an unreadable log"
+ assert "void rather than as clean" in body
+
+
+def test_the_native_resolver_still_has_a_lexical_fallback() -> None:
+ """The point of the change is the acquisition, not the ladder. A host where emit fails has to
+ degrade exactly as a host that could not compile already did, which is a path the installer
+ has always taken and still completes on.
+ """
+ text = _text("install.ps1")
+ assert "Write-StudioFinalPathDegraded" in text
+ assert "Get-StudioLexicalPath" in text
+ # Constrained Language Mode forbids defining types at all, by emit as by Add-Type.
+ assert '$languageMode -ne "FullLanguage"' in text
diff --git a/tests/studio/test_native_path_resolver_degrades.ps1 b/tests/studio/test_native_path_resolver_degrades.ps1
new file mode 100644
index 00000000000..90562c23ee3
--- /dev/null
+++ b/tests/studio/test_native_path_resolver_degrades.ps1
@@ -0,0 +1,265 @@
+# Behaviour of install.ps1's native helpers when the native side is not available,
+# which is the state a blocked or locked-down machine is in.
+#
+# The change this guards replaced every Add-Type compile with reflection emit. The
+# ladders around them did not move: an exact path when the native call works, the
+# lexical path when it does not with $Exact false so the runtime lock fails closed,
+# and for the process scan a native image path first, then Get-Process, then
+# Win32_Process. This runs the real functions, parsed out of install.ps1, and checks
+# that every rung still answers.
+#
+# POSIX is the harness on purpose. Two different failures live here and they are not
+# the same: reflection emit SUCCEEDS on this engine, so the types build exactly as
+# they do on 5.1, while kernel32 and shell32 do not resolve, so every call through
+# them fails exactly as it does on a Windows host whose antivirus took the helper
+# away. Between them that covers the build side and the call side. The Windows lanes
+# cover the case where the call itself succeeds.
+
+$ErrorActionPreference = "Stop"
+
+# Refuse rather than assert nonsense. Half the checks below require the native calls to
+# FAIL, which on a healthy Windows host they do not, so a Windows lane running this file
+# would report a pile of failures that say nothing about the code. It was scheduled on a
+# windows-latest job once; exiting here means the next time it happens the message names
+# the reason instead.
+if ($IsWindows -or $env:OS -eq "Windows_NT") {
+ Write-Host "SKIP: this file describes a host where kernel32 does not resolve, which Windows is not."
+ exit 0
+}
+
+$repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
+
+# The parser, not tests/studio_setup_ps1/Get-FunctionSource.ps1, which counts braces
+# without tokenizing and so runs past the end of any function holding a brace inside
+# a string. Resolve-StudioLinkTarget has one ('Volume{'), and the naive walk swallowed
+# the rest of the file. The parser is exact and is right here in the box.
+$script:InstallAst = $null
+function Get-InstallFunctionSource {
+ param([Parameter(Mandatory = $true)][string]$Path, [Parameter(Mandatory = $true)][string]$Name)
+ if ($null -eq $script:InstallAst) {
+ $errors = $null; $tokens = $null
+ $script:InstallAst = [System.Management.Automation.Language.Parser]::ParseFile(
+ $Path, [ref]$tokens, [ref]$errors)
+ if ($errors.Count -gt 0) { throw "$Path : $($errors[0].Message)" }
+ }
+ $found = $script:InstallAst.FindAll({
+ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
+ $args[0].Name -eq $Name
+ }, $true)
+ if ($found.Count -eq 0) { return $null }
+ return $found[0].Extent.Text
+}
+
+$failures = 0
+function Check {
+ param([string]$Name, [bool]$Ok)
+ if ($Ok) { Write-Host " PASS $Name" -ForegroundColor Green }
+ else { Write-Host " FAIL $Name" -ForegroundColor Red; $script:failures++ }
+}
+
+$installPath = [System.IO.Path]::Combine($repoRoot, "install.ps1")
+$text = Get-Content -LiteralPath $installPath -Raw
+
+# ── Source contract ──
+$setupPath = [System.IO.Path]::Combine($repoRoot, "studio", "setup.ps1")
+foreach ($pair in @(@("install.ps1", $installPath), @("studio/setup.ps1", $setupPath))) {
+ $body = Get-Content -LiteralPath $pair[1] -Raw
+ # -MemberDefinition compiles as surely as -TypeDefinition does. -AssemblyName
+ # only loads an assembly that already exists and reaches no compiler, so it is
+ # not what this bans.
+ $compiles = [regex]::Matches($body, "(?m)^[ \t]*Add-Type\b(?![^\r\n]*-AssemblyName)")
+ Check "$($pair[0]) compiles no C# at all" ($compiles.Count -eq 0)
+ Check "$($pair[0]) emits its native imports instead" ($body -match "DefinePInvokeMethod")
+ # Both spellings of "define a dynamic assembly". Windows PowerShell 5.1 on .NET
+ # Framework and pwsh on .NET Core do not agree about which one exists, and only
+ # one of them can be exercised from here: losing either branch would degrade
+ # every install on the other host with no error to see, since the caller caches
+ # the failure and falls through to the lexical path.
+ Check "$($pair[0]) tries the static DefineDynamicAssembly" (
+ $body -match "\[System\.Reflection\.Emit\.AssemblyBuilder\]::DefineDynamicAssembly")
+ Check "$($pair[0]) falls back to the AppDomain spelling" (
+ $body -match "\[AppDomain\]::CurrentDomain\.DefineDynamicAssembly")
+}
+
+# ── The resolver, run for real ──
+# The whole ladder, not only its top rung: the claim under test is that a host
+# without the native side still gets an answer, and gets told it is inexact.
+$fns = @(
+ "Write-StudioLine", "Write-StudioFinalPathDegraded",
+ "Test-StudioCanDefineNativeTypes", "Test-StudioEmitInChildProcess", "New-StudioDynamicAssembly",
+ "New-StudioEmittedNativeType",
+ "Initialize-StudioFinalPathNativeType", "Get-StudioNativeFinalPath",
+ "Resolve-StudioLinkTarget", "Get-StudioSubstTarget", "Get-StudioLexicalPath",
+ "Resolve-StudioFinalPathInfo",
+ "Initialize-StudioProcessImageNativeType", "Get-StudioNativeProcessImagePath",
+ "Get-StudioProcessImagePath"
+)
+$src = @()
+foreach ($fn in $fns) {
+ $found = Get-InstallFunctionSource -Path $installPath -Name $fn
+ Check "install.ps1 defines $fn" ($null -ne $found)
+ if ($found) { $src += $found }
+}
+
+if ($src.Count -eq $fns.Count) {
+ $harness = @"
+`$ErrorActionPreference = "Stop"
+function Write-StudioLine { param([string]`$Message, `$ForegroundColor) Write-Host `$Message }
+`$script:StudioFinalPathNativeState = `$null
+`$script:StudioFinalPathWarned = `$false
+`$script:StudioNativeResolveWarned = `$false
+`$script:StudioSubstMap = `$null
+`$script:StudioProcessImageNativeState = `$null
+`$script:StudioProcessImageTable = `$null
+`$script:StudioProcessImageWarned = `$false
+$($src -join "`n")
+
+# Snapshot first. A runner is a shared machine, and anything else that wrote a
+# matching name into the shared temp directory in the preceding minute would
+# otherwise be counted as this emission's output and fail the run for it.
+`$temp = [System.IO.Path]::GetTempPath()
+`$patterns = @(".dll", ".cs", ".cmdline", ".err", ".out")
+`$before = @(Get-ChildItem -LiteralPath `$temp -File -ErrorAction SilentlyContinue |
+ Where-Object { `$_.Extension -in `$patterns } | Select-Object -ExpandProperty FullName)
+
+# 1. The type must define without a compiler, on this engine as on 5.1.
+`$defined = Initialize-StudioFinalPathNativeType
+Write-Host "EMIT_OK: `$defined"
+Write-Host "TYPE_PRESENT: `$(`$null -ne ("UnslothStudioFinalPathV3" -as [type]))"
+
+# 2. Nothing may be written to the temporary directory by defining it. Set
+# difference against the snapshot above, not a recency window: what this asserts
+# is that emission wrote nothing, and the clock cannot tell those apart.
+`$after = @(Get-ChildItem -LiteralPath `$temp -File -ErrorAction SilentlyContinue |
+ Where-Object { `$_.Extension -in `$patterns } | Select-Object -ExpandProperty FullName)
+`$fresh = @(`$after | Where-Object { `$before -notcontains `$_ })
+Write-Host "TEMP_ARTIFACTS: `$(`$fresh.Count)"
+
+# 3. The dynamic assembly must exist only in memory.
+if (`$defined) {
+ `$asm = ("UnslothStudioFinalPathV3" -as [type]).Assembly
+ Write-Host "IN_MEMORY_ONLY: `$([string]::IsNullOrEmpty(`$asm.Location))"
+}
+
+# 4. A native call that cannot work must answer null, not throw. kernel32 is
+# absent here, which is what a host missing the helper looks like.
+`$threw = `$false
+`$answer = "unset"
+try { `$answer = Get-StudioNativeFinalPath -Path `$temp } catch { `$threw = `$true }
+Write-Host "NATIVE_THREW: `$threw"
+Write-Host "NATIVE_ANSWER_NULL: `$([string]::IsNullOrEmpty(`$answer))"
+
+# 5. The other two emitted types must define with the same signatures the C# they
+# replace declared. `out uint` has to arrive as a by-ref, and a void return has
+# to stay void, or the call marshals wrongly on a host where it does run.
+`$vt = New-StudioEmittedNativeType -TypeName "StudioVTNative" -Imports @(
+ @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]; Args = @([int]) },
+ @{ Name = "GetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32].MakeByRefType()) },
+ @{ Name = "SetConsoleMode"; Library = "kernel32.dll"; Return = [bool]
+ Args = @([IntPtr], [uint32]) })
+`$icon = New-StudioEmittedNativeType -TypeName "UnslothShellIconRefresh" -Imports @(
+ @{ Name = "SHChangeNotify"; Library = "shell32.dll"; Return = [System.Void]
+ Args = @([int], [uint32], [string], [IntPtr]) })
+Write-Host "VT_EMIT_OK: `$vt"
+Write-Host "ICON_EMIT_OK: `$icon"
+`$gcm = ("StudioVTNative" -as [type]).GetMethod("GetConsoleMode")
+Write-Host "VT_BYREF_OUT: `$(`$gcm.GetParameters()[1].ParameterType.IsByRef)"
+`$shcn = ("UnslothShellIconRefresh" -as [type]).GetMethod("SHChangeNotify")
+Write-Host "ICON_RETURNS_VOID: `$(`$shcn.ReturnType -eq [System.Void])"
+
+# 6. Defining a type twice must not corrupt the first one. Callers guard with
+# -as [type], but a partial first attempt would otherwise leave a half-built
+# type behind, which is what Add-Type's "already exists" error used to prevent.
+`$redefineThrew = `$false
+try { `$null = New-StudioEmittedNativeType -TypeName "StudioVTNative" -Imports @(
+ @{ Name = "GetStdHandle"; Library = "kernel32.dll"; Return = [IntPtr]; Args = @([int]) }) }
+catch { `$redefineThrew = `$true }
+Write-Host "REDEFINE_THREW: `$redefineThrew"
+Write-Host "REDEFINE_TYPE_INTACT: `$(`$null -ne (("StudioVTNative" -as [type]).GetMethod("SetConsoleMode")))"
+
+# 7. The other rung. Resolve-StudioFinalPathInfo is what every caller actually
+# uses, and the point of the change is that it still answers when the native
+# side cannot: a lexical path, and Exact false so the runtime lock fails closed.
+`$info = `$null
+`$ladderThrew = `$false
+try { `$info = Resolve-StudioFinalPathInfo -Path `$temp } catch { `$ladderThrew = `$true; Write-Host "LADDER_ERROR: `$(`$_.Exception.Message)" }
+Write-Host "LADDER_THREW: `$ladderThrew"
+if (`$info) {
+ Write-Host "LADDER_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$info.Path))"
+ Write-Host "LADDER_EXACT: `$(`$info.Exact)"
+}
+
+# 8. The process-image ladder. Two states, and they are different states: the
+# emit SUCCEEDS here even though kernel32 does not resolve, so this takes the
+# native rung and must answer null without throwing, exactly as it did when the
+# type came from a compile that succeeded and a call that then failed. Forcing
+# the type unavailable is what exercises the Get-Process rung below it.
+`$imgThrew = `$false
+`$img = "unset"
+try { `$img = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$imgThrew = `$true }
+Write-Host "PROCIMG_NATIVE_STATE: `$(Initialize-StudioProcessImageNativeType)"
+Write-Host "PROCIMG_THREW: `$imgThrew"
+Write-Host "PROCIMG_NATIVE_NULL: `$([string]::IsNullOrWhiteSpace(`$img))"
+
+# The real initializer caches on type PRESENCE, so clearing the state variable
+# would not un-define what step 8 just built. Override the guard instead: the rungs
+# under test are the ones below it, and this is the only way to reach them once a
+# type exists in the session.
+function Initialize-StudioProcessImageNativeType { return `$false }
+`$fallbackThrew = `$false
+`$fallback = "unset"
+try { `$fallback = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$fallbackThrew = `$true }
+Write-Host "PROCIMG_FALLBACK_THREW: `$fallbackThrew"
+Write-Host "PROCIMG_FALLBACK_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$fallback))"
+Write-Host "PROCIMG_WARNED: `$script:StudioProcessImageWarned"
+"@
+ $file = Join-Path ([System.IO.Path]::GetTempPath()) ("uns_native_" + [guid]::NewGuid().ToString("N") + ".ps1")
+ Set-Content -LiteralPath $file -Value $harness -Encoding utf8
+ $exe = (Get-Command pwsh -ErrorAction SilentlyContinue).Source
+ if (-not $exe) { $exe = (Get-Command powershell).Source }
+ try {
+ $out = & $exe -NoProfile -File $file 2>&1 | Out-String
+ } finally {
+ Remove-Item -LiteralPath $file -ErrorAction SilentlyContinue
+ }
+
+ # A parse failure in the generated harness would otherwise read as every check
+ # failing at once, with nothing saying why.
+ if ($out -notmatch "EMIT_OK") { Write-Host " ---- harness output ----"; Write-Host $out }
+ Check "the native type defines with no compiler" ($out -match "EMIT_OK: True")
+ Check "the emitted type is really there" ($out -match "TYPE_PRESENT: True")
+ Check "defining it writes nothing to the temporary directory" ($out -match "TEMP_ARTIFACTS: 0")
+ Check "the assembly exists only in memory" ($out -match "IN_MEMORY_ONLY: True")
+ # The whole point of the ladder: a native call that cannot work is not fatal.
+ Check "a native call that cannot work does not throw" ($out -match "NATIVE_THREW: False")
+ Check "a native call that cannot work answers null" ($out -match "NATIVE_ANSWER_NULL: True")
+ Check "the console thunk emits too" ($out -match "VT_EMIT_OK: True")
+ Check "the icon-refresh thunk emits too" ($out -match "ICON_EMIT_OK: True")
+ # These two are what a hand-written emit gets wrong: `out uint` silently
+ # becoming a by-value uint, and a void return becoming something else.
+ Check "GetConsoleMode's out parameter is by-ref" ($out -match "VT_BYREF_OUT: True")
+ Check "SHChangeNotify still returns void" ($out -match "ICON_RETURNS_VOID: True")
+ Check "defining a type twice does not throw" ($out -match "REDEFINE_THREW: False")
+ Check "and does not corrupt the first definition" ($out -match "REDEFINE_TYPE_INTACT: True")
+ # The rung that matters to callers, not just the one that failed.
+ Check "the full path ladder does not throw" ($out -match "LADDER_THREW: False")
+ Check "the full path ladder still answers" ($out -match "LADDER_ANSWERED: True")
+ Check "and reports the answer as inexact" ($out -match "LADDER_EXACT: False")
+ # The native rung is REACHED here: emit succeeds on this engine, kernel32 does
+ # not resolve, and null without an exception is what the caller skips on.
+ Check "the process image type emits too" ($out -match "PROCIMG_NATIVE_STATE: True")
+ Check "the process image ladder does not throw" ($out -match "PROCIMG_THREW: False")
+ Check "an unusable native process query answers null" ($out -match "PROCIMG_NATIVE_NULL: True")
+ # And with the type forced away, the rungs below it still answer, which is what
+ # keeps a host from finding no running processes and overwriting an open venv.
+ Check "the fallback rung does not throw" ($out -match "PROCIMG_FALLBACK_THREW: False")
+ Check "the fallback rung still names our own image" ($out -match "PROCIMG_FALLBACK_ANSWERED: True")
+ Check "and says out loud that it is degraded" ($out -match "PROCIMG_WARNED: True")
+}
+
+if ($failures -gt 0) {
+ Write-Host "$failures check(s) failed" -ForegroundColor Red
+ exit 1
+}
+Write-Host "All checks passed" -ForegroundColor Green
diff --git a/tests/test_installer_profile_hardening.py b/tests/test_installer_profile_hardening.py
index f5d3d43f0ff..f03ba017cf4 100644
--- a/tests/test_installer_profile_hardening.py
+++ b/tests/test_installer_profile_hardening.py
@@ -28,6 +28,7 @@ from unsloth_pwsh_runner import run_pwsh
REPO_ROOT = Path(__file__).resolve().parents[1]
INSTALL_PS1 = REPO_ROOT / "install.ps1"
+SETUP_PS1 = REPO_ROOT / "studio" / "setup.ps1"
STUDIO_COMMAND = REPO_ROOT / "unsloth_cli" / "commands" / "studio.py"
@@ -659,8 +660,14 @@ def test_module_autoloading_is_restored():
@requires_pwsh
-def test_install_ps1_parses():
- """A syntax error here is a total install failure, and the file is not imported by anything."""
[email protected]("script", [INSTALL_PS1, SETUP_PS1], ids = ["install.ps1", "setup.ps1"])
+def test_the_powershell_entrypoints_parse(script):
+ """A syntax error here is a total install failure, and neither file is imported by anything.
+
+ setup.ps1 is here for the same reason install.ps1 is, and because the tests that read it
+ extract single functions: an extraction still parses when the file around it does not, so
+ nothing else in the suite would notice a broken brace at file scope.
+ """
# A nonzero exit here is claimed to mean install.ps1 has a syntax error, and pwsh aborting before it ever reached
# the parser exits nonzero too.
res = run_pwsh(
@@ -670,7 +677,7 @@ def test_install_ps1_parses():
"-NonInteractive",
"-Command",
"$errs = $null; $null = [System.Management.Automation.Language.Parser]::ParseFile("
- f"{_ps_literal(INSTALL_PS1)}, [ref]$null, [ref]$errs); "
+ f"{_ps_literal(script)}, [ref]$null, [ref]$errs); "
'if ($errs) { $errs | ForEach-Object { "ERR $($_.Extent.StartLineNumber): '
'$($_.Message)" }; exit 1 }',
],