none
.NET 2.0 application can be affected by .NET 3.5 installation RRS feed

  • Question

  • Hi
    I have the following problem: an application developed with.NET 2.0 changed its behaviour when the .NET 3.5 was installed.
    Is it possible?
    The problem is about a particular static function inside a class. The function is as follow:

    public static class SerializerShort
    {
            public static short Deserialize(byte[] buffer, int index, out int byteCount)
            {
                byteCount = sizeof(short);
                return BitConverter.ToInt16(buffer, index);
            }
    }

    if I call this function in the following way:

    int byteCount = 0;
    int index = 0;
    byte[] buffer = new byte[100];
    // some initialization of buffer

    return (RecordId)SerializerShort.Deserialize(buffer, index + byteCount, out byteCount);

    in a PC with just .NET 2.0 installed the byteCount output is ok and also the index variable inside the static function is ok.
    After installed .NET 3.5 SP1 the behaviuor of the code changes: inside the static function after the assign of byteCount also the value of the index variable changes. It looks like the value of the index variable is affected by the change of the byteCount variable.
    We found out that a code like the following solves the problem:

    int byteCount = 0;
    int outCount = 0;
    int index = 0;
    byte[] buffer = new byte[100];
    // some initialization of buffer

    return (RecordId)SerializerShort.Deserialize(buffer, index + byteCount, out outCount);

    I'd like to understand if the installation of .NET 3.5 can affect is some way the functionality of application developed by .NET 2.0. Or otherwise if my code is wrong and why?

    Marco

    Thursday, December 18, 2008 2:12 PM

Answers

  • Yes, I can repro this bug using BC's code.  To see it, use the Release build and select Tools + Options, Debugging, General, turn off "Supress JIT optimizations on module load".  This is clearly a bug in the JIT compiler when it inlines your Deserialize() method.  It doesn't generate code for the index + byteCount expression properly, using a hardcoded value of "2" instead of 0.  Workarounds are lifting the index + byteCount expression out of the method call or turning off inlining by using MethodImplOptions.NoInlining.

    This is a very serious bug in the optimizer for the 3.5 SP1 JIT compiler.  Bring it to the attention of Microsoft by posting to connect.microsoft.com, reference this thread to document your problem.  Calling Microsoft CSS directly is certainly an option too to get quicker feedback on this issue.  You are guaranteed to get your money back, this is clearly a bug.

    Hans Passant.
    • Marked as answer by marco.beninca Friday, December 19, 2008 2:46 PM
    • Unmarked as answer by marco.beninca Friday, December 19, 2008 2:51 PM
    • Marked as answer by Zhi-Xin Ye Tuesday, December 23, 2008 8:41 AM
    Friday, December 19, 2008 2:07 PM
    Moderator
  • You should do it.  Then, if Microsoft has any questions, they can ask the right person actually affected by the problem.  Since you post to this forum, you already have a Microsoft Windows Live ID login and it should be a quick and easy process.

    Go to http://connect.microsoft.com/visualstudio and press the Submit a Bug button.
    • Marked as answer by Zhi-Xin Ye Tuesday, December 23, 2008 8:41 AM
    Saturday, December 20, 2008 12:35 AM

All replies

  • > I'd like to understand if the installation of .NET 3.5 can affect is some way the functionality of application developed by .NET 2.0.

    .NET 3.5 includes .NET 2.0 SP1.  In general, one would expect that any problems you find isolated to the systems where .NET 3.5 is installed would also occur on .NET 2.0 systems where the .NET 2.0 SP1 service pack is installed.

    My system has .NET 3.5 SP1 (http://www.microsoft.com/downloads/details.aspx?FamilyID=ab99342f-5d1a-413d-8319-81da479ab0d7&DisplayLang=en), which should equate to .NET 2.0 SP2, installed.  The following program produced the correct results:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
     
    namespace ConsoleApplication1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                int byteCount = 0;  
                int index = 0;  
                byte[] buffer = new byte[100];  
                buffer[0] = 0x12;  
                buffer[1] = 0x34;  
                short shrt = SerializerShort.Deserialize(buffer, index + byteCount, out byteCount);  
                Console.WriteLine(shrt.ToString("x")); // Prints "3412"  
                Console.WriteLine(index); // Prints "0"  
                Console.WriteLine(byteCount); // Prints "2"  
                Console.WriteLine(Environment.Version); // Prints "2.0.50727.3053"  
                Console.ReadLine();  
            }  
     
            public static class SerializerShort  
            {  
                public static short Deserialize(byte[] buffer, int index, out int byteCount)  
                {  
                    byteCount = sizeof(short);  
                    return BitConverter.ToInt16(buffer, index);  
                }  
            }  
        }  
    }  
     


    Friday, December 19, 2008 1:18 AM
  • Hi
    I tried your code and it creates the problem anyway.
    The situation is as follow:
    • I create your test application in VS 2005 SP1 in a PC without the .NET 3.5 installed. I compiled it with .NET 2.0 SP2
    • In my PC everything is ok... the output is

    3412
    0
    2
    2.0.5072731433

    • When I install the test application on a PC with .NET 1.1, .NET 2.0 SP2, .NET 3.0 SP2, and .NET 3.5 SP1 installed the problem appears: the output is as follow:

    0
    0
    2
    2.0.5072731433

    So the problem can be reproduced sistematically. Please check!

    Try also the code below in the same situation

    using System;  
    using System.Collections.Generic;  
    //using System.Linq;  
    using System.Text;  
     
    namespace ConsoleApplication1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                int byteCount = 0;  
                int index = 0;  
                byte[] buffer = new byte[100];  
                buffer[0] = 0x12;  
                buffer[1] = 0x34;  
                buffer[2] = 0x56;  
                buffer[3] = 0x78;  
                short shrt = SerializerShort.Deserialize(buffer, index + byteCount, out byteCount);  
                Console.WriteLine(shrt.ToString("x")); // Prints "3412"   
                byteCount = 0;  
                int i1 = SerializerInt.Deserialize(buffer, index + byteCount, out byteCount);  
                Console.WriteLine(i1.ToString("x")); // Prints "78563412"     
                Console.WriteLine(index); // Prints "0"     
                Console.WriteLine(byteCount); // Prints "2"     
                Console.WriteLine(Environment.Version); // Prints "2.0.50727.3053"     
                Console.ReadLine();  
            }  
     
            public static class SerializerShort  
            {  
                public static short Deserialize(byte[] buffer, int index, out int byteCount)  
                {  
                    byteCount = sizeof(short);  
                    return BitConverter.ToInt16(buffer, index);  
                }  
            }  
            public static class SerializerInt  
            {  
                public static int Deserialize(byte[] buffer, int index, out int byteCount)  
                {  
                    byteCount = sizeof(byte);  
                    return BitConverter.ToInt32(buffer, index);  
                }  
            }  
     
        }  
    }  
     
     
     


    My most worry is that all my code written in 2.0 should be re-tested again in the case of a machine with 3.5 installed. Also I cannot know if the costumer machine where my software is running has the 3.5 installed or not.

    I pretty sure that the installation of 3.5 affected the behaviour of the application... so what I ask is to investigate if this situation is a bug or not. Please help!

    Marco
    Friday, December 19, 2008 7:34 AM
  • Yes, I can repro this bug using BC's code.  To see it, use the Release build and select Tools + Options, Debugging, General, turn off "Supress JIT optimizations on module load".  This is clearly a bug in the JIT compiler when it inlines your Deserialize() method.  It doesn't generate code for the index + byteCount expression properly, using a hardcoded value of "2" instead of 0.  Workarounds are lifting the index + byteCount expression out of the method call or turning off inlining by using MethodImplOptions.NoInlining.

    This is a very serious bug in the optimizer for the 3.5 SP1 JIT compiler.  Bring it to the attention of Microsoft by posting to connect.microsoft.com, reference this thread to document your problem.  Calling Microsoft CSS directly is certainly an option too to get quicker feedback on this issue.  You are guaranteed to get your money back, this is clearly a bug.

    Hans Passant.
    • Marked as answer by marco.beninca Friday, December 19, 2008 2:46 PM
    • Unmarked as answer by marco.beninca Friday, December 19, 2008 2:51 PM
    • Marked as answer by Zhi-Xin Ye Tuesday, December 23, 2008 8:41 AM
    Friday, December 19, 2008 2:07 PM
    Moderator
  • Hi
    Thanks that's ok. Can you post the bug on connect.microsoft.com for us?
    Marco
    Friday, December 19, 2008 2:51 PM
  • You should do it.  Then, if Microsoft has any questions, they can ask the right person actually affected by the problem.  Since you post to this forum, you already have a Microsoft Windows Live ID login and it should be a quick and easy process.

    Go to http://connect.microsoft.com/visualstudio and press the Submit a Bug button.
    • Marked as answer by Zhi-Xin Ye Tuesday, December 23, 2008 8:41 AM
    Saturday, December 20, 2008 12:35 AM