Skip to content

Don't expose a memory reference for non-pointer evaluation results#1601

Open
tieo wants to merge 1 commit into
microsoft:mainfrom
tieo:fix-scalar-memory-reference
Open

Don't expose a memory reference for non-pointer evaluation results#1601
tieo wants to merge 1 commit into
microsoft:mainfrom
tieo:fix-scalar-memory-reference

Conversation

@tieo

@tieo tieo commented Jun 29, 2026

Copy link
Copy Markdown

Problem

In a debug data-tip (or the Variables view), hovering a non-pointer scalar shows the value correctly but attaches a memoryReference, so VS Code renders the "view binary data" hex icon and clicking it navigates to an address equal to the value. For example, hovering a uint32_t stsdig whose value is 1 offers a memory view that opens at address 0x1.

Cause

AD7Property.GetMemoryContext (src/MIDebugEngine/AD7.Impl/AD7Property.cs) tries to interpret the result's display value as a memory address, for every type:

// try to interpret the result as an address
string v = _variableInformation.Value;
...
UInt64.TryParse(v, NumberStyles.Any, ...)   // "1" -> addr = 1
ppMemory = new AD7MemoryAddress(_engine, addr, null);

For a pointer this is correct — the value is an address (0x...). For a plain scalar (e.g. int/uint32_t) the value is a number, not an address, so UInt64.TryParse("1") succeeds and the memory reference becomes 0x1. OpenDebugAD7 then reports that as memoryReference on the evaluate/variables response, and VS Code's memory navigation goes to address 1.

Fix

Only treat the value as an address when the result is a pointer (or an array, which already re-evaluates to its address with &(...) further down). For any other type, return S_GETMEMORYCONTEXT_NO_MEMORY_CONTEXT so no memoryReference is reported and no spurious memory icon/navigation appears.

bool isArrayValue = v[0] == '[' && v[v.Length - 1] == ']';
string typeName = _variableInformation.TypeName;
bool isPointer = !string.IsNullOrEmpty(typeName) && typeName.TrimEnd().EndsWith("*", StringComparison.Ordinal);
if (!isArrayValue && !isPointer)
{
    return AD7_HRESULT.S_GETMEMORYCONTEXT_NO_MEMORY_CONTEXT;
}

TypeName is already on IVariableInformation, and the same EndsWith("*") pointer test is used elsewhere in Variables.cs (IsPointer).

Testing

Built the patched engine and ran it against a live cppdbg/gdb session:

  • Pointer (int32_t *, const uint32_t *) — still shows its address and the memory icon, unchanged.
  • Scalar (uint32_t) — now shows just the value, with no memory icon and no navigation to 0x<value>.
  • Array — unchanged (still resolves to its base address via the existing &(...) path).

Note: the change was written with the help of an LLM and verified manually in VS Code against a real gdb target.

@gregg-miskelly

Copy link
Copy Markdown
Member

Hi @tieo thank you for taking the time to investigate this.

I didn't debug this, so apologies if I am incorrect, but, at least in Visual Studio, I believe your change would break entering an address into the memory or disassembly window. I haven't looked at how the memory or disassembly windows work in VS Code to know if they let the user enter an expression for the memory address to know if they have the same problem or not.

Are you looking specifically to block the memory window just from data tips?

A non-pointer scalar shown in a data tip or the Variables view carried a
memoryReference, so VS Code rendered the "view binary data" icon and
navigated to an address equal to the value (hovering a uint32_t of 1
opened a memory view at 0x1).

The earlier approach gated AD7Property.GetMemoryContext in the engine, but
that method is shared with Visual Studio, where the Memory and Disassembly
windows resolve a typed address expression through it. Restricting it
there breaks entering a scalar or address expression into those windows.

Move the restriction to the DAP layer: in
AD7Utils.GetMemoryReferenceFromIDebugProperty, emit a memoryReference only
when the property type is a pointer or an array. GetMemoryContext is left
unchanged, so the Visual Studio memory and disassembly navigation keep
working, while VS Code no longer offers a memory view for scalars. VS Code
has no free-form address entry, so gating the reference removes no entry
point there.
@tieo tieo force-pushed the fix-scalar-memory-reference branch from a3a8a4f to 033b659 Compare June 30, 2026 10:22
@tieo

tieo commented Jun 30, 2026

Copy link
Copy Markdown
Author

Sadly I don't have Visual Studio (just Code), but to quickly explain why I made this:

What it currently looks like:
image
(see the int value being treated like an address, same as the pointer)

What it should look like (and does if I use the engine patch above):
image

I'm sadly pretty clueless about the inner workings of the engine or how it affects VS, which is why I used an LLM to make the edit. It also proposed to move the edit to OpenDebugAD7.dll (see new commit, tested again only in VSC).
I could also convert to an issue if that's better than "Vibe Coding" my way through this 🤠.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants