Skip to content

Commit bd0123d

Browse files
authored
use host byte order consistently in precompiled module files (#26493)
1 parent 0fffb36 commit bd0123d

File tree

3 files changed

+44
-57
lines changed

3 files changed

+44
-57
lines changed

base/loading.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ function compilecache(pkg::PkgId)
12001200
if success(create_expr_cache(path, cachefile, concrete_deps, pkg.uuid))
12011201
# append checksum to the end of the .ji file:
12021202
open(cachefile, "a+") do f
1203-
write(f, hton(_crc32c(seekstart(f))))
1203+
write(f, _crc32c(seekstart(f)))
12041204
end
12051205
else
12061206
error("Failed to precompile $name to $cachefile.")
@@ -1211,35 +1211,35 @@ end
12111211
module_build_id(m::Module) = ccall(:jl_module_build_id, UInt64, (Any,), m)
12121212

12131213
isvalid_cache_header(f::IOStream) = (0 != ccall(:jl_read_verify_header, Cint, (Ptr{Cvoid},), f.ios))
1214-
isvalid_file_crc(f::IOStream) = (_crc32c(seekstart(f), filesize(f) - 4) == ntoh(read(f, UInt32)))
1214+
isvalid_file_crc(f::IOStream) = (_crc32c(seekstart(f), filesize(f) - 4) == read(f, UInt32))
12151215

12161216
function parse_cache_header(f::IO)
12171217
modules = Vector{Pair{PkgId, UInt64}}()
12181218
while true
1219-
n = ntoh(read(f, Int32))
1219+
n = read(f, Int32)
12201220
n == 0 && break
12211221
sym = String(read(f, n)) # module name
1222-
uuid = UUID((ntoh(read(f, UInt64)), ntoh(read(f, UInt64)))) # pkg UUID
1223-
build_id = ntoh(read(f, UInt64)) # build UUID (mostly just a timestamp)
1222+
uuid = UUID((read(f, UInt64), read(f, UInt64))) # pkg UUID
1223+
build_id = read(f, UInt64) # build UUID (mostly just a timestamp)
12241224
push!(modules, PkgId(uuid, sym) => build_id)
12251225
end
1226-
totbytes = ntoh(read(f, Int64)) # total bytes for file dependencies
1226+
totbytes = read(f, Int64) # total bytes for file dependencies
12271227
# read the list of requirements
12281228
# and split the list into include and requires statements
12291229
includes = Tuple{PkgId, String, Float64}[]
12301230
requires = Pair{PkgId, PkgId}[]
12311231
while true
1232-
n2 = ntoh(read(f, Int32))
1232+
n2 = read(f, Int32)
12331233
n2 == 0 && break
12341234
depname = String(read(f, n2))
1235-
mtime = ntoh(read(f, Float64))
1236-
n1 = ntoh(read(f, Int32))
1235+
mtime = read(f, Float64)
1236+
n1 = read(f, Int32)
12371237
# map ids to keys
12381238
modkey = (n1 == 0) ? PkgId("") : modules[n1].first
12391239
if n1 != 0
12401240
# consume (and ignore) the module path too
12411241
while true
1242-
n1 = ntoh(read(f, Int32))
1242+
n1 = read(f, Int32)
12431243
totbytes -= 4
12441244
n1 == 0 && break
12451245
skip(f, n1) # String(read(f, n1))
@@ -1254,15 +1254,15 @@ function parse_cache_header(f::IO)
12541254
totbytes -= 4 + 4 + n2 + 8
12551255
end
12561256
@assert totbytes == 12 "header of cache file appears to be corrupt"
1257-
srctextpos = ntoh(read(f, Int64))
1257+
srctextpos = read(f, Int64)
12581258
# read the list of modules that are required to be present during loading
12591259
required_modules = Vector{Pair{PkgId, UInt64}}()
12601260
while true
1261-
n = ntoh(read(f, Int32))
1261+
n = read(f, Int32)
12621262
n == 0 && break
12631263
sym = String(read(f, n)) # module name
1264-
uuid = UUID((ntoh(read(f, UInt64)), ntoh(read(f, UInt64)))) # pkg UUID
1265-
build_id = ntoh(read(f, UInt64)) # build id
1264+
uuid = UUID((read(f, UInt64), read(f, UInt64))) # pkg UUID
1265+
build_id = read(f, UInt64) # build id
12661266
push!(required_modules, PkgId(uuid, sym) => build_id)
12671267
end
12681268
return modules, (includes, requires), required_modules, srctextpos
@@ -1302,10 +1302,10 @@ end
13021302

13031303
function _read_dependency_src(io::IO, filename::AbstractString)
13041304
while !eof(io)
1305-
filenamelen = ntoh(read(io, Int32))
1305+
filenamelen = read(io, Int32)
13061306
filenamelen == 0 && break
13071307
fn = String(read(io, filenamelen))
1308-
len = ntoh(read(io, UInt64))
1308+
len = read(io, UInt64)
13091309
if fn == filename
13101310
return String(read(io, len))
13111311
end

src/dump.c

+15-23
Original file line numberDiff line numberDiff line change
@@ -128,55 +128,47 @@ static arraylist_t builtin_typenames;
128128
#define write_int8(s, n) write_uint8(s, n)
129129
#define read_int8(s) read_uint8(s)
130130

131-
/* read and write in network (bigendian) order: */
131+
/* read and write in host byte order */
132132

133133
static void write_int32(ios_t *s, int32_t i)
134134
{
135-
write_uint8(s, (i>>24) & 0xff);
136-
write_uint8(s, (i>>16) & 0xff);
137-
write_uint8(s, (i>> 8) & 0xff);
138-
write_uint8(s, i & 0xff);
135+
ios_write(s, (char*)&i, 4);
139136
}
140137

141138
static int32_t read_int32(ios_t *s)
142139
{
143-
int b3 = read_uint8(s);
144-
int b2 = read_uint8(s);
145-
int b1 = read_uint8(s);
146-
int b0 = read_uint8(s);
147-
return b0 | (b1<<8) | (b2<<16) | (b3<<24);
140+
int32_t x = 0;
141+
ios_read(s, (char*)&x, 4);
142+
return x;
148143
}
149144

150145
static void write_uint64(ios_t *s, uint64_t i)
151146
{
152-
write_int32(s, (i>>32) & 0xffffffff);
153-
write_int32(s, i & 0xffffffff);
147+
ios_write(s, (char*)&i, 8);
154148
}
155149

156150
static uint64_t read_uint64(ios_t *s)
157151
{
158-
uint64_t b1 = (uint32_t)read_int32(s);
159-
uint64_t b0 = (uint32_t)read_int32(s);
160-
return b0 | (b1<<32);
152+
uint64_t x = 0;
153+
ios_read(s, (char*)&x, 8);
154+
return x;
161155
}
162156

163157
static void write_int64(ios_t *s, int64_t i)
164158
{
165-
write_int32(s, (i>>32) & 0xffffffff);
166-
write_int32(s, i & 0xffffffff);
159+
ios_write(s, (char*)&i, 8);
167160
}
168161

169162
static void write_uint16(ios_t *s, uint16_t i)
170163
{
171-
write_uint8(s, (i>> 8) & 0xff);
172-
write_uint8(s, i & 0xff);
164+
ios_write(s, (char*)&i, 2);
173165
}
174166

175167
static uint16_t read_uint16(ios_t *s)
176168
{
177-
int b1 = read_uint8(s);
178-
int b0 = read_uint8(s);
179-
return b0 | (b1<<8);
169+
int16_t x = 0;
170+
ios_read(s, (char*)&x, 2);
171+
return x;
180172
}
181173

182174
static void writetag(ios_t *s, void *v)
@@ -1067,7 +1059,7 @@ static void write_mod_list(ios_t *s, jl_array_t *a)
10671059
}
10681060

10691061
// "magic" string and version header of .ji file
1070-
static const int JI_FORMAT_VERSION = 5;
1062+
static const int JI_FORMAT_VERSION = 6;
10711063
static const char JI_MAGIC[] = "\373jli\r\n\032\n"; // based on PNG signature
10721064
static const uint16_t BOM = 0xFEFF; // byte-order marker
10731065
static void write_header(ios_t *s)

src/staticdata.c

+13-18
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,21 @@ enum RefTags {
107107
#define RELOC_TAG_OFFSET 28
108108

109109

110-
/* read and write in network (bigendian) order: */
110+
/* read and write in host byte order */
111111

112112
#define write_uint8(s, n) ios_putc((n), (s))
113113
#define read_uint8(s) ((uint8_t)ios_getc((s)))
114114

115115
static void write_uint32(ios_t *s, uint32_t i)
116116
{
117-
write_uint8(s, (i >> 24) & 0xff);
118-
write_uint8(s, (i >> 16) & 0xff);
119-
write_uint8(s, (i >> 8) & 0xff);
120-
write_uint8(s, i & 0xff);
117+
ios_write(s, (char*)&i, 4);
121118
}
122119

123120
static uint32_t read_uint32(ios_t *s)
124121
{
125-
uint32_t b3 = read_uint8(s);
126-
uint32_t b2 = read_uint8(s);
127-
uint32_t b1 = read_uint8(s);
128-
uint32_t b0 = read_uint8(s);
129-
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
122+
uint32_t x = 0;
123+
ios_read(s, (char*)&x, 4);
124+
return x;
130125
}
131126

132127

@@ -767,11 +762,11 @@ static void jl_write_gv_syms(jl_serializer_state *s, jl_sym_t *v)
767762
}
768763

769764

770-
static inline uint32_t load_uint32_be(uintptr_t *base)
765+
static inline uint32_t load_uint32(uintptr_t *base)
771766
{
772767
uint32_t v = **(uint32_t**)base;
773768
*base += 4;
774-
return ntohl(v);
769+
return v;
775770
}
776771

777772

@@ -781,7 +776,7 @@ static void jl_read_symbols(jl_serializer_state *s)
781776
uintptr_t base = (uintptr_t)&s->symbols->buf[0];
782777
uintptr_t end = base + s->symbols->size;
783778
while (base < end) {
784-
uint32_t len = load_uint32_be(&base);
779+
uint32_t len = load_uint32(&base);
785780
const char *str = (const char*)base;
786781
base += len + 1;
787782
//printf("symbol %3d: %s\n", len, str);
@@ -884,7 +879,7 @@ static void jl_read_relocations(jl_serializer_state *s, uint8_t bits)
884879
size_t size = s->s->size;
885880
while (1) {
886881
uintptr_t val = (uintptr_t)&s->relocs->buf[s->relocs->bpos];
887-
uint32_t offset = load_uint32_be(&val);
882+
uint32_t offset = load_uint32(&val);
888883
s->relocs->bpos += sizeof(uint32_t);
889884
if (offset == 0)
890885
break;
@@ -904,7 +899,7 @@ void gc_sweep_sysimg(void)
904899
if (relocs == 0)
905900
return;
906901
while (1) {
907-
uint32_t offset = load_uint32_be(&relocs);
902+
uint32_t offset = load_uint32(&relocs);
908903
if (offset == 0)
909904
break;
910905
jl_taggedvalue_t *o = (jl_taggedvalue_t*)(base + offset);
@@ -931,7 +926,7 @@ static jl_value_t *jl_read_value(jl_serializer_state *s)
931926
uintptr_t base = (uintptr_t)&s->s->buf[0];
932927
size_t size = s->s->size;
933928
uintptr_t val = base + s->s->bpos;
934-
uint32_t offset = load_uint32_be(&val);
929+
uint32_t offset = load_uint32(&val);
935930
s->s->bpos += sizeof(uint32_t);
936931
if (offset == 0)
937932
return NULL;
@@ -952,7 +947,7 @@ static void jl_update_all_fptrs(jl_serializer_state *s)
952947
uint32_t clone_idx = 0;
953948
for (i = 0; i < sysimg_fvars_max; i++) {
954949
uintptr_t val = (uintptr_t)&linfos[i];
955-
uint32_t offset = load_uint32_be(&val);
950+
uint32_t offset = load_uint32(&val);
956951
if (offset != 0) {
957952
int cfunc = 0;
958953
if (offset & ((uintptr_t)1 << (8 * sizeof(uint32_t) - 1))) {
@@ -997,7 +992,7 @@ static void jl_update_all_gvars(jl_serializer_state *s)
997992
uintptr_t gvars = (uintptr_t)&s->gvar_record->buf[0];
998993
uintptr_t end = gvars + s->gvar_record->size;
999994
while (gvars < end) {
1000-
uint32_t offset = load_uint32_be(&gvars);
995+
uint32_t offset = load_uint32(&gvars);
1001996
if (offset) {
1002997
uintptr_t v = get_item_for_reloc(s, base, size, offset);
1003998
*sysimg_gvars(sysimg_gvars_base, gvname_index) = v;

0 commit comments

Comments
 (0)