Skip to content

Commit 7c99e43

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Ensure serialized file is little endian
ruby/prism@0c762ee68a
1 parent cbdac2f commit 7c99e43

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

prism/defines.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,14 @@
7474
# define snprintf _snprintf
7575
#endif
7676

77+
/**
78+
* Defined PRISM_WORDS_BIGENDIAN so we can ensure our serialization happens in
79+
* little endian format regardless of platform.
80+
*/
81+
#if defined(WORDS_BIGENDIAN)
82+
# define PRISM_WORDS_BIGENDIAN
83+
#elif defined(AC_APPLE_UNIVERSAL_BUILD) && defined(__BIG_ENDIAN__)
84+
# define PRISM_WORDS_BIGENDIAN
85+
#endif
86+
7787
#endif

prism/templates/lib/prism/serialize.rb.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module Prism
137137

138138
comments, magic_comments, errors, warnings = load_metadata
139139

140-
@constant_pool_offset = io.read(4).unpack1("L")
140+
@constant_pool_offset = io.read(4).unpack1("L<")
141141
@constant_pool = Array.new(load_varint, nil)
142142

143143
[load_node, comments, magic_comments, errors, warnings]
@@ -167,7 +167,7 @@ module Prism
167167
end
168168

169169
def load_serialized_length
170-
io.read(4).unpack1("L")
170+
io.read(4).unpack1("L<")
171171
end
172172

173173
def load_optional_node
@@ -206,8 +206,8 @@ module Prism
206206

207207
unless constant
208208
offset = constant_pool_offset + index * 8
209-
start = serialized.unpack1("L", offset: offset)
210-
length = serialized.unpack1("L", offset: offset + 4)
209+
start = serialized.unpack1("L<", offset: offset)
210+
length = serialized.unpack1("L<", offset: offset + 4)
211211

212212
constant =
213213
if start.nobits?(1 << 31)

prism/templates/src/serialize.c.erb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ pm_serialize_string(pm_parser_t *parser, pm_string_t *string, pm_buffer_t *buffe
4747
}
4848
}
4949

50+
/**
51+
* Serialize a 32-bit integer to the given address always in little-endian.
52+
*/
53+
static void
54+
pm_serialize_32(char *address, uint32_t value) {
55+
#ifdef PRISM_WORDS_BIGENDIAN
56+
address[0] = (char) ((value >> 24) & 0xFF);
57+
address[1] = (char) ((value >> 16) & 0xFF);
58+
address[2] = (char) ((value >> 8) & 0xFF);
59+
address[3] = (char) (value & 0xFF);
60+
#else
61+
memcpy(address, &value, sizeof(uint32_t));
62+
#endif
63+
}
64+
5065
static void
5166
pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
5267
pm_buffer_append_byte(buffer, (uint8_t) PM_NODE_TYPE(node));
@@ -118,7 +133,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
118133
<%- if node.needs_serialized_length? -%>
119134
// serialize length
120135
uint32_t length = pm_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
121-
memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
136+
pm_serialize_32(buffer->value + length_offset, length);
122137
<%- end -%>
123138
break;
124139
}
@@ -231,7 +246,7 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
231246
// Now we're going to serialize the offset of the constant pool back where
232247
// we left space for it.
233248
uint32_t length = pm_sizet_to_u32(buffer->length);
234-
memcpy(buffer->value + offset, &length, sizeof(uint32_t));
249+
pm_serialize_32(buffer->value + offset, length);
235250

236251
// Now we're going to serialize the constant pool.
237252
offset = buffer->length;
@@ -258,18 +273,18 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
258273
assert(content_offset < owned_mask);
259274
content_offset |= owned_mask;
260275

261-
memcpy(buffer->value + buffer_offset, &content_offset, 4);
276+
pm_serialize_32(buffer->value + buffer_offset, content_offset);
262277
pm_buffer_append_bytes(buffer, constant->start, constant->length);
263278
} else {
264279
// Since this is a shared constant, we are going to write its
265280
// source offset directly into the buffer.
266281
uint32_t source_offset = pm_ptrdifft_to_u32(constant->start - parser->start);
267-
memcpy(buffer->value + buffer_offset, &source_offset, 4);
282+
pm_serialize_32(buffer->value + buffer_offset, source_offset);
268283
}
269284

270285
// Now we can write the length of the constant into the buffer.
271286
uint32_t constant_length = pm_sizet_to_u32(constant->length);
272-
memcpy(buffer->value + buffer_offset + 4, &constant_length, 4);
287+
pm_serialize_32(buffer->value + buffer_offset + 4, constant_length);
273288
}
274289
}
275290
}

0 commit comments

Comments
 (0)