0

I'm playing around with native code generation from C#. I'm using HeapCreate and HeapAlloc to allocate executable memory to which I write x86-64 instructions. I then use Marshal.GetDelegateForFunctionPointer to turn that into a .NET delegate and call it. I am able to properly create and call a simple function that returns a constant value.

The issue is that I am unable to step into that function in VS2010. When I use "step into" in the disassembly window on the call opcode, it simply steps over and I never get to see the disassembled function. I've tried browsing to my executable memory address in the disassembly window or adding a breakpoint at that address but Visual Studio complains that the memory is not executable. I've also tried generating an "int 3" instruction but it only causes my application to exit without warning (as if Environment.Exit was called). I've done a simple test program in C++ that uses the same technique to generate a function and call it and I can step into it without any issues.

How can I step into a native function I've generated in an allocated chunk of executable memory in a .NET Visual Studio project? Does anything prevent this from being possible? (I've got the 'just my code' option unchecked)

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Trillian
  • 6,207
  • 1
  • 26
  • 36

2 Answers2

1

Did you set "Enable Unmanaged code debugging" as described on MSDN?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
jcopenha
  • 3,935
  • 1
  • 17
  • 15
  • Aaah I hoped I had only forgotten such a flag. Visual Studio still won't step into the call for some reason, but injecting an "int 3" breakpoint now works and I can step from there. It's not ideal but I should be able to work this way. Thanks! – Trillian Feb 08 '12 at 01:26
1

If you're really using HeapAlloc, I'm surprised that this worked at all. I was under the impression that memory allocated by HeapAlloc doesn't have execute permission on it. I usually use VirtualAlloc to allocate such memory and explicitly set the execute flag with VirtualProtect.

  • HeapCreate provides a HEAP_CREATE_ENABLE_EXECUTE flag ("Use this flag heap in applications that run code from the heap"). I didn't try with VirtualAlloc but it seems more complex to use. – Trillian Feb 08 '12 at 01:23