Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid integer truncation #1330

Closed
wants to merge 3 commits into from
Closed

Conversation

reuk
Copy link
Contributor

@reuk reuk commented Sep 2, 2017

The build is broken on 32-bit platforms, which is important because we should support OS X universal binaries. This patch fixes the build errors by explicitly using 64-bit integers rather than depending on size_t to be 64 bits wide.

@@ -962,7 +963,7 @@ bool interpretert::unbounded_size(const typet &type)
/// get allocated 2^32 address space each (of a 2^64 sized space).
/// \param type: a structured type
/// \return Size of the given type
size_t interpretert::get_size(const typet &type)
uint64_t interpretert::get_size(const typet &type)
{
if(unbounded_size(type))
return 2ULL << 32ULL;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value here gets truncated to 0 when size_t is 32 bits wide.

Copy link
Collaborator

@tautschnig tautschnig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch; shouldn't we really be using mp_integer here?

@tautschnig
Copy link
Collaborator

There are actually more problems in this code:

  • interpretert::unbounded_size does not consider unions or symbol types
  • interpretert::get_size also uses unsigned internally.

I believe unbounded objects need to be handled properly at the call site of get_size.

@kroening
Copy link
Member

kroening commented Sep 3, 2017

Yes, mp_integer exists exactly to avoid such problems on all architectures.

@reuk
Copy link
Contributor Author

reuk commented Sep 4, 2017

I started looking at this, but it seems like changing that one function to return mp_integer will lead to a great number of further alterations. For example, it in turn requires that base_address_to_actual_size(std::size_t) be changed to take an mp_integer argument, which is then passed to memory.find(address), where memory is a sparse_vector. Either sparse_vector will need a find taking mp_integer, or we will need to report an error if the mp_integer is truncated when converting it to uint64_t.

Maybe this would be better addressed by someone who is already familiar with this code.

@tautschnig
Copy link
Collaborator

Maybe this would be better addressed by someone who is already familiar with this code.

Looking at the commit subjects, without actually having done a git blame: @romainbrenguier ?

@reuk
Copy link
Contributor Author

reuk commented Sep 5, 2017

Would it be worth merging this fix in now, and filing an issue to do a proper cleanup? At the moment, we don't have a working 32-bit build, and this patch at least resolves the immediate problem.

@tautschnig
Copy link
Collaborator

From a plain technical point of view, lacking all the management insight: do the proper fix, even if it means rewriting several functions. If questions about the code arise, git blame will tell whom to ask.

@reuk reuk force-pushed the reuk/integer-size-bugfix branch from 5e09d48 to 377fe4e Compare September 5, 2017 15:40
@tautschnig
Copy link
Collaborator

Thank you for the further work! I think all instances of uint64_t should go away, though: types are either compile-platform types (std::size_t) whenever the limitations of the runtime environment apply, or be unbounded (mp_integer).

@reuk reuk force-pushed the reuk/integer-size-bugfix branch from 6af34a9 to 661e095 Compare September 5, 2017 17:19
@reuk
Copy link
Contributor Author

reuk commented Sep 5, 2017

I've tried to remove all instances of unsigned and uint64_t from the interpreter files. Use of auto is deliberate. It ensures that the correct numeric type is used, regardless of refactorings like these (this patch would have been much smaller if auto was used in the first place).

The guiding rationale has been to leave collection types unchanged. Conversions from mp_integer to size_t occur when the value is used as an index into an array. Otherwise, we try to prefer mp_integer.

@tautschnig
Copy link
Collaborator

I'll be taking a proper look in the morning - does this now require #1341 to build? (Which I'd be fine with, would just like to understand the CI failures.)

@reuk
Copy link
Contributor Author

reuk commented Sep 6, 2017

Yes, this depends on having properly-converting operators on big-int and therefore requires #1341

@reuk
Copy link
Contributor Author

reuk commented Sep 6, 2017

#1341 blocked indefinitely, closing this.

@reuk reuk closed this Sep 6, 2017
@tautschnig tautschnig reopened this Oct 4, 2017
@tautschnig
Copy link
Collaborator

Joining my to-do list.

@tautschnig tautschnig assigned tautschnig and unassigned reuk Oct 4, 2017
@romainbrenguier
Copy link
Contributor

@tautschnig

Maybe this would be better addressed by someone who is already familiar with this code.

Looking at the commit subjects, without actually having done a git blame: @romainbrenguier ?

Actually @smowton knows that code better

@tautschnig
Copy link
Collaborator

@reuk: #1341 has been merged, would you be able to look into this one again? Please let me know how I can be of help, if at all.

@tautschnig tautschnig assigned reuk and unassigned tautschnig Oct 12, 2017
@kroening
Copy link
Member

I think that mp_integer is the right answer for the addresses; we do so everywhere else, and the cost isn't significantly higher than that of the 64-bit integers.

@tautschnig
Copy link
Collaborator

Closing in favour of #1484, which seems to be doing almost the same. (I don't know why this one hasn't been amended rather than doing duplicate work.)

@tautschnig tautschnig closed this Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants