Skip to content

[cDAC] Arm64Unwinder. An overflow in the arithmetic expression (ulong)spOffset of type ulong may occur#129967

Closed
AristidesASm wants to merge 1 commit into
dotnet:mainfrom
AristidesASm:main
Closed

[cDAC] Arm64Unwinder. An overflow in the arithmetic expression (ulong)spOffset of type ulong may occur#129967
AristidesASm wants to merge 1 commit into
dotnet:mainfrom
AristidesASm:main

Conversation

@AristidesASm

Copy link
Copy Markdown
Contributor

problem line(s):

 if (spOffset < 0)
        {
        context.Sp -= (ulong)spOffset;
       }

Casting a negative int to a unsigned ulong will result is widening conversions rather than simply taking the absolute value
ex:

int negativeValue = -5;
(ulong)negativeValue; // this will be 18446744073709551611

but:

int negativeValue = -5;
ulong beginValue = 555;
beginValue -= (ulong)negativeValue;
Console.WriteLine(beginValue);
// beginValue will be 560 ( again not expected result)

Base on comment:

    /// Specifies a stack offset. Positive values are simply used
    /// as a base offset. Negative values assume a pre-decrement behavior:

, we expect subtraction. So to achieve the desired result without connecting additional libraries, we can use the approach in this commit


Found by Linux Verification Center (linuxtesting.org) with SVACE.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 29, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan

Copy link
Copy Markdown
Member

According to the native version:

if (SpOffset < 0) {
ContextRecord->Sp -= SpOffset;
}

-= negative value is the expected behavior. The cast is simply to satisfy the type.

@AristidesASm

Copy link
Copy Markdown
Contributor Author

According to the native version:

if (SpOffset < 0) {
ContextRecord->Sp -= SpOffset;
}

-= negative value is the expected behavior. The cast is simply to satisfy the type.

logic is right, but here another problem:
cast negative signed to unsigned type don't get modulo value.
Please see explain in PR message

@huoyaoyuan

Copy link
Copy Markdown
Member

cast negative signed to unsigned type don't get modulo value.

There's no intent to get the modulo value. -= spOffset is just a shorthand of += (-spOffset). In C#, integer overflow is well defined as wrapping.

Casting a negative int to a unsigned ulong will result is widening conversions rather than simply taking the absolute value

Nobody will ever expect to get the absolute value.

@huoyaoyuan

Copy link
Copy Markdown
Member
int negativeValue = -5;
ulong beginValue = 555;
beginValue -= (ulong)negativeValue;
Console.WriteLine(beginValue);
// beginValue will be 560 ( again not expected result)

This is very expected behavior and well-defined in C#. 555 - (-5) is 555 + 5 is 560.

@AristidesASm

Copy link
Copy Markdown
Contributor Author
int negativeValue = -5;
ulong beginValue = 555;
beginValue -= (ulong)negativeValue;
Console.WriteLine(beginValue);
// beginValue will be 560 ( again not expected result)

This is very expected behavior and well-defined in C#. 555 - (-5) is 555 + 5 is 560.

I see.
I got Mistake with understanding work logic. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Diagnostics-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants