Skip to content

Commit 16c2872

Browse files
jwnhyMichael137
authored andcommitted
Reland "[lldb][DWARFExpression] Fix DW_OP_div to use signed division"
This patch resolves an issue where a value is incorrectly displayed if it is represented by DW_OP_div. This issue is caused by lldb evaluating operands of DW_OP_div as unsigned and performed unintended unsigned division. This issue is resolved by creating two temporary signed scalar and performing signed division. (Addresses GH#61727) Differential Revision: https://reviews.llvm.org/D147370
1 parent f9dd3ea commit 16c2872

File tree

2 files changed

+238
-2
lines changed

2 files changed

+238
-2
lines changed

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,8 +1436,12 @@ bool DWARFExpression::Evaluate(
14361436
return false;
14371437
} else {
14381438
stack.pop_back();
1439-
stack.back() =
1440-
stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1439+
Scalar divisor, dividend;
1440+
divisor = tmp.ResolveValue(exe_ctx);
1441+
dividend = stack.back().ResolveValue(exe_ctx);
1442+
divisor.MakeSigned();
1443+
dividend.MakeSigned();
1444+
stack.back() = dividend / divisor;
14411445
if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
14421446
if (error_ptr)
14431447
error_ptr->SetErrorString("Divide failed.");
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
# Test handling of values represented via DW_OP_div
2+
# RUN: %clang -c --target=x86_64-pc-linux -o %t %s
3+
# RUN: %lldb %t -o "expression -T -- g" -o "exit" | FileCheck %s
4+
5+
# Failing case was:
6+
# (int) $0 = 0
7+
# CHECK: (int) $0 = 2
8+
#
9+
# This file is crafted by hand on the basis of a very simple C program.
10+
11+
# The original (NOT THIS FILE) error-triggering code is:
12+
# #include "stdint.h"
13+
# static volatile uint64_t g = 0;
14+
# static const int32_t f()
15+
# {
16+
# uint32_t i;
17+
# for (i = 0; (i != 10); i++)
18+
# ++g;
19+
# return 0;
20+
#
21+
# }
22+
#
23+
# int main()
24+
# {
25+
# f();
26+
# return 0;
27+
#
28+
# }
29+
# When the above code is compiled by Clang 15.0.7 with -O1, it generates desired DW_OP_div for testing.
30+
# Due to cross-platform issue, this case is hand-crafted for testing.
31+
32+
.text
33+
.file "DW_OP_div-with-signed.c"
34+
.file 0 "DW_OP_div-with-signed.c" # Dummy file
35+
.globl main # -- Begin function main
36+
.p2align 4, 0x90
37+
.type main,@function
38+
main: # @main
39+
.Lfunc_begin0:
40+
.loc 0 4 0 # DW_OP_div-with-signed.c:4:0
41+
.cfi_startproc
42+
# %bb.0:
43+
.loc 0 5 5 prologue_end # DW_OP_div-with-signed.c:5:5
44+
xorl %eax, %eax
45+
retq
46+
.Ltmp0:
47+
.Lfunc_end0:
48+
.size main, .Lfunc_end0-main
49+
.cfi_endproc
50+
# -- End function
51+
.type g,@object # @g
52+
.data
53+
.globl g
54+
.p2align 2, 0x0
55+
g:
56+
.long 3735928559 # 0xdeadbeef
57+
.size g, 4
58+
59+
.section .debug_abbrev,"",@progbits
60+
.byte 1 # Abbreviation Code
61+
.byte 17 # DW_TAG_compile_unit
62+
.byte 1 # DW_CHILDREN_yes
63+
.byte 37 # DW_AT_producer
64+
.byte 37 # DW_FORM_strx1
65+
.byte 19 # DW_AT_language
66+
.byte 5 # DW_FORM_data2
67+
.byte 3 # DW_AT_name
68+
.byte 37 # DW_FORM_strx1
69+
.byte 114 # DW_AT_str_offsets_base
70+
.byte 23 # DW_FORM_sec_offset
71+
.byte 16 # DW_AT_stmt_list
72+
.byte 23 # DW_FORM_sec_offset
73+
.byte 27 # DW_AT_comp_dir
74+
.byte 37 # DW_FORM_strx1
75+
.byte 17 # DW_AT_low_pc
76+
.byte 27 # DW_FORM_addrx
77+
.byte 18 # DW_AT_high_pc
78+
.byte 6 # DW_FORM_data4
79+
.byte 115 # DW_AT_addr_base
80+
.byte 23 # DW_FORM_sec_offset
81+
.byte 0 # EOM(1)
82+
.byte 0 # EOM(2)
83+
.byte 2 # Abbreviation Code
84+
.byte 52 # DW_TAG_variable
85+
.byte 0 # DW_CHILDREN_no
86+
.byte 3 # DW_AT_name
87+
.byte 37 # DW_FORM_strx1
88+
.byte 73 # DW_AT_type
89+
.byte 19 # DW_FORM_ref4
90+
.byte 63 # DW_AT_external
91+
.byte 25 # DW_FORM_flag_present
92+
.byte 58 # DW_AT_decl_file
93+
.byte 11 # DW_FORM_data1
94+
.byte 59 # DW_AT_decl_line
95+
.byte 11 # DW_FORM_data1
96+
.byte 2 # DW_AT_location
97+
.byte 24 # DW_FORM_exprloc
98+
.byte 0 # EOM(1)
99+
.byte 0 # EOM(2)
100+
.byte 3 # Abbreviation Code
101+
.byte 36 # DW_TAG_base_type
102+
.byte 0 # DW_CHILDREN_no
103+
.byte 3 # DW_AT_name
104+
.byte 37 # DW_FORM_strx1
105+
.byte 62 # DW_AT_encoding
106+
.byte 11 # DW_FORM_data1
107+
.byte 11 # DW_AT_byte_size
108+
.byte 11 # DW_FORM_data1
109+
.byte 0 # EOM(1)
110+
.byte 0 # EOM(2)
111+
.byte 4 # Abbreviation Code
112+
.byte 46 # DW_TAG_subprogram
113+
.byte 0 # DW_CHILDREN_no
114+
.byte 17 # DW_AT_low_pc
115+
.byte 27 # DW_FORM_addrx
116+
.byte 18 # DW_AT_high_pc
117+
.byte 6 # DW_FORM_data4
118+
.byte 64 # DW_AT_frame_base
119+
.byte 24 # DW_FORM_exprloc
120+
.byte 122 # DW_AT_call_all_calls
121+
.byte 25 # DW_FORM_flag_present
122+
.byte 3 # DW_AT_name
123+
.byte 37 # DW_FORM_strx1
124+
.byte 58 # DW_AT_decl_file
125+
.byte 11 # DW_FORM_data1
126+
.byte 59 # DW_AT_decl_line
127+
.byte 11 # DW_FORM_data1
128+
.byte 73 # DW_AT_type
129+
.byte 19 # DW_FORM_ref4
130+
.byte 63 # DW_AT_external
131+
.byte 25 # DW_FORM_flag_present
132+
.byte 0 # EOM(1)
133+
.byte 0 # EOM(2)
134+
.byte 0 # EOM(3)
135+
.section .debug_info,"",@progbits
136+
.Lcu_begin0:
137+
.long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
138+
.Ldebug_info_start0:
139+
.short 5 # DWARF version number
140+
.byte 1 # DWARF Unit Type
141+
.byte 8 # Address Size (in bytes)
142+
.long .debug_abbrev # Offset Into Abbrev. Section
143+
.byte 1 # Abbrev [1] 0xc:0x36 DW_TAG_compile_unit
144+
.byte 0 # DW_AT_producer
145+
.short 29 # DW_AT_language
146+
.byte 1 # DW_AT_name
147+
.long .Lstr_offsets_base0 # DW_AT_str_offsets_base
148+
.long .Lline_table_start0 # DW_AT_stmt_list
149+
.byte 2 # DW_AT_comp_dir
150+
.byte 1 # DW_AT_low_pc
151+
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
152+
.long .Laddr_table_base0 # DW_AT_addr_base
153+
.byte 2 # Abbrev [2] 0x23:0xb DW_TAG_variable
154+
.byte 3 # DW_AT_name
155+
.long 60 # DW_AT_type
156+
# DW_AT_external
157+
.byte 0 # DW_AT_decl_file
158+
.byte 1 # DW_AT_decl_line
159+
.byte 16 # DW_AT_location
160+
.byte 49
161+
.byte 49
162+
.byte 16 # DW_OP_constu
163+
.byte 255 # 4294967295
164+
.byte 255 #
165+
.byte 255 #
166+
.byte 255 #
167+
.byte 15 #
168+
.byte 26 # DW_OP_and
169+
.byte 17 # DW_OP_consts
170+
.byte 3 # 3
171+
.byte 28 # DW_OP_minus
172+
.byte 17 # DW_OP_consts
173+
.byte 127 # -1
174+
.byte 27 # DW_OP_div
175+
.byte 159
176+
.byte 3 # Abbrev [3] 0x2e:0x4 DW_TAG_base_type
177+
.byte 4 # DW_AT_name
178+
.byte 5 # DW_AT_encoding
179+
.byte 4 # DW_AT_byte_size
180+
.byte 4 # Abbrev [4] 0x32:0xf DW_TAG_subprogram
181+
.byte 1 # DW_AT_low_pc
182+
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
183+
.byte 1 # DW_AT_frame_base
184+
.byte 87
185+
# DW_AT_call_all_calls
186+
.byte 5 # DW_AT_name
187+
.byte 0 # DW_AT_decl_file
188+
.byte 3 # DW_AT_decl_line
189+
.long 60 # DW_AT_type
190+
# DW_AT_external
191+
.byte 0 # End Of Children Mark
192+
.Ldebug_info_end0:
193+
.section .debug_str_offsets,"",@progbits
194+
.long 28 # Length of String Offsets Set
195+
.short 5
196+
.short 0
197+
.Lstr_offsets_base0:
198+
.section .debug_str,"MS",@progbits,1
199+
.Linfo_string0:
200+
.asciz "clang version 17.0.0 (https://github.com/llvm/llvm-project.git 1b18ab358a8e91e69a54f676c77f6a4cda8ba02d)" # string offset=0
201+
.Linfo_string1:
202+
.asciz "DW_OP_div-with-signed.c" # string offset=105
203+
.Linfo_string2:
204+
.asciz "" # string offset=129
205+
.Linfo_string3:
206+
.asciz "g" # string offset=166
207+
.Linfo_string4:
208+
.asciz "int" # string offset=168
209+
.Linfo_string5:
210+
.asciz "main" # string offset=172
211+
.section .debug_str_offsets,"",@progbits
212+
.long .Linfo_string0
213+
.long .Linfo_string1
214+
.long .Linfo_string2
215+
.long .Linfo_string3
216+
.long .Linfo_string4
217+
.long .Linfo_string5
218+
.section .debug_addr,"",@progbits
219+
.long .Ldebug_addr_end0-.Ldebug_addr_start0 # Length of contribution
220+
.Ldebug_addr_start0:
221+
.short 5 # DWARF version number
222+
.byte 8 # Address size
223+
.byte 0 # Segment selector size
224+
.Laddr_table_base0:
225+
.quad g
226+
.quad .Lfunc_begin0
227+
.Ldebug_addr_end0:
228+
.ident "clang version 17.0.0 (https://github.com/llvm/llvm-project.git 1b18ab358a8e91e69a54f676c77f6a4cda8ba02d)"
229+
.section ".note.GNU-stack","",@progbits
230+
.addrsig
231+
.section .debug_line,"",@progbits
232+
.Lline_table_start0:

0 commit comments

Comments
 (0)