diff mbox

[01/14] Fix latent bug in dwarf2_find_containing_comp_unit

Message ID 20200215165444.32653-2-tom@tromey.com
State New
Headers show

Commit Message

Tom Tromey Feb. 15, 2020, 4:54 p.m. UTC
dwarf2_find_containing_comp_unit has this in its binary search:

      if (mid_cu->is_dwz > offset_in_dwz
	  || (mid_cu->is_dwz == offset_in_dwz
	      && mid_cu->sect_off + mid_cu->length >= sect_off))
	high = mid;

The intent here is to determine whether SECT_OFF appears in or before
MID_CU.

I believe this has an off-by-one error, and that the check should use
">" rather than ">=".  If the two side are equal, then SECT_OFF
actually appears at the start of the next CU.

I've had this patch kicking around for ages but I forget how I found
the problem.

gdb/ChangeLog
2020-02-15  Tom Tromey  <tom@tromey.com>

	* dwarf2/read.c (dwarf2_find_containing_comp_unit): Use ">", not
	">=", in binary search.
---
 gdb/ChangeLog     | 5 +++++
 gdb/dwarf2/read.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

Comments

Simon Marchi Feb. 19, 2020, 3:42 a.m. UTC | #1
On 2020-02-15 11:54 a.m., Tom Tromey wrote:
> dwarf2_find_containing_comp_unit has this in its binary search:
> 
>       if (mid_cu->is_dwz > offset_in_dwz
> 	  || (mid_cu->is_dwz == offset_in_dwz
> 	      && mid_cu->sect_off + mid_cu->length >= sect_off))
> 	high = mid;
> 
> The intent here is to determine whether SECT_OFF appears in or before
> MID_CU.
> 
> I believe this has an off-by-one error, and that the check should use
> ">" rather than ">=".  If the two side are equal, then SECT_OFF
> actually appears at the start of the next CU.
> 
> I've had this patch kicking around for ages but I forget how I found
> the problem.
> 
> gdb/ChangeLog
> 2020-02-15  Tom Tromey  <tom@tromey.com>
> 
> 	* dwarf2/read.c (dwarf2_find_containing_comp_unit): Use ">", not
> 	">=", in binary search.
> ---
>  gdb/ChangeLog     | 5 +++++
>  gdb/dwarf2/read.c | 2 +-
>  2 files changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> index e74383e01dc..e373cc0af2c 100644
> --- a/gdb/dwarf2/read.c
> +++ b/gdb/dwarf2/read.c
> @@ -24171,7 +24171,7 @@ dwarf2_find_containing_comp_unit (sect_offset sect_off,
>        mid_cu = dwarf2_per_objfile->all_comp_units[mid];
>        if (mid_cu->is_dwz > offset_in_dwz
>  	  || (mid_cu->is_dwz == offset_in_dwz
> -	      && mid_cu->sect_off + mid_cu->length >= sect_off))
> +	      && mid_cu->sect_off + mid_cu->length > sect_off))
>  	high = mid;
>        else
>  	low = mid + 1;

I can only convince myself of these things by plugging in real numbers.  So let's say
mid_cu's range is [100,150[, so 150 bytes long, and we are looking for sect_off == 150.
150 is outside (just after) mid_cu's range, so the right answer will be the cu just
after this one.

Without your change, we would have gone in the "if", and therefore excluded the right
answer.  With your change, we would have gone in the "else", and therefore chosen the
range with the right answer.  So that looks correct to me.

I'm going to ask if you thought about a way to test this.  I don't think writing a typical
test case for this is feasible.  By refactoring the code a bit, we could maybe factor out the
meat of this function into one that operates on an std::vector<dwarf2_per_cu_data> instead
of on a dwarf2_per_objfile.  It should then be feasible to create an std::vector with
dwarf2_per_cu_data elements out of thin air to unit-test the function, including edge
cases like this.

Also, do you understand what's the logic with this dwz stuff?  Is it that all the CUs
coming from a dwz file are at the end of the list, or something like that?

Simon
Tom Tromey Feb. 19, 2020, 2:08 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> I'm going to ask if you thought about a way to test this.  I
Simon> don't think writing a typical test case for this is feasible.

Yeah.  And like I said, I don't remember how I encountered this.
Maybe only with some other dubious patch in place.

Simon>  By refactoring the code a bit, we could maybe factor out the
Simon> meat of this function into one that operates on an std::vector<dwarf2_per_cu_data> instead
Simon> of on a dwarf2_per_objfile.  It should then be feasible to create an std::vector with
Simon> dwarf2_per_cu_data elements out of thin air to unit-test the function, including edge
Simon> cases like this.

Another idea that occurred to me is that we could just use
std::binary_search here.  Then instead of implementing a binary search,
we'd only need to implement a comparison function -- which seems
simpler.

Anyway I will try to write a unit test, that's a good idea.

Simon> Also, do you understand what's the logic with this dwz stuff?  Is it that all the CUs
Simon> coming from a dwz file are at the end of the list, or something like that?

Yes, exactly.  Maybe this can be cleaned up a bit after this series,
since now we have the index directly in the dwarf2_per_cu_data.  I am
not sure offhand.

Tom
Tom Tromey Feb. 20, 2020, 12:11 a.m. UTC | #3
Simon> By refactoring the code a bit, we could maybe factor out the meat
Simon> of this function into one that operates on an
Simon> std::vector<dwarf2_per_cu_data> instead of on a
Simon> dwarf2_per_objfile.  It should then be feasible to create an
Simon> std::vector with dwarf2_per_cu_data elements out of thin air to
Simon> unit-test the function, including edge cases like this.

Here is a new version of this patch.
I propose landing it separately, because it is just a straightforward
latent bug fix.

Simon> Also, do you understand what's the logic with this dwz stuff?  Is it that all the CUs
Simon> coming from a dwz file are at the end of the list, or something like that?

Tom> Yes, exactly.

To expand on that, in multi-file mode, dwz may create a combined, shared
file of partial symtabs.  When reading the DWARF for the main file, gdb
may find it needs the dwz file; and in this case the additional CUs are
put into the same vector, at the end.  It's done this way so that we
can easily find the correct CU (since the section offsets between the
main and shared dwz file will overlap).

It would probably be better to try to share these CUs across objfiles.
That may be somewhat involved given the current code, though.

Tom
diff mbox

Patch

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index e74383e01dc..e373cc0af2c 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -24171,7 +24171,7 @@  dwarf2_find_containing_comp_unit (sect_offset sect_off,
       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
       if (mid_cu->is_dwz > offset_in_dwz
 	  || (mid_cu->is_dwz == offset_in_dwz
-	      && mid_cu->sect_off + mid_cu->length >= sect_off))
+	      && mid_cu->sect_off + mid_cu->length > sect_off))
 	high = mid;
       else
 	low = mid + 1;