Skip to content

Do not hardcode char/bytes having 8 bits#917

Merged
tautschnig merged 1 commit intodiffblue:developfrom
tautschnig:byte-config
Oct 18, 2022
Merged

Do not hardcode char/bytes having 8 bits#917
tautschnig merged 1 commit intodiffblue:developfrom
tautschnig:byte-config

Conversation

@tautschnig
Copy link
Collaborator

@tautschnig tautschnig commented May 13, 2017

The C standard does not guarantee that char is exactly 8 bits, and there are DSPs (such Texas Instruments C55x) that do not have 8-bit-bytes. Use the configuration value instead.

@tautschnig
Copy link
Collaborator Author

tautschnig commented May 23, 2017

Marking do-not-merge as it includes a commit that overlaps with #955.

@tautschnig tautschnig force-pushed the byte-config branch 3 times, most recently from d6010a2 to 4047f38 Compare May 23, 2017 17:43
@tautschnig tautschnig assigned kroening and unassigned tautschnig May 25, 2017
@tautschnig tautschnig force-pushed the byte-config branch 2 times, most recently from c5fb9c2 to ebc4c75 Compare June 28, 2017 14:16
@tautschnig tautschnig force-pushed the byte-config branch 2 times, most recently from ff0e2f6 to 437462a Compare July 15, 2017 18:22
@tautschnig tautschnig changed the base branch from master to develop August 22, 2017 12:25
@tautschnig tautschnig assigned tautschnig and unassigned kroening Sep 2, 2017
@tautschnig tautschnig changed the title Do not assume that chars/bytes have 8 bits (and some cleanup) [depends: #1331,#1332,#1333] Do not assume that chars/bytes have 8 bits (and some cleanup) Sep 2, 2017
@tautschnig tautschnig changed the title [depends: #1331,#1332,#1333] Do not assume that chars/bytes have 8 bits (and some cleanup) [depends: #1333] Do not assume that chars/bytes have 8 bits (and some cleanup) Sep 4, 2017
@tautschnig
Copy link
Collaborator Author

tautschnig commented May 30, 2018

Marking do-not-merge as with the changes from #2246 the byte width should become part of byte_extract/byte_update.

@thomasspriggs
Copy link
Contributor

@tautschnig I have just been taking a look at this PR as part of the effort to clean-up the backlog of open PRs. You stated that this PR was marked "Do not merge" due to overlap with #955. However #955 has now been merged. Does this mean that this PR can now be rebased and merged, or is the effort no longer worthwhile due to the amount of accumulated merge conflicts?

@tautschnig
Copy link
Collaborator Author

@thomasspriggs Thank you for undertaking a spring clean! This PR, however, is still of interest to me. I can't quite promise when I'll get to work on it, but I do intend to get this merged eventually.

@tautschnig
Copy link
Collaborator Author

Cleanup done, but "the changes from #2246 the byte width should become part of byte_extract/byte_update." is still to be done.

@codecov
Copy link

codecov bot commented Apr 3, 2021

Codecov Report

Base: 77.99% // Head: 74.12% // Decreases project coverage by -3.87% ⚠️

Coverage data is based on head (4c871ca) compared to base (99ce20c).
Patch coverage: 92.15% of modified lines in pull request are covered.

❗ Current head 4c871ca differs from pull request most recent head 9ab636f. Consider uploading reports for the commit 9ab636f to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #917      +/-   ##
===========================================
- Coverage    77.99%   74.12%   -3.88%     
===========================================
  Files         1619     1444     -175     
  Lines       187184   157472   -29712     
===========================================
- Hits        145999   116729   -29270     
+ Misses       41185    40743     -442     
Impacted Files Coverage Δ
src/analyses/goto_rw.cpp 52.80% <0.00%> (-13.80%) ⬇️
src/ansi-c/literals/convert_character_literal.cpp 51.21% <0.00%> (-34.50%) ⬇️
src/goto-symex/symex_function_call.cpp 92.89% <ø> (-2.72%) ⬇️
src/goto-symex/symex_other.cpp 84.40% <ø> (-2.01%) ⬇️
src/solvers/flattening/boolbv_index.cpp 79.57% <0.00%> (+6.66%) ⬆️
unit/util/expr_cast/expr_cast.cpp 100.00% <ø> (ø)
unit/util/pointer_offset_size.cpp 100.00% <ø> (ø)
src/util/simplify_expr_struct.cpp 69.04% <66.66%> (-5.34%) ⬇️
src/solvers/flattening/boolbv_byte_extract.cpp 69.62% <75.00%> (-0.51%) ⬇️
src/util/pointer_offset_size.cpp 92.08% <82.50%> (-0.76%) ⬇️
... and 1497 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

The C standard does not guarantee that char is exactly 8 bits, and there
are DSPs (such Texas Instruments C55x) that do not have 8-bit-bytes. Use
the configuration value instead.
Copy link
Member

@peterschrammel peterschrammel left a comment

Choose a reason for hiding this comment

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

tautschnig created on 13 May 2017

Last one on the stack?

std::vector<std::size_t> get_ones_pos(std::size_t v)
{
const std::size_t length = sizeof(std::size_t) * 8;
const std::size_t length = sizeof(std::size_t) * CHAR_BIT;
Copy link
Contributor

Choose a reason for hiding this comment

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

The PR description states that you are using the width from the configuration. But this line is using the width for the machine for which cbmc is being compiled. Shouldn't config.ansi_c.char_width be used instead? The distinction could be important if cbmc is to be used for program written for a machine such as a DSP like you suggested in the PR desription. Wouldn't these architectures be more likely to be targeted using a cross compiler rather than porting cbmc to the target hardware itself?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You are right in that care is required where CHAR_BIT is the right choice and where config.ansi_c.char_width is to be used instead. I hope to have made the right choice in all places, but of course may have made mistakes. For this particular case: as far as I understand the code, this is about the width of bytes that CBMC is being compiled for, and not whatever the verification target might be. So I claim that CHAR_BIT is correct. Now you are also right in that, in all likelihood, one would use a cross compiler for such a DSP, and it may well be that no platform that CBMC ever runs on has CHAR_BIT != 8 - but I'd also like to avoid magic numbers, making it very difficult to tell whether the use of "8" is one that should be config.ansi_c.char_width instead, or is actually fine for it's the compile-time byte width on all relevant platforms.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you are convinced that we are depending on the right thing in this case, then I am happy to merge this as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This specific case it is to enumerate various bit sequences, and this enumeration is done by the compiled CBMC binary.

@tautschnig
Copy link
Collaborator Author

tautschnig created on 13 May 2017

Last one on the stack?

🤣 You wish!

Copy link
Contributor

@thomasspriggs thomasspriggs left a comment

Choose a reason for hiding this comment

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

FYI - CHAR_BIT is the C standard library version of this constant. The C++ equivalent is std::numeric_limits<char>::digits. In this case digits refers to binary digits on the underlying hardware and std::numeric_limits<char>::radix is 2.

🤔 I am happy for debugging and addition of tests for a platform where char is not 8 bits to be left for a follow-up PR if and when this becomes a priority.

bit_field_bits += w;
result += bit_field_bits / 8;
bit_field_bits %= 8;
result += bit_field_bits / config.ansi_c.char_width;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm assuming we guarantee that char_width is always different than zero in all these cases, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, though we might one day want to have some architecture sanity check.

Copy link
Contributor

Choose a reason for hiding this comment

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

It can be loaded from file through configt::set_from_symbol_table and symtab2gb. Therefore it could theoretically be set to 0 in the file and I am not sure we have appropriate validation that the value from file is not 0.


array_typet is_fresh_baset::get_memmap_type()
{
return array_typet(c_bool_typet(8), infinity_exprt(size_type()));
Copy link
Collaborator

@remi-delmas-3000 remi-delmas-3000 Oct 18, 2022

Choose a reason for hiding this comment

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

Do we really need to propagate this to instrumentation code ? This code does not represent anything running on an actual processor. Should c_bool_typet(8) be deprecated completely ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm ok with an arbitrary type, but we must not have "8" as a magic number. I assumed this was "8" in the first place so as to have byte granularity? Was I wrong?

Copy link
Contributor

Choose a reason for hiding this comment

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

My usual assumption is that architecture should be well defined at the point of running the front end. This is because the C front end runs an external C pre-processor which can have architecture specific macro expansions. As goto-instrument runs after the front end, it is operating on an architecture specific program form. Note for example that the c_bool_type() construction function in src/util/c_types.cpp references the same config field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants