C# program causes bluescreen?
Asked Answered
D

2

7

This is only the important stuff that the bluescreen shows. I'm on Windows 7 x64.

"A problem has been detected and Windows has been shut down to prevent damage to your computer.

PROCESS_HAS_LOCKED_PAGES

* STOP: 0x00000076 (0x0000000000000000, 0xfffffa8009dcd060, 0x0000000000000011, 0x0000000000000000)"

I can't work on it now because every time I close it I get a bluescreen! The program doesn't do anything yet except run the background worker below. It pings all addresses that could be part of the user's home network and attempts to connect to a certain port that another program will be listening on.

private void NetworkScanner_DoWork(object sender, DoWorkEventArgs e)
    {
        bool ExceptionEncountered = false;
        int IPsProcessed = 0;

        NetworkSearcherOutput = "Starting network scanner...";
        NetworkSearcher.ReportProgress(0);
        Thread.Sleep(1000);

        foreach (IPAddress IP in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IP.AddressFamily == AddressFamily.InterNetwork)
            {
                string[] Octets = IP.ToString().Split('.');
                Octets[3] = "0";

                IPAddress CurrentAddressIteration = StringArrayToIP(Octets);
                while (GetLastOctet(CurrentAddressIteration) != 255)
                {
                    PingReply Reply = new Ping().Send(CurrentAddressIteration, 5);

                    if (Reply.Status == IPStatus.Success)
                    {
                        NetworkSearcherOutput = CurrentAddressIteration.ToString() + " sent response.";
                        NetworkSearcher.ReportProgress(0);
                        Thread.Sleep(500);

                        InClient Client = new InClient(CurrentAddressIteration);

                        try
                        {
                            Client.Connect();

                            SnapshotBox.Image = Client.Receive(typeof(Image));

                            NetworkSearcherOutput = CurrentAddressIteration.ToString() + " is running program.";
                            NetworkSearcher.ReportProgress(0);
                            Thread.Sleep(1000);
                        }

                        catch (Exception E)
                        {
                            // A socket exception is expected when the client is not running the program.
                            if (E is SocketException)
                            {
                                Client.Close();

                                NetworkSearcherOutput = CurrentAddressIteration.ToString() + " is not running program.";
                                NetworkSearcher.ReportProgress(0);
                                Thread.Sleep(1000);
                            }

                            //Unhandled exception. Show messagebox and close.
                            else
                            {
                                MessageBox.Show("Network scanner encountered an unhandled exception.\n\n" + E.GetType().ToString() + ": " + E.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                ExceptionEncountered = true;
                                break;
                            }
                        }
                    }

                    else
                    {
                        NetworkSearcherOutput = CurrentAddressIteration.ToString() + " did not respond.";
                        NetworkSearcher.ReportProgress(0);
                    }

                    IPsProcessed++;

                    if (IPsProcessed == 5)
                    {
                        NetworkSearcher.ReportProgress(2);
                        IPsProcessed = 0;
                    }

                    Octets = CurrentAddressIteration.ToString().Split('.');
                    Octets[3] = (Int32.Parse(Octets[3]) + 1).ToString();
                    CurrentAddressIteration = StringArrayToIP(Octets);
                }
            }
        }

        if (!ExceptionEncountered)
        {
            NetworkSearcherOutput = "Network scanning complete.";
            NetworkSearcher.ReportProgress(0);
            NetworkSearcher.ReportProgress(100);
        }

        else
        {
            NetworkSearcherOutput = "Network scanning encountered an error.";
            NetworkSearcher.ReportProgress(-1);
        }

I thought C# programs were supposed to never cause bluescreens?

Dactylogram answered 2/11, 2011 at 4:23 Comment(10)
You're not supposed to be able to cause bluescreens directly. but if the underlying code from Microsoft has a bug it could cause itSumpter
No program is "supposed" to create blue screens...Individual
support.microsoft.com/kb/256010 implies this is related to a driver misbehaving, perhaps a network driver based on your program. This KB indicates ways of indicating which driver is likely at fault; it has nothing to do with your managed code.Cheerio
No, user-mode code can't cause a blue screen, even if it's from Microsoft. Are you running any VPN software, or security software?Sava
@Andrew, I think you'll find that the ping'o'death was supposed to cause BSODs but, admittedly, that was a special case :-) In any case, what if (like me) you have a class A home network? :-)Mathi
I once had a piece of VPN software with "endpoint security", that crashed when too many DLLs were opened in a short period of time. We had been blaming Visual Studio, but the same problem happened with a program I wrote to test this theory - it just loaded all the assemblies it could find, in a loop. Only a few loops were necessary to crash the system.Sava
@Mathi - got me there! heheIndividual
I'm not running any VPN or security software. Thank you Joe for the Microsoft link. I should be able to find out what's going on.Dactylogram
And the culprit is tcpip.sys, should it help anyone else.Dactylogram
@WildBamaBoy: If this is .NET 4. Known bug. Please see my answer.Walloping
E
4

Just to be clear, there is no way for "user" mode code to forcibly create a blue screen in windows, unless it uses undocumented APIs and or forces bad data into a driver. Your C# code is likely not be at fault here, as if you use the user mode classes (Socket) then the socket is responsible for not crashing your computer.

As @Joe has commented Microsoft Support KB Article 256010 clearly describes this stop message, but better yet has clear instructions on capturing the driver name responsible for this error.

Note that any software firewall that you have installed also is involved at kernel mode level so could also be responsible for this error. I recommend you follow the KB articles advice and try to find out what is at fault. But you could also ensure that you have updated your network drivers and firewall/VPN software to the latest stable versions.

Edibles answered 2/11, 2011 at 4:40 Comment(8)
Condense your code to a reproduction and submit to MS Connect. You may have just found a bug in Windows 7.Edibles
Perhaps those who have more wisdom than I can correct me, but I'm fairly sure that Socket is implemented in TcpIp.sysEdibles
@Spence: Sorry you are incorrect. This is a bug in .NET 4. And already reported. See my answer.Walloping
No, it's generating a BSOD. Therefore the bug is either in the windows API, or in a driver accessing the API from Kernel Mode. Your MS connect has clearly shown that the bug occurs after a call to a Windows DLL. Granted the code is using a different API in .Net 4.0, it doesn't make that API any less broken...Edibles
@leppie: Just because it is .NET 4 that is triggering the bug does not mean that the bug is in .NET 4!Teena
@Spence: Or the usage of the API is broken. Either way, this is related to iphlpapi.dll. Not sure if that is being used elsewhere in Windows.Walloping
Doesn't matter. The Windows Kernel is responsible (either through code or contract) to guarantee the integrity of operating system (i.e hardware, memory, CPU scheduling etc.). If ANY use of an API triggers a BSOD then this API is at fault, provided it is published. The only exception is using an undocumented API to do so, however this is a security issue now, if user mode code can cause a BSOD then this is a Denial of Service attackEdibles
Note when you are writing kernel mode code (Drivers/Antivirus) then the API will have a contract that must be met, as there is no OS to protect you at this level.Edibles
W
7

I discovered this issue a few weeks back. It only happens when using .NET 4.

Reported at MS Connect.

Edit:

(Will*) Add this link to MS Connect bug report.

*login.live.com is going into an infinite loop again...

Walloping answered 2/11, 2011 at 5:45 Comment(1)
I just want to verify that yes it only happens in .NET 4. I switched frameworks and the crash no longer happens. However I tested the BSOD causing program on another PC with VS 2010 and nothing happened... Someone give it a try.Dactylogram
E
4

Just to be clear, there is no way for "user" mode code to forcibly create a blue screen in windows, unless it uses undocumented APIs and or forces bad data into a driver. Your C# code is likely not be at fault here, as if you use the user mode classes (Socket) then the socket is responsible for not crashing your computer.

As @Joe has commented Microsoft Support KB Article 256010 clearly describes this stop message, but better yet has clear instructions on capturing the driver name responsible for this error.

Note that any software firewall that you have installed also is involved at kernel mode level so could also be responsible for this error. I recommend you follow the KB articles advice and try to find out what is at fault. But you could also ensure that you have updated your network drivers and firewall/VPN software to the latest stable versions.

Edibles answered 2/11, 2011 at 4:40 Comment(8)
Condense your code to a reproduction and submit to MS Connect. You may have just found a bug in Windows 7.Edibles
Perhaps those who have more wisdom than I can correct me, but I'm fairly sure that Socket is implemented in TcpIp.sysEdibles
@Spence: Sorry you are incorrect. This is a bug in .NET 4. And already reported. See my answer.Walloping
No, it's generating a BSOD. Therefore the bug is either in the windows API, or in a driver accessing the API from Kernel Mode. Your MS connect has clearly shown that the bug occurs after a call to a Windows DLL. Granted the code is using a different API in .Net 4.0, it doesn't make that API any less broken...Edibles
@leppie: Just because it is .NET 4 that is triggering the bug does not mean that the bug is in .NET 4!Teena
@Spence: Or the usage of the API is broken. Either way, this is related to iphlpapi.dll. Not sure if that is being used elsewhere in Windows.Walloping
Doesn't matter. The Windows Kernel is responsible (either through code or contract) to guarantee the integrity of operating system (i.e hardware, memory, CPU scheduling etc.). If ANY use of an API triggers a BSOD then this API is at fault, provided it is published. The only exception is using an undocumented API to do so, however this is a security issue now, if user mode code can cause a BSOD then this is a Denial of Service attackEdibles
Note when you are writing kernel mode code (Drivers/Antivirus) then the API will have a contract that must be met, as there is no OS to protect you at this level.Edibles

© 2022 - 2024 — McMap. All rights reserved.