Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect and unexpected optimizer behavior for Interlocked.Exchange with short value #7810

Closed
stefan-schweiger opened this issue Apr 4, 2017 · 2 comments

Comments

@stefan-schweiger
Copy link

NOTE: Most likely this does not just affect Interlocked.Exchange, but it's easy to reproduce this way.

When turning on the optimization for the following code you get different (and also wrong) output values than from building the application without turning on the optimizer.

using System;
using System.Threading;

class Program
{
    private static int _intToExchange = -1;
    private static short _innerShort = 2;

    static void Main( string[] args )
    {
        var oldValue = Interlocked.Exchange(ref _intToExchange, _innerShort);
        Console.WriteLine( "It was:   {0}", oldValue );
        Console.WriteLine( "It is:    {0}", _intToExchange );
        Console.WriteLine( "Expected: {0}", _innerShort );
    }
}

Without using optimization the output is as expected:

It was:   -1
It is:    2
Expected: 2

But when you turn on optimization (e.g. with <Optimize>true</Optimize> in .csproj) this is the output:

It was:   65535
It is:    -65534
Expected: 2

More about this bug can also be found here on stackoverflow.

@mikedn
Copy link
Contributor

mikedn commented Apr 5, 2017

Most likely this does not just affect Interlocked.Exchange, but it's easy to reproduce this way.

As far as I can tell this affects Interlocked.Add and Interlocked.Exchange. Do you have other examples?

@stefan-schweiger
Copy link
Author

I don't have any other examples, it was just my best guess that this would probably also affect other methods with similar signatures or internals.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants