libabigail/include/abg-comp-filter.h
Dodji Seketeli e02d3b85e3 Bug 29811 - Better categorize harmless unknown array size changes
Let's compile the code snippet:

$ echo "unsigned int is_basic_table[];" | gcc -g -c -o test-v0.o -x c -

Let's see what abidw sees from it:

$ abidw test-v0.o | cat -n

     1	<abi-corpus version='2.1' path='test-v0.o' architecture='elf-amd-x86_64'>
     2	  <elf-variable-symbols>
     3	    <elf-symbol name='is_basic_table' size='4' type='object-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     4	  </elf-variable-symbols>
     5	  <abi-instr address-size='64' path='&lt;stdin&gt;' comp-dir-path='/home/dodji/git/libabigail/PR29811/prtests' language='LANG_C11'>
     6	    <type-decl name='unsigned int' size-in-bits='32' id='type-id-1'/>
     7	    <array-type-def dimensions='1' type-id='type-id-1' size-in-bits='infinite' id='type-id-2'>
     8	      <subrange length='infinite' id='type-id-3'/>
     9	    </array-type-def>
    10	    <var-decl name='is_basic_table' type-id='type-id-2' mangled-name='is_basic_table' visibility='default' filepath='/home/dodji/git/libabigail/PR29811/prtests/&lt;stdin&gt;' line='1' column='1' elf-symbol-id='is_basic_table'/>
    11	  </abi-instr>
    12	</abi-corpus>

See how the at line 7, the array type of ID 'type-id-2' has an unknown
size.  This is the type of the 'is_basic_table' variable defined at
line 10.  Note however that the symbol size of the is_basic_table
symbol is 4 bytes (32 bits).

Now, let's compile a similar code where the is_basic_table variable is
now initialized:

$ echo "unsigned int is_basic_table[] = {0};" | gcc -g -c -o test-v1.o -x c -

$ $ abidw test-v1.o | cat -n

     1	<abi-corpus version='2.1' path='test-v1.o' architecture='elf-amd-x86_64'>
     2	  <elf-variable-symbols>
     3	    <elf-symbol name='is_basic_table' size='4' type='object-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     4	  </elf-variable-symbols>
     5	  <abi-instr address-size='64' path='&lt;stdin&gt;' comp-dir-path='/home/dodji/git/libabigail/PR29811/prtests' language='LANG_C11'>
     6	    <type-decl name='unsigned int' size-in-bits='32' id='type-id-1'/>
     7	    <array-type-def dimensions='1' type-id='type-id-1' size-in-bits='32' id='type-id-2'>
     8	      <subrange length='1' type-id='type-id-3' id='type-id-4'/>
     9	    </array-type-def>
    10	    <type-decl name='unsigned long int' size-in-bits='64' id='type-id-3'/>
    11	    <var-decl name='is_basic_table' type-id='type-id-2' mangled-name='is_basic_table' visibility='default' filepath='/home/dodji/git/libabigail/PR29811/prtests/&lt;stdin&gt;' line='1' column='1' elf-symbol-id='is_basic_table'/>
    12	  </abi-instr>
    13	</abi-corpus>

Now, see like at line 7, the array type is now of 4 bytes (32 bits).
Note that the size of is_basic_table is still 32 bits.

Normally, abidiff-ing test-v0 and test-v1 should tell us that the two
versions of the is_basic_table variable are compatible because
fundamentally the structure and the size of the ELF symbol
is_basic_table hasn't changed, even if in the first case, it's an
array of unknown size.  It's ELF symbol size was already 32 bits.

Here is what abidiff says:

$ abidiff test-v0.o test-v1.o
Functions changes summary: 0 Removed, 0 Changed, 0 Added function
Variables changes summary: 0 Removed, 1 Changed, 0 Added variable

1 Changed variable:

  [C] 'unsigned int is_basic_table[]' was changed to 'unsigned int is_basic_table[1]' at <stdin>:1:1:
    type of variable changed:
      type name changed from 'unsigned int[]' to 'unsigned int[1]'
      array type size changed from infinity to 32
      array type subrange 1 changed length from infinity to 1

$

This is because the comparison engine doesn't recognize we are looking
at a type change that is harmless because the ELF size hasn't changed
and because this is an array of one dimension so fundamentally, the
"meaning" of the type of the array hasn't fundamentally changed for
ABI-related purposes.

This patch teaches the diff node categorizer to recognise that we are
in a case where the (one dimension) array of unknown size actually is
the type of an array which symbol size is 4 bytes.  In the second
case, the one dimension array has a size of 4 bytes, just as its ELF
symbol size.  The diff node categorizer then categorizes the diff node
into the existing category BENIGN_INFINITE_ARRAY_CHANGE_CATEGORY,
which is a harmless diff node category.  Everything then falls into
place to filter the change out.  Also, the patch adapts the diff
reporter to better describe this type of harmless array variable type
changes.

The output then becomes:

$ abidiff test-v0.o test-v1.o
Functions changes summary: 0 Removed, 0 Changed, 0 Added function
Variables changes summary: 0 Removed, 0 Changed (1 filtered out), 0 Added variable

$

The change is filtered out.

To have details about the change that has been filtered out, one has
to use "--harmless" option:

$ abidiff --harmless test-v0.o test-v1.o
Functions changes summary: 0 Removed, 0 Changed, 0 Added function
Variables changes summary: 0 Removed, 1 Changed, 0 Added variable

1 Changed variable:

  [C] 'unsigned int is_basic_table[]' was changed to 'unsigned int is_basic_table[1]' at <stdin>:1:1:
    size of variable symbol ( 32 (in bits)) hasn't changed
    but it does have a harmless type change
    type of variable changed:
      type name changed from 'unsigned int[]' to 'unsigned int[1]'
      array type size changed from 'unknown' to 32
      array type subrange 1 changed length from 'unknown' to 1

$

	* include/abg-comp-filter.h
	(is_var_1_dim_unknown_size_array_change): Declare new function.
	* src/abg-comp-filter.cc (is_var_1_dim_unknown_size_array_change):
	Define new function.
	(has_benign_array_of_unknown_size_change): Rename
	has_benign_infinite_array_change into this.  Make this call the
	new is_var_1_dim_unknown_size_array_change.
	(categorize_harmless_diff_node): Adjust the call to
	has_benign_infinite_array_change into the new
	has_benign_array_of_unknown_size_change.
	* include/abg-ir.h (var_equals_modulo_types): Declare new
	function.  Make it friend of class decl_base.
	* src/abg-default-reporter.cc (default_reporter::report): In the
	overload for var_diff, call the new
	maybe_report_diff_for_variable.
	* src/abg-ir.cc (var_equals_modulo_types): Factorize this out of
	the equals() function for var_decl.
	(equals): In the overload for var_decl, call the new
	var_equals_modulo_types.
	* src/abg-reporter-priv.h (maybe_report_diff_for_variable):
	Declare new function.
	* src/abg-reporter-priv.cc (maybe_report_diff_for_variable):
	Define new function.
	* tests/data/test-diff-filter/test-PR29811-0-report-0.txt: Add
	new reference test output.
	* tests/data/test-diff-filter/test-PR29811-0-report-1.txt:
	Likewise.
	* tests/data/test-diff-filter/test-PR29811-0-v{0,1}.o: Add new
	binary test inputs.
	* tests/data/test-diff-filter/test-PR29811-0-v{0,1}.c: Add source
	code of the binary test inputs.
	* tests/data/Makefile.am: Add new test input files above to source
	distribution.
	* tests/test-diff-filter.cc (in_out_specs): Add new tests to
	harness.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
2023-01-01 18:21:56 +01:00

163 lines
3.9 KiB
C++

// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- Mode: C++ -*-
//
// Copyright (C) 2013-2023 Red Hat, Inc.
//
// Author: Dodji Seketeli
/// @file
///
/// This header declares filters for the diff trees resulting from
/// comparing ABI Corpora.
#ifndef __ABG_COMP_FILTER_H__
#define __ABG_COMP_FILTER_H__
#include "abg-comparison.h"
namespace abigail
{
namespace comparison
{
/// Facilities to walk, categorize and possibly filter nodes of the
/// diff tree.
namespace filtering
{
bool
has_harmless_name_change(const decl_base_sptr& f, const decl_base_sptr& s);
bool union_diff_has_harmless_changes(const diff *d);
bool
has_harmful_name_change(const decl_base_sptr& f, const decl_base_sptr& s);
bool
has_harmful_name_change(const diff* dif);
bool
has_virtual_mem_fn_change(const function_decl_diff* diff);
bool
is_decl_only_class_with_size_change(const class_or_union& first,
const class_or_union& second);
bool
is_decl_only_class_with_size_change(const class_or_union_sptr& first,
const class_or_union_sptr& second);
bool
is_decl_only_class_with_size_change(const diff *diff);
bool
has_decl_only_def_change(const decl_base_sptr& first,
const decl_base_sptr& second);
bool
has_decl_only_def_change(const diff *d);
bool
has_class_decl_only_def_change(const class_or_union_sptr& first,
const class_or_union_sptr& second);
bool
has_enum_decl_only_def_change(const enum_type_decl_sptr& first,
const enum_type_decl_sptr& second);
bool
has_class_decl_only_def_change(const diff *diff);
bool
has_enum_decl_only_def_change(const diff *diff);
bool
has_basic_type_name_change(const diff *);
bool
has_class_or_union_type_name_change(const diff *d);
bool
has_basic_or_class_type_name_change(const diff *d);
bool
is_mostly_distinct_diff(const diff *d);
bool
has_anonymous_data_member_change(const diff *d);
bool
has_anonymous_data_member_change(const diff_sptr &d);
bool
has_data_member_replaced_by_anon_dm(const diff* diff);
bool
is_var_1_dim_unknown_size_array_change(const diff*);
bool
is_var_1_dim_unknown_size_array_change(const var_decl_sptr& var1,
const var_decl_sptr& var2);
struct filter_base;
/// Convenience typedef for a shared pointer to filter_base
typedef shared_ptr<filter_base> filter_base_sptr;
/// Convenience typedef for a vector of filter_base_sptr
typedef std::vector<filter_base_sptr> filters;
/// The base class for the diff tree node filter.
///
/// It's intended to walk a tree of diff nodes and tag each relevant
/// name into one or several categories depending on well choosen
/// properties of the diff nodes.
struct filter_base : public diff_node_visitor
{
friend void
apply_filter(filter_base_sptr f, diff_sptr deef);
}; //end class filter_base
void
apply_filter(filter_base& filter, diff_sptr d);
void
apply_filter(filter_base& filter, corpus_diff_sptr d);
void
apply_filter(filter_base_sptr filter, diff_sptr d);
class harmless_filter;
/// Convenience typedef for a shared pointer to a harmless_filter.
typedef shared_ptr<harmless_filter> harmless_filter_sptr;
/// A filter that walks the diff nodes tree and tags relevant diff
/// nodes into categories considered to represent harmless changes.
class harmless_filter : public filter_base
{
virtual bool
visit(diff*, bool);
virtual void
visit_end(diff*);
}; // end class harmless_filter
class harmless_harmful_filter;
/// A convenience typedef for a shared pointer to harmful_filter.
typedef shared_ptr<harmless_harmful_filter> harmful_harmless_filter_sptr;
/// A filter that walks the diff nodes tree and tags relevant diff
/// nodes into categories considered to represent potentially harmless
/// or harmful changes.
class harmless_harmful_filter : public filter_base
{
virtual bool
visit(diff*, bool);
virtual void
visit_end(diff*);
}; // end class harmless_harmful_filter
} // end namespace filtering
} // end namespace comparison
} // end namespace abigail
#endif // __ABG_COMP_FILTER_H__