Skip to content

The agent that grows with you

fix: bash and PowerShell marker readers take the oversized-number corpus cases

Tekniumb17bcfc70427
diff --git a/scripts/desktop-update/marker-claim.ps1 b/scripts/desktop-update/marker-claim.ps1
index b6db5a5abb6..f905b3d53bf 100644
--- a/scripts/desktop-update/marker-claim.ps1
+++ b/scripts/desktop-update/marker-claim.ps1
@@ -96,7 +96,7 @@ function Get-MarkerLineage([int]$Desktop, $Info) {
V1 = ($null -eq $Info.Ct -and $Info.DelegatePid -le 0); NamesDesktop = ($named -eq $Desktop)
NamedAlive = $namedAlive; NamedIsOurParent = ($x -gt 0 -and $named -eq $x)
NamedParentIsDesktop = ($namedAlive -and $named -eq $x -and $xParent -eq $Desktop)
- EnvStartedMatches = (Test-MarkerEnvStartedAt "$($Info.StartedAt)")
+ EnvStartedMatches = (Test-MarkerEnvStartedAt $Info.StartedAtText)
}
$launcher = Test-MarkerLauncherRule $facts
}
@@ -201,7 +201,7 @@ function Add-MarkerDelegate([int[]]$Candidates) {
$probe = Get-LiveProcessCt $candidate
if (-not $probe.Alive -or $null -eq $probe.Ct) { continue }
$line = "delegate:$candidate ct:$(Format-Ct $probe.Ct)"
- if (-not (Set-MarkerBodyLocked (Format-MarkerBody $info.Pid $info.StartedAt $info.CtText $line $info.Runs))) { return 'lost' }
+ if (-not (Set-MarkerBodyLocked (Format-MarkerBody $info.Pid $info.StartedAtText $info.CtText $line $info.Runs))) { return 'lost' }
Write-HandoffLog "update marker now names updater pid $candidate as its delegate"
return 'published'
}
diff --git a/scripts/desktop-update/marker.ps1 b/scripts/desktop-update/marker.ps1
index 2a8dd24afdc..26f7f7150d0 100644
--- a/scripts/desktop-update/marker.ps1
+++ b/scripts/desktop-update/marker.ps1
@@ -116,13 +116,24 @@ function Get-MarkerLineValue([string]$Line) {
}
function ConvertTo-MarkerPid([string]$Text) {
- # ASCII digits that fit u32, else $null (a malformed pid).
- if ($Text -cnotmatch '\A[0-9]{1,10}\z') { return $null }
+ # ASCII digits (any count, leading zeros allowed) that fit u32, else $null (a malformed pid).
+ if ($Text -cnotmatch '\A[0-9]+\z') { return $null }
+ $Text = $Text.TrimStart('0')
+ if ($Text.Length -eq 0) { return [int64]0 }
+ if ($Text.Length -gt 10) { return $null }
$value = [uint64]$Text
if ($value -gt 4294967295) { return $null }
return [int64]$value
}
+function ConvertTo-MarkerCt([string]$Text) {
+ # A creation time past double's range is +Infinity -- it never matches a live process.
+ # (Windows PowerShell's .NET Framework throws OverflowException there; .NET Core does not.)
+ $value = 0.0
+ if ([double]::TryParse($Text, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$value)) { return $value }
+ return [double]::PositiveInfinity
+}
+
function ConvertFrom-MarkerText([string]$Text) {
# $null = malformed, which every reader treats as dead.
if ($null -eq $Text) { return $null }
@@ -130,15 +141,20 @@ function ConvertFrom-MarkerText([string]$Text) {
$lines = @($Text -split "`n" | ForEach-Object { Get-MarkerLineValue $_ })
if ($lines.Count -lt 2) { return $null }
$ownerPid = ConvertTo-MarkerPid $lines[0]
- if ($null -eq $ownerPid -or $lines[1] -cnotmatch '\A[0-9]{1,18}\z') { return $null }
- $invariant = [Globalization.CultureInfo]::InvariantCulture
+ if ($null -eq $ownerPid -or $lines[1] -cnotmatch '\A[0-9]+\z') { return $null }
+ # Line 2 fits u64 (any digit count), else malformed. StartedAt (arithmetic) clamps
+ # past int64 to "far future"; StartedAtText keeps the exact digits for rewrites.
+ $startedText = $lines[1].TrimStart('0')
+ if ($startedText.Length -eq 0) { $startedText = '0' }
+ if ($startedText.Length -gt 20 -or ($startedText.Length -eq 20 -and [decimal]$startedText -gt [decimal]'18446744073709551615')) { return $null }
+ $started = if ($startedText.Length -le 18) { [int64]$startedText } else { [int64]::MaxValue }
$info = @{
- Pid = $ownerPid; StartedAt = [int64]$lines[1]; Ct = $null; CtText = $null
+ Pid = $ownerPid; StartedAt = $started; StartedAtText = $startedText; Ct = $null; CtText = $null
DelegatePid = 0; DelegateCt = $null; DelegateCtText = $null; Run = $null; Runs = @()
}
if ($lines.Count -ge 3 -and $lines[2] -cmatch '\Act:([0-9]+(\.[0-9]+)?)\z') {
$info.CtText = $Matches[1]
- $info.Ct = [double]::Parse($Matches[1], $invariant)
+ $info.Ct = ConvertTo-MarkerCt $Matches[1]
}
$runs = New-Object System.Collections.Generic.List[string]
for ($i = 3; $i -lt $lines.Count; $i++) {
@@ -148,7 +164,7 @@ function ConvertFrom-MarkerText([string]$Text) {
if ($null -ne $delegatePid -and $delegatePid -gt 0) {
$info.DelegatePid = $delegatePid
$info.DelegateCtText = $Matches[2]
- $info.DelegateCt = [double]::Parse($Matches[2], $invariant)
+ $info.DelegateCt = ConvertTo-MarkerCt $Matches[2]
}
} elseif ($line -cmatch '\Arun:([A-Za-z0-9._-]{1,128})\z') {
if ($null -eq $info.Run) { $info.Run = $Matches[1] }
@@ -159,7 +175,7 @@ function ConvertFrom-MarkerText([string]$Text) {
return $info
}
-function Format-MarkerBody([int64]$OwnerPid, [int64]$StartedAt, $CtText, $DelegateLine, $Runs) {
+function Format-MarkerBody([int64]$OwnerPid, [string]$StartedAt, $CtText, $DelegateLine, $Runs) {
# Canonical, LF framing on purpose: the Rust/TS/Python readers split on "\n".
$body = "$OwnerPid`n$StartedAt`n"
if ($CtText) { $body += "ct:$CtText`n" }
@@ -222,13 +238,13 @@ function Get-MarkerReleaseAction($Info, $Ctx) {
if ($Info.Pid -eq $Ctx.OwnPid -and $ownerState -eq 'ours') {
if ($Info.DelegatePid -gt 0 -and $Info.DelegatePid -ne $Ctx.OwnPid -and $delegateState -eq 'live') {
# A7 rule 5: hand over to the live delegate, keeping started_at and runs.
- return @{ Action = 'rewrite'; Text = (Format-MarkerBody $Info.DelegatePid $Info.StartedAt $Info.DelegateCtText $null $Info.Runs) }
+ return @{ Action = 'rewrite'; Text = (Format-MarkerBody $Info.DelegatePid $Info.StartedAtText $Info.DelegateCtText $null $Info.Runs) }
}
return @{ Action = 'delete'; Text = $null }
}
if ($Info.DelegatePid -gt 0 -and $Info.DelegatePid -eq $Ctx.OwnPid -and $delegateState -eq 'ours') {
if ($ownerState -ne 'dead') {
- return @{ Action = 'rewrite'; Text = (Format-MarkerBody $Info.Pid $Info.StartedAt $Info.CtText $null $Info.Runs) }
+ return @{ Action = 'rewrite'; Text = (Format-MarkerBody $Info.Pid $Info.StartedAtText $Info.CtText $null $Info.Runs) }
}
return @{ Action = 'delete'; Text = $null }
}
diff --git a/scripts/desktop-update/marker.sh b/scripts/desktop-update/marker.sh
index f5285cbb050..591a98769c4 100644
--- a/scripts/desktop-update/marker.sh
+++ b/scripts/desktop-update/marker.sh
@@ -10,7 +10,7 @@
# <pid>\n<started_at>\nct:<creation time>\n[delegate:<pid> ct:<ct>\n][run:<id>\n]
#
# Each line drops one leading BOM (line 1), one trailing CR and surrounding
-# spaces/tabs. Line 1 (a u32 pid) and line 2 (an integer) are required, else
+# spaces/tabs. Line 1 (a u32 pid) and line 2 (digits that fit u64) are required, else
# the marker is MALFORMED. A line 3 that is not `ct:<n>` makes it v1. Lines 4+
# are tagged: the first well-formed `delegate:<pid> ct:<n>` and the first
# well-formed `run:<id>` count; anything else is ignored.
@@ -149,7 +149,17 @@ EOF_MARKER
marker_u32 "$l1" || return 0
M_STARTED="${l2#"${l2%%[!0]*}"}"
M_STARTED="${M_STARTED:-0}"
- M_STARTED_DIGITS="$M_STARTED" # exact value (no leading zeros), before the age clamp
+ M_STARTED_DIGITS="$M_STARTED" # exact value (no leading zeros) for rewrites; M_STARTED is clamped
+ # line 2 fits u64 (any digit count), compared as two 10-digit halves -- never in
+ # shell arithmetic, which is signed 64-bit
+ if [ "${#M_STARTED}" -gt 20 ]; then
+ M_STARTED="" M_STARTED_DIGITS=""; return 0
+ elif [ "${#M_STARTED}" -eq 20 ]; then
+ local hi=$(( 10#${M_STARTED:0:10} )) lo=$(( 10#${M_STARTED:10} ))
+ if [ "$hi" -gt 1844674407 ] || { [ "$hi" -eq 1844674407 ] && [ "$lo" -gt 3709551615 ]; }; then
+ M_STARTED="" M_STARTED_DIGITS=""; return 0
+ fi
+ fi
[ "${#M_STARTED}" -le 18 ] || M_STARTED=999999999999999999 # far future: "young"
M_PID="$U32"
}
@@ -408,7 +418,7 @@ marker_add_delegate_locked() { # pid ct -> name the update child as line 4 under
log "update marker is no longer ours; no delegate written"; return 0
fi
if [ -n "$M_DPID" ] && [ "$M_DPID" != "$1" ] && [ "$J_DELEGATE_STATE" -eq 2 ]; then return 0; fi
- marker_replace "$(marker_canonical "$M_PID" "$M_STARTED" "$M_CT" "$1" "$2")"$'\n' \
+ marker_replace "$(marker_canonical "$M_PID" "$M_STARTED_DIGITS" "$M_CT" "$1" "$2")"$'\n' \
&& log "update marker names update pid $1 (ct $2) as its delegate"
}
@@ -435,7 +445,7 @@ marker_release_locked() { # A7 rule 5 / corpus "release"
[ -n "$M_PID" ] || return 0
if [ "$M_PID" = "$MY_PID" ] && [ "$J_OWNER_STATE" -eq 0 ]; then
if [ -n "$M_DPID" ] && [ "$M_DPID" != "$MY_PID" ] && [ "$J_DELEGATE_STATE" -eq 2 ]; then
- marker_replace "$(marker_canonical "$M_DPID" "$M_STARTED" "$M_DCT")"$'\n'
+ marker_replace "$(marker_canonical "$M_DPID" "$M_STARTED_DIGITS" "$M_DCT")"$'\n'
log "handed the update marker to its live delegate pid $M_DPID (hermes update)"
else
rm -f "$MARKER" 2>/dev/null
@@ -444,7 +454,7 @@ marker_release_locked() { # A7 rule 5 / corpus "release"
fi
if [ "$M_DPID" = "$MY_PID" ] && [ "$J_DELEGATE_STATE" -eq 0 ]; then
if [ "$J_OWNER_STATE" -ne 1 ]; then
- marker_replace "$(marker_canonical "$M_PID" "$M_STARTED" "$M_CT")"$'\n'
+ marker_replace "$(marker_canonical "$M_PID" "$M_STARTED_DIGITS" "$M_CT")"$'\n'
else
rm -f "$MARKER" 2>/dev/null
fi