Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

by jtstroup 20. June 2009 02:20

 

I received a very interesting bug that’s been plaguing our product for a long time now. The problem is that intermittently our WinForm client users would view an image using the WebBrowser Control that would cause the program to crash. Listing this error:

 

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

 

This problem happen when our users attempted to use a SVG Viewer or Tiff Viewer ActiveX object. (Which worked perfectly in IE outside of our app) and this did not occur on every machine. Some machines would work, others would not. We were finally able to narrow it down to those using MS Vista’s 32 bit operating system.

 

After loads of research I was able to narrow it down to Vistas Data Execution Prevention (DEP).

 

Snippet from Wikipedia about this:

 

Data Execution Prevention (DEP) is a security feature that is intended to prevent an application or service from executing code from a non-executable memory region. This helps prevent certain exploits that store code via a buffer overflow, for example. DEP runs in two modes: hardware-enforced DEP for CPUs that can mark memory pages as nonexecutable, and software-enforced DEP with a limited prevention for CPUs that do not have hardware support. Software-enforced DEP does not protect from execution of code in data pages, but instead from another type of attack (SEH overwrite).

DEP was introduced in Windows XP Service Pack 2 and is included in Windows XP Tablet PC Edition 2005, Windows Server 2003Service Pack 1 and later,[1] Windows Vista, and Windows Server 2008.

 

Software configuration

 

So how does one configure DEP. There are multiple ways of configuring this. Boot.ini (XP and VISTA) ,command line and using Microsoft’s ACT 5.0. By far the easiest method is to use the command line.

 

1)      Find your Command prompt and right click and select “run as administrator

2)      Then type “bcdedit.exe /set {current} nx XXXXXXX”

a.       Where XXXXXXX can be the following:

OptIn ( 2 ): This setting is the default configuration for Windows XP. On systems with processors that can implement hardware-enforced DEP, DEP is enabled by default for limited system binaries and programs that "opt-in." With this option, only Windows system binaries are covered by DEP by default.

OptOut( 3 ): This setting is the default configuration for Windows 2003 SP1. DEP is enabled by default for all processes. A list of specific programs that should not have DEP applied can be entered using the System dialog box in Control Panel. Network administrators can use the Application Compatibility Toolkit to "opt-out" one or more programs from DEP protection. System compatibility fixes, or shims, for DEP do take effect. Also note that Windows silently disables DEP for certain executables, such as those packaged with ASPack. [5]

AlwaysOn( 1 ): This setting provides full DEP coverage for the whole system. All processes always run with DEP applied. The exceptions list to exempt specific programs from DEP protection is not available. System compatibility fixes for DEP do not take effect. Programs that have been opted-out by using the Application Compatibility Toolkit run with DEP applied.

AlwaysOff( 0 ): This setting does not provide any DEP coverage for any part of the system, regardless of hardware DEP support. (except in Windows Vista Ultimate)

 

So your commandline might look something like this:

“ bcdedit.exe /set {current} nx AlwaysOff

Once you are done with this you will need to restart your computer.

 

To check the status of your DEP Policy you can run this command:

 

wmic os get dataexecutionprevention_supportpolicy” This will return a number value.  See values above to see what numbers mean.

 

Now this is how we defined what the problem was. But turning off DEP entirely isn’t a viable solution since that will expose your computer to many evil virus and hacking code.

 

Vista provides an interface to select specific programs to be marked as DEP non compliance. You can get to it by going to:

 

1)      Start

2)      Right Click “My Computer” à Properties

3)      Advanced System Settings

4)      Advanced Tab

5)      Under Performance select “Settings

6)      Select the tab “Data Execution Prevention

 

Here is where you are able to add DEP for all programs and services except those I select. This will set your DEP Policy to 3 (OptOut). You can then select the exe programs that enable you to mark that program as DEP noncompliant.

 

Well that might work for other people, but it didn’t work for me on my machine.  Everytime I tried to select my program’s exe, I got the message “This program must run with data execution protection(DEP) enabled. You cannot turn off DEP for this program.

 

Later I found a website that lists a NXCOMPAT and the C# compiler.

Turns out that you can add this switch to your postbuild event of your project and it will mark your compiled project as DEP non compliant.

 

1) Right click on project in Visual Studio

2) Properties

3) Build Events

4) Edit Post Build ...

 

REM Mark project as DEP Noncompliant

call "$(DevEnvDir)..\..\VC\bin\vcvars32.bat"

call "$(DevEnvDir)..\..\VC\bin\editbin.exe" /NXCOMPAT:NO "$(TargetPath)"

 

Another way to do this is to open up the Visual Studio command prompt, browse to your exe location and type:

editbin.exe /NXCOMPAT:NO YourProgram.exe

 

That’s it, now our product will compile and it fixes many crash log errors we were receiving previously.

 

Tags:

C# Development

Comments (12) -

thomas
thomas
9/8/2010 12:07:18 PM #

I'm using win7, 64bit. use webbrowser in multithread. each thread launches a new form which hosts the webbrowser. for some reasons, all solutions above do not work. the error still pops. any other suggestions?

thanks

Reply

jtstroup
jtstroup
9/13/2010 6:00:25 AM #

Thomas,

Try these items in order.

#1: Turn off "Enable the Visual Studio hosting process" under the "Debug" tab. This is because VS makes it's own executable, filename appended with .vshost.exe, which doesn't seem to have the NXCOMPAT bit set, or perhaps it runs as a subprocess of Visual Studio which uses DEP.

#2: If the first way to turn off DEP I listed above doesn't work, try this:
* Run cmd as Adminstrator.
* Call '$(DevEnvDir)..\VC\vcvarsall.bat" x86.
* Type editbin /NXCOMPAT:NO $(TargetPath) and press Enter

#3: If you have any information can you post the top 10 lines from your 'Output' window?

#4: Last resort, Although tedious, one of the "actions" you can preform to debug this is to execute all your prebuild commands yourself from the Visual Studio cmd. In this way some additional information might shed light on exactly what the problem may be.

Let me know what you've tried and the results and we can try to resolve this from that point.



Reply

thomas
thomas
10/8/2010 7:25:59 AM #

Hi, Josh


I followed all the steps, however, the problem still exists.

editbin /NXCOMPAT:NO $(TargetPath)

it does not output anything except the:
Microsoft (R) coff/pe editor.....
...... all right reserved.

no success/fail information.

if I use bcdedit.exe check, it shows nx always off.

but the problem exists.

and the exception happens with the webbrowser when I try to get its document, webbrowser.document.

if in VS (express) debug mode, if I click continue, the program goes on without problem (even in the thread that pops the exception). but if it is not in debug mode, the program just quits.

I tried to find a solution everywhere, but never be able to find one.

the exception happens randomly, and it is always related with webrbrowser's document. it seems somewhere in the webbrowser control, it has a big "internal" problem for multithread usage.

this drive me crazy! Frown

any other suggestions?


thanks

thomas

Reply

thomas
thomas
10/9/2010 6:14:35 PM #

Hi, jtstroup

I tried to reply your message, but for some reasons, my

reply always disappeared. But, I'm trying it one more time

since I really need fix the problem.

I tried the suggestions, but the problem still exists.

I used the command to turn off DEP
bcdedit.exe /set {current} nx AlwaysOff

and then if I use:
wmic os get dataexecutionprevention_supportpolicy

it shows that the DEP is alwaysoff for current boot.


I also use:
editbin.exe /NXCOMPAT:NO MyProgram.exe

to set the exe noncompliant. The output window shows:
Microsoft (R) coff/pe editor version 10.00.30319.01
CopyRight(c) Microsoft Corporation. All rights reserverd.

no more information.

but, the access violation exception still pops.

I noticed that, the exception does not popup ONLY when I

run one thread (or at least I never saw it). This makes me

think maybe the root of the problem exists in IE?

any other suggestions? I searched online for a long time,

but still can not find a solution, which drives me mad, Smile


thanks

thomas

Reply

Joshua
Joshua
11/1/2010 5:16:41 AM #

Thomas,

Sorry I was getting massive spam getting past my filters and screwing up my site. It should be fixed now.


Can you get this to work in a single threaded environment. Possible try that first and then possible we'll move to the multithreaded environment. That way we can eliminate any other errors and then when we make the move from single threaded to multi we can focus only on the changes that occur from that.

Reply

thomas
thomas
11/20/2010 6:10:24 PM #

yes, I tried single thread, and there is no problem. The software I worked on is a tool to automate browser for testing performance. I think the problem is the browser. Probably, the IE browser itself is a single thread (or maybe should say a single process) application. For example, when we open multiple tabs in IE, we can only see one process in taskmgr, but, if we use chrome, we will see one prcess for one tab.

so, maybe when we use multithread to control multiple process, something inside the IE browser got exceptions. Just my guess.


thanks

Thomas

Reply

jtstroup
jtstroup
12/13/2010 10:03:34 AM #

Sorry Thomas,

I don't know what to tell you. I'm fresh out of ideas. Maybe somebody else might have some ideas.

Reply

quanga
quanga
1/13/2011 9:51:40 AM #

This post ended several days of aggravation for me.  Just wanted to say thanks!

Reply

Shane Wealti
Shane Wealti
3/10/2011 5:43:51 AM #

I was having this same problem trying to use a Visual FoxPro based COM object in my C# Visual Studio 2008 .NET 3.5 framework – Windows Form and using editbin.exe /NXCOMPAT:NO MyProgram.exe fixed my probem. Thank you so much! I spent several days trying to figure out what was causing this problem. It worked fine under Windows XP Mode but in my 32-bit Windows 7 PC it would crash at the same spot every time with this error.

Reply

jtstroup
jtstroup
3/10/2011 8:08:18 AM #

Thank you guys for posting, I'm happy that my head banging has helped others and you are letting me know. I appreciate the feedback and comments.

Reply

Khoa Pham
Khoa Pham Vietnam
8/23/2012 9:00:16 PM #

Hi jtstroup,
I was having the same problem with Shane Wealti.
Thank you so much!
Pham Khoa

Reply

jtstroup
jtstroup United States
8/27/2012 5:16:19 AM #

Thanks for letting me know it's helped. I appreciate it!

Reply

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Month List