2

I have a 64 bit reference to an object where the low order 32 bits of the reference are getting overwritten with 0xFFFFFFFF. I can't figure out how to set a data breakpoint on the bytes for the reference itself because the watch window gives me no way to acquire the address of the reference.

Matthias
  • 4,481
  • 12
  • 45
  • 84
Joseph
  • 308
  • 1
  • 9
  • One possible way: find some code where the reference is initialized or the value is updated. Look at the disassembly to see how the code is accessing the reference to get the value referred to. – 1201ProgramAlarm Aug 09 '18 at 21:36
  • Can Visual Studio set a data breakpoint on a specific address? If so, you could figure out an address of an object adjacent to the reference, and then offset it. – HolyBlackCat Aug 09 '18 at 21:51
  • I'm not sure I understand the problem. What is wrong wit adding &yourreference in the Data Breakpoint dialog? It also accepts expressions. – zdf Aug 09 '18 at 22:12
  • References don't have their own memory addresses (see [Is there any way to find the address of a reference?](https://stackoverflow.com/questions/1950779/)). `&yourreference` returns the address that `yourreference` refers to, not the address of `yourreference` itself. So you can't set a data breakpoint on a reference directly, only on the address that it references to. Change the reference into a pointer instead, then you can get the address of the pointer and set a data breakpoint on it. – Remy Lebeau Aug 09 '18 at 22:12
  • @RemyLebeau I think he needs the address of the referenced data. – zdf Aug 09 '18 at 22:14
  • @ZDF it is clear from the question that the reference itself is what is being overwritten, not the object that is being referred to. He wants to put a breakpoint on the reference itself to catch when it is being overwritten. You can't do that with references, but you can with pointers. – Remy Lebeau Aug 09 '18 at 22:15
  • @RemyLebeau I do not see how he knows the reference itself is changed. – zdf Aug 09 '18 at 22:17
  • 2
    @ZDF references are commonly *implemented* using pointers. While a reference doesn't have an identifyable address in code, it usually does have an addressable value in memory that you can see in a debugger. – Remy Lebeau Aug 09 '18 at 22:22
  • @RemyLebeau So, he sees the address, but he doesn't know it? I would say the question is not clear. – zdf Aug 09 '18 at 22:27
  • @HolyBlackCat I didn't say he couldn't put a breakpoint on a reference at all, just that he couldn't use an expression like `&yourreference` to get that address of the reference itself. If he can work out the real address of the reference itself, he can put a breakpoint on it. – Remy Lebeau Aug 09 '18 at 22:33
  • @RemyLebeau I guess I'm too sleepy, didn't read the comment properly. :/ – HolyBlackCat Aug 09 '18 at 22:35
  • Which version of Visual Studio? The concept you want is to view the breakpoint or breakpoint properties. Usually there is a "condition" area, where you can set the condition that causes the breakpoint to trigger. Visual Studio may also have the ability to trigger or stop execution when a memory location meets a condition. – Thomas Matthews Aug 09 '18 at 23:44

2 Answers2

1

I see two solutions (if I correctly understood the problem):

  • change the reference to a pointer;
  • add a dummy variable in front of your reference - see the code below - and set the break-point to its address.

class object_t
{
public:
  int i;
};

class test_t
{
public:
  int64_t dummy {};
  object_t& ro;
  test_t( object_t& aro ) : ro { aro } {}
};

int main()
{
  object_t obj;
  test_t c { obj };

  // without dummy
  int64_t* p = (int64_t*)&c;
  *(int32_t*)p = 0xffffffff; // simulates memory corruption
  c.ro.i = 0; // exception

  // with dummy
  int64_t* p = (int64_t*)&c;
  *(int32_t*)p = 0xffffffff; // will break 

  return 0;
}
zdf
  • 4,382
  • 3
  • 18
  • 29
  • 1
    This is what I ended up doing, but this is in boost beast so I didn't want to change it. Oh well, it worked. – Joseph Aug 12 '18 at 00:44
0

I don't know any direct way to do this. But, here's a possible solution:

  • first, find where the variable is approximately: if you have a variable next to it, then get its address. If no variable nearby, then if the reference on the stack, then get the stack pointer (esp/rsp on x86). If the reference is in an object which is not on stack, then use the this pointer.
  • second, use the memory window, go to this approximate address, and search for the value of the reference, it will be somewhere nearby.
geza
  • 28,403
  • 6
  • 61
  • 135
  • I guess a simpler solution would be to change the reference to a pointer. – zdf Aug 10 '18 at 09:33
  • @ZDF: if you're willing to modify the code... But what if this means a lot of changes (changing `.` to `->`, adding `*`)? What if the reference is a const reference, which has a temporary bound to? The process I outlined can be done pretty fast. Maybe recompilation/link time takes longer than what I wrote. – geza Aug 10 '18 at 09:37