analyzer: move class record_layout to its own .h/.cc
No functional change intended. gcc/ChangeLog: * Makefile.in (ANALYZER_OBJS): Add analyzer/record-layout.o. gcc/analyzer/ChangeLog: * record-layout.cc: New file, based on material in region-model.cc. * record-layout.h: Likewise. * region-model.cc: Include "analyzer/record-layout.h". (class record_layout): Move to record-layout.cc and .h Signed-off-by: David Malcolm <dmalcolm@redhat.com>
This commit is contained in:
parent
b0f19336f2
commit
37e1634ef1
4 changed files with 218 additions and 131 deletions
|
@ -1331,6 +1331,7 @@ ANALYZER_OBJS = \
|
|||
analyzer/program-point.o \
|
||||
analyzer/program-state.o \
|
||||
analyzer/ranges.o \
|
||||
analyzer/record-layout.o \
|
||||
analyzer/region.o \
|
||||
analyzer/region-model.o \
|
||||
analyzer/region-model-asm.o \
|
||||
|
|
125
gcc/analyzer/record-layout.cc
Normal file
125
gcc/analyzer/record-layout.cc
Normal file
|
@ -0,0 +1,125 @@
|
|||
/* Implementation of class record_layout.
|
||||
Copyright (C) 2022-2023 Free Software Foundation, Inc.
|
||||
Contributed by David Malcolm <dmalcolm@redhat.com>.
|
||||
|
||||
This file is part of GCC.
|
||||
|
||||
GCC is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3, or (at your option)
|
||||
any later version.
|
||||
|
||||
GCC is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GCC; see the file COPYING3. If not see
|
||||
<http://www.gnu.org/licenses/>. */
|
||||
|
||||
#include "config.h"
|
||||
#define INCLUDE_MEMORY
|
||||
#include "system.h"
|
||||
#include "coretypes.h"
|
||||
#include "tree.h"
|
||||
#include "function.h"
|
||||
#include "basic-block.h"
|
||||
#include "gimple.h"
|
||||
#include "diagnostic.h"
|
||||
#include "tree-diagnostic.h"
|
||||
#include "analyzer/analyzer.h"
|
||||
#include "analyzer/record-layout.h"
|
||||
|
||||
#if ENABLE_ANALYZER
|
||||
|
||||
namespace ana {
|
||||
|
||||
/* class record_layout. */
|
||||
|
||||
record_layout::record_layout (tree record_type)
|
||||
{
|
||||
gcc_assert (TREE_CODE (record_type) == RECORD_TYPE);
|
||||
|
||||
for (tree iter = TYPE_FIELDS (record_type); iter != NULL_TREE;
|
||||
iter = DECL_CHAIN (iter))
|
||||
{
|
||||
if (TREE_CODE (iter) == FIELD_DECL)
|
||||
{
|
||||
int iter_field_offset = int_bit_position (iter);
|
||||
bit_size_t size_in_bits;
|
||||
if (!int_size_in_bits (TREE_TYPE (iter), &size_in_bits))
|
||||
size_in_bits = 0;
|
||||
|
||||
maybe_pad_to (iter_field_offset);
|
||||
|
||||
/* Add field. */
|
||||
m_items.safe_push (item (bit_range (iter_field_offset,
|
||||
size_in_bits),
|
||||
iter, false));
|
||||
}
|
||||
}
|
||||
|
||||
/* Add any trailing padding. */
|
||||
bit_size_t size_in_bits;
|
||||
if (int_size_in_bits (record_type, &size_in_bits))
|
||||
maybe_pad_to (size_in_bits);
|
||||
}
|
||||
|
||||
void
|
||||
record_layout::dump_to_pp (pretty_printer *pp) const
|
||||
{
|
||||
unsigned i;
|
||||
item *it;
|
||||
FOR_EACH_VEC_ELT (m_items, i, it)
|
||||
{
|
||||
it->dump_to_pp (pp);
|
||||
pp_newline (pp);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
record_layout::dump () const
|
||||
{
|
||||
pretty_printer pp;
|
||||
pp_format_decoder (&pp) = default_tree_printer;
|
||||
pp.buffer->stream = stderr;
|
||||
dump_to_pp (&pp);
|
||||
pp_flush (&pp);
|
||||
}
|
||||
|
||||
const record_layout::item *
|
||||
record_layout::get_item_at (bit_offset_t offset) const
|
||||
{
|
||||
unsigned i;
|
||||
item *it;
|
||||
FOR_EACH_VEC_ELT (m_items, i, it)
|
||||
if (it->contains_p (offset))
|
||||
return it;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Subroutine of ctor. Add padding item to NEXT_OFFSET if necessary. */
|
||||
|
||||
void
|
||||
record_layout::maybe_pad_to (bit_offset_t next_offset)
|
||||
{
|
||||
if (m_items.length () > 0)
|
||||
{
|
||||
const item &last_item = m_items[m_items.length () - 1];
|
||||
bit_offset_t offset_after_last_item
|
||||
= last_item.get_next_bit_offset ();
|
||||
if (next_offset > offset_after_last_item)
|
||||
{
|
||||
bit_size_t padding_size
|
||||
= next_offset - offset_after_last_item;
|
||||
m_items.safe_push (item (bit_range (offset_after_last_item,
|
||||
padding_size),
|
||||
last_item.m_field, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ana
|
||||
|
||||
#endif /* #if ENABLE_ANALYZER */
|
91
gcc/analyzer/record-layout.h
Normal file
91
gcc/analyzer/record-layout.h
Normal file
|
@ -0,0 +1,91 @@
|
|||
/* Declaration of class record_layout.
|
||||
Copyright (C) 2022-2023 Free Software Foundation, Inc.
|
||||
Contributed by David Malcolm <dmalcolm@redhat.com>.
|
||||
|
||||
This file is part of GCC.
|
||||
|
||||
GCC is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3, or (at your option)
|
||||
any later version.
|
||||
|
||||
GCC is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GCC; see the file COPYING3. If not see
|
||||
<http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef GCC_ANALYZER_RECORD_LAYOUT_H
|
||||
#define GCC_ANALYZER_RECORD_LAYOUT_H
|
||||
|
||||
#include "analyzer/store.h"
|
||||
|
||||
namespace ana {
|
||||
|
||||
/* Information of the layout of a RECORD_TYPE, capturing it as a vector
|
||||
of items, where each item is either a field or padding. */
|
||||
|
||||
class record_layout
|
||||
{
|
||||
public:
|
||||
/* An item within a record; either a field, or padding after a field. */
|
||||
struct item
|
||||
{
|
||||
public:
|
||||
item (const bit_range &br,
|
||||
tree field,
|
||||
bool is_padding)
|
||||
: m_bit_range (br),
|
||||
m_field (field),
|
||||
m_is_padding (is_padding)
|
||||
{
|
||||
}
|
||||
|
||||
bit_offset_t get_start_bit_offset () const
|
||||
{
|
||||
return m_bit_range.get_start_bit_offset ();
|
||||
}
|
||||
bit_offset_t get_next_bit_offset () const
|
||||
{
|
||||
return m_bit_range.get_next_bit_offset ();
|
||||
}
|
||||
|
||||
bool contains_p (bit_offset_t offset) const
|
||||
{
|
||||
return m_bit_range.contains_p (offset);
|
||||
}
|
||||
|
||||
void dump_to_pp (pretty_printer *pp) const
|
||||
{
|
||||
if (m_is_padding)
|
||||
pp_printf (pp, "padding after %qD", m_field);
|
||||
else
|
||||
pp_printf (pp, "%qD", m_field);
|
||||
pp_string (pp, ", ");
|
||||
m_bit_range.dump_to_pp (pp);
|
||||
}
|
||||
|
||||
bit_range m_bit_range;
|
||||
tree m_field;
|
||||
bool m_is_padding;
|
||||
};
|
||||
|
||||
record_layout (tree record_type);
|
||||
|
||||
void dump_to_pp (pretty_printer *pp) const;
|
||||
DEBUG_FUNCTION void dump () const;
|
||||
|
||||
const record_layout::item *get_item_at (bit_offset_t offset) const;
|
||||
|
||||
private:
|
||||
void maybe_pad_to (bit_offset_t next_offset);
|
||||
|
||||
auto_vec<item> m_items;
|
||||
};
|
||||
|
||||
} // namespace ana
|
||||
|
||||
#endif /* GCC_ANALYZER_RECORD_LAYOUT_H */
|
|
@ -78,6 +78,7 @@ along with GCC; see the file COPYING3. If not see
|
|||
#include "analyzer/checker-event.h"
|
||||
#include "analyzer/checker-path.h"
|
||||
#include "analyzer/feasible-graph.h"
|
||||
#include "analyzer/record-layout.h"
|
||||
|
||||
#if ENABLE_ANALYZER
|
||||
|
||||
|
@ -6123,137 +6124,6 @@ region_model::unset_dynamic_extents (const region *reg)
|
|||
m_dynamic_extents.remove (reg);
|
||||
}
|
||||
|
||||
/* Information of the layout of a RECORD_TYPE, capturing it as a vector
|
||||
of items, where each item is either a field or padding. */
|
||||
|
||||
class record_layout
|
||||
{
|
||||
public:
|
||||
/* An item within a record; either a field, or padding after a field. */
|
||||
struct item
|
||||
{
|
||||
public:
|
||||
item (const bit_range &br,
|
||||
tree field,
|
||||
bool is_padding)
|
||||
: m_bit_range (br),
|
||||
m_field (field),
|
||||
m_is_padding (is_padding)
|
||||
{
|
||||
}
|
||||
|
||||
bit_offset_t get_start_bit_offset () const
|
||||
{
|
||||
return m_bit_range.get_start_bit_offset ();
|
||||
}
|
||||
bit_offset_t get_next_bit_offset () const
|
||||
{
|
||||
return m_bit_range.get_next_bit_offset ();
|
||||
}
|
||||
|
||||
bool contains_p (bit_offset_t offset) const
|
||||
{
|
||||
return m_bit_range.contains_p (offset);
|
||||
}
|
||||
|
||||
void dump_to_pp (pretty_printer *pp) const
|
||||
{
|
||||
if (m_is_padding)
|
||||
pp_printf (pp, "padding after %qD", m_field);
|
||||
else
|
||||
pp_printf (pp, "%qD", m_field);
|
||||
pp_string (pp, ", ");
|
||||
m_bit_range.dump_to_pp (pp);
|
||||
}
|
||||
|
||||
bit_range m_bit_range;
|
||||
tree m_field;
|
||||
bool m_is_padding;
|
||||
};
|
||||
|
||||
record_layout (tree record_type)
|
||||
{
|
||||
gcc_assert (TREE_CODE (record_type) == RECORD_TYPE);
|
||||
|
||||
for (tree iter = TYPE_FIELDS (record_type); iter != NULL_TREE;
|
||||
iter = DECL_CHAIN (iter))
|
||||
{
|
||||
if (TREE_CODE (iter) == FIELD_DECL)
|
||||
{
|
||||
int iter_field_offset = int_bit_position (iter);
|
||||
bit_size_t size_in_bits;
|
||||
if (!int_size_in_bits (TREE_TYPE (iter), &size_in_bits))
|
||||
size_in_bits = 0;
|
||||
|
||||
maybe_pad_to (iter_field_offset);
|
||||
|
||||
/* Add field. */
|
||||
m_items.safe_push (item (bit_range (iter_field_offset,
|
||||
size_in_bits),
|
||||
iter, false));
|
||||
}
|
||||
}
|
||||
|
||||
/* Add any trailing padding. */
|
||||
bit_size_t size_in_bits;
|
||||
if (int_size_in_bits (record_type, &size_in_bits))
|
||||
maybe_pad_to (size_in_bits);
|
||||
}
|
||||
|
||||
void dump_to_pp (pretty_printer *pp) const
|
||||
{
|
||||
unsigned i;
|
||||
item *it;
|
||||
FOR_EACH_VEC_ELT (m_items, i, it)
|
||||
{
|
||||
it->dump_to_pp (pp);
|
||||
pp_newline (pp);
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_FUNCTION void dump () const
|
||||
{
|
||||
pretty_printer pp;
|
||||
pp_format_decoder (&pp) = default_tree_printer;
|
||||
pp.buffer->stream = stderr;
|
||||
dump_to_pp (&pp);
|
||||
pp_flush (&pp);
|
||||
}
|
||||
|
||||
const record_layout::item *get_item_at (bit_offset_t offset) const
|
||||
{
|
||||
unsigned i;
|
||||
item *it;
|
||||
FOR_EACH_VEC_ELT (m_items, i, it)
|
||||
if (it->contains_p (offset))
|
||||
return it;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
private:
|
||||
/* Subroutine of ctor. Add padding item to NEXT_OFFSET if necessary. */
|
||||
|
||||
void maybe_pad_to (bit_offset_t next_offset)
|
||||
{
|
||||
if (m_items.length () > 0)
|
||||
{
|
||||
const item &last_item = m_items[m_items.length () - 1];
|
||||
bit_offset_t offset_after_last_item
|
||||
= last_item.get_next_bit_offset ();
|
||||
if (next_offset > offset_after_last_item)
|
||||
{
|
||||
bit_size_t padding_size
|
||||
= next_offset - offset_after_last_item;
|
||||
m_items.safe_push (item (bit_range (offset_after_last_item,
|
||||
padding_size),
|
||||
last_item.m_field, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto_vec<item> m_items;
|
||||
};
|
||||
|
||||
/* A subclass of pending_diagnostic for complaining about uninitialized data
|
||||
being copied across a trust boundary to an untrusted output
|
||||
(e.g. copy_to_user infoleaks in the Linux kernel). */
|
||||
|
|
Loading…
Add table
Reference in a new issue