| 1 |
/* Text art visualizations within -fanalyzer. |
| 2 |
Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#define INCLUDE_ALGORITHM |
| 22 |
#define INCLUDE_MAP |
| 23 |
#define INCLUDE_SET |
| 24 |
#define INCLUDE_VECTOR |
| 25 |
#include "system.h" |
| 26 |
#include "coretypes.h" |
| 27 |
#include "coretypes.h" |
| 28 |
#include "tree.h" |
| 29 |
#include "function.h" |
| 30 |
#include "basic-block.h" |
| 31 |
#include "gimple.h" |
| 32 |
#include "diagnostic-core.h" |
| 33 |
#include "diagnostic.h" |
| 34 |
#include "intl.h" |
| 35 |
#include "make-unique.h" |
| 36 |
#include "tree-diagnostic.h" /* for default_tree_printer. */ |
| 37 |
#include "analyzer/analyzer.h" |
| 38 |
#include "analyzer/region-model.h" |
| 39 |
#include "analyzer/access-diagram.h" |
| 40 |
#include "text-art/ruler.h" |
| 41 |
#include "fold-const.h" |
| 42 |
#include "analyzer/analyzer-selftests.h" |
| 43 |
|
| 44 |
#if ENABLE_ANALYZER |
| 45 |
|
| 46 |
/* Consider this code: |
| 47 |
int32_t arr[10]; |
| 48 |
arr[10] = x; |
| 49 |
where we've emitted a buffer overflow diagnostic like this: |
| 50 |
out-of-bounds write from byte 40 till byte 43 but 'arr' ends at byte 40 |
| 51 |
|
| 52 |
We want to emit a diagram that visualizes: |
| 53 |
- the spatial relationship between the valid region to access, versus |
| 54 |
the region that was actually accessed: does it overlap, was it touching, |
| 55 |
close, or far away? Was it before or after in memory? What are the |
| 56 |
relative sizes involved? |
| 57 |
- the direction of the access (read vs write) |
| 58 |
|
| 59 |
The following code supports emitting diagrams similar to the following: |
| 60 |
|
| 61 |
# +--------------------------------+ |
| 62 |
# |write from ‘x’ (type: ‘int32_t’)| |
| 63 |
# +--------------------------------+ |
| 64 |
# | |
| 65 |
# | |
| 66 |
# v |
| 67 |
# +---------+-----------+-----------+ +--------------------------------+ |
| 68 |
# | [0] | ... | [9] | | after valid range | |
| 69 |
# +---------+-----------+-----------+ | | |
| 70 |
# | ‘arr’ (type: ‘int32_t[10]’) | | | |
| 71 |
# +---------------------------------+ +--------------------------------+ |
| 72 |
# |~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~| |~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~| |
| 73 |
# | | |
| 74 |
# +---------+--------+ +---------+---------+ |
| 75 |
# |capacity: 40 bytes| |overflow of 4 bytes| |
| 76 |
# +------------------+ +-------------------+ |
| 77 |
|
| 78 |
where the diagram is laid out via table columns where each table column |
| 79 |
represents either a range of bits/bytes, or is a spacing column (to highlight |
| 80 |
the boundary between valid vs invalid accesses). The table columns can be |
| 81 |
seen via -fanalyzer-debug-text-art. For example, here there are 5 table |
| 82 |
columns ("tc0" through "tc4"): |
| 83 |
|
| 84 |
# +---------+-----------+-----------+---+--------------------------------+ |
| 85 |
# | tc0 | tc1 | tc2 |tc3| tc4 | |
| 86 |
# +---------+-----------+-----------+---+--------------------------------+ |
| 87 |
# |bytes 0-3|bytes 4-35 |bytes 36-39| | bytes 40-43 | |
| 88 |
# +---------+-----------+-----------+ +--------------------------------+ |
| 89 |
# |
| 90 |
# +--------------------------------+ |
| 91 |
# |write from ‘x’ (type: ‘int32_t’)| |
| 92 |
# +--------------------------------+ |
| 93 |
# | |
| 94 |
# | |
| 95 |
# v |
| 96 |
# +---------+-----------+-----------+ +--------------------------------+ |
| 97 |
# | [0] | ... | [9] | | after valid range | |
| 98 |
# +---------+-----------+-----------+ | | |
| 99 |
# | ‘arr’ (type: ‘int32_t[10]’) | | | |
| 100 |
# +---------------------------------+ +--------------------------------+ |
| 101 |
# |~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~| |~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~| |
| 102 |
# | | |
| 103 |
# +---------+--------+ +---------+---------+ |
| 104 |
# |capacity: 40 bytes| |overflow of 4 bytes| |
| 105 |
# +------------------+ +-------------------+ |
| 106 |
|
| 107 |
The diagram is built up from the following: |
| 108 |
|
| 109 |
# +--------------------------------+ |
| 110 |
# | ITEM FOR SVALUE/ACCESSED REGION| |
| 111 |
# +--------------------------------+ |
| 112 |
# | |
| 113 |
# | DIRECTION WIDGET |
| 114 |
# v |
| 115 |
# +---------------------------------+ +--------------------------------+ |
| 116 |
# | VALID REGION | | INVALID ACCESS | |
| 117 |
# +---------------------------------+ +--------------------------------+ |
| 118 |
# |
| 119 |
# | VALID-VS-INVALID RULER | |
| 120 |
|
| 121 |
i.e. a vbox_widget containing 4 child widgets laid out vertically: |
| 122 |
- ALIGNED CHILD WIDGET: ITEM FOR SVALUE/ACCESSED REGION |
| 123 |
- DIRECTION WIDGET |
| 124 |
- ALIGNED CHILD WIDGET: VALID AND INVALID ACCESSES |
| 125 |
- VALID-VS-INVALID RULER. |
| 126 |
|
| 127 |
A more complicated example, given this overflow: |
| 128 |
char buf[100]; |
| 129 |
strcpy (buf, LOREM_IPSUM); |
| 130 |
|
| 131 |
01| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 132 |
02| |[0]|[1]|[2]|[3]|[4]|[5]| ... |[440]|[441]|[442]|[443]|[444]|[445]| |
| 133 |
03| +---+---+---+---+---+---+ +-----+-----+-----+-----+-----+-----+ |
| 134 |
04| |'L'|'o'|'r'|'e'|'m'|' '| | 'o' | 'r' | 'u' | 'm' | '.' | NUL | |
| 135 |
05| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 136 |
06| | string literal (type: 'char[446]') | |
| 137 |
07| +----------------------------------------------------------------------+ |
| 138 |
08| | | | | | | | | | | | | | | | |
| 139 |
09| | | | | | | | | | | | | | | | |
| 140 |
10| v v v v v v v v v v v v v v v |
| 141 |
11| +---+---------------------+----++--------------------------------------+ |
| 142 |
12| |[0]| ... |[99]|| after valid range | |
| 143 |
13| +---+---------------------+----+| | |
| 144 |
14| | 'buf' (type: 'char[100]') || | |
| 145 |
15| +------------------------------++--------------------------------------+ |
| 146 |
16| |~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~||~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~| |
| 147 |
17| | | |
| 148 |
18| +---------+---------+ +----------+----------+ |
| 149 |
19| |capacity: 100 bytes| |overflow of 346 bytes| |
| 150 |
20| +-------------------+ +---------------------+ |
| 151 |
|
| 152 |
which is: |
| 153 |
|
| 154 |
01| ALIGNED CHILD WIDGET (lines 01-07): (string_region_spatial_item)-+-----+ |
| 155 |
02| |[0]|[1]|[2]|[3]|[4]|[5]| ... |[440]|[441]|[442]|[443]|[444]|[445]| |
| 156 |
03| +---+---+---+---+---+---+ +-----+-----+-----+-----+-----+-----+ |
| 157 |
04| |'L'|'o'|'r'|'e'|'m'|' '| | 'o' | 'r' | 'u' | 'm' | '.' | NUL | |
| 158 |
05| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 159 |
06| | string literal (type: 'char[446]') | |
| 160 |
07| +----------------------------------------------------------------------+ |
| 161 |
08| DIRECTION WIDGET (lines 08-10) | | | | | | | |
| 162 |
09| | | | | | | | | | | | | | | | |
| 163 |
10| v v v v v v v v v v v v v v v |
| 164 |
11| ALIGNED CHILD WIDGET (lines 11-15)-------------------------------------+ |
| 165 |
12| VALID REGION ... |[99]|| INVALID ACCESS | |
| 166 |
13| +---+---------------------+----+| | |
| 167 |
14| | 'buf' (type: 'char[100]') || | |
| 168 |
15| +------------------------------++--------------------------------------+ |
| 169 |
16| VALID-VS-INVALID RULER (lines 16-20): ~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~| |
| 170 |
17| | | |
| 171 |
18| +---------+---------+ +----------+----------+ |
| 172 |
19| |capacity: 100 bytes| |overflow of 346 bytes| |
| 173 |
20| +-------------------+ +---------------------+ |
| 174 |
|
| 175 |
We build the diagram in several phases: |
| 176 |
- (1) we construct an access_diagram_impl widget. Within the ctor, we have |
| 177 |
these subphases: |
| 178 |
- (1.1) find all of the boundaries of interest |
| 179 |
- (1.2) use the boundaries to build a bit_table_map, associating bit ranges |
| 180 |
with table columns (e.g. "byte 0 is column 0, bytes 1-98 are column 2" etc) |
| 181 |
- (1.3) create child widgets that share this table-based geometry |
| 182 |
- (2) ask the widget for its size request |
| 183 |
- (2.1) column widths and row heights for the table are computed by |
| 184 |
access_diagram_impl::calc_req_size |
| 185 |
- (2.2) child widgets request sizes based on these widths/heights |
| 186 |
- (3) create a canvas of the appropriate size |
| 187 |
- (4) paint the widget hierarchy to the canvas. */ |
| 188 |
|
| 189 |
|
| 190 |
using namespace text_art; |
| 191 |
|
| 192 |
namespace ana { |
| 193 |
|
| 194 |
static styled_string |
| 195 |
fmt_styled_string (style_manager &sm, |
| 196 |
const char *fmt, ...) |
| 197 |
ATTRIBUTE_GCC_DIAG(2, 3); |
| 198 |
|
| 199 |
static styled_string |
| 200 |
fmt_styled_string (style_manager &sm, |
| 201 |
const char *fmt, ...) |
| 202 |
{ |
| 203 |
va_list ap; |
| 204 |
va_start (ap, fmt); |
| 205 |
styled_string result |
| 206 |
= styled_string::from_fmt_va (sm, default_tree_printer, fmt, &ap); |
| 207 |
va_end (ap); |
| 208 |
return result; |
| 209 |
} |
| 210 |
|
| 211 |
class access_diagram_impl; |
| 212 |
class bit_to_table_map; |
| 213 |
|
| 214 |
static void |
| 215 |
pp_bit_size_t (pretty_printer *pp, bit_size_t num_bits) |
| 216 |
{ |
| 217 |
if (num_bits % BITS_PER_UNIT == 0) |
| 218 |
{ |
| 219 |
byte_size_t num_bytes = num_bits / BITS_PER_UNIT; |
| 220 |
if (num_bytes == 1) |
| 221 |
pp_printf (pp, _("%wi byte"), num_bytes.to_uhwi ()); |
| 222 |
else |
| 223 |
pp_printf (pp, _("%wi bytes"), num_bytes.to_uhwi ()); |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
if (num_bits == 1) |
| 228 |
pp_printf (pp, _("%wi bit"), num_bits.to_uhwi ()); |
| 229 |
else |
| 230 |
pp_printf (pp, _("%wi bits"), num_bits.to_uhwi ()); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static styled_string |
| 235 |
get_access_size_str (style_manager &sm, |
| 236 |
const access_operation &op, |
| 237 |
access_range accessed_range, |
| 238 |
tree type) |
| 239 |
{ |
| 240 |
bit_size_expr num_bits (accessed_range.get_size (op.m_model.get_manager ())); |
| 241 |
if (type) |
| 242 |
{ |
| 243 |
styled_string s; |
| 244 |
pretty_printer pp; |
| 245 |
pp_format_decoder (&pp) = default_tree_printer; |
| 246 |
if (num_bits.maybe_print_for_user (&pp, op.m_model)) |
| 247 |
{ |
| 248 |
if (op.m_dir == DIR_READ) |
| 249 |
return fmt_styled_string (sm, |
| 250 |
_("read of %qT (%s)"), |
| 251 |
type, |
| 252 |
pp_formatted_text (&pp)); |
| 253 |
else |
| 254 |
return fmt_styled_string (sm, |
| 255 |
_("write of %qT (%s)"), |
| 256 |
type, |
| 257 |
pp_formatted_text (&pp)); |
| 258 |
} |
| 259 |
} |
| 260 |
if (op.m_dir == DIR_READ) |
| 261 |
{ |
| 262 |
if (auto p |
| 263 |
= num_bits.maybe_get_formatted_str (sm, op.m_model, |
| 264 |
_("read of %wi bit"), |
| 265 |
_("read of %wi bits"), |
| 266 |
_("read of %wi byte"), |
| 267 |
_("read of %wi bytes"), |
| 268 |
_("read of %qs bits"), |
| 269 |
_("read of %qs bytes"))) |
| 270 |
return std::move (*p.get ()); |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
if (auto p |
| 275 |
= num_bits.maybe_get_formatted_str (sm, op.m_model, |
| 276 |
_("write of %wi bit"), |
| 277 |
_("write of %wi bits"), |
| 278 |
_("write of %wi byte"), |
| 279 |
_("write of %wi bytes"), |
| 280 |
_("write of %qs bits"), |
| 281 |
_("write of %qs bytes"))) |
| 282 |
return std::move (*p.get ()); |
| 283 |
} |
| 284 |
|
| 285 |
if (type) |
| 286 |
{ |
| 287 |
if (op.m_dir == DIR_READ) |
| 288 |
return fmt_styled_string (sm, _("read of %qT"), type); |
| 289 |
else |
| 290 |
return fmt_styled_string (sm, _("write of %qT"), type); |
| 291 |
} |
| 292 |
|
| 293 |
if (op.m_dir == DIR_READ) |
| 294 |
return styled_string (sm, _("read")); |
| 295 |
else |
| 296 |
return styled_string (sm, _("write")); |
| 297 |
} |
| 298 |
|
| 299 |
/* Subroutine of clean_up_for_diagram. */ |
| 300 |
|
| 301 |
static tree |
| 302 |
strip_any_cast (tree expr) |
| 303 |
{ |
| 304 |
if (TREE_CODE (expr) == NOP_EXPR |
| 305 |
|| TREE_CODE (expr) == NON_LVALUE_EXPR) |
| 306 |
expr = TREE_OPERAND (expr, 0); |
| 307 |
return expr; |
| 308 |
} |
| 309 |
|
| 310 |
/* Duplicate EXPR, replacing any SSA names with the underlying variable. */ |
| 311 |
|
| 312 |
tree |
| 313 |
remove_ssa_names (tree expr) |
| 314 |
{ |
| 315 |
if (TREE_CODE (expr) == SSA_NAME |
| 316 |
&& SSA_NAME_VAR (expr)) |
| 317 |
return SSA_NAME_VAR (expr); |
| 318 |
tree t = copy_node (expr); |
| 319 |
for (int i = 0; i < TREE_OPERAND_LENGTH (expr); i++) |
| 320 |
if (TREE_OPERAND (expr, i)) |
| 321 |
TREE_OPERAND (t, i) = remove_ssa_names (TREE_OPERAND (expr, i)); |
| 322 |
return t; |
| 323 |
} |
| 324 |
|
| 325 |
/* We want to be able to print tree expressions from the analyzer, |
| 326 |
which is in the middle end. |
| 327 |
|
| 328 |
We could use the front-end pretty_printer's formatting routine, |
| 329 |
but: |
| 330 |
(a) some have additional state in a pretty_printer subclass, so we'd |
| 331 |
need to clone global_dc->printer |
| 332 |
(b) the "aka" type information added by the C and C++ frontends are |
| 333 |
too verbose when building a diagram, and there isn't a good way to ask |
| 334 |
for a less verbose version of them. |
| 335 |
|
| 336 |
Hence we use default_tree_printer. |
| 337 |
However, we want to avoid printing SSA names, and instead print the |
| 338 |
underlying var name. |
| 339 |
Ideally there would be a better tree printer for use by middle end |
| 340 |
warnings, but as workaround, this function clones a tree, replacing |
| 341 |
SSA names with the var names. */ |
| 342 |
|
| 343 |
tree |
| 344 |
clean_up_for_diagram (tree expr) |
| 345 |
{ |
| 346 |
tree without_ssa_names = remove_ssa_names (expr); |
| 347 |
return strip_any_cast (without_ssa_names); |
| 348 |
} |
| 349 |
|
| 350 |
/* struct bit_size_expr. */ |
| 351 |
|
| 352 |
/* Attempt to generate a user-facing styled string that mentions this |
| 353 |
bit_size_expr. |
| 354 |
Use MODEL for extracting representative tree values where necessary. |
| 355 |
The CONCRETE_* format strings should contain a single %wi. |
| 356 |
The SYMBOLIC_* format strings should contain a single %qs. |
| 357 |
Return nullptr if unable to represent the expression. */ |
| 358 |
|
| 359 |
std::unique_ptr<text_art::styled_string> |
| 360 |
bit_size_expr::maybe_get_formatted_str (text_art::style_manager &sm, |
| 361 |
const region_model &model, |
| 362 |
const char *concrete_single_bit_fmt, |
| 363 |
const char *concrete_plural_bits_fmt, |
| 364 |
const char *concrete_single_byte_fmt, |
| 365 |
const char *concrete_plural_bytes_fmt, |
| 366 |
const char *symbolic_bits_fmt, |
| 367 |
const char *symbolic_bytes_fmt) const |
| 368 |
{ |
| 369 |
region_model_manager &mgr = *model.get_manager (); |
| 370 |
if (const svalue *num_bytes = maybe_get_as_bytes (mgr)) |
| 371 |
{ |
| 372 |
if (tree cst = num_bytes->maybe_get_constant ()) |
| 373 |
{ |
| 374 |
byte_size_t concrete_num_bytes = wi::to_offset (cst); |
| 375 |
if (!wi::fits_uhwi_p (concrete_num_bytes)) |
| 376 |
return nullptr; |
| 377 |
if (concrete_num_bytes == 1) |
| 378 |
return ::make_unique <text_art::styled_string> |
| 379 |
(fmt_styled_string (sm, concrete_single_byte_fmt, |
| 380 |
concrete_num_bytes.to_uhwi ())); |
| 381 |
else |
| 382 |
return ::make_unique <text_art::styled_string> |
| 383 |
(fmt_styled_string (sm, concrete_plural_bytes_fmt, |
| 384 |
concrete_num_bytes.to_uhwi ())); |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
pretty_printer pp; |
| 389 |
pp_format_decoder (&pp) = default_tree_printer; |
| 390 |
if (!num_bytes->maybe_print_for_user (&pp, model)) |
| 391 |
return nullptr; |
| 392 |
return ::make_unique <text_art::styled_string> |
| 393 |
(fmt_styled_string (sm, symbolic_bytes_fmt, |
| 394 |
pp_formatted_text (&pp))); |
| 395 |
} |
| 396 |
} |
| 397 |
else if (tree cst = m_num_bits.maybe_get_constant ()) |
| 398 |
{ |
| 399 |
bit_size_t concrete_num_bits = wi::to_offset (cst); |
| 400 |
if (!wi::fits_uhwi_p (concrete_num_bits)) |
| 401 |
return nullptr; |
| 402 |
if (concrete_num_bits == 1) |
| 403 |
return ::make_unique <text_art::styled_string> |
| 404 |
(fmt_styled_string (sm, concrete_single_bit_fmt, |
| 405 |
concrete_num_bits.to_uhwi ())); |
| 406 |
else |
| 407 |
return ::make_unique <text_art::styled_string> |
| 408 |
(fmt_styled_string (sm, concrete_plural_bits_fmt, |
| 409 |
concrete_num_bits.to_uhwi ())); |
| 410 |
} |
| 411 |
else |
| 412 |
{ |
| 413 |
pretty_printer pp; |
| 414 |
pp_format_decoder (&pp) = default_tree_printer; |
| 415 |
if (!m_num_bits.maybe_print_for_user (&pp, model)) |
| 416 |
return nullptr; |
| 417 |
return ::make_unique <text_art::styled_string> |
| 418 |
(fmt_styled_string (sm, symbolic_bits_fmt, |
| 419 |
pp_formatted_text (&pp))); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
bool |
| 424 |
bit_size_expr::maybe_print_for_user (pretty_printer *pp, |
| 425 |
const region_model &model) const |
| 426 |
{ |
| 427 |
if (tree cst = m_num_bits.maybe_get_constant ()) |
| 428 |
{ |
| 429 |
bit_size_t concrete_num_bits = wi::to_offset (cst); |
| 430 |
pp_bit_size_t (pp, concrete_num_bits); |
| 431 |
return true; |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
if (const svalue *num_bytes = maybe_get_as_bytes (*model.get_manager ())) |
| 436 |
{ |
| 437 |
pretty_printer tmp_pp; |
| 438 |
pp_format_decoder (&tmp_pp) = default_tree_printer; |
| 439 |
if (!num_bytes->maybe_print_for_user (&tmp_pp, model)) |
| 440 |
return false; |
| 441 |
pp_printf (pp, _("%qs bytes"), pp_formatted_text (&tmp_pp)); |
| 442 |
return true; |
| 443 |
} |
| 444 |
else |
| 445 |
{ |
| 446 |
pretty_printer tmp_pp; |
| 447 |
pp_format_decoder (&tmp_pp) = default_tree_printer; |
| 448 |
if (!m_num_bits.maybe_print_for_user (&tmp_pp, model)) |
| 449 |
return false; |
| 450 |
pp_printf (pp, _("%qs bits"), pp_formatted_text (&tmp_pp)); |
| 451 |
return true; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/* Attempt to get a symbolic value for this symbolic bit size, |
| 457 |
expressed in bytes. |
| 458 |
Return null if it's not known to divide exactly. */ |
| 459 |
|
| 460 |
const svalue * |
| 461 |
bit_size_expr::maybe_get_as_bytes (region_model_manager &mgr) const |
| 462 |
{ |
| 463 |
if (tree cst = m_num_bits.maybe_get_constant ()) |
| 464 |
{ |
| 465 |
bit_offset_t concrete_bits = wi::to_offset (cst); |
| 466 |
if (concrete_bits % BITS_PER_UNIT != 0) |
| 467 |
/* Not an exact multiple, so fail. */ |
| 468 |
return nullptr; |
| 469 |
} |
| 470 |
const svalue *bits_per_byte |
| 471 |
= mgr.get_or_create_int_cst (NULL_TREE, BITS_PER_UNIT); |
| 472 |
return mgr.maybe_fold_binop (NULL_TREE, EXACT_DIV_EXPR, |
| 473 |
&m_num_bits, bits_per_byte); |
| 474 |
} |
| 475 |
|
| 476 |
/* struct access_range. */ |
| 477 |
|
| 478 |
access_range::access_range (const region *base_region, const bit_range &bits) |
| 479 |
: m_start (region_offset::make_concrete (base_region, |
| 480 |
bits.get_start_bit_offset ())), |
| 481 |
m_next (region_offset::make_concrete (base_region, |
| 482 |
bits.get_next_bit_offset ())) |
| 483 |
{ |
| 484 |
} |
| 485 |
|
| 486 |
access_range::access_range (const region *base_region, const byte_range &bytes) |
| 487 |
: m_start (region_offset::make_concrete (base_region, |
| 488 |
bytes.get_start_bit_offset ())), |
| 489 |
m_next (region_offset::make_concrete (base_region, |
| 490 |
bytes.get_next_bit_offset ())) |
| 491 |
{ |
| 492 |
} |
| 493 |
|
| 494 |
access_range::access_range (const region ®, region_model_manager *mgr) |
| 495 |
: m_start (strip_types (reg.get_offset (mgr), *mgr)), |
| 496 |
m_next (strip_types (reg.get_next_offset (mgr), *mgr)) |
| 497 |
{ |
| 498 |
} |
| 499 |
|
| 500 |
bit_size_expr |
| 501 |
access_range::get_size (region_model_manager *mgr) const |
| 502 |
{ |
| 503 |
const svalue &start_bit_offset = m_start.calc_symbolic_bit_offset (mgr); |
| 504 |
const svalue &next_bit_offset = m_next.calc_symbolic_bit_offset (mgr); |
| 505 |
return bit_size_expr |
| 506 |
(*mgr->get_or_create_binop (NULL_TREE, MINUS_EXPR, |
| 507 |
&next_bit_offset, &start_bit_offset)); |
| 508 |
} |
| 509 |
|
| 510 |
bool |
| 511 |
access_range::contains_p (const access_range &other) const |
| 512 |
{ |
| 513 |
return (m_start <= other.m_start |
| 514 |
&& other.m_next <= m_next); |
| 515 |
} |
| 516 |
|
| 517 |
bool |
| 518 |
access_range::empty_p () const |
| 519 |
{ |
| 520 |
bit_range concrete_bits (0, 0); |
| 521 |
if (!as_concrete_bit_range (&concrete_bits)) |
| 522 |
return false; |
| 523 |
return concrete_bits.empty_p (); |
| 524 |
} |
| 525 |
|
| 526 |
void |
| 527 |
access_range::dump_to_pp (pretty_printer *pp, bool simple) const |
| 528 |
{ |
| 529 |
if (m_start.concrete_p () && m_next.concrete_p ()) |
| 530 |
{ |
| 531 |
bit_range bits (m_start.get_bit_offset (), |
| 532 |
m_next.get_bit_offset () - m_start.get_bit_offset ()); |
| 533 |
bits.dump_to_pp (pp); |
| 534 |
return; |
| 535 |
} |
| 536 |
pp_character (pp, '['); |
| 537 |
m_start.dump_to_pp (pp, simple); |
| 538 |
pp_string (pp, " to "); |
| 539 |
m_next.dump_to_pp (pp, simple); |
| 540 |
pp_character (pp, ')'); |
| 541 |
} |
| 542 |
|
| 543 |
DEBUG_FUNCTION void |
| 544 |
access_range::dump (bool simple) const |
| 545 |
{ |
| 546 |
tree_dump_pretty_printer pp (stderr); |
| 547 |
dump_to_pp (&pp, simple); |
| 548 |
pp_newline (&pp); |
| 549 |
} |
| 550 |
|
| 551 |
void |
| 552 |
access_range::log (const char *title, logger &logger) const |
| 553 |
{ |
| 554 |
logger.start_log_line (); |
| 555 |
logger.log_partial ("%s: ", title); |
| 556 |
dump_to_pp (logger.get_printer (), true); |
| 557 |
logger.end_log_line (); |
| 558 |
} |
| 559 |
|
| 560 |
/* struct access_operation. */ |
| 561 |
|
| 562 |
access_range |
| 563 |
access_operation::get_valid_bits () const |
| 564 |
{ |
| 565 |
const svalue *capacity_in_bytes_sval = m_model.get_capacity (m_base_region); |
| 566 |
return access_range |
| 567 |
(region_offset::make_concrete (m_base_region, 0), |
| 568 |
region_offset::make_byte_offset (m_base_region, capacity_in_bytes_sval), |
| 569 |
*get_manager ()); |
| 570 |
} |
| 571 |
|
| 572 |
access_range |
| 573 |
access_operation::get_actual_bits () const |
| 574 |
{ |
| 575 |
return access_range (m_reg, get_manager ()); |
| 576 |
} |
| 577 |
|
| 578 |
/* If there are any bits accessed invalidly before the valid range, |
| 579 |
return true and write their range to *OUT. |
| 580 |
Return false if there aren't, or if there's a problem |
| 581 |
(e.g. symbolic ranges. */ |
| 582 |
|
| 583 |
bool |
| 584 |
access_operation::maybe_get_invalid_before_bits (access_range *out) const |
| 585 |
{ |
| 586 |
access_range valid_bits (get_valid_bits ()); |
| 587 |
access_range actual_bits (get_actual_bits ()); |
| 588 |
|
| 589 |
if (actual_bits.m_start >= valid_bits.m_start) |
| 590 |
{ |
| 591 |
/* No part of accessed range is before the valid range. */ |
| 592 |
return false; |
| 593 |
} |
| 594 |
else if (actual_bits.m_next > valid_bits.m_start) |
| 595 |
{ |
| 596 |
/* Get part of accessed range that's before the valid range. */ |
| 597 |
*out = access_range (actual_bits.m_start, valid_bits.m_start, |
| 598 |
*get_manager ()); |
| 599 |
return true; |
| 600 |
} |
| 601 |
else |
| 602 |
{ |
| 603 |
/* Accessed range is fully before valid range. */ |
| 604 |
*out = actual_bits; |
| 605 |
return true; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
/* If there are any bits accessed invalidly after the valid range, |
| 610 |
return true and write their range to *OUT. |
| 611 |
Return false if there aren't, or if there's a problem. */ |
| 612 |
|
| 613 |
bool |
| 614 |
access_operation::maybe_get_invalid_after_bits (access_range *out) const |
| 615 |
{ |
| 616 |
access_range valid_bits (get_valid_bits ()); |
| 617 |
access_range actual_bits (get_actual_bits ()); |
| 618 |
|
| 619 |
if (actual_bits.m_next <= valid_bits.m_next) |
| 620 |
{ |
| 621 |
/* No part of accessed range is after the valid range. */ |
| 622 |
return false; |
| 623 |
} |
| 624 |
else if (actual_bits.m_start < valid_bits.m_next) |
| 625 |
{ |
| 626 |
/* Get part of accessed range that's after the valid range. */ |
| 627 |
*out = access_range (valid_bits.m_next, actual_bits.m_next, |
| 628 |
*get_manager ()); |
| 629 |
return true; |
| 630 |
} |
| 631 |
else |
| 632 |
{ |
| 633 |
/* Accessed range is fully after valid range. */ |
| 634 |
*out = actual_bits; |
| 635 |
return true; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
/* A class for capturing all of the region offsets of interest (both concrete |
| 640 |
and symbolic), to help align everything in the diagram. |
| 641 |
Boundaries can be soft or hard; hard boundaries are emphasized visually |
| 642 |
(e.g. the boundary between valid vs invalid accesses). |
| 643 |
|
| 644 |
Offsets in the boundaries are all expressed relative to the base |
| 645 |
region of the access_operation. */ |
| 646 |
|
| 647 |
class boundaries |
| 648 |
{ |
| 649 |
public: |
| 650 |
enum class kind { HARD, SOFT}; |
| 651 |
|
| 652 |
boundaries (const region &base_reg, logger *logger) |
| 653 |
: m_base_reg (base_reg), m_logger (logger) |
| 654 |
{ |
| 655 |
} |
| 656 |
|
| 657 |
void add (region_offset offset, enum kind k) |
| 658 |
{ |
| 659 |
m_all_offsets.insert (offset); |
| 660 |
if (k == kind::HARD) |
| 661 |
m_hard_offsets.insert (offset); |
| 662 |
} |
| 663 |
|
| 664 |
void add (const access_range &range, enum kind kind) |
| 665 |
{ |
| 666 |
add (range.m_start, kind); |
| 667 |
add (range.m_next, kind); |
| 668 |
if (m_logger) |
| 669 |
{ |
| 670 |
m_logger->start_log_line (); |
| 671 |
m_logger->log_partial ("added access_range: "); |
| 672 |
range.dump_to_pp (m_logger->get_printer (), true); |
| 673 |
m_logger->log_partial (" (%s)", |
| 674 |
(kind == boundaries::kind::HARD) |
| 675 |
? "HARD" : "soft"); |
| 676 |
m_logger->end_log_line (); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
void add (const region ®, region_model_manager *mgr, enum kind kind) |
| 681 |
{ |
| 682 |
add (access_range (reg.get_offset (mgr), |
| 683 |
reg.get_next_offset (mgr), |
| 684 |
*mgr), |
| 685 |
kind); |
| 686 |
} |
| 687 |
|
| 688 |
void add (const byte_range bytes, enum kind kind) |
| 689 |
{ |
| 690 |
add (access_range (&m_base_reg, bytes), kind); |
| 691 |
} |
| 692 |
|
| 693 |
void add_all_bytes_in_range (const byte_range &bytes) |
| 694 |
{ |
| 695 |
for (byte_offset_t byte_idx = bytes.get_start_byte_offset (); |
| 696 |
byte_idx <= bytes.get_next_byte_offset (); |
| 697 |
byte_idx = byte_idx + 1) |
| 698 |
add (region_offset::make_concrete (&m_base_reg, byte_idx * 8), |
| 699 |
kind::SOFT); |
| 700 |
} |
| 701 |
|
| 702 |
void add_all_bytes_in_range (const access_range &range) |
| 703 |
{ |
| 704 |
byte_range bytes (0, 0); |
| 705 |
bool valid = range.as_concrete_byte_range (&bytes); |
| 706 |
gcc_assert (valid); |
| 707 |
add_all_bytes_in_range (bytes); |
| 708 |
} |
| 709 |
|
| 710 |
void log (logger &logger) const |
| 711 |
{ |
| 712 |
logger.log ("boundaries:"); |
| 713 |
logger.inc_indent (); |
| 714 |
for (auto offset : m_all_offsets) |
| 715 |
{ |
| 716 |
enum kind k = get_kind (offset); |
| 717 |
logger.start_log_line (); |
| 718 |
logger.log_partial ("%s: ", (k == kind::HARD) ? "HARD" : "soft"); |
| 719 |
offset.dump_to_pp (logger.get_printer (), true); |
| 720 |
logger.end_log_line (); |
| 721 |
} |
| 722 |
logger.dec_indent (); |
| 723 |
} |
| 724 |
|
| 725 |
enum kind get_kind (region_offset offset) const |
| 726 |
{ |
| 727 |
gcc_assert (m_all_offsets.find (offset) != m_all_offsets.end ()); |
| 728 |
if (m_hard_offsets.find (offset) != m_hard_offsets.end ()) |
| 729 |
return kind::HARD; |
| 730 |
else |
| 731 |
return kind::SOFT; |
| 732 |
} |
| 733 |
|
| 734 |
std::set<region_offset>::const_iterator begin () const |
| 735 |
{ |
| 736 |
return m_all_offsets.begin (); |
| 737 |
} |
| 738 |
std::set<region_offset>::const_iterator end () const |
| 739 |
{ |
| 740 |
return m_all_offsets.end (); |
| 741 |
} |
| 742 |
std::set<region_offset>::size_type size () const |
| 743 |
{ |
| 744 |
return m_all_offsets.size (); |
| 745 |
} |
| 746 |
|
| 747 |
std::vector<region_offset> |
| 748 |
get_hard_boundaries_in_range (byte_offset_t min_offset, |
| 749 |
byte_offset_t max_offset) const |
| 750 |
{ |
| 751 |
std::vector<region_offset> result; |
| 752 |
for (auto &offset : m_hard_offsets) |
| 753 |
{ |
| 754 |
if (!offset.concrete_p ()) |
| 755 |
continue; |
| 756 |
byte_offset_t byte; |
| 757 |
if (!offset.get_concrete_byte_offset (&byte)) |
| 758 |
continue; |
| 759 |
if (byte < min_offset) |
| 760 |
continue; |
| 761 |
if (byte > max_offset) |
| 762 |
continue; |
| 763 |
result.push_back (offset); |
| 764 |
} |
| 765 |
return result; |
| 766 |
} |
| 767 |
|
| 768 |
private: |
| 769 |
const region &m_base_reg; |
| 770 |
logger *m_logger; |
| 771 |
std::set<region_offset> m_all_offsets; |
| 772 |
std::set<region_offset> m_hard_offsets; |
| 773 |
}; |
| 774 |
|
| 775 |
/* A widget that wraps a table but offloads column-width calculation |
| 776 |
to a shared object, so that we can vertically line up multiple tables |
| 777 |
and have them all align their columns. |
| 778 |
|
| 779 |
For example, in: |
| 780 |
|
| 781 |
01| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 782 |
02| |[0]|[1]|[2]|[3]|[4]|[5]| ... |[440]|[441]|[442]|[443]|[444]|[445]| |
| 783 |
03| +---+---+---+---+---+---+ +-----+-----+-----+-----+-----+-----+ |
| 784 |
04| |'L'|'o'|'r'|'e'|'m'|' '| | 'o' | 'r' | 'u' | 'm' | '.' | NUL | |
| 785 |
05| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 786 |
06| | string literal (type: 'char[446]') | |
| 787 |
07| +----------------------------------------------------------------------+ |
| 788 |
08| | | | | | | | | | | | | | | | |
| 789 |
09| | | | | | | | | | | | | | | | |
| 790 |
10| v v v v v v v v v v v v v v v |
| 791 |
11|+---+---------------------+----++--------------------------------------+ |
| 792 |
12||[0]| ... |[99]|| after valid range | |
| 793 |
13|+---+---------------------+----+| | |
| 794 |
14|| 'buf' (type: 'char[100]') || | |
| 795 |
15|+------------------------------++--------------------------------------+ |
| 796 |
16||~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~||~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~| |
| 797 |
17| | | |
| 798 |
18| +---------+---------+ +----------+----------+ |
| 799 |
19| |capacity: 100 bytes| |overflow of 346 bytes| |
| 800 |
20| +-------------------+ +---------------------+ |
| 801 |
|
| 802 |
rows 01-07 and rows 11-15 are x_aligned_table_widget instances. */ |
| 803 |
|
| 804 |
class x_aligned_table_widget : public leaf_widget |
| 805 |
{ |
| 806 |
public: |
| 807 |
x_aligned_table_widget (table t, |
| 808 |
const theme &theme, |
| 809 |
table_dimension_sizes &col_widths) |
| 810 |
: m_table (std::move (t)), |
| 811 |
m_theme (theme), |
| 812 |
m_col_widths (col_widths), |
| 813 |
m_row_heights (t.get_size ().h), |
| 814 |
m_cell_sizes (m_col_widths, m_row_heights), |
| 815 |
m_tg (m_table, m_cell_sizes) |
| 816 |
{ |
| 817 |
} |
| 818 |
|
| 819 |
const char *get_desc () const override |
| 820 |
{ |
| 821 |
return "x_aligned_table_widget"; |
| 822 |
} |
| 823 |
|
| 824 |
canvas::size_t calc_req_size () final override |
| 825 |
{ |
| 826 |
/* We don't compute the size requirements; |
| 827 |
the parent should have done this. */ |
| 828 |
return m_tg.get_canvas_size (); |
| 829 |
} |
| 830 |
|
| 831 |
void paint_to_canvas (canvas &canvas) final override |
| 832 |
{ |
| 833 |
m_table.paint_to_canvas (canvas, |
| 834 |
get_top_left (), |
| 835 |
m_tg, |
| 836 |
m_theme); |
| 837 |
} |
| 838 |
|
| 839 |
const table &get_table () const { return m_table; } |
| 840 |
table_cell_sizes &get_cell_sizes () { return m_cell_sizes; } |
| 841 |
void recalc_coords () |
| 842 |
{ |
| 843 |
m_tg.recalc_coords (); |
| 844 |
} |
| 845 |
|
| 846 |
private: |
| 847 |
table m_table; |
| 848 |
const theme &m_theme; |
| 849 |
table_dimension_sizes &m_col_widths; // Reference to shared column widths |
| 850 |
table_dimension_sizes m_row_heights; // Unique row heights |
| 851 |
table_cell_sizes m_cell_sizes; |
| 852 |
table_geometry m_tg; |
| 853 |
}; |
| 854 |
|
| 855 |
/* A widget for printing arrows between the accessed region |
| 856 |
and the svalue, showing the direction of the access. |
| 857 |
|
| 858 |
For example, in: |
| 859 |
|
| 860 |
01| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 861 |
02| |[0]|[1]|[2]|[3]|[4]|[5]| ... |[440]|[441]|[442]|[443]|[444]|[445]| |
| 862 |
03| +---+---+---+---+---+---+ +-----+-----+-----+-----+-----+-----+ |
| 863 |
04| |'L'|'o'|'r'|'e'|'m'|' '| | 'o' | 'r' | 'u' | 'm' | '.' | NUL | |
| 864 |
05| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 865 |
06| | string literal (type: 'char[446]') | |
| 866 |
07| +----------------------------------------------------------------------+ |
| 867 |
08| | | | | | | | | | | | | | | | |
| 868 |
09| | | | | | | | | | | | | | | | |
| 869 |
10| v v v v v v v v v v v v v v v |
| 870 |
11|+---+---------------------+----++--------------------------------------+ |
| 871 |
12||[0]| ... |[99]|| after valid range | |
| 872 |
13|+---+---------------------+----+| | |
| 873 |
14|| 'buf' (type: 'char[100]') || | |
| 874 |
15|+------------------------------++--------------------------------------+ |
| 875 |
16||~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~||~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~| |
| 876 |
17| | | |
| 877 |
18| +---------+---------+ +----------+----------+ |
| 878 |
19| |capacity: 100 bytes| |overflow of 346 bytes| |
| 879 |
20| +-------------------+ +---------------------+ |
| 880 |
|
| 881 |
rows 8-10 are the direction widget. */ |
| 882 |
|
| 883 |
class direction_widget : public leaf_widget |
| 884 |
{ |
| 885 |
public: |
| 886 |
direction_widget (const access_diagram_impl &dia_impl, |
| 887 |
const bit_to_table_map &btm) |
| 888 |
: leaf_widget (), |
| 889 |
m_dia_impl (dia_impl), |
| 890 |
m_btm (btm) |
| 891 |
{ |
| 892 |
} |
| 893 |
const char *get_desc () const override |
| 894 |
{ |
| 895 |
return "direction_widget"; |
| 896 |
} |
| 897 |
canvas::size_t calc_req_size () final override |
| 898 |
{ |
| 899 |
/* Get our width from our siblings. */ |
| 900 |
return canvas::size_t (0, 3); |
| 901 |
} |
| 902 |
void paint_to_canvas (canvas &canvas) final override; |
| 903 |
|
| 904 |
private: |
| 905 |
const access_diagram_impl &m_dia_impl; |
| 906 |
const bit_to_table_map &m_btm; |
| 907 |
}; |
| 908 |
|
| 909 |
/* A widget for adding an x_ruler to a diagram based on table columns, |
| 910 |
offloading column-width calculation to shared objects, so that the ruler |
| 911 |
lines up with other tables in the diagram. |
| 912 |
|
| 913 |
For example, in: |
| 914 |
|
| 915 |
01| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 916 |
02| |[0]|[1]|[2]|[3]|[4]|[5]| ... |[440]|[441]|[442]|[443]|[444]|[445]| |
| 917 |
03| +---+---+---+---+---+---+ +-----+-----+-----+-----+-----+-----+ |
| 918 |
04| |'L'|'o'|'r'|'e'|'m'|' '| | 'o' | 'r' | 'u' | 'm' | '.' | NUL | |
| 919 |
05| +---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 920 |
06| | string literal (type: 'char[446]') | |
| 921 |
07| +----------------------------------------------------------------------+ |
| 922 |
08| | | | | | | | | | | | | | | | |
| 923 |
09| | | | | | | | | | | | | | | | |
| 924 |
10| v v v v v v v v v v v v v v v |
| 925 |
11|+---+---------------------+----++--------------------------------------+ |
| 926 |
12||[0]| ... |[99]|| after valid range | |
| 927 |
13|+---+---------------------+----+| | |
| 928 |
14|| 'buf' (type: 'char[100]') || | |
| 929 |
15|+------------------------------++--------------------------------------+ |
| 930 |
16||~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~||~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~| |
| 931 |
17| | | |
| 932 |
18| +---------+---------+ +----------+----------+ |
| 933 |
19| |capacity: 100 bytes| |overflow of 346 bytes| |
| 934 |
20| +-------------------+ +---------------------+ |
| 935 |
|
| 936 |
rows 16-20 are the x_aligned_x_ruler_widget. */ |
| 937 |
|
| 938 |
class x_aligned_x_ruler_widget : public leaf_widget |
| 939 |
{ |
| 940 |
public: |
| 941 |
x_aligned_x_ruler_widget (const access_diagram_impl &dia_impl, |
| 942 |
const theme &theme) |
| 943 |
: m_dia_impl (dia_impl), |
| 944 |
m_theme (theme) |
| 945 |
{ |
| 946 |
} |
| 947 |
|
| 948 |
const char *get_desc () const override |
| 949 |
{ |
| 950 |
return "x_aligned_ruler_widget"; |
| 951 |
} |
| 952 |
|
| 953 |
void add_range (const table::range_t &x_range, |
| 954 |
styled_string text, |
| 955 |
style::id_t style_id) |
| 956 |
{ |
| 957 |
m_labels.push_back (label (x_range, std::move (text), style_id)); |
| 958 |
} |
| 959 |
|
| 960 |
canvas::size_t calc_req_size () final override |
| 961 |
{ |
| 962 |
x_ruler r (make_x_ruler ()); |
| 963 |
return r.get_size (); |
| 964 |
} |
| 965 |
|
| 966 |
void paint_to_canvas (canvas &canvas) final override |
| 967 |
{ |
| 968 |
x_ruler r (make_x_ruler ()); |
| 969 |
r.paint_to_canvas (canvas, |
| 970 |
get_top_left (), |
| 971 |
m_theme); |
| 972 |
} |
| 973 |
|
| 974 |
private: |
| 975 |
struct label |
| 976 |
{ |
| 977 |
label (const table::range_t &table_x_range, |
| 978 |
styled_string text, |
| 979 |
style::id_t style_id) |
| 980 |
: m_table_x_range (table_x_range), |
| 981 |
m_text (std::move (text)), |
| 982 |
m_style_id (style_id) |
| 983 |
{ |
| 984 |
} |
| 985 |
table::range_t m_table_x_range; |
| 986 |
styled_string m_text; |
| 987 |
style::id_t m_style_id; |
| 988 |
}; |
| 989 |
|
| 990 |
x_ruler make_x_ruler () const; |
| 991 |
|
| 992 |
const access_diagram_impl &m_dia_impl; |
| 993 |
const theme &m_theme; |
| 994 |
std::vector<label> m_labels; |
| 995 |
}; |
| 996 |
|
| 997 |
/* A two-way mapping between access_ranges and table columns, for use by |
| 998 |
spatial_item subclasses for creating tables. |
| 999 |
For example when visualizing a bogus access of 'int arr[10];' |
| 1000 |
at 'arr[10]', we might have: |
| 1001 |
- table column 0 is "bytes 0-3" (for arr[0]) |
| 1002 |
- table column 1 is "bytes 4-35" (for arr[1] through arr[8]) |
| 1003 |
- table column 2 is "bytes 36-39 (for arr[9]) |
| 1004 |
- table column 3 is blank to emphasize a hard boundary between |
| 1005 |
valid/invalid accesses. |
| 1006 |
- table column 4 is "bytes 40-44" (for arr[10]) |
| 1007 |
|
| 1008 |
We store this as a pair of maps from region_offset to table x; in |
| 1009 |
the abvove example: |
| 1010 |
|
| 1011 |
region offset table_x prev_table_x |
| 1012 |
bit 0 (aka byte 0) 0 (none) |
| 1013 |
bit 32 (aka byte 4) 1 0 |
| 1014 |
bit 288 (aka byte 36) 2 1 |
| 1015 |
bit 320 (aka byte 40) 4 2 |
| 1016 |
bit 352 (aka byte 44) (none) (none) |
| 1017 |
|
| 1018 |
so that e.g given the half-open byte range [0, 40) |
| 1019 |
we can determine the closed range of table x [0, 2]. */ |
| 1020 |
|
| 1021 |
class bit_to_table_map |
| 1022 |
{ |
| 1023 |
public: |
| 1024 |
/* Populate m_table_x_for_bit and m_bit_for_table_x. */ |
| 1025 |
void populate (const boundaries &boundaries, |
| 1026 |
region_model_manager &mgr, |
| 1027 |
logger *logger) |
| 1028 |
{ |
| 1029 |
LOG_SCOPE (logger); |
| 1030 |
|
| 1031 |
int table_x = 0; |
| 1032 |
std::vector <region_offset> vec_boundaries (boundaries.begin (), |
| 1033 |
boundaries.end ()); |
| 1034 |
|
| 1035 |
/* Sort into an order that makes sense. */ |
| 1036 |
std::sort (vec_boundaries.begin (), |
| 1037 |
vec_boundaries.end ()); |
| 1038 |
|
| 1039 |
if (logger) |
| 1040 |
{ |
| 1041 |
logger->log ("vec_boundaries"); |
| 1042 |
logger->inc_indent (); |
| 1043 |
for (unsigned idx = 0; idx < vec_boundaries.size (); idx++) |
| 1044 |
{ |
| 1045 |
logger->start_log_line (); |
| 1046 |
logger->log_partial ("idx: %i: ", idx); |
| 1047 |
vec_boundaries[idx].dump_to_pp (logger->get_printer (), true); |
| 1048 |
logger->end_log_line (); |
| 1049 |
} |
| 1050 |
logger->dec_indent (); |
| 1051 |
} |
| 1052 |
|
| 1053 |
for (size_t idx = 0; idx < vec_boundaries.size (); idx++) |
| 1054 |
{ |
| 1055 |
const region_offset &offset = vec_boundaries[idx]; |
| 1056 |
if (idx > 0 && (idx + 1) < vec_boundaries.size ()) |
| 1057 |
{ |
| 1058 |
if (boundaries.get_kind (offset) == boundaries::kind::HARD) |
| 1059 |
table_x += 1; |
| 1060 |
} |
| 1061 |
m_table_x_for_offset[offset] = table_x; |
| 1062 |
if ((idx + 1) < vec_boundaries.size ()) |
| 1063 |
{ |
| 1064 |
const region_offset &next_offset = vec_boundaries[idx + 1]; |
| 1065 |
m_table_x_for_prev_offset[next_offset] = table_x; |
| 1066 |
m_range_for_table_x[table_x] |
| 1067 |
= access_range (offset, next_offset, mgr); |
| 1068 |
} |
| 1069 |
table_x += 1; |
| 1070 |
} |
| 1071 |
m_num_columns = table_x - 1; |
| 1072 |
|
| 1073 |
if (logger) |
| 1074 |
log (*logger); |
| 1075 |
} |
| 1076 |
|
| 1077 |
unsigned get_num_columns () const |
| 1078 |
{ |
| 1079 |
return m_num_columns; |
| 1080 |
} |
| 1081 |
|
| 1082 |
table::range_t get_table_x_for_range (const access_range &range) const |
| 1083 |
{ |
| 1084 |
return table::range_t (get_table_x_for_offset (range.m_start), |
| 1085 |
get_table_x_for_prev_offset (range.m_next) + 1); |
| 1086 |
} |
| 1087 |
|
| 1088 |
table::rect_t get_table_rect (const access_range &range, |
| 1089 |
const int table_y, const int table_h) const |
| 1090 |
{ |
| 1091 |
const table::range_t x_range (get_table_x_for_range (range)); |
| 1092 |
return table::rect_t (table::coord_t (x_range.start, table_y), |
| 1093 |
table::size_t (x_range.get_size (), table_h)); |
| 1094 |
} |
| 1095 |
|
| 1096 |
table::rect_t get_table_rect (const region *base_reg, |
| 1097 |
const bit_range &bits, |
| 1098 |
const int table_y, const int table_h) const |
| 1099 |
{ |
| 1100 |
const access_range range (base_reg, bits); |
| 1101 |
return get_table_rect (range, table_y, table_h); |
| 1102 |
} |
| 1103 |
|
| 1104 |
table::rect_t get_table_rect (const region *base_reg, |
| 1105 |
const byte_range &bytes, |
| 1106 |
const int table_y, const int table_h) const |
| 1107 |
{ |
| 1108 |
return get_table_rect (base_reg, bytes.as_bit_range (), table_y, table_h); |
| 1109 |
} |
| 1110 |
|
| 1111 |
bool maybe_get_access_range_for_table_x (int table_x, |
| 1112 |
access_range *out) const |
| 1113 |
{ |
| 1114 |
auto slot = m_range_for_table_x.find (table_x); |
| 1115 |
if (slot == m_range_for_table_x.end ()) |
| 1116 |
return false; |
| 1117 |
*out = slot->second; |
| 1118 |
return true; |
| 1119 |
} |
| 1120 |
|
| 1121 |
void log (logger &logger) const |
| 1122 |
{ |
| 1123 |
logger.log ("table columns"); |
| 1124 |
logger.inc_indent (); |
| 1125 |
for (unsigned table_x = 0; table_x < get_num_columns (); table_x++) |
| 1126 |
{ |
| 1127 |
logger.start_log_line (); |
| 1128 |
logger.log_partial ("table_x: %i", table_x); |
| 1129 |
access_range range_for_column (NULL, bit_range (0, 0)); |
| 1130 |
if (maybe_get_access_range_for_table_x (table_x, &range_for_column)) |
| 1131 |
{ |
| 1132 |
logger.log_partial (": range: "); |
| 1133 |
range_for_column.dump_to_pp (logger.get_printer (), true); |
| 1134 |
} |
| 1135 |
logger.end_log_line (); |
| 1136 |
} |
| 1137 |
logger.dec_indent (); |
| 1138 |
} |
| 1139 |
|
| 1140 |
int get_table_x_for_offset (region_offset offset) const |
| 1141 |
{ |
| 1142 |
auto slot = m_table_x_for_offset.find (offset); |
| 1143 |
|
| 1144 |
/* If this fails, then we probably failed to fully populate m_boundaries |
| 1145 |
in find_boundaries. */ |
| 1146 |
gcc_assert (slot != m_table_x_for_offset.end ()); |
| 1147 |
|
| 1148 |
return slot->second; |
| 1149 |
} |
| 1150 |
|
| 1151 |
private: |
| 1152 |
int get_table_x_for_prev_offset (region_offset offset) const |
| 1153 |
{ |
| 1154 |
auto slot = m_table_x_for_prev_offset.find (offset); |
| 1155 |
|
| 1156 |
/* If this fails, then we probably failed to fully populate m_boundaries |
| 1157 |
in find_boundaries. */ |
| 1158 |
gcc_assert (slot != m_table_x_for_prev_offset.end ()); |
| 1159 |
|
| 1160 |
return slot->second; |
| 1161 |
} |
| 1162 |
|
| 1163 |
std::map<region_offset, int> m_table_x_for_offset; |
| 1164 |
std::map<region_offset, int> m_table_x_for_prev_offset; |
| 1165 |
std::map<int, access_range> m_range_for_table_x; |
| 1166 |
unsigned m_num_columns; |
| 1167 |
}; |
| 1168 |
|
| 1169 |
/* Base class for something in the diagram that participates |
| 1170 |
in two steps of diagram creation: |
| 1171 |
(a) populating a boundaries instance with the boundaries of interest |
| 1172 |
(b) creating a table instance for itself. |
| 1173 |
|
| 1174 |
Offsets in the boundaries are all expressed relative to the base |
| 1175 |
region of the access_operation. */ |
| 1176 |
|
| 1177 |
class spatial_item |
| 1178 |
{ |
| 1179 |
public: |
| 1180 |
virtual ~spatial_item () {} |
| 1181 |
virtual void add_boundaries (boundaries &out, logger *) const = 0; |
| 1182 |
|
| 1183 |
virtual table make_table (const bit_to_table_map &btm, |
| 1184 |
style_manager &sm) const = 0; |
| 1185 |
}; |
| 1186 |
|
| 1187 |
/* A spatial_item that involves showing an svalue at a particular offset. */ |
| 1188 |
|
| 1189 |
class svalue_spatial_item : public spatial_item |
| 1190 |
{ |
| 1191 |
public: |
| 1192 |
enum class kind |
| 1193 |
{ |
| 1194 |
WRITTEN, |
| 1195 |
EXISTING |
| 1196 |
}; |
| 1197 |
protected: |
| 1198 |
svalue_spatial_item (const svalue &sval, |
| 1199 |
access_range bits, |
| 1200 |
enum kind kind) |
| 1201 |
: m_sval (sval), m_bits (bits), m_kind (kind) |
| 1202 |
{ |
| 1203 |
} |
| 1204 |
|
| 1205 |
const svalue &m_sval; |
| 1206 |
access_range m_bits; |
| 1207 |
enum kind m_kind; |
| 1208 |
}; |
| 1209 |
|
| 1210 |
static std::unique_ptr<spatial_item> |
| 1211 |
make_existing_svalue_spatial_item (const svalue *sval, |
| 1212 |
const access_range &bits, |
| 1213 |
const theme &theme); |
| 1214 |
|
| 1215 |
class compound_svalue_spatial_item : public svalue_spatial_item |
| 1216 |
{ |
| 1217 |
public: |
| 1218 |
compound_svalue_spatial_item (const compound_svalue &sval, |
| 1219 |
const access_range &bits, |
| 1220 |
enum kind kind, |
| 1221 |
const theme &theme) |
| 1222 |
: svalue_spatial_item (sval, bits, kind), |
| 1223 |
m_compound_sval (sval) |
| 1224 |
{ |
| 1225 |
const binding_map &map = m_compound_sval.get_map (); |
| 1226 |
auto_vec <const binding_key *> binding_keys; |
| 1227 |
for (auto iter : map) |
| 1228 |
{ |
| 1229 |
const binding_key *key = iter.first; |
| 1230 |
const svalue *bound_sval = iter.second; |
| 1231 |
if (const concrete_binding *concrete_key |
| 1232 |
= key->dyn_cast_concrete_binding ()) |
| 1233 |
{ |
| 1234 |
access_range range (nullptr, |
| 1235 |
concrete_key->get_bit_range ()); |
| 1236 |
if (std::unique_ptr<spatial_item> child |
| 1237 |
= make_existing_svalue_spatial_item (bound_sval, |
| 1238 |
range, |
| 1239 |
theme)) |
| 1240 |
m_children.push_back (std::move (child)); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
void add_boundaries (boundaries &out, logger *logger) const final override |
| 1246 |
{ |
| 1247 |
LOG_SCOPE (logger); |
| 1248 |
for (auto &iter : m_children) |
| 1249 |
iter->add_boundaries (out, logger); |
| 1250 |
} |
| 1251 |
|
| 1252 |
table make_table (const bit_to_table_map &btm, |
| 1253 |
style_manager &sm) const final override |
| 1254 |
{ |
| 1255 |
std::vector<table> child_tables; |
| 1256 |
int max_rows = 0; |
| 1257 |
for (auto &iter : m_children) |
| 1258 |
{ |
| 1259 |
table child_table (iter->make_table (btm, sm)); |
| 1260 |
max_rows = MAX (max_rows, child_table.get_size ().h); |
| 1261 |
child_tables.push_back (std::move (child_table)); |
| 1262 |
} |
| 1263 |
table t (table::size_t (btm.get_num_columns (), max_rows)); |
| 1264 |
for (auto &&child_table : child_tables) |
| 1265 |
t.add_other_table (std::move (child_table), |
| 1266 |
table::coord_t (0, 0)); |
| 1267 |
return t; |
| 1268 |
} |
| 1269 |
|
| 1270 |
private: |
| 1271 |
const compound_svalue &m_compound_sval; |
| 1272 |
std::vector<std::unique_ptr<spatial_item>> m_children; |
| 1273 |
}; |
| 1274 |
|
| 1275 |
/* Loop through the TABLE_X_RANGE columns of T, adding |
| 1276 |
cells containing "..." in any unoccupied ranges of table cell. */ |
| 1277 |
|
| 1278 |
static void |
| 1279 |
add_ellipsis_to_gaps (table &t, |
| 1280 |
style_manager &sm, |
| 1281 |
const table::range_t &table_x_range, |
| 1282 |
const table::range_t &table_y_range) |
| 1283 |
{ |
| 1284 |
int table_x = table_x_range.get_min (); |
| 1285 |
while (table_x < table_x_range.get_next ()) |
| 1286 |
{ |
| 1287 |
/* Find a run of unoccupied table cells. */ |
| 1288 |
const int start_table_x = table_x; |
| 1289 |
while (table_x < table_x_range.get_next () |
| 1290 |
&& !t.get_placement_at (table::coord_t (table_x, |
| 1291 |
table_y_range.get_min ()))) |
| 1292 |
table_x++; |
| 1293 |
const table::range_t unoccupied_x_range (start_table_x, table_x); |
| 1294 |
if (unoccupied_x_range.get_size () > 0) |
| 1295 |
t.set_cell_span (table::rect_t (unoccupied_x_range, table_y_range), |
| 1296 |
styled_string (sm, "...")); |
| 1297 |
/* Skip occupied table cells. */ |
| 1298 |
while (table_x < table_x_range.get_next () |
| 1299 |
&& t.get_placement_at (table::coord_t (table_x, |
| 1300 |
table_y_range.get_min ()))) |
| 1301 |
table_x++; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* Subclass of spatial_item for visualizing the region of memory |
| 1306 |
that's valid to access relative to the base region of region accessed in |
| 1307 |
the operation. */ |
| 1308 |
|
| 1309 |
class valid_region_spatial_item : public spatial_item |
| 1310 |
{ |
| 1311 |
public: |
| 1312 |
valid_region_spatial_item (const access_operation &op, |
| 1313 |
diagnostic_event_id_t region_creation_event_id, |
| 1314 |
const theme &theme) |
| 1315 |
: m_op (op), |
| 1316 |
m_region_creation_event_id (region_creation_event_id), |
| 1317 |
m_boundaries (nullptr), |
| 1318 |
m_existing_sval (op.m_model.get_store_value (op.m_base_region, nullptr)), |
| 1319 |
m_existing_sval_spatial_item |
| 1320 |
(make_existing_svalue_spatial_item (m_existing_sval, |
| 1321 |
op.get_valid_bits (), |
| 1322 |
theme)) |
| 1323 |
{ |
| 1324 |
} |
| 1325 |
|
| 1326 |
void add_boundaries (boundaries &out, logger *logger) const final override |
| 1327 |
{ |
| 1328 |
LOG_SCOPE (logger); |
| 1329 |
m_boundaries = &out; |
| 1330 |
access_range valid_bits = m_op.get_valid_bits (); |
| 1331 |
if (logger) |
| 1332 |
{ |
| 1333 |
logger->start_log_line (); |
| 1334 |
logger->log_partial ("valid bits: "); |
| 1335 |
valid_bits.dump_to_pp (logger->get_printer (), true); |
| 1336 |
logger->end_log_line (); |
| 1337 |
} |
| 1338 |
out.add (valid_bits, boundaries::kind::HARD); |
| 1339 |
|
| 1340 |
if (m_existing_sval_spatial_item) |
| 1341 |
{ |
| 1342 |
if (logger) |
| 1343 |
{ |
| 1344 |
logger->start_log_line (); |
| 1345 |
logger->log_partial ("existing svalue: "); |
| 1346 |
m_existing_sval->dump_to_pp (logger->get_printer (), true); |
| 1347 |
logger->end_log_line (); |
| 1348 |
} |
| 1349 |
m_existing_sval_spatial_item->add_boundaries (out, logger); |
| 1350 |
} |
| 1351 |
|
| 1352 |
/* Support for showing first and final element in array types. */ |
| 1353 |
if (tree base_type = m_op.m_base_region->get_type ()) |
| 1354 |
if (TREE_CODE (base_type) == ARRAY_TYPE) |
| 1355 |
{ |
| 1356 |
if (logger) |
| 1357 |
logger->log ("showing first and final element in array type"); |
| 1358 |
region_model_manager *mgr = m_op.m_model.get_manager (); |
| 1359 |
tree domain = TYPE_DOMAIN (base_type); |
| 1360 |
if (domain && TYPE_MIN_VALUE (domain) && TYPE_MAX_VALUE (domain)) |
| 1361 |
{ |
| 1362 |
const svalue *min_idx_sval |
| 1363 |
= mgr->get_or_create_constant_svalue (TYPE_MIN_VALUE (domain)); |
| 1364 |
const svalue *max_idx_sval |
| 1365 |
= mgr->get_or_create_constant_svalue (TYPE_MAX_VALUE (domain)); |
| 1366 |
const region *min_element = |
| 1367 |
mgr->get_element_region (m_op.m_base_region, |
| 1368 |
TREE_TYPE (base_type), |
| 1369 |
min_idx_sval); |
| 1370 |
out.add (*min_element, mgr, boundaries::kind::SOFT); |
| 1371 |
const region *max_element = |
| 1372 |
mgr->get_element_region (m_op.m_base_region, |
| 1373 |
TREE_TYPE (base_type), |
| 1374 |
max_idx_sval); |
| 1375 |
out.add (*max_element, mgr, boundaries::kind::SOFT); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
/* Subroutine of make_table when base region has ARRAY_TYPE. */ |
| 1381 |
void add_array_elements_to_table (table &t, |
| 1382 |
const bit_to_table_map &btm, |
| 1383 |
style_manager &sm) const |
| 1384 |
{ |
| 1385 |
tree base_type = m_op.m_base_region->get_type (); |
| 1386 |
gcc_assert (TREE_CODE (base_type) == ARRAY_TYPE); |
| 1387 |
gcc_assert (m_boundaries != nullptr); |
| 1388 |
|
| 1389 |
tree domain = TYPE_DOMAIN (base_type); |
| 1390 |
if (!(domain && TYPE_MIN_VALUE (domain) && TYPE_MAX_VALUE (domain))) |
| 1391 |
return; |
| 1392 |
|
| 1393 |
const int table_y = 0; |
| 1394 |
const int table_h = 1; |
| 1395 |
const table::range_t table_y_range (table_y, table_y + table_h); |
| 1396 |
|
| 1397 |
t.add_row (); |
| 1398 |
|
| 1399 |
const table::range_t min_x_range |
| 1400 |
= maybe_add_array_index_to_table (t, btm, sm, table_y_range, |
| 1401 |
TYPE_MIN_VALUE (domain)); |
| 1402 |
const table::range_t max_x_range |
| 1403 |
= maybe_add_array_index_to_table (t, btm, sm, table_y_range, |
| 1404 |
TYPE_MAX_VALUE (domain)); |
| 1405 |
|
| 1406 |
if (TREE_TYPE (base_type) == char_type_node) |
| 1407 |
{ |
| 1408 |
/* For a char array,: if there are any hard boundaries in |
| 1409 |
m_boundaries that are *within* the valid region, |
| 1410 |
then show those index values. */ |
| 1411 |
std::vector<region_offset> hard_boundaries |
| 1412 |
= m_boundaries->get_hard_boundaries_in_range |
| 1413 |
(tree_to_shwi (TYPE_MIN_VALUE (domain)), |
| 1414 |
tree_to_shwi (TYPE_MAX_VALUE (domain))); |
| 1415 |
for (auto &offset : hard_boundaries) |
| 1416 |
{ |
| 1417 |
const int table_x = btm.get_table_x_for_offset (offset); |
| 1418 |
if (!offset.concrete_p ()) |
| 1419 |
continue; |
| 1420 |
byte_offset_t byte; |
| 1421 |
if (!offset.get_concrete_byte_offset (&byte)) |
| 1422 |
continue; |
| 1423 |
table::range_t table_x_range (table_x, table_x + 1); |
| 1424 |
t.maybe_set_cell_span (table::rect_t (table_x_range, |
| 1425 |
table_y_range), |
| 1426 |
fmt_styled_string (sm, "[%wi]", |
| 1427 |
byte.to_shwi ())); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
add_ellipsis_to_gaps (t, sm, |
| 1432 |
table::range_t (min_x_range.get_next (), |
| 1433 |
max_x_range.get_min ()), |
| 1434 |
table_y_range); |
| 1435 |
} |
| 1436 |
|
| 1437 |
table::range_t |
| 1438 |
maybe_add_array_index_to_table (table &t, |
| 1439 |
const bit_to_table_map &btm, |
| 1440 |
style_manager &sm, |
| 1441 |
const table::range_t table_y_range, |
| 1442 |
tree idx_cst) const |
| 1443 |
{ |
| 1444 |
region_model_manager * const mgr = m_op.get_manager (); |
| 1445 |
tree base_type = m_op.m_base_region->get_type (); |
| 1446 |
const svalue *idx_sval |
| 1447 |
= mgr->get_or_create_constant_svalue (idx_cst); |
| 1448 |
const region *element_reg = mgr->get_element_region (m_op.m_base_region, |
| 1449 |
TREE_TYPE (base_type), |
| 1450 |
idx_sval); |
| 1451 |
const access_range element_range (*element_reg, mgr); |
| 1452 |
const table::range_t element_x_range |
| 1453 |
= btm.get_table_x_for_range (element_range); |
| 1454 |
|
| 1455 |
t.maybe_set_cell_span (table::rect_t (element_x_range, |
| 1456 |
table_y_range), |
| 1457 |
fmt_styled_string (sm, "[%E]", idx_cst)); |
| 1458 |
|
| 1459 |
return element_x_range; |
| 1460 |
} |
| 1461 |
|
| 1462 |
table make_table (const bit_to_table_map &btm, |
| 1463 |
style_manager &sm) const final override |
| 1464 |
{ |
| 1465 |
table t (table::size_t (btm.get_num_columns (), 0)); |
| 1466 |
|
| 1467 |
if (tree base_type = m_op.m_base_region->get_type ()) |
| 1468 |
if (TREE_CODE (base_type) == ARRAY_TYPE) |
| 1469 |
add_array_elements_to_table (t, btm, sm); |
| 1470 |
|
| 1471 |
/* Make use of m_existing_sval_spatial_item, if any. */ |
| 1472 |
if (m_existing_sval_spatial_item) |
| 1473 |
{ |
| 1474 |
table table_for_existing |
| 1475 |
= m_existing_sval_spatial_item->make_table (btm, sm); |
| 1476 |
const int table_y = t.add_rows (table_for_existing.get_size ().h); |
| 1477 |
t.add_other_table (std::move (table_for_existing), |
| 1478 |
table::coord_t (0, table_y)); |
| 1479 |
} |
| 1480 |
|
| 1481 |
access_range valid_bits = m_op.get_valid_bits (); |
| 1482 |
const int table_y = t.add_row (); |
| 1483 |
const int table_h = 1; |
| 1484 |
table::rect_t rect = btm.get_table_rect (valid_bits, table_y, table_h); |
| 1485 |
styled_string s; |
| 1486 |
switch (m_op.m_base_region->get_kind ()) |
| 1487 |
{ |
| 1488 |
default: |
| 1489 |
s = styled_string (sm, _("region")); |
| 1490 |
break; |
| 1491 |
case RK_DECL: |
| 1492 |
{ |
| 1493 |
const decl_region *decl_reg |
| 1494 |
= as_a <const decl_region *> (m_op.m_base_region); |
| 1495 |
tree decl = decl_reg->get_decl (); |
| 1496 |
s = fmt_styled_string (sm, "%qE (type: %qT)", |
| 1497 |
decl, |
| 1498 |
TREE_TYPE (decl)); |
| 1499 |
} |
| 1500 |
break; |
| 1501 |
case RK_HEAP_ALLOCATED: |
| 1502 |
{ |
| 1503 |
if (m_region_creation_event_id.known_p ()) |
| 1504 |
s = fmt_styled_string (sm, _("buffer allocated on heap at %@"), |
| 1505 |
&m_region_creation_event_id); |
| 1506 |
else |
| 1507 |
s = styled_string (sm, _("heap-allocated buffer")); |
| 1508 |
} |
| 1509 |
break; |
| 1510 |
case RK_ALLOCA: |
| 1511 |
{ |
| 1512 |
if (m_region_creation_event_id.known_p ()) |
| 1513 |
s = fmt_styled_string (sm, _("buffer allocated on stack at %@"), |
| 1514 |
&m_region_creation_event_id); |
| 1515 |
else |
| 1516 |
s = styled_string (sm, _("stack-allocated buffer")); |
| 1517 |
} |
| 1518 |
break; |
| 1519 |
case RK_STRING: |
| 1520 |
{ |
| 1521 |
const string_region *string_reg |
| 1522 |
= as_a <const string_region *> (m_op.m_base_region); |
| 1523 |
tree string_cst = string_reg->get_string_cst (); |
| 1524 |
s = fmt_styled_string (sm, _("string literal (type: %qT)"), |
| 1525 |
TREE_TYPE (string_cst)); |
| 1526 |
} |
| 1527 |
break; |
| 1528 |
} |
| 1529 |
t.set_cell_span (rect, std::move (s)); |
| 1530 |
|
| 1531 |
return t; |
| 1532 |
} |
| 1533 |
|
| 1534 |
private: |
| 1535 |
const access_operation &m_op; |
| 1536 |
diagnostic_event_id_t m_region_creation_event_id; |
| 1537 |
mutable const boundaries *m_boundaries; |
| 1538 |
const svalue *m_existing_sval; |
| 1539 |
std::unique_ptr<spatial_item> m_existing_sval_spatial_item; |
| 1540 |
}; |
| 1541 |
|
| 1542 |
/* Subclass of spatial_item for visualizing the region of memory |
| 1543 |
that's actually accessed by the read or write, for reads and |
| 1544 |
for write cases where we don't know the svalue written. */ |
| 1545 |
|
| 1546 |
class accessed_region_spatial_item : public spatial_item |
| 1547 |
{ |
| 1548 |
public: |
| 1549 |
accessed_region_spatial_item (const access_operation &op) : m_op (op) {} |
| 1550 |
|
| 1551 |
void add_boundaries (boundaries &out, logger *logger) const final override |
| 1552 |
{ |
| 1553 |
LOG_SCOPE (logger); |
| 1554 |
access_range actual_bits = m_op.get_actual_bits (); |
| 1555 |
if (logger) |
| 1556 |
{ |
| 1557 |
logger->start_log_line (); |
| 1558 |
logger->log_partial ("actual bits: "); |
| 1559 |
actual_bits.dump_to_pp (logger->get_printer (), true); |
| 1560 |
logger->end_log_line (); |
| 1561 |
} |
| 1562 |
out.add (actual_bits, boundaries::kind::HARD); |
| 1563 |
} |
| 1564 |
|
| 1565 |
table make_table (const bit_to_table_map &btm, |
| 1566 |
style_manager &sm) const final override |
| 1567 |
{ |
| 1568 |
table t (table::size_t (btm.get_num_columns (), 1)); |
| 1569 |
|
| 1570 |
access_range actual_bits = m_op.get_actual_bits (); |
| 1571 |
const int table_y = 0; |
| 1572 |
const int table_h = 1; |
| 1573 |
table::rect_t rect = btm.get_table_rect (actual_bits, table_y, table_h); |
| 1574 |
t.set_cell_span (rect, styled_string (get_label_string (sm))); |
| 1575 |
|
| 1576 |
return t; |
| 1577 |
} |
| 1578 |
|
| 1579 |
private: |
| 1580 |
styled_string get_label_string (style_manager &sm) const |
| 1581 |
{ |
| 1582 |
const access_range accessed_bits (m_op.get_actual_bits ()); |
| 1583 |
return get_access_size_str (sm, |
| 1584 |
m_op, |
| 1585 |
accessed_bits, |
| 1586 |
m_op.m_reg.get_type ()); |
| 1587 |
} |
| 1588 |
|
| 1589 |
const access_operation &m_op; |
| 1590 |
}; |
| 1591 |
|
| 1592 |
/* Subclass of spatial_item for when we know the svalue being written |
| 1593 |
to the accessed region. |
| 1594 |
Can be subclassed to give visualizations of specific kinds of svalue. */ |
| 1595 |
|
| 1596 |
class written_svalue_spatial_item : public spatial_item |
| 1597 |
{ |
| 1598 |
public: |
| 1599 |
written_svalue_spatial_item (const access_operation &op, |
| 1600 |
const svalue &sval, |
| 1601 |
access_range actual_bits) |
| 1602 |
: m_op (op), m_sval (sval), m_actual_bits (actual_bits) |
| 1603 |
{} |
| 1604 |
|
| 1605 |
void add_boundaries (boundaries &out, logger *logger) const override |
| 1606 |
{ |
| 1607 |
LOG_SCOPE (logger); |
| 1608 |
out.add (m_actual_bits, boundaries::kind::HARD); |
| 1609 |
} |
| 1610 |
|
| 1611 |
table make_table (const bit_to_table_map &btm, |
| 1612 |
style_manager &sm) const override |
| 1613 |
{ |
| 1614 |
table t (table::size_t (btm.get_num_columns (), 0)); |
| 1615 |
|
| 1616 |
const int table_y = t.add_row (); |
| 1617 |
const int table_h = 1; |
| 1618 |
table::rect_t rect = btm.get_table_rect (m_actual_bits, table_y, table_h); |
| 1619 |
t.set_cell_span (rect, styled_string (get_label_string (sm))); |
| 1620 |
return t; |
| 1621 |
} |
| 1622 |
|
| 1623 |
protected: |
| 1624 |
styled_string get_label_string (style_manager &sm) const |
| 1625 |
{ |
| 1626 |
tree rep_tree = m_op.m_model.get_representative_tree (&m_sval); |
| 1627 |
if (rep_tree) |
| 1628 |
{ |
| 1629 |
if (TREE_CODE (rep_tree) == SSA_NAME) |
| 1630 |
if (tree var = SSA_NAME_VAR (rep_tree)) |
| 1631 |
rep_tree = var; |
| 1632 |
switch (TREE_CODE (rep_tree)) |
| 1633 |
{ |
| 1634 |
default: |
| 1635 |
break; |
| 1636 |
case INTEGER_CST: |
| 1637 |
return fmt_styled_string (sm, _("write of %<(%T) %E%>"), |
| 1638 |
TREE_TYPE (rep_tree), |
| 1639 |
rep_tree); |
| 1640 |
|
| 1641 |
case PARM_DECL: |
| 1642 |
case VAR_DECL: |
| 1643 |
return fmt_styled_string (sm, _("write from %qE (type: %qT)"), |
| 1644 |
rep_tree, |
| 1645 |
TREE_TYPE (rep_tree)); |
| 1646 |
break; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
const access_range accessed_bits (m_op.get_actual_bits ()); |
| 1651 |
return get_access_size_str (sm, |
| 1652 |
m_op, |
| 1653 |
accessed_bits, |
| 1654 |
m_sval.get_type ()); |
| 1655 |
} |
| 1656 |
|
| 1657 |
const access_operation &m_op; |
| 1658 |
const svalue &m_sval; |
| 1659 |
access_range m_actual_bits; |
| 1660 |
}; |
| 1661 |
|
| 1662 |
/* Subclass of svalue_spatial_item for initial_svalue of a string_region |
| 1663 |
i.e. for string literals. |
| 1664 |
|
| 1665 |
There are three cases: |
| 1666 |
(a) for long strings, show just the head and tail of the string, |
| 1667 |
with an ellipsis: |
| 1668 |
+---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 1669 |
|[0]|[1]|[2]|[3]|[4]|[5]| |[440]|[441]|[442]|[443]|[444]|[445]| |
| 1670 |
+---+---+---+---+---+---+ ... +-----+-----+-----+-----+-----+-----+ |
| 1671 |
|‘L’|‘o’|‘r’|‘e’|‘m’|‘ ’| | ‘o’ | ‘r’ | ‘u’ | ‘m’ | ‘.’ | NUL | |
| 1672 |
+---+---+---+---+---+---+----------+-----+-----+-----+-----+-----+-----+ |
| 1673 |
| string literal (type: ‘char[446]’) | |
| 1674 |
+----------------------------------------------------------------------+ |
| 1675 |
(b) For sufficiently short strings, show the full string: |
| 1676 |
+----------+---------+---------+---------+---------+ +-----------------+ |
| 1677 |
| [0] | [1] | [2] | [3] | [4] | | [5] | |
| 1678 |
+----------+---------+---------+---------+---------+ +-----------------+ |
| 1679 |
| ‘h’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ | | NUL | |
| 1680 |
+----------+---------+---------+---------+---------+-+-----------------+ |
| 1681 |
| string literal (type: ‘char[6]’) | |
| 1682 |
+----------------------------------------------------------------------+ |
| 1683 |
(c) for non-ASCII strings that are short enough to show the full string, |
| 1684 |
show how unicode code points of the bytes decoded as UTF-8: |
| 1685 |
+-----+-----+-----+----+----++----+----+----+----+----+----+----+------+ |
| 1686 |
| [0] | [1] | [2] |[3] |[4] ||[5] |[6] |[7] |[8] |[9] |[10]|[11]| [12] | |
| 1687 |
+-----+-----+-----+----+----++----+----+----+----+----+----+----+------+ |
| 1688 |
|0xe6 |0x96 |0x87 |0xe5|0xad||0x97|0xe5|0x8c|0x96|0xe3|0x81|0x91| 0x00 | |
| 1689 |
+-----+-----+-----+----+----++----+----+----+----+----+----+----+------+ |
| 1690 |
| U+6587 | U+5b57 | U+5316 | U+3051 |U+0000| |
| 1691 |
+-----------------+---------------+--------------+--------------+------+ |
| 1692 |
| string literal (type: ‘char[13]’) | |
| 1693 |
+----------------------------------------------------------------------+ |
| 1694 |
and show the characters themselves if unicode is supported and they are not |
| 1695 |
control characters: |
| 1696 |
┌─────┬─────┬─────┬────┬────┐┌────┬────┬────┬────┬────┬────┬────┬──────┐ |
| 1697 |
│ [0] │ [1] │ [2] │[3] │[4] ││[5] │[6] │[7] │[8] │[9] │[10]│[11]│ [12] │ |
| 1698 |
├─────┼─────┼─────┼────┼────┤├────┼────┼────┼────┼────┼────┼────┼──────┤ |
| 1699 |
│0xe6 │0x96 │0x87 │0xe5│0xad││0x97│0xe5│0x8c│0x96│0xe3│0x81│0x91│ 0x00 │ |
| 1700 |
├─────┴─────┴─────┼────┴────┴┴────┼────┴────┴────┼────┴────┴────┼──────┤ |
| 1701 |
│ U+6587 │ U+5b57 │ U+5316 │ U+3051 │U+0000│ |
| 1702 |
├─────────────────┼───────────────┼──────────────┼──────────────┼──────┤ |
| 1703 |
│ 文 │ 字 │ 化 │ け │ NUL │ |
| 1704 |
├─────────────────┴───────────────┴──────────────┴──────────────┴──────┤ |
| 1705 |
│ string literal (type: ‘char[13]’) │ |
| 1706 |
└──────────────────────────────────────────────────────────────────────┘ |
| 1707 |
*/ |
| 1708 |
|
| 1709 |
class string_literal_spatial_item : public svalue_spatial_item |
| 1710 |
{ |
| 1711 |
public: |
| 1712 |
string_literal_spatial_item (const svalue &sval, |
| 1713 |
access_range actual_bits, |
| 1714 |
const string_region &string_reg, |
| 1715 |
const theme &theme, |
| 1716 |
enum kind kind) |
| 1717 |
: svalue_spatial_item (sval, actual_bits, kind), |
| 1718 |
m_string_reg (string_reg), |
| 1719 |
m_theme (theme), |
| 1720 |
m_ellipsis_threshold (param_analyzer_text_art_string_ellipsis_threshold), |
| 1721 |
m_ellipsis_head_len (param_analyzer_text_art_string_ellipsis_head_len), |
| 1722 |
m_ellipsis_tail_len (param_analyzer_text_art_string_ellipsis_tail_len), |
| 1723 |
m_show_full_string (calc_show_full_string ()), |
| 1724 |
m_show_utf8 (m_show_full_string && !pure_ascii_p ()) |
| 1725 |
{ |
| 1726 |
} |
| 1727 |
|
| 1728 |
void add_boundaries (boundaries &out, logger *logger) const override |
| 1729 |
{ |
| 1730 |
LOG_SCOPE (logger); |
| 1731 |
out.add (m_bits, m_kind == svalue_spatial_item::kind::WRITTEN |
| 1732 |
? boundaries::kind::HARD |
| 1733 |
: boundaries::kind::SOFT); |
| 1734 |
|
| 1735 |
tree string_cst = get_string_cst (); |
| 1736 |
/* TREE_STRING_LENGTH is sizeof, not strlen. */ |
| 1737 |
if (m_show_full_string) |
| 1738 |
out.add_all_bytes_in_range (m_bits); |
| 1739 |
else |
| 1740 |
{ |
| 1741 |
byte_range bytes (0, 0); |
| 1742 |
bool valid = m_bits.as_concrete_byte_range (&bytes); |
| 1743 |
gcc_assert (valid); |
| 1744 |
byte_range head_of_string (bytes.get_start_byte_offset (), |
| 1745 |
m_ellipsis_head_len); |
| 1746 |
out.add_all_bytes_in_range (head_of_string); |
| 1747 |
byte_range tail_of_string |
| 1748 |
((bytes.get_start_byte_offset () |
| 1749 |
+ TREE_STRING_LENGTH (string_cst) |
| 1750 |
- m_ellipsis_tail_len), |
| 1751 |
m_ellipsis_tail_len); |
| 1752 |
out.add_all_bytes_in_range (tail_of_string); |
| 1753 |
/* Adding the above pair of ranges will also effectively add |
| 1754 |
the boundaries of the range of ellipsized chars, as they're |
| 1755 |
exactly in between head_of_string and tail_of_string. */ |
| 1756 |
} |
| 1757 |
} |
| 1758 |
|
| 1759 |
table make_table (const bit_to_table_map &btm, |
| 1760 |
style_manager &sm) const override |
| 1761 |
{ |
| 1762 |
table t (table::size_t (btm.get_num_columns (), 0)); |
| 1763 |
|
| 1764 |
const int byte_idx_table_y = (m_kind == svalue_spatial_item::kind::WRITTEN |
| 1765 |
? t.add_row () |
| 1766 |
: -1); |
| 1767 |
const int byte_val_table_y = t.add_row (); |
| 1768 |
|
| 1769 |
byte_range bytes (0, 0); |
| 1770 |
bool valid = m_bits.as_concrete_byte_range (&bytes); |
| 1771 |
gcc_assert (valid); |
| 1772 |
tree string_cst = get_string_cst (); |
| 1773 |
if (m_show_full_string) |
| 1774 |
{ |
| 1775 |
for (byte_offset_t byte_idx_within_cluster |
| 1776 |
= bytes.get_start_byte_offset (); |
| 1777 |
byte_idx_within_cluster < bytes.get_next_byte_offset (); |
| 1778 |
byte_idx_within_cluster = byte_idx_within_cluster + 1) |
| 1779 |
add_column_for_byte |
| 1780 |
(t, btm, sm, byte_idx_within_cluster, |
| 1781 |
byte_idx_within_cluster - bytes.get_start_byte_offset (), |
| 1782 |
byte_idx_table_y, byte_val_table_y); |
| 1783 |
|
| 1784 |
if (m_show_utf8) |
| 1785 |
{ |
| 1786 |
const bool show_unichars = m_theme.unicode_p (); |
| 1787 |
const int utf8_code_point_table_y = t.add_row (); |
| 1788 |
int utf8_character_table_y; |
| 1789 |
if (show_unichars) |
| 1790 |
utf8_character_table_y = t.add_row (); |
| 1791 |
|
| 1792 |
/* We don't actually want the display widths here, but |
| 1793 |
it's an easy way to decode UTF-8. */ |
| 1794 |
cpp_char_column_policy policy (8, cpp_wcwidth); |
| 1795 |
cpp_display_width_computation dw (TREE_STRING_POINTER (string_cst), |
| 1796 |
TREE_STRING_LENGTH (string_cst), |
| 1797 |
policy); |
| 1798 |
while (!dw.done ()) |
| 1799 |
{ |
| 1800 |
cpp_decoded_char decoded_char; |
| 1801 |
dw.process_next_codepoint (&decoded_char); |
| 1802 |
|
| 1803 |
if (!decoded_char.m_valid_ch) |
| 1804 |
continue; |
| 1805 |
size_t start_byte_idx |
| 1806 |
= decoded_char.m_start_byte - TREE_STRING_POINTER (string_cst); |
| 1807 |
byte_size_t size_in_bytes |
| 1808 |
= decoded_char.m_next_byte - decoded_char.m_start_byte; |
| 1809 |
byte_range cluster_bytes_for_codepoint |
| 1810 |
(start_byte_idx + bytes.get_start_byte_offset (), |
| 1811 |
size_in_bytes); |
| 1812 |
|
| 1813 |
const table::rect_t code_point_table_rect |
| 1814 |
= btm.get_table_rect (&m_string_reg, |
| 1815 |
cluster_bytes_for_codepoint, |
| 1816 |
utf8_code_point_table_y, 1); |
| 1817 |
char buf[100]; |
| 1818 |
sprintf (buf, "U+%04x", decoded_char.m_ch); |
| 1819 |
t.set_cell_span (code_point_table_rect, |
| 1820 |
styled_string (sm, buf)); |
| 1821 |
|
| 1822 |
if (show_unichars) |
| 1823 |
{ |
| 1824 |
const table::rect_t character_table_rect |
| 1825 |
= btm.get_table_rect (&m_string_reg, |
| 1826 |
cluster_bytes_for_codepoint, |
| 1827 |
utf8_character_table_y, 1); |
| 1828 |
if (cpp_is_printable_char (decoded_char.m_ch)) |
| 1829 |
t.set_cell_span (character_table_rect, |
| 1830 |
styled_string (decoded_char.m_ch)); |
| 1831 |
else if (decoded_char.m_ch == 0) |
| 1832 |
t.set_cell_span (character_table_rect, |
| 1833 |
styled_string (sm, "NUL")); |
| 1834 |
else |
| 1835 |
t.set_cell_span (character_table_rect, |
| 1836 |
styled_string (sm, "")); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
} |
| 1840 |
} |
| 1841 |
else |
| 1842 |
{ |
| 1843 |
/* Head of string. */ |
| 1844 |
for (int byte_idx = 0; byte_idx < m_ellipsis_head_len; byte_idx++) |
| 1845 |
add_column_for_byte (t, btm, sm, |
| 1846 |
byte_idx + bytes.get_start_byte_offset (), |
| 1847 |
byte_idx, |
| 1848 |
byte_idx_table_y, byte_val_table_y); |
| 1849 |
|
| 1850 |
/* Ellipsis. */ |
| 1851 |
const byte_range ellipsis_bytes |
| 1852 |
(m_ellipsis_head_len + bytes.get_start_byte_offset (), |
| 1853 |
TREE_STRING_LENGTH (string_cst) |
| 1854 |
- (m_ellipsis_head_len + m_ellipsis_tail_len)); |
| 1855 |
const table::rect_t table_rect |
| 1856 |
= ((byte_idx_table_y != -1) |
| 1857 |
? btm.get_table_rect (&m_string_reg, ellipsis_bytes, |
| 1858 |
byte_idx_table_y, 2) |
| 1859 |
: btm.get_table_rect (&m_string_reg, ellipsis_bytes, |
| 1860 |
byte_val_table_y, 1)); |
| 1861 |
t.set_cell_span(table_rect, styled_string (sm, "...")); |
| 1862 |
|
| 1863 |
/* Tail of string. */ |
| 1864 |
for (int byte_idx |
| 1865 |
= (TREE_STRING_LENGTH (string_cst) - m_ellipsis_tail_len); |
| 1866 |
byte_idx < TREE_STRING_LENGTH (string_cst); |
| 1867 |
byte_idx++) |
| 1868 |
add_column_for_byte (t, btm, sm, |
| 1869 |
byte_idx + bytes.get_start_byte_offset (), |
| 1870 |
byte_idx, |
| 1871 |
byte_idx_table_y, byte_val_table_y); |
| 1872 |
} |
| 1873 |
|
| 1874 |
if (m_kind == svalue_spatial_item::kind::WRITTEN) |
| 1875 |
{ |
| 1876 |
const int summary_table_y = t.add_row (); |
| 1877 |
t.set_cell_span (btm.get_table_rect (&m_string_reg, bytes, |
| 1878 |
summary_table_y, 1), |
| 1879 |
fmt_styled_string (sm, |
| 1880 |
_("string literal (type: %qT)"), |
| 1881 |
TREE_TYPE (string_cst))); |
| 1882 |
} |
| 1883 |
|
| 1884 |
return t; |
| 1885 |
} |
| 1886 |
|
| 1887 |
tree get_string_cst () const { return m_string_reg.get_string_cst (); } |
| 1888 |
|
| 1889 |
private: |
| 1890 |
bool calc_show_full_string () const |
| 1891 |
{ |
| 1892 |
tree string_cst = get_string_cst (); |
| 1893 |
if (TREE_STRING_LENGTH (string_cst) < m_ellipsis_threshold) |
| 1894 |
return true; |
| 1895 |
if (TREE_STRING_LENGTH (string_cst) < |
| 1896 |
(m_ellipsis_head_len + m_ellipsis_tail_len)) |
| 1897 |
return true; |
| 1898 |
return false; |
| 1899 |
} |
| 1900 |
|
| 1901 |
bool pure_ascii_p () const |
| 1902 |
{ |
| 1903 |
tree string_cst = get_string_cst (); |
| 1904 |
for (unsigned byte_idx = 0; |
| 1905 |
byte_idx < (unsigned) TREE_STRING_LENGTH (string_cst); |
| 1906 |
byte_idx++) |
| 1907 |
{ |
| 1908 |
unsigned char ch = TREE_STRING_POINTER (string_cst)[byte_idx]; |
| 1909 |
if (ch >= 0x80) |
| 1910 |
return false; |
| 1911 |
} |
| 1912 |
return true; |
| 1913 |
} |
| 1914 |
|
| 1915 |
void add_column_for_byte (table &t, const bit_to_table_map &btm, |
| 1916 |
style_manager &sm, |
| 1917 |
const byte_offset_t byte_idx_within_cluster, |
| 1918 |
const byte_offset_t byte_idx_within_string, |
| 1919 |
const int byte_idx_table_y, |
| 1920 |
const int byte_val_table_y) const |
| 1921 |
{ |
| 1922 |
tree string_cst = get_string_cst (); |
| 1923 |
gcc_assert (byte_idx_within_string >= 0); |
| 1924 |
gcc_assert (byte_idx_within_string < TREE_STRING_LENGTH (string_cst)); |
| 1925 |
|
| 1926 |
const byte_range bytes (byte_idx_within_cluster, 1); |
| 1927 |
if (byte_idx_table_y != -1) |
| 1928 |
{ |
| 1929 |
const table::rect_t idx_table_rect |
| 1930 |
= btm.get_table_rect (&m_string_reg, bytes, byte_idx_table_y, 1); |
| 1931 |
t.set_cell_span (idx_table_rect, |
| 1932 |
fmt_styled_string (sm, "[%wu]", |
| 1933 |
byte_idx_within_string.ulow ())); |
| 1934 |
} |
| 1935 |
|
| 1936 |
char byte_val |
| 1937 |
= TREE_STRING_POINTER (string_cst)[byte_idx_within_string.ulow ()]; |
| 1938 |
const table::rect_t val_table_rect |
| 1939 |
= btm.get_table_rect (&m_string_reg, bytes, byte_val_table_y, 1); |
| 1940 |
table_cell_content content (make_cell_content_for_byte (sm, byte_val)); |
| 1941 |
t.set_cell_span (val_table_rect, std::move (content)); |
| 1942 |
} |
| 1943 |
|
| 1944 |
table_cell_content make_cell_content_for_byte (style_manager &sm, |
| 1945 |
unsigned char byte_val) const |
| 1946 |
{ |
| 1947 |
if (!m_show_utf8) |
| 1948 |
{ |
| 1949 |
if (byte_val == '\0') |
| 1950 |
return styled_string (sm, "NUL"); |
| 1951 |
else if (byte_val < 0x80) |
| 1952 |
if (ISPRINT (byte_val)) |
| 1953 |
return fmt_styled_string (sm, "%qc", byte_val); |
| 1954 |
} |
| 1955 |
char buf[100]; |
| 1956 |
sprintf (buf, "0x%02x", byte_val); |
| 1957 |
return styled_string (sm, buf); |
| 1958 |
} |
| 1959 |
|
| 1960 |
const string_region &m_string_reg; |
| 1961 |
const theme &m_theme; |
| 1962 |
const int m_ellipsis_threshold; |
| 1963 |
const int m_ellipsis_head_len; |
| 1964 |
const int m_ellipsis_tail_len; |
| 1965 |
const bool m_show_full_string; |
| 1966 |
const bool m_show_utf8; |
| 1967 |
}; |
| 1968 |
|
| 1969 |
static std::unique_ptr<spatial_item> |
| 1970 |
make_written_svalue_spatial_item (const access_operation &op, |
| 1971 |
const svalue &sval, |
| 1972 |
access_range actual_bits, |
| 1973 |
const theme &theme) |
| 1974 |
{ |
| 1975 |
if (const initial_svalue *initial_sval = sval.dyn_cast_initial_svalue ()) |
| 1976 |
if (const string_region *string_reg |
| 1977 |
= initial_sval->get_region ()->dyn_cast_string_region ()) |
| 1978 |
return make_unique <string_literal_spatial_item> |
| 1979 |
(sval, actual_bits, |
| 1980 |
*string_reg, theme, |
| 1981 |
svalue_spatial_item::kind::WRITTEN); |
| 1982 |
return make_unique <written_svalue_spatial_item> (op, sval, actual_bits); |
| 1983 |
} |
| 1984 |
|
| 1985 |
static std::unique_ptr<spatial_item> |
| 1986 |
make_existing_svalue_spatial_item (const svalue *sval, |
| 1987 |
const access_range &bits, |
| 1988 |
const theme &theme) |
| 1989 |
{ |
| 1990 |
if (!sval) |
| 1991 |
return nullptr; |
| 1992 |
|
| 1993 |
switch (sval->get_kind ()) |
| 1994 |
{ |
| 1995 |
default: |
| 1996 |
return nullptr; |
| 1997 |
|
| 1998 |
case SK_INITIAL: |
| 1999 |
{ |
| 2000 |
const initial_svalue *initial_sval = (const initial_svalue *)sval; |
| 2001 |
if (const string_region *string_reg |
| 2002 |
= initial_sval->get_region ()->dyn_cast_string_region ()) |
| 2003 |
return make_unique <string_literal_spatial_item> |
| 2004 |
(*sval, bits, |
| 2005 |
*string_reg, theme, |
| 2006 |
svalue_spatial_item::kind::EXISTING); |
| 2007 |
return nullptr; |
| 2008 |
} |
| 2009 |
|
| 2010 |
case SK_COMPOUND: |
| 2011 |
return make_unique<compound_svalue_spatial_item> |
| 2012 |
(*((const compound_svalue *)sval), |
| 2013 |
bits, |
| 2014 |
svalue_spatial_item::kind::EXISTING, |
| 2015 |
theme); |
| 2016 |
} |
| 2017 |
} |
| 2018 |
|
| 2019 |
/* Widget subclass implementing access diagrams. */ |
| 2020 |
|
| 2021 |
class access_diagram_impl : public vbox_widget |
| 2022 |
{ |
| 2023 |
public: |
| 2024 |
access_diagram_impl (const access_operation &op, |
| 2025 |
diagnostic_event_id_t region_creation_event_id, |
| 2026 |
style_manager &sm, |
| 2027 |
const theme &theme, |
| 2028 |
logger *logger) |
| 2029 |
: m_op (op), |
| 2030 |
m_region_creation_event_id (region_creation_event_id), |
| 2031 |
m_sm (sm), |
| 2032 |
m_theme (theme), |
| 2033 |
m_logger (logger), |
| 2034 |
m_invalid (false), |
| 2035 |
m_valid_region_spatial_item (op, region_creation_event_id, theme), |
| 2036 |
m_accessed_region_spatial_item (op), |
| 2037 |
m_btm (), |
| 2038 |
m_calc_req_size_called (false) |
| 2039 |
{ |
| 2040 |
LOG_SCOPE (logger); |
| 2041 |
|
| 2042 |
if (logger) |
| 2043 |
{ |
| 2044 |
access_range invalid_before_bits; |
| 2045 |
if (op.maybe_get_invalid_before_bits (&invalid_before_bits)) |
| 2046 |
invalid_before_bits.log ("invalid before range", *logger); |
| 2047 |
access_range invalid_after_bits; |
| 2048 |
if (op.maybe_get_invalid_after_bits (&invalid_after_bits)) |
| 2049 |
invalid_after_bits.log ("invalid after range", *logger); |
| 2050 |
|
| 2051 |
if (op.m_sval_hint) |
| 2052 |
{ |
| 2053 |
logger->start_log_line (); |
| 2054 |
logger->log_partial ("sval_hint: "); |
| 2055 |
op.m_sval_hint->dump_to_pp (logger->get_printer (), true); |
| 2056 |
logger->end_log_line (); |
| 2057 |
} |
| 2058 |
} |
| 2059 |
|
| 2060 |
/* Register painting styles. */ |
| 2061 |
{ |
| 2062 |
style valid_style (get_style_from_color_cap_name ("valid")); |
| 2063 |
m_valid_style_id = m_sm.get_or_create_id (valid_style); |
| 2064 |
|
| 2065 |
style invalid_style (get_style_from_color_cap_name ("invalid")); |
| 2066 |
m_invalid_style_id = m_sm.get_or_create_id (invalid_style); |
| 2067 |
} |
| 2068 |
|
| 2069 |
if (op.m_sval_hint) |
| 2070 |
{ |
| 2071 |
access_range actual_bits = m_op.get_actual_bits (); |
| 2072 |
m_written_svalue_spatial_item |
| 2073 |
= make_written_svalue_spatial_item (m_op, |
| 2074 |
*op.m_sval_hint, |
| 2075 |
actual_bits, |
| 2076 |
m_theme); |
| 2077 |
} |
| 2078 |
|
| 2079 |
/* Two passes: |
| 2080 |
First, figure out all of the boundaries of interest. |
| 2081 |
Then use that to build child widgets showing the regions of interest, |
| 2082 |
with a common tabular layout. */ |
| 2083 |
|
| 2084 |
m_boundaries = find_boundaries (); |
| 2085 |
if (logger) |
| 2086 |
m_boundaries->log (*logger); |
| 2087 |
|
| 2088 |
/* Populate m_table_x_for_bit and m_bit_for_table_x. |
| 2089 |
Each table column represents the range [offset, next_offset). |
| 2090 |
We don't create a column in the table for the final offset, but we |
| 2091 |
do populate it, so that looking at the table_x of one beyond the |
| 2092 |
final table column gives us the upper bound offset. */ |
| 2093 |
m_btm.populate (*m_boundaries, *m_op.get_manager (), logger); |
| 2094 |
|
| 2095 |
/* Gracefully reject cases where the boundary sorting has gone wrong |
| 2096 |
(due to awkward combinations of symbolic values). */ |
| 2097 |
{ |
| 2098 |
table::range_t actual_bits_x_range |
| 2099 |
= m_btm.get_table_x_for_range (m_op.get_actual_bits ()); |
| 2100 |
if (actual_bits_x_range.get_size () <= 0) |
| 2101 |
{ |
| 2102 |
if (logger) |
| 2103 |
logger->log ("giving up: bad table columns for actual_bits"); |
| 2104 |
m_invalid = true; |
| 2105 |
return; |
| 2106 |
} |
| 2107 |
table::range_t valid_bits_x_range |
| 2108 |
= m_btm.get_table_x_for_range (m_op.get_valid_bits ()); |
| 2109 |
if (valid_bits_x_range.get_size () <= 0) |
| 2110 |
{ |
| 2111 |
if (logger) |
| 2112 |
logger->log ("giving up: bad table columns for valid_bits"); |
| 2113 |
m_invalid = true; |
| 2114 |
return; |
| 2115 |
} |
| 2116 |
} |
| 2117 |
|
| 2118 |
m_col_widths |
| 2119 |
= make_unique <table_dimension_sizes> (m_btm.get_num_columns ()); |
| 2120 |
|
| 2121 |
/* Now create child widgets. */ |
| 2122 |
|
| 2123 |
if (flag_analyzer_debug_text_art) |
| 2124 |
{ |
| 2125 |
table t_headings (make_headings_table ()); |
| 2126 |
add_aligned_child_table (std::move (t_headings)); |
| 2127 |
} |
| 2128 |
|
| 2129 |
if (m_written_svalue_spatial_item) |
| 2130 |
{ |
| 2131 |
table t_sval (m_written_svalue_spatial_item->make_table (m_btm, m_sm)); |
| 2132 |
add_aligned_child_table (std::move (t_sval)); |
| 2133 |
} |
| 2134 |
else |
| 2135 |
{ |
| 2136 |
table t_accessed |
| 2137 |
(m_accessed_region_spatial_item.make_table (m_btm, m_sm)); |
| 2138 |
add_aligned_child_table (std::move (t_accessed)); |
| 2139 |
} |
| 2140 |
|
| 2141 |
add_direction_widget (); |
| 2142 |
|
| 2143 |
table t_valid (m_valid_region_spatial_item.make_table (m_btm, m_sm)); |
| 2144 |
add_invalid_accesses_to_region_table (t_valid); |
| 2145 |
add_aligned_child_table (std::move (t_valid)); |
| 2146 |
|
| 2147 |
add_valid_vs_invalid_ruler (); |
| 2148 |
} |
| 2149 |
|
| 2150 |
const char *get_desc () const override |
| 2151 |
{ |
| 2152 |
return "access_diagram_impl"; |
| 2153 |
} |
| 2154 |
|
| 2155 |
canvas::size_t calc_req_size () final override |
| 2156 |
{ |
| 2157 |
if (m_invalid) |
| 2158 |
return canvas::size_t (0, 0); |
| 2159 |
|
| 2160 |
/* Now compute the size requirements for the tables. */ |
| 2161 |
for (auto iter : m_aligned_table_widgets) |
| 2162 |
iter->get_cell_sizes ().pass_1 (iter->get_table ()); |
| 2163 |
for (auto iter : m_aligned_table_widgets) |
| 2164 |
iter->get_cell_sizes ().pass_2 (iter->get_table ()); |
| 2165 |
|
| 2166 |
adjust_to_scale(); |
| 2167 |
|
| 2168 |
/* ...and relayout the tables. */ |
| 2169 |
for (auto iter : m_aligned_table_widgets) |
| 2170 |
iter->recalc_coords (); |
| 2171 |
|
| 2172 |
/* Populate the canvas_x per table_x. */ |
| 2173 |
m_col_start_x.clear (); |
| 2174 |
int iter_canvas_x = 0; |
| 2175 |
for (auto w : m_col_widths->m_requirements) |
| 2176 |
{ |
| 2177 |
m_col_start_x.push_back (iter_canvas_x); |
| 2178 |
iter_canvas_x += w + 1; |
| 2179 |
} |
| 2180 |
m_col_start_x.push_back (iter_canvas_x); |
| 2181 |
|
| 2182 |
m_calc_req_size_called = true; |
| 2183 |
|
| 2184 |
return vbox_widget::calc_req_size (); |
| 2185 |
} |
| 2186 |
|
| 2187 |
int get_canvas_x_for_table_x (int table_x) const |
| 2188 |
{ |
| 2189 |
gcc_assert (m_calc_req_size_called); |
| 2190 |
return m_col_start_x[table_x]; |
| 2191 |
} |
| 2192 |
|
| 2193 |
canvas::range_t get_canvas_x_range (const table::range_t &table_x_range) const |
| 2194 |
{ |
| 2195 |
gcc_assert (m_calc_req_size_called); |
| 2196 |
return canvas::range_t (get_canvas_x_for_table_x (table_x_range.start), |
| 2197 |
get_canvas_x_for_table_x (table_x_range.next)); |
| 2198 |
} |
| 2199 |
|
| 2200 |
const access_operation &get_op () const { return m_op; } |
| 2201 |
|
| 2202 |
style::id_t get_style_id_for_validity (bool is_valid) const |
| 2203 |
{ |
| 2204 |
return is_valid ? m_valid_style_id : m_invalid_style_id; |
| 2205 |
} |
| 2206 |
|
| 2207 |
const theme &get_theme () const { return m_theme; } |
| 2208 |
|
| 2209 |
private: |
| 2210 |
/* Figure out all of the boundaries of interest when visualizing ths op. */ |
| 2211 |
std::unique_ptr<boundaries> |
| 2212 |
find_boundaries () const |
| 2213 |
{ |
| 2214 |
std::unique_ptr<boundaries> result |
| 2215 |
= make_unique<boundaries> (*m_op.m_base_region, m_logger); |
| 2216 |
|
| 2217 |
m_valid_region_spatial_item.add_boundaries (*result, m_logger); |
| 2218 |
m_accessed_region_spatial_item.add_boundaries (*result, m_logger); |
| 2219 |
if (m_written_svalue_spatial_item) |
| 2220 |
m_written_svalue_spatial_item->add_boundaries (*result, m_logger); |
| 2221 |
|
| 2222 |
return result; |
| 2223 |
} |
| 2224 |
|
| 2225 |
void add_aligned_child_table (table t) |
| 2226 |
{ |
| 2227 |
x_aligned_table_widget *w |
| 2228 |
= new x_aligned_table_widget (std::move (t), m_theme, *m_col_widths); |
| 2229 |
m_aligned_table_widgets.push_back (w); |
| 2230 |
add_child (std::unique_ptr<widget> (w)); |
| 2231 |
} |
| 2232 |
|
| 2233 |
/* Create a table showing headings for use by -fanalyzer-debug-text-art, for |
| 2234 |
example: |
| 2235 |
+---------+-----------+-----------+---+--------------------------------+ |
| 2236 |
| tc0 | tc1 | tc2 |tc3| tc4 | |
| 2237 |
+---------+-----------+-----------+---+--------------------------------+ |
| 2238 |
|bytes 0-3|bytes 4-35 |bytes 36-39| | bytes 40-43 | |
| 2239 |
+---------+-----------+-----------+ +--------------------------------+ |
| 2240 |
which has: |
| 2241 |
- a row showing the table column numbers, labelled "tc0", "tc1", etc |
| 2242 |
- a row showing the memory range of each table column that has one. */ |
| 2243 |
|
| 2244 |
table make_headings_table () const |
| 2245 |
{ |
| 2246 |
table t (table::size_t (m_btm.get_num_columns (), 2)); |
| 2247 |
|
| 2248 |
for (int table_x = 0; table_x < t.get_size ().w; table_x++) |
| 2249 |
{ |
| 2250 |
const int table_y = 0; |
| 2251 |
t.set_cell (table::coord_t (table_x, table_y), |
| 2252 |
fmt_styled_string (m_sm, "tc%i", table_x)); |
| 2253 |
} |
| 2254 |
for (int table_x = 0; table_x < t.get_size ().w; table_x++) |
| 2255 |
{ |
| 2256 |
const int table_y = 1; |
| 2257 |
access_range range_for_column (NULL, bit_range (0, 0)); |
| 2258 |
if (m_btm.maybe_get_access_range_for_table_x (table_x, |
| 2259 |
&range_for_column)) |
| 2260 |
{ |
| 2261 |
pretty_printer pp; |
| 2262 |
pp_format_decoder (&pp) = default_tree_printer; |
| 2263 |
range_for_column.dump_to_pp (&pp, true); |
| 2264 |
t.set_cell (table::coord_t (table_x, table_y), |
| 2265 |
styled_string (m_sm, pp_formatted_text (&pp))); |
| 2266 |
} |
| 2267 |
} |
| 2268 |
|
| 2269 |
return t; |
| 2270 |
} |
| 2271 |
|
| 2272 |
void add_direction_widget () |
| 2273 |
{ |
| 2274 |
add_child (::make_unique<direction_widget> (*this, m_btm)); |
| 2275 |
} |
| 2276 |
|
| 2277 |
void add_invalid_accesses_to_region_table (table &t_region) |
| 2278 |
{ |
| 2279 |
gcc_assert (t_region.get_size ().w == (int)m_btm.get_num_columns ()); |
| 2280 |
|
| 2281 |
const int table_y = 0; |
| 2282 |
const int table_h = t_region.get_size ().h; |
| 2283 |
|
| 2284 |
access_range invalid_before_bits; |
| 2285 |
if (m_op.maybe_get_invalid_before_bits (&invalid_before_bits)) |
| 2286 |
{ |
| 2287 |
t_region.set_cell_span (m_btm.get_table_rect (invalid_before_bits, |
| 2288 |
table_y, table_h), |
| 2289 |
styled_string (m_sm, |
| 2290 |
_("before valid range"))); |
| 2291 |
} |
| 2292 |
access_range invalid_after_bits; |
| 2293 |
if (m_op.maybe_get_invalid_after_bits (&invalid_after_bits)) |
| 2294 |
{ |
| 2295 |
t_region.set_cell_span (m_btm.get_table_rect (invalid_after_bits, |
| 2296 |
table_y, table_h), |
| 2297 |
styled_string (m_sm, |
| 2298 |
_("after valid range"))); |
| 2299 |
} |
| 2300 |
} |
| 2301 |
|
| 2302 |
void maybe_add_gap (x_aligned_x_ruler_widget *w, |
| 2303 |
const access_range &lower, |
| 2304 |
const access_range &upper) const |
| 2305 |
{ |
| 2306 |
LOG_SCOPE (m_logger); |
| 2307 |
if (m_logger) |
| 2308 |
{ |
| 2309 |
lower.log ("lower", *m_logger); |
| 2310 |
upper.log ("upper", *m_logger); |
| 2311 |
} |
| 2312 |
region_model_manager *mgr = m_op.get_manager (); |
| 2313 |
const svalue &lower_next = lower.m_next.calc_symbolic_bit_offset (mgr); |
| 2314 |
const svalue &upper_start = upper.m_start.calc_symbolic_bit_offset (mgr); |
| 2315 |
const svalue *num_bits_gap |
| 2316 |
= mgr->get_or_create_binop (NULL_TREE, MINUS_EXPR, |
| 2317 |
&upper_start, &lower_next); |
| 2318 |
if (m_logger) |
| 2319 |
m_logger->log ("num_bits_gap: %qs", num_bits_gap->get_desc ().get ()); |
| 2320 |
|
| 2321 |
const svalue *zero = mgr->get_or_create_int_cst (NULL_TREE, 0); |
| 2322 |
tristate ts_gt_zero = m_op.m_model.eval_condition (num_bits_gap, |
| 2323 |
GT_EXPR, |
| 2324 |
zero); |
| 2325 |
if (ts_gt_zero.is_false ()) |
| 2326 |
{ |
| 2327 |
if (m_logger) |
| 2328 |
m_logger->log ("rejecting as not > 0"); |
| 2329 |
return; |
| 2330 |
} |
| 2331 |
|
| 2332 |
bit_size_expr num_bits (*num_bits_gap); |
| 2333 |
if (auto p = num_bits.maybe_get_formatted_str (m_sm, m_op.m_model, |
| 2334 |
_("%wi bit"), |
| 2335 |
_("%wi bits"), |
| 2336 |
_("%wi byte"), |
| 2337 |
_("%wi bytes"), |
| 2338 |
_("%qs bits"), |
| 2339 |
_("%qs bytes"))) |
| 2340 |
{ |
| 2341 |
styled_string label = std::move (*p.get ()); |
| 2342 |
w->add_range (m_btm.get_table_x_for_range |
| 2343 |
(access_range (lower.m_next, |
| 2344 |
upper.m_start, |
| 2345 |
*mgr)), |
| 2346 |
std::move (label), |
| 2347 |
style::id_plain); |
| 2348 |
} |
| 2349 |
} |
| 2350 |
|
| 2351 |
styled_string |
| 2352 |
make_warning_string (styled_string &&text) |
| 2353 |
{ |
| 2354 |
styled_string result; |
| 2355 |
if (!m_theme.emojis_p ()) |
| 2356 |
return std::move (text); |
| 2357 |
|
| 2358 |
result.append (styled_string (0x26A0, /* U+26A0 WARNING SIGN. */ |
| 2359 |
true)); |
| 2360 |
/* U+26A0 WARNING SIGN has East_Asian_Width == Neutral, but in its |
| 2361 |
emoji variant is printed (by vte at least) with a 2nd half |
| 2362 |
overlapping the next char. Hence we add two spaces here: a space |
| 2363 |
to be covered by this overlap, plus another space of padding. */ |
| 2364 |
result.append (styled_string (m_sm, " ")); |
| 2365 |
result.append (std::move (text)); |
| 2366 |
return result; |
| 2367 |
} |
| 2368 |
|
| 2369 |
/* Add a ruler child widet showing valid, invalid, and gaps. */ |
| 2370 |
void add_valid_vs_invalid_ruler () |
| 2371 |
{ |
| 2372 |
LOG_SCOPE (m_logger); |
| 2373 |
|
| 2374 |
x_aligned_x_ruler_widget *w |
| 2375 |
= new x_aligned_x_ruler_widget (*this, m_theme); |
| 2376 |
|
| 2377 |
access_range invalid_before_bits; |
| 2378 |
if (m_op.maybe_get_invalid_before_bits (&invalid_before_bits)) |
| 2379 |
{ |
| 2380 |
if (m_logger) |
| 2381 |
invalid_before_bits.log ("invalid_before_bits", *m_logger); |
| 2382 |
bit_size_expr num_before_bits |
| 2383 |
(invalid_before_bits.get_size (m_op.get_manager ())); |
| 2384 |
std::unique_ptr<styled_string> label; |
| 2385 |
if (m_op.m_dir == DIR_READ) |
| 2386 |
label = num_before_bits.maybe_get_formatted_str |
| 2387 |
(m_sm, m_op.m_model, |
| 2388 |
_("under-read of %wi bit"), |
| 2389 |
_("under-read of %wi bits"), |
| 2390 |
_("under-read of %wi byte"), |
| 2391 |
_("under-read of %wi bytes"), |
| 2392 |
_("under-read of %qs bits"), |
| 2393 |
_("under-read of %qs bytes")); |
| 2394 |
else |
| 2395 |
label = num_before_bits.maybe_get_formatted_str |
| 2396 |
(m_sm, m_op.m_model, |
| 2397 |
_("underwrite of %wi bit"), |
| 2398 |
_("underwrite of %wi bits"), |
| 2399 |
_("underwrite of %wi byte"), |
| 2400 |
_("underwrite of %wi bytes"), |
| 2401 |
_("underwrite of %qs bits"), |
| 2402 |
_("underwrite of %qs bytes")); |
| 2403 |
if (label) |
| 2404 |
w->add_range (m_btm.get_table_x_for_range (invalid_before_bits), |
| 2405 |
make_warning_string (std::move (*label)), |
| 2406 |
m_invalid_style_id); |
| 2407 |
} |
| 2408 |
else |
| 2409 |
{ |
| 2410 |
if (m_logger) |
| 2411 |
m_logger->log ("no invalid_before_bits"); |
| 2412 |
} |
| 2413 |
|
| 2414 |
/* It would be nice to be able to use std::optional<access_range> here, |
| 2415 |
but std::optional is C++17. */ |
| 2416 |
bool got_valid_bits = false; |
| 2417 |
access_range valid_bits (m_op.get_valid_bits ()); |
| 2418 |
bit_size_expr num_valid_bits (valid_bits.get_size (m_op.get_manager ())); |
| 2419 |
if (m_logger) |
| 2420 |
valid_bits.log ("valid_bits", *m_logger); |
| 2421 |
|
| 2422 |
got_valid_bits = true; |
| 2423 |
maybe_add_gap (w, invalid_before_bits, valid_bits); |
| 2424 |
|
| 2425 |
std::unique_ptr<styled_string> label; |
| 2426 |
if (m_op.m_dir == DIR_READ) |
| 2427 |
label = num_valid_bits.maybe_get_formatted_str (m_sm, |
| 2428 |
m_op.m_model, |
| 2429 |
_("size: %wi bit"), |
| 2430 |
_("size: %wi bits"), |
| 2431 |
_("size: %wi byte"), |
| 2432 |
_("size: %wi bytes"), |
| 2433 |
_("size: %qs bits"), |
| 2434 |
_("size: %qs bytes")); |
| 2435 |
else |
| 2436 |
label |
| 2437 |
= num_valid_bits.maybe_get_formatted_str (m_sm, |
| 2438 |
m_op.m_model, |
| 2439 |
_("capacity: %wi bit"), |
| 2440 |
_("capacity: %wi bits"), |
| 2441 |
_("capacity: %wi byte"), |
| 2442 |
_("capacity: %wi bytes"), |
| 2443 |
_("capacity: %qs bits"), |
| 2444 |
_("capacity: %qs bytes")); |
| 2445 |
if (label) |
| 2446 |
w->add_range (m_btm.get_table_x_for_range (m_op.get_valid_bits ()), |
| 2447 |
std::move (*label), |
| 2448 |
m_valid_style_id); |
| 2449 |
|
| 2450 |
access_range invalid_after_bits; |
| 2451 |
if (m_op.maybe_get_invalid_after_bits (&invalid_after_bits)) |
| 2452 |
{ |
| 2453 |
if (got_valid_bits) |
| 2454 |
maybe_add_gap (w, valid_bits, invalid_after_bits); |
| 2455 |
|
| 2456 |
if (m_logger) |
| 2457 |
invalid_before_bits.log ("invalid_after_bits", *m_logger); |
| 2458 |
|
| 2459 |
bit_size_expr num_after_bits |
| 2460 |
(invalid_after_bits.get_size (m_op.get_manager ())); |
| 2461 |
std::unique_ptr<styled_string> label; |
| 2462 |
if (m_op.m_dir == DIR_READ) |
| 2463 |
label = num_after_bits.maybe_get_formatted_str |
| 2464 |
(m_sm, m_op.m_model, |
| 2465 |
_("over-read of %wi bit"), |
| 2466 |
_("over-read of %wi bits"), |
| 2467 |
_("over-read of %wi byte"), |
| 2468 |
_("over-read of %wi bytes"), |
| 2469 |
_("over-read of %qs bits"), |
| 2470 |
_("over-read of %qs bytes")); |
| 2471 |
else |
| 2472 |
label = num_after_bits.maybe_get_formatted_str |
| 2473 |
(m_sm, m_op.m_model, |
| 2474 |
_("overflow of %wi bit"), |
| 2475 |
_("overflow of %wi bits"), |
| 2476 |
_("overflow of %wi byte"), |
| 2477 |
_("overflow of %wi bytes"), |
| 2478 |
_("overflow of %qs bits"), |
| 2479 |
_("overflow of %qs bytes")); |
| 2480 |
if (label) |
| 2481 |
w->add_range (m_btm.get_table_x_for_range (invalid_after_bits), |
| 2482 |
make_warning_string (std::move (*label)), |
| 2483 |
m_invalid_style_id); |
| 2484 |
} |
| 2485 |
else |
| 2486 |
{ |
| 2487 |
if (m_logger) |
| 2488 |
m_logger->log ("no invalid_after_bits"); |
| 2489 |
} |
| 2490 |
|
| 2491 |
add_child (std::unique_ptr<widget> (w)); |
| 2492 |
} |
| 2493 |
|
| 2494 |
/* Subroutine of calc_req_size. |
| 2495 |
Try to allocate surplus canvas width to table columns to make the |
| 2496 |
per table-column canvas widths closer to being to scale. |
| 2497 |
See e.g.: |
| 2498 |
https://en.wikipedia.org/wiki/Fair_item_allocation |
| 2499 |
https://en.wikipedia.org/wiki/Mathematics_of_apportionment |
| 2500 |
*/ |
| 2501 |
void adjust_to_scale () |
| 2502 |
{ |
| 2503 |
LOG_SCOPE (m_logger); |
| 2504 |
const unsigned num_columns = m_btm.get_num_columns (); |
| 2505 |
std::vector<bit_offset_t> bit_sizes (num_columns); |
| 2506 |
for (unsigned table_x = 0; table_x < num_columns; table_x++) |
| 2507 |
{ |
| 2508 |
access_range range_for_column (NULL, bit_range (0, 0)); |
| 2509 |
if (m_btm.maybe_get_access_range_for_table_x (table_x, |
| 2510 |
&range_for_column)) |
| 2511 |
{ |
| 2512 |
bit_size_t size_in_bits; |
| 2513 |
if (!range_for_column.get_size_in_bits (&size_in_bits)) |
| 2514 |
size_in_bits = BITS_PER_UNIT; // arbitrary non-zero value |
| 2515 |
gcc_assert (size_in_bits > 0); |
| 2516 |
bit_sizes[table_x] = size_in_bits; |
| 2517 |
} |
| 2518 |
else |
| 2519 |
bit_sizes[table_x] = 0; |
| 2520 |
} |
| 2521 |
|
| 2522 |
while (adjust_to_scale_once (bit_sizes)) |
| 2523 |
{ |
| 2524 |
} |
| 2525 |
} |
| 2526 |
bool adjust_to_scale_once (const std::vector<bit_offset_t> &bit_sizes) |
| 2527 |
{ |
| 2528 |
LOG_SCOPE (m_logger); |
| 2529 |
|
| 2530 |
const unsigned num_columns = m_btm.get_num_columns (); |
| 2531 |
|
| 2532 |
/* Find the total canvas width currently required. |
| 2533 |
Require one extra canvas column for the right-hand border |
| 2534 |
of the table. */ |
| 2535 |
int total_width = 1; |
| 2536 |
for (unsigned table_x = 0; table_x < num_columns; table_x++) |
| 2537 |
{ |
| 2538 |
int canvas_w = m_col_widths->m_requirements[table_x]; |
| 2539 |
gcc_assert (canvas_w >= 0); |
| 2540 |
total_width += canvas_w + 1; |
| 2541 |
} |
| 2542 |
|
| 2543 |
const int max_width = param_analyzer_text_art_ideal_canvas_width; |
| 2544 |
if (total_width >= max_width) |
| 2545 |
{ |
| 2546 |
if (m_logger) |
| 2547 |
m_logger->log ("bailing out: total_width=%i ,>= max_width (%i)\n", |
| 2548 |
total_width, max_width); |
| 2549 |
return false; |
| 2550 |
} |
| 2551 |
|
| 2552 |
const int fixed_point = 1024; |
| 2553 |
std::vector<bit_offset_t> canvas_w_per_bit (num_columns); |
| 2554 |
for (unsigned table_x = 0; table_x < num_columns; table_x++) |
| 2555 |
{ |
| 2556 |
bit_offset_t bit_size = bit_sizes[table_x]; |
| 2557 |
if (bit_size > 0) |
| 2558 |
canvas_w_per_bit[table_x] |
| 2559 |
= (m_col_widths->m_requirements[table_x] * fixed_point) / bit_size; |
| 2560 |
else |
| 2561 |
canvas_w_per_bit[table_x] = INT_MAX; |
| 2562 |
} |
| 2563 |
|
| 2564 |
/* Find the min canvas per bit, and give an extra canvas column to |
| 2565 |
the table column that has least. */ |
| 2566 |
size_t min_idx = std::distance (canvas_w_per_bit.begin (), |
| 2567 |
std::min_element (canvas_w_per_bit.begin (), |
| 2568 |
canvas_w_per_bit.end ())); |
| 2569 |
m_col_widths->m_requirements[min_idx] += 1; |
| 2570 |
if (m_logger) |
| 2571 |
m_logger->log ("adding 1 canvas_w to column %i\n", (int)min_idx); |
| 2572 |
|
| 2573 |
return true; // keep going |
| 2574 |
} |
| 2575 |
|
| 2576 |
const access_operation &m_op; |
| 2577 |
diagnostic_event_id_t m_region_creation_event_id; |
| 2578 |
style_manager &m_sm; |
| 2579 |
const theme &m_theme; |
| 2580 |
logger *m_logger; |
| 2581 |
/* In lieu of being able to throw exceptions, a flag to mark this object |
| 2582 |
as "invalid". */ |
| 2583 |
bool m_invalid; |
| 2584 |
|
| 2585 |
style::id_t m_valid_style_id; |
| 2586 |
style::id_t m_invalid_style_id; |
| 2587 |
|
| 2588 |
valid_region_spatial_item m_valid_region_spatial_item; |
| 2589 |
accessed_region_spatial_item m_accessed_region_spatial_item; |
| 2590 |
std::unique_ptr<spatial_item> m_written_svalue_spatial_item; |
| 2591 |
|
| 2592 |
std::unique_ptr<boundaries> m_boundaries; |
| 2593 |
|
| 2594 |
bit_to_table_map m_btm; |
| 2595 |
|
| 2596 |
bool m_calc_req_size_called; |
| 2597 |
|
| 2598 |
/* Column widths shared by all x_aligned_table_widget, |
| 2599 |
created once we know how many columns we need. */ |
| 2600 |
std::unique_ptr<table_dimension_sizes> m_col_widths; |
| 2601 |
|
| 2602 |
/* All of the child x_aligned_table_widget that share |
| 2603 |
column widths. */ |
| 2604 |
std::vector<x_aligned_table_widget *> m_aligned_table_widgets; |
| 2605 |
|
| 2606 |
/* Mapping from table_x to canvas_x. */ |
| 2607 |
std::vector<int> m_col_start_x; |
| 2608 |
}; |
| 2609 |
|
| 2610 |
x_ruler |
| 2611 |
x_aligned_x_ruler_widget::make_x_ruler () const |
| 2612 |
{ |
| 2613 |
x_ruler r (x_ruler::label_dir::BELOW); |
| 2614 |
for (auto& iter : m_labels) |
| 2615 |
{ |
| 2616 |
canvas::range_t canvas_x_range |
| 2617 |
= m_dia_impl.get_canvas_x_range (iter.m_table_x_range); |
| 2618 |
/* Include the end-point. */ |
| 2619 |
canvas_x_range.next++; |
| 2620 |
r.add_label (canvas_x_range, iter.m_text.copy (), iter.m_style_id, |
| 2621 |
x_ruler::label_kind::TEXT_WITH_BORDER); |
| 2622 |
} |
| 2623 |
return r; |
| 2624 |
} |
| 2625 |
|
| 2626 |
/* class direction_widget : public leaf_widget. */ |
| 2627 |
|
| 2628 |
/* Paint arrows indicating the direction of the access (read vs write), |
| 2629 |
but only in the X-extent corresponding to the region that's actually |
| 2630 |
accessed. */ |
| 2631 |
|
| 2632 |
void |
| 2633 |
direction_widget::paint_to_canvas (canvas &canvas) |
| 2634 |
{ |
| 2635 |
const access_range accessed_bits (m_dia_impl.get_op ().get_actual_bits ()); |
| 2636 |
|
| 2637 |
const access_range valid_bits (m_dia_impl.get_op ().get_valid_bits ()); |
| 2638 |
|
| 2639 |
for (unsigned table_x = 0; table_x < m_btm.get_num_columns (); table_x++) |
| 2640 |
{ |
| 2641 |
access_range column_access_range; |
| 2642 |
if (m_btm.maybe_get_access_range_for_table_x (table_x, |
| 2643 |
&column_access_range)) |
| 2644 |
{ |
| 2645 |
/* Only paint arrows in the accessed region. */ |
| 2646 |
if (!accessed_bits.contains_p (column_access_range)) |
| 2647 |
continue; |
| 2648 |
|
| 2649 |
/* Are we within the valid region? */ |
| 2650 |
const bool is_valid (valid_bits.contains_p (column_access_range)); |
| 2651 |
const style::id_t style_id |
| 2652 |
= m_dia_impl.get_style_id_for_validity (is_valid); |
| 2653 |
const canvas::range_t x_canvas_range |
| 2654 |
= m_dia_impl.get_canvas_x_range (table::range_t (table_x, |
| 2655 |
table_x + 1)); |
| 2656 |
const int canvas_x = x_canvas_range.get_midpoint (); |
| 2657 |
m_dia_impl.get_theme ().paint_y_arrow |
| 2658 |
(canvas, |
| 2659 |
canvas_x, |
| 2660 |
canvas::range_t (get_y_range ()), |
| 2661 |
(m_dia_impl.get_op ().m_dir == DIR_READ |
| 2662 |
? theme::y_arrow_dir::UP |
| 2663 |
: theme::y_arrow_dir::DOWN), |
| 2664 |
style_id); |
| 2665 |
} |
| 2666 |
} |
| 2667 |
} |
| 2668 |
|
| 2669 |
/* class access_diagram : public text_art::wrapper_widget. */ |
| 2670 |
|
| 2671 |
/* To hide the implementation details, this is merely a wrapper around |
| 2672 |
an access_diagram_impl. */ |
| 2673 |
|
| 2674 |
access_diagram::access_diagram (const access_operation &op, |
| 2675 |
diagnostic_event_id_t region_creation_event_id, |
| 2676 |
style_manager &sm, |
| 2677 |
const theme &theme, |
| 2678 |
logger *logger) |
| 2679 |
: wrapper_widget (make_unique <access_diagram_impl> (op, |
| 2680 |
region_creation_event_id, |
| 2681 |
sm, |
| 2682 |
theme, |
| 2683 |
logger)) |
| 2684 |
{ |
| 2685 |
} |
| 2686 |
|
| 2687 |
#if CHECKING_P |
| 2688 |
|
| 2689 |
namespace selftest { |
| 2690 |
|
| 2691 |
/* Implementation detail of ASSERT_EQ_TYPELESS_INTEGER. */ |
| 2692 |
|
| 2693 |
static void |
| 2694 |
assert_eq_typeless_integer (const location &loc, |
| 2695 |
const svalue *sval, |
| 2696 |
int expected_int_val) |
| 2697 |
{ |
| 2698 |
ASSERT_NE_AT (loc, sval, nullptr); |
| 2699 |
ASSERT_EQ_AT (loc, sval->get_kind (), SK_CONSTANT); |
| 2700 |
ASSERT_EQ_AT (loc, |
| 2701 |
wi::to_offset (sval->maybe_get_constant ()), |
| 2702 |
expected_int_val); |
| 2703 |
ASSERT_EQ_AT (loc, sval->get_type (), NULL_TREE); |
| 2704 |
} |
| 2705 |
|
| 2706 |
/* Assert that SVAL is a constant_svalue equal to EXPECTED_INT_VAL, |
| 2707 |
with NULL_TREE as its type. */ |
| 2708 |
|
| 2709 |
#define ASSERT_EQ_TYPELESS_INTEGER(SVAL, EXPECTED_INT_VAL) \ |
| 2710 |
SELFTEST_BEGIN_STMT \ |
| 2711 |
assert_eq_typeless_integer ((SELFTEST_LOCATION), \ |
| 2712 |
(SVAL), \ |
| 2713 |
(EXPECTED_INT_VAL)); \ |
| 2714 |
SELFTEST_END_STMT |
| 2715 |
|
| 2716 |
|
| 2717 |
/* Various tests of bit_size_expr::maybe_get_as_bytes. */ |
| 2718 |
|
| 2719 |
static void |
| 2720 |
test_bit_size_expr_to_bytes () |
| 2721 |
{ |
| 2722 |
region_model_manager mgr; |
| 2723 |
|
| 2724 |
/* 40 bits: should be 5 bytes. */ |
| 2725 |
{ |
| 2726 |
bit_size_expr num_bits (*mgr.get_or_create_int_cst (NULL_TREE, 40)); |
| 2727 |
const svalue *as_bytes = num_bits.maybe_get_as_bytes (mgr); |
| 2728 |
ASSERT_EQ_TYPELESS_INTEGER (as_bytes, 5); |
| 2729 |
} |
| 2730 |
|
| 2731 |
/* 41 bits: should not convert to bytes. */ |
| 2732 |
{ |
| 2733 |
bit_size_expr num_bits (*mgr.get_or_create_int_cst (NULL_TREE, 41)); |
| 2734 |
const svalue *as_bytes = num_bits.maybe_get_as_bytes (mgr); |
| 2735 |
ASSERT_EQ (as_bytes, nullptr); |
| 2736 |
} |
| 2737 |
|
| 2738 |
tree n = build_global_decl ("n", size_type_node); |
| 2739 |
|
| 2740 |
const svalue *init_n |
| 2741 |
= mgr.get_or_create_initial_value (mgr.get_region_for_global (n)); |
| 2742 |
|
| 2743 |
const svalue *n_times_8 |
| 2744 |
= mgr.get_or_create_binop (NULL_TREE, MULT_EXPR, |
| 2745 |
init_n, |
| 2746 |
mgr.get_or_create_int_cst (NULL_TREE, 8)); |
| 2747 |
|
| 2748 |
/* (n * 8) bits should be n bytes */ |
| 2749 |
{ |
| 2750 |
bit_size_expr num_bits (*n_times_8); |
| 2751 |
const svalue *as_bytes = num_bits.maybe_get_as_bytes (mgr); |
| 2752 |
ASSERT_EQ (as_bytes, mgr.get_or_create_cast (NULL_TREE, init_n)); |
| 2753 |
} |
| 2754 |
|
| 2755 |
/* (n * 8) + 16 bits should be n + 2 bytes */ |
| 2756 |
{ |
| 2757 |
bit_size_expr num_bits |
| 2758 |
(*mgr.get_or_create_binop (NULL_TREE, PLUS_EXPR, |
| 2759 |
n_times_8, |
| 2760 |
mgr.get_or_create_int_cst (NULL_TREE, 16))); |
| 2761 |
const svalue *as_bytes = num_bits.maybe_get_as_bytes (mgr); |
| 2762 |
ASSERT_EQ (as_bytes->get_kind (), SK_BINOP); |
| 2763 |
const binop_svalue *binop = as_bytes->dyn_cast_binop_svalue (); |
| 2764 |
ASSERT_EQ (binop->get_op (), PLUS_EXPR); |
| 2765 |
ASSERT_EQ (binop->get_arg0 (), mgr.get_or_create_cast (NULL_TREE, init_n)); |
| 2766 |
ASSERT_EQ_TYPELESS_INTEGER (binop->get_arg1 (), 2); |
| 2767 |
} |
| 2768 |
} |
| 2769 |
|
| 2770 |
/* Run all of the selftests within this file. */ |
| 2771 |
|
| 2772 |
void |
| 2773 |
analyzer_access_diagram_cc_tests () |
| 2774 |
{ |
| 2775 |
test_bit_size_expr_to_bytes (); |
| 2776 |
} |
| 2777 |
|
| 2778 |
} // namespace selftest |
| 2779 |
|
| 2780 |
#endif /* CHECKING_P */ |
| 2781 |
|
| 2782 |
} // namespace ana |
| 2783 |
|
| 2784 |
#endif /* #if ENABLE_ANALYZER */ |