Skip to content

Commit 10a5c8e

Browse files
committed
Do not assume that build architecture has byte/char==8 bits
The C/C++ standard does not guarantee this, and limits.h/climits has the CHAR_BIT define set properly. All changes in this commit only refer to the platform the tool is being built for. The analysis target requires a separate set of changes where the byte width must be taken from the configuration or annotations within expressions.
1 parent 6e88f92 commit 10a5c8e

14 files changed

+89
-57
lines changed

jbmc/src/janalyzer/janalyzer_parse_options.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "janalyzer_parse_options.h"
1313

14+
#include <climits>
1415
#include <cstdlib> // exit()
1516
#include <fstream>
1617
#include <iostream>
@@ -357,8 +358,9 @@ int janalyzer_parse_optionst::doit()
357358
// Print a banner
358359
//
359360
log.status() << "JANALYZER version " << CBMC_VERSION << " "
360-
<< sizeof(void *) * 8 << "-bit " << config.this_architecture()
361-
<< " " << config.this_operating_system() << messaget::eom;
361+
<< sizeof(void *) * CHAR_BIT << "-bit "
362+
<< config.this_architecture() << " "
363+
<< config.this_operating_system() << messaget::eom;
362364

363365
register_languages();
364366

jbmc/src/jbmc/jbmc_parse_options.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "jbmc_parse_options.h"
1313

14-
#include <fstream>
14+
#include <climits>
1515
#include <cstdlib> // exit()
16+
#include <fstream>
1617
#include <iostream>
1718
#include <memory>
1819

@@ -481,8 +482,9 @@ int jbmc_parse_optionst::doit()
481482
//
482483
// Print a banner
483484
//
484-
log.status() << "JBMC version " << CBMC_VERSION << " " << sizeof(void *) * 8
485-
<< "-bit " << config.this_architecture() << " "
485+
log.status() << "JBMC version " << CBMC_VERSION << " "
486+
<< sizeof(void *) * CHAR_BIT << "-bit "
487+
<< config.this_architecture() << " "
486488
<< config.this_operating_system() << messaget::eom;
487489

488490
// output the options

jbmc/src/jdiff/jdiff_parse_options.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Peter Schrammel
1111

1212
#include "jdiff_parse_options.h"
1313

14+
#include <climits>
1415
#include <cstdlib> // exit()
1516
#include <fstream>
1617
#include <iostream>
@@ -194,8 +195,9 @@ int jdiff_parse_optionst::doit()
194195
//
195196
// Print a banner
196197
//
197-
log.status() << "JDIFF version " << CBMC_VERSION << " " << sizeof(void *) * 8
198-
<< "-bit " << config.this_architecture() << " "
198+
log.status() << "JDIFF version " << CBMC_VERSION << " "
199+
<< sizeof(void *) * CHAR_BIT << "-bit "
200+
<< config.this_architecture() << " "
199201
<< config.this_operating_system() << messaget::eom;
200202

201203
if(cmdline.args.size() != 2)

src/ansi-c/library/gcc.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ inline void __sync_synchronize(void)
3535

3636
/* FUNCTION: __builtin_ffs */
3737

38+
#ifndef __CPROVER_LIMITS_H_INCLUDED
39+
# include <limits.h>
40+
# define __CPROVER_LIMITS_H_INCLUDED
41+
#endif
42+
3843
int __builtin_clz(unsigned int x);
3944

4045
inline int __builtin_ffs(int x)
@@ -47,11 +52,16 @@ inline int __builtin_ffs(int x)
4752
unsigned int u = (unsigned int)x;
4853
#pragma CPROVER check pop
4954

50-
return sizeof(int) * 8 - __builtin_clz(u & ~(u - 1));
55+
return sizeof(int) * CHAR_BIT - __builtin_clz(u & ~(u - 1));
5156
}
5257

5358
/* FUNCTION: __builtin_ffsl */
5459

60+
#ifndef __CPROVER_LIMITS_H_INCLUDED
61+
# include <limits.h>
62+
# define __CPROVER_LIMITS_H_INCLUDED
63+
#endif
64+
5565
int __builtin_clzl(unsigned long x);
5666

5767
inline int __builtin_ffsl(long x)
@@ -64,11 +74,16 @@ inline int __builtin_ffsl(long x)
6474
unsigned long u = (unsigned long)x;
6575
#pragma CPROVER check pop
6676

67-
return sizeof(long) * 8 - __builtin_clzl(u & ~(u - 1));
77+
return sizeof(long) * CHAR_BIT - __builtin_clzl(u & ~(u - 1));
6878
}
6979

7080
/* FUNCTION: __builtin_ffsll */
7181

82+
#ifndef __CPROVER_LIMITS_H_INCLUDED
83+
# include <limits.h>
84+
# define __CPROVER_LIMITS_H_INCLUDED
85+
#endif
86+
7287
int __builtin_clzll(unsigned long long x);
7388

7489
inline int __builtin_ffsll(long long x)
@@ -81,7 +96,7 @@ inline int __builtin_ffsll(long long x)
8196
unsigned long long u = (unsigned long long)x;
8297
#pragma CPROVER check pop
8398

84-
return sizeof(long long) * 8 - __builtin_clzll(u & ~(u - 1));
99+
return sizeof(long long) * CHAR_BIT - __builtin_clzll(u & ~(u - 1));
85100
}
86101

87102
/* FUNCTION: __atomic_test_and_set */

src/ansi-c/literals/convert_character_literal.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "convert_character_literal.h"
1313

14+
#include <climits>
15+
1416
#include <util/arith_tools.h>
1517
#include <util/c_types.h>
1618
#include <util/std_expr.h>
@@ -51,7 +53,7 @@ exprt convert_character_literal(
5153
for(unsigned i=0; i<value.size(); i++)
5254
{
5355
mp_integer z=(unsigned char)(value[i]);
54-
z=z<<((value.size()-i-1)*8);
56+
z = z << ((value.size() - i - 1) * CHAR_BIT);
5557
x+=z;
5658
}
5759

@@ -84,7 +86,7 @@ exprt convert_character_literal(
8486
for(unsigned i=0; i<value.size(); i++)
8587
{
8688
mp_integer z=(unsigned char)(value[i]);
87-
z=z<<((value.size()-i-1)*8);
89+
z = z << ((value.size() - i - 1) * CHAR_BIT);
8890
x+=z;
8991
}
9092

src/cbmc/cbmc_parse_options.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "cbmc_parse_options.h"
1313

14-
#include <fstream>
14+
#include <climits>
1515
#include <cstdlib> // exit()
16+
#include <fstream>
1617
#include <iostream>
1718
#include <memory>
1819

@@ -516,8 +517,9 @@ int cbmc_parse_optionst::doit()
516517
//
517518
// Print a banner
518519
//
519-
log.status() << "CBMC version " << CBMC_VERSION << " " << sizeof(void *) * 8
520-
<< "-bit " << config.this_architecture() << " "
520+
log.status() << "CBMC version " << CBMC_VERSION << " "
521+
<< sizeof(void *) * CHAR_BIT << "-bit "
522+
<< config.this_architecture() << " "
521523
<< config.this_operating_system() << messaget::eom;
522524

523525
//

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "goto_analyzer_parse_options.h"
1313

14+
#include <climits>
1415
#include <cstdlib> // exit()
15-
#include <iostream>
1616
#include <fstream>
17+
#include <iostream>
1718
#include <memory>
1819

1920
#include <ansi-c/ansi_c_language.h>
@@ -402,8 +403,9 @@ int goto_analyzer_parse_optionst::doit()
402403
// Print a banner
403404
//
404405
log.status() << "GOTO-ANALYSER version " << CBMC_VERSION << " "
405-
<< sizeof(void *) * 8 << "-bit " << config.this_architecture()
406-
<< " " << config.this_operating_system() << messaget::eom;
406+
<< sizeof(void *) * CHAR_BIT << "-bit "
407+
<< config.this_architecture() << " "
408+
<< config.this_operating_system() << messaget::eom;
407409

408410
register_languages();
409411

src/goto-cc/ms_cl_cmdline.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ Author: Daniel Kroening
1111

1212
#include "ms_cl_cmdline.h"
1313

14-
#include <cstring>
14+
#include <climits>
1515
#include <cstdlib>
16-
#include <iostream>
16+
#include <cstring>
1717
#include <fstream>
18+
#include <iostream>
1819

1920
#include <util/unicode.h>
2021

@@ -165,7 +166,7 @@ static std::istream &my_wgetline(std::istream &in, std::wstring &dest)
165166
break; // line end
166167
}
167168
else
168-
dest+=wchar_t(ch1+(ch2<<8));
169+
dest += wchar_t(ch1 + (ch2 << CHAR_BIT));
169170
}
170171

171172
return in;

src/goto-diff/goto_diff_parse_options.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Peter Schrammel
1111

1212
#include "goto_diff_parse_options.h"
1313

14+
#include <climits>
1415
#include <cstdlib> // exit()
1516
#include <fstream>
1617
#include <iostream>
@@ -162,8 +163,9 @@ int goto_diff_parse_optionst::doit()
162163
// Print a banner
163164
//
164165
log.status() << "GOTO-DIFF version " << CBMC_VERSION << " "
165-
<< sizeof(void *) * 8 << "-bit " << config.this_architecture()
166-
<< " " << config.this_operating_system() << messaget::eom;
166+
<< sizeof(void *) * CHAR_BIT << "-bit "
167+
<< config.this_architecture() << " "
168+
<< config.this_operating_system() << messaget::eom;
167169

168170
if(cmdline.args.size()!=2)
169171
{

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ mp_integer gdb_value_extractort::get_type_size(const typet &type) const
103103
{
104104
const auto maybe_size = pointer_offset_bits(type, ns);
105105
CHECK_RETURN(maybe_size.has_value());
106-
return *maybe_size / 8;
106+
return *maybe_size / CHAR_BIT;
107107
}
108108

109109
void gdb_value_extractort::analyze_symbols(const std::vector<irep_idt> &symbols)

src/util/config.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "config.h"
1010

11+
#include <climits>
1112
#include <cstdlib>
1213

1314
#include "arith_tools.h"
@@ -1010,42 +1011,43 @@ bool configt::set(const cmdlinet &cmdline)
10101011
if(arch==this_arch && os==this_os)
10111012
{
10121013
INVARIANT(
1013-
ansi_c.int_width == sizeof(int) * 8,
1014+
ansi_c.int_width == sizeof(int) * CHAR_BIT,
10141015
"int width shall be equal to the system int width");
10151016
INVARIANT(
1016-
ansi_c.long_int_width == sizeof(long) * 8,
1017+
ansi_c.long_int_width == sizeof(long) * CHAR_BIT,
10171018
"long int width shall be equal to the system long int width");
10181019
INVARIANT(
1019-
ansi_c.bool_width == sizeof(bool) * 8,
1020+
ansi_c.bool_width == sizeof(bool) * CHAR_BIT,
10201021
"bool width shall be equal to the system bool width");
10211022
INVARIANT(
1022-
ansi_c.char_width == sizeof(char) * 8,
1023+
ansi_c.char_width == sizeof(char) * CHAR_BIT,
10231024
"char width shall be equal to the system char width");
10241025
INVARIANT(
1025-
ansi_c.short_int_width == sizeof(short) * 8,
1026+
ansi_c.short_int_width == sizeof(short) * CHAR_BIT,
10261027
"short int width shall be equal to the system short int width");
10271028
INVARIANT(
1028-
ansi_c.long_long_int_width == sizeof(long long) * 8,
1029+
ansi_c.long_long_int_width == sizeof(long long) * CHAR_BIT,
10291030
"long long int width shall be equal to the system long long int width");
10301031
INVARIANT(
1031-
ansi_c.pointer_width == sizeof(void *) * 8,
1032+
ansi_c.pointer_width == sizeof(void *) * CHAR_BIT,
10321033
"pointer width shall be equal to the system pointer width");
10331034
INVARIANT(
1034-
ansi_c.single_width == sizeof(float) * 8,
1035+
ansi_c.single_width == sizeof(float) * CHAR_BIT,
10351036
"float width shall be equal to the system float width");
10361037
INVARIANT(
1037-
ansi_c.double_width == sizeof(double) * 8,
1038+
ansi_c.double_width == sizeof(double) * CHAR_BIT,
10381039
"double width shall be equal to the system double width");
10391040
INVARIANT(
1040-
ansi_c.char_is_unsigned == (static_cast<char>(255) == 255),
1041+
ansi_c.char_is_unsigned ==
1042+
(static_cast<char>((1 << CHAR_BIT) - 1) == (1 << CHAR_BIT) - 1),
10411043
"char_is_unsigned flag shall indicate system char unsignedness");
10421044

1043-
#ifndef _WIN32
1045+
#ifndef _WIN32
10441046
// On Windows, long double width varies by compiler
10451047
INVARIANT(
1046-
ansi_c.long_double_width == sizeof(long double) * 8,
1048+
ansi_c.long_double_width == sizeof(long double) * CHAR_BIT,
10471049
"long double width shall be equal to the system long double width");
1048-
#endif
1050+
#endif
10491051
}
10501052

10511053
// the following allows overriding the defaults

src/util/irep_hash.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Author: Michael Tautschnig, [email protected]
3535
// MURMURHASH2A compares most favourably on String6 with 3076 fewer
3636
// calls (2.9%)
3737

38+
#include <climits>
3839
#include <cstddef> // std::size_t
3940

4041
#ifdef _MSC_VER
@@ -117,10 +118,9 @@ inline std::size_t basic_hash_finalize(
117118

118119
// Boost uses the symbol hash_combine, if you're getting problems here then
119120
// you've probably included a Boost header after this one
120-
#define hash_combine(h1, h2) \
121-
basic_hash_combine<sizeof(std::size_t)*8>(h1, h2)
122-
#define hash_finalize(h1, len) \
123-
basic_hash_finalize(h1, len)
121+
# define hash_combine(h1, h2) \
122+
basic_hash_combine<sizeof(std::size_t) * CHAR_BIT>(h1, h2)
123+
# define hash_finalize(h1, len) basic_hash_finalize(h1, len)
124124

125125
#endif
126126

@@ -224,10 +224,10 @@ inline std::size_t murmurhash2a_hash_finalize<64>(
224224
return h1;
225225
}
226226

227-
#define hash_combine(h1, h2) \
228-
murmurhash2a_hash_combine<sizeof(std::size_t)*8>(h1, h2)
229-
#define hash_finalize(h1, len) \
230-
murmurhash2a_hash_finalize<sizeof(std::size_t)*8>(h1, len)
227+
# define hash_combine(h1, h2) \
228+
murmurhash2a_hash_combine<sizeof(std::size_t) * CHAR_BIT>(h1, h2)
229+
# define hash_finalize(h1, len) \
230+
murmurhash2a_hash_finalize<sizeof(std::size_t) * CHAR_BIT>(h1, len)
231231

232232
#endif
233233

@@ -340,10 +340,10 @@ inline std::size_t murmurhash3_hash_finalize<64>(
340340
return fmix64(h1);
341341
}
342342

343-
#define hash_combine(h1, h2) \
344-
murmurhash3_hash_combine<sizeof(std::size_t)*8>(h1, h2)
345-
#define hash_finalize(h1, len) \
346-
murmurhash3_hash_finalize<sizeof(std::size_t)*8>(h1, len)
343+
# define hash_combine(h1, h2) \
344+
murmurhash3_hash_combine<sizeof(std::size_t) * CHAR_BIT>(h1, h2)
345+
# define hash_finalize(h1, len) \
346+
murmurhash3_hash_finalize<sizeof(std::size_t) * CHAR_BIT>(h1, len)
347347

348348
#endif
349349

src/util/irep_serialization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Date: May 2007
1313

1414
#include "irep_serialization.h"
1515

16-
#include <sstream>
16+
#include <climits>
1717
#include <iostream>
18+
#include <sstream>
1819

1920
#include "exception_utils.h"
2021
#include "string_hash.h"
@@ -161,7 +162,7 @@ std::size_t irep_serializationt::read_gb_word(std::istream &in)
161162

162163
while(in.good())
163164
{
164-
if(shift_distance >= sizeof(res) * 8)
165+
if(shift_distance >= sizeof(res) * CHAR_BIT)
165166
throw deserialization_exceptiont("input number too large");
166167

167168
unsigned char ch=static_cast<unsigned char>(in.get());

0 commit comments

Comments
 (0)