llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DebugInfo][MemCpyOpt] memcpyopt leaves local arrays described by unwritten stack slots

Open MrEven132 opened this issue 4 months ago • 4 comments

Description

After running memcpyopt, LLDB prints different contents for the local arrays a and b at line 7.

At the source level, line 7 is reached after:

char a[4] = "abc", b[4], c[4];
memcpy(b, a, 4);
memcpy(c, b, 4);

So both a and b should contain "abc" when stopped at:

return c[0];

Before memcpyopt, LLDB reports a = "abc" and b = "abc". After memcpyopt, LLDB reports both arrays as empty strings in this run.

Reproducer

case.c:

#include <string.h>

char func() {
    char a[4] = "abc", b[4], c[4];
    memcpy(b, a, 4);
    memcpy(c, b, 4);
    return c[0];
}

int main() {
    func();
    return 0;
}

Build pipeline:

clang -g -O0 -Xclang -disable-O0-optnone -emit-llvm -S case.c -o case.ll
opt -passes="instsimplify" -S case.ll -o src.ll
opt -passes="memcpyopt" -S src.ll -o tgt.ll

clang src.ll -o src.out
clang tgt.ll -o tgt.out

Debugging Script

#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

clang -g -O0 -Xclang -disable-O0-optnone -emit-llvm -S case.c -o case.ll
opt -passes="instsimplify" -S case.ll -o src.ll
opt -passes="memcpyopt" -S src.ll -o tgt.ll

clang src.ll -o src.out
clang tgt.ll -o tgt.out

for exe in src.out tgt.out; do
  echo "== $exe =="
  lldb --batch \
    -o 'breakpoint set --file case.c --line 7' \
    -o run \
    -o 'frame variable a b' \
    -- "./$exe"
  echo
done

Actual Behavior

Before memcpyopt, LLDB reports the expected array contents at case.c:7:12:

== src.out ==
Breakpoint 1: where = src.out`func + 25 at case.c:7:12

frame #0: ... src.out`func at case.c:7:12

(lldb) frame variable a b
(char[4]) a = "abc"
(char[4]) b = "abc"

After memcpyopt, LLDB reports different contents:

== tgt.out ==
Breakpoint 1: where = tgt.out`func + 13 at case.c:7:12

frame #0: ... tgt.out`func at case.c:7:12

(lldb) frame variable a b
(char[4]) a = ""
(char[4]) b = ""

Expected Behavior

At line 7, LLDB should not show incorrect concrete contents for a or b.

The source-level values of both arrays are "abc" at this point. If memcpyopt cannot preserve those aggregate values after forwarding/removing the copies, then a and b should be reported as unavailable/optimized out instead of being described by stack slots whose contents no longer match the source variables.

Observed Directly in .ll

Before memcpyopt, the full func body in src.ll initializes a, then copies a to b, and then copies b to c:

define dso_local signext i8 @func() #0 !dbg !9 {
  %1 = alloca [4 x i8], align 1
  %2 = alloca [4 x i8], align 1
  %3 = alloca [4 x i8], align 1
    #dbg_declare(ptr %1, !14, !DIExpression(), !18)
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr align 1 @__const.func.a, i64 4, i1 false), !dbg !18
    #dbg_declare(ptr %2, !19, !DIExpression(), !20)
    #dbg_declare(ptr %3, !21, !DIExpression(), !22)
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %2, ptr align 1 %1, i64 4, i1 false), !dbg !23
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 %2, i64 4, i1 false), !dbg !24
  %4 = load i8, ptr %3, align 1, !dbg !25
  ret i8 %4, !dbg !26
}

After memcpyopt, the full func body in tgt.ll forwards the copies and only writes to the storage for c:

define dso_local signext i8 @func() #0 !dbg !9 {
  %1 = alloca [4 x i8], align 1
  %2 = alloca [4 x i8], align 1
  %3 = alloca [4 x i8], align 1
    #dbg_declare(ptr %1, !14, !DIExpression(), !18)
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 @__const.func.a, i64 4, i1 false), !dbg !18
    #dbg_declare(ptr %2, !19, !DIExpression(), !20)
    #dbg_declare(ptr %3, !21, !DIExpression(), !22)
  %4 = load i8, ptr %3, align 1, !dbg !23
  ret i8 %4, !dbg !24
}

The dbg_declare intrinsics for a and b still describe %1 and %2 as their storage:

!14 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 4, type: !15)
!19 = !DILocalVariable(name: "b", scope: !9, file: !1, line: 4, type: !15)

However, after memcpyopt, %1 and %2 are no longer initialized. This leaves the debugger reading the wrong memory for a and b.

Environment

clang version 24.0.0git
llvm-project revision: b443896c13aded8b40d7bae4a5a9adbc96fd0d31
Target: x86_64-unknown-linux-gnu

LLVM version 24.0.0git
LLDB version 24.0.0git
Linux x86_64

Notes

My guess is that memcpyopt correctly forwards the memory copies for the optimized program value, but does not update or invalidate the debug declarations for the source objects whose backing stores are no longer written. This is especially visible for aggregate local variables because LLDB reads their contents through the address recorded by dbg_declare.

MrEven132 avatar Aug 03 '26 08:08 MrEven132

@llvm/issue-subscribers-debuginfo

Author: MrEven132

## Description

After running memcpyopt, LLDB prints different contents for the local arrays a and b at line 7.

At the source level, line 7 is reached after:

char a[4] = "abc", b[4], c[4];
memcpy(b, a, 4);
memcpy(c, b, 4);

So both a and b should contain "abc" when stopped at:

return c[0];

Before memcpyopt, LLDB reports a = "abc" and b = "abc". After memcpyopt, LLDB reports both arrays as empty strings in this run.

Reproducer

case.c:

#include &lt;string.h&gt;

char func() {
    char a[4] = "abc", b[4], c[4];
    memcpy(b, a, 4);
    memcpy(c, b, 4);
    return c[0];
}

int main() {
    func();
    return 0;
}

Build pipeline:

clang -g -O0 -Xclang -disable-O0-optnone -emit-llvm -S case.c -o case.ll
opt -passes="instsimplify" -S case.ll -o src.ll
opt -passes="memcpyopt" -S src.ll -o tgt.ll

clang src.ll -o src.out
clang tgt.ll -o tgt.out

Debugging Script

#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

clang -g -O0 -Xclang -disable-O0-optnone -emit-llvm -S case.c -o case.ll
opt -passes="instsimplify" -S case.ll -o src.ll
opt -passes="memcpyopt" -S src.ll -o tgt.ll

clang src.ll -o src.out
clang tgt.ll -o tgt.out

for exe in src.out tgt.out; do
  echo "== $exe =="
  lldb --batch \
    -o 'breakpoint set --file case.c --line 7' \
    -o run \
    -o 'frame variable a b' \
    -- "./$exe"
  echo
done

Actual Behavior

Before memcpyopt, LLDB reports the expected array contents at case.c:7:12:

== src.out ==
Breakpoint 1: where = src.out`func + 25 at case.c:7:12

frame #<!-- -->0: ... src.out`func at case.c:7:12

(lldb) frame variable a b
(char[4]) a = "abc"
(char[4]) b = "abc"

After memcpyopt, LLDB reports different contents:

== tgt.out ==
Breakpoint 1: where = tgt.out`func + 13 at case.c:7:12

frame #<!-- -->0: ... tgt.out`func at case.c:7:12

(lldb) frame variable a b
(char[4]) a = ""
(char[4]) b = ""

Expected Behavior

At line 7, LLDB should not show incorrect concrete contents for a or b.

The source-level values of both arrays are "abc" at this point. If memcpyopt cannot preserve those aggregate values after forwarding/removing the copies, then a and b should be reported as unavailable/optimized out instead of being described by stack slots whose contents no longer match the source variables.

Observed Directly in .ll

Before memcpyopt, the full func body in src.ll initializes a, then copies a to b, and then copies b to c:

define dso_local signext i8 @<!-- -->func() #<!-- -->0 !dbg !9 {
  %1 = alloca [4 x i8], align 1
  %2 = alloca [4 x i8], align 1
  %3 = alloca [4 x i8], align 1
    #dbg_declare(ptr %1, !14, !DIExpression(), !18)
  call void @<!-- -->llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr align 1 @<!-- -->__const.func.a, i64 4, i1 false), !dbg !18
    #dbg_declare(ptr %2, !19, !DIExpression(), !20)
    #dbg_declare(ptr %3, !21, !DIExpression(), !22)
  call void @<!-- -->llvm.memcpy.p0.p0.i64(ptr align 1 %2, ptr align 1 %1, i64 4, i1 false), !dbg !23
  call void @<!-- -->llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 %2, i64 4, i1 false), !dbg !24
  %4 = load i8, ptr %3, align 1, !dbg !25
  ret i8 %4, !dbg !26
}

After memcpyopt, the full func body in tgt.ll forwards the copies and only writes to the storage for c:

define dso_local signext i8 @<!-- -->func() #<!-- -->0 !dbg !9 {
  %1 = alloca [4 x i8], align 1
  %2 = alloca [4 x i8], align 1
  %3 = alloca [4 x i8], align 1
    #dbg_declare(ptr %1, !14, !DIExpression(), !18)
  call void @<!-- -->llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 @<!-- -->__const.func.a, i64 4, i1 false), !dbg !18
    #dbg_declare(ptr %2, !19, !DIExpression(), !20)
    #dbg_declare(ptr %3, !21, !DIExpression(), !22)
  %4 = load i8, ptr %3, align 1, !dbg !23
  ret i8 %4, !dbg !24
}

The dbg_declare intrinsics for a and b still describe %1 and %2 as their storage:

!14 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 4, type: !15)
!19 = !DILocalVariable(name: "b", scope: !9, file: !1, line: 4, type: !15)

However, after memcpyopt, %1 and %2 are no longer initialized. This leaves the debugger reading the wrong memory for a and b.

Environment

clang version 24.0.0git
llvm-project revision: b443896c13aded8b40d7bae4a5a9adbc96fd0d31
Target: x86_64-unknown-linux-gnu

LLVM version 24.0.0git
LLDB version 24.0.0git
Linux x86_64

Notes

My guess is that memcpyopt correctly forwards the memory copies for the optimized program value, but does not update or invalidate the debug declarations for the source objects whose backing stores are no longer written. This is especially visible for aggregate local variables because LLDB reads their contents through the address recorded by dbg_declare.

Hi, thanks for the report. It's a known limitation / design of dbg_declares that with optimisations enabled they may end up describing locations with stale values. Assignment tracking (#dbg_assign) fixes this to some extent - how does this look if you run the pass declare-to-assign first? Clang will automatically add that pass under certain conditions (non-LTO optimized build, plus a few other caveats).

OCHyams avatar Aug 03 '26 10:08 OCHyams

Thanks for the explanation. That makes sense to me. I agree that the original dbg_declare behavior is likely a known limitation/design issue when optimizations leave the declared storage with stale values.

I also tried the assignment-tracking path by running declare-to-assign before memcpyopt. It does improve this case to some extent: the debugger no longer materializes some variables from the stale stack slots.

However, I observed a separate #dbg_assign issue #213642 : after memcpyopt, the surviving memcpy writes to the storage for c, but it keeps the DIAssignID associated with a.

So I think this dbg_declare issue can be treated as the known limitation, while the assignment-tracking-specific problem is tracked in the new issue.

MrEven132 avatar Aug 03 '26 10:08 MrEven132

Thanks for looking into the dbg_assign issue too! I've commented over there I think you're right, that that one looks like a bug. Are you happy to close this one?

OCHyams avatar Aug 06 '26 08:08 OCHyams