8000 compile.c: align IBF dumped data · willnet/ruby@b6185e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit b6185e1

Browse files
committed
compile.c: align IBF dumped data
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63113 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 23228b6 commit b6185e1

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

compile.c

Lines changed: 55 additions & 39 deletions
< E7EE /tr>
Original file line numberDiff line numberDiff line change
@@ -8272,7 +8272,9 @@ ibf_load_alloc(const struct ibf_load *load, ibf_offset_t offset, int size)
82728272
return buff;
82738273
}
82748274

8275-
#define IBF_W(b, type, n) (type *)(VALUE)ibf_dump_write(dump, (b), sizeof(type) * (n))
8275+
#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
8276+
8277+
#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
82768278
#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
82778279
#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
82788280
#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type) * (n))
@@ -8798,7 +8800,8 @@ ibf_dump_iseq_each(struct ibf_dump *dump, const rb_iseq_t *iseq)
87988800
dump_body.variable.coverage = Qnil;
87998801
dump_body.variable.original_iseq = Qnil;
88008802

8801-
return ibf_dump_write(dump, &dump_body, sizeof(dump_body));
8803+
IBF_W_ALIGN(struct rb_iseq_constant_body);
8804+
return IBF_WV(dump_body);
88028805
}
88038806

88048807
static VALUE
@@ -9009,7 +9012,10 @@ struct ibf_object_symbol {
90099012
};
90109013

90119014
#define IBF_OBJHEADER(offset) (struct ibf_object_header *)(load->buff + (offset))
9012-
#define IBF_OBJBODY(type, offset) (type *)(load->buff + sizeof(struct ibf_object_header) + (offset))
9015+
#define IBF_OBJBODY(type, offset) (type *)(load->buff + IBF_OBJALIGNED(type, offset))
9016+
#define IBF_OBJALIGNED(type, offset) \
9017+
(((sizeof(struct ibf_object_header) + (offset) - 1) / RUBY_ALIGNOF(type) + 1) * \
9018+
RUBY_ALIGNOF(type))
90139019

90149020
NORETURN(static void ibf_dump_object_unsupported(struct ibf_dump *dump, VALUE obj));
90159021

@@ -9071,7 +9077,7 @@ static void
90719077
ibf_dump_object_float(struct ibf_dump *dump, VALUE obj)
90729078
{
90739079
double dbl = RFLOAT_VALUE(obj);
9074-
ibf_dump_write(dump, &dbl, sizeof(dbl));
9080+
IBF_W(&dbl, double, 1);
90759081
}
90769082

90779083
static VALUE
@@ -9087,15 +9093,17 @@ ibf_dump_object_string(struct ibf_dump *dump, VALUE obj)
90879093
long encindex = (long)rb_enc_get_index(obj);
90889094
long len = RSTRING_LEN(obj);
90899095
const char *ptr = RSTRING_PTR(obj);
9096+
long buff[2];
90909097

90919098
if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
90929099
rb_encoding *enc = rb_enc_from_index((int)encindex);
90939100
const char *enc_name = rb_enc_name(enc);
90949101
encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump, rb_str_new2(enc_name));
90959102
}
90969103

9097-
IBF_WV(encindex);
9098-
IBF_WV(len);
9104+
buff[0] = encindex;
9105+
buff[1] = len;
9106+
IBF_W(buff, long, 2);
90999107
IBF_WP(ptr, char, len);
91009108
}
91019109

@@ -9126,7 +9134,7 @@ ibf_dump_object_regexp(struct ibf_dump *dump, VALUE obj)
91269134
IBF_ZERO(regexp);
91279135
regexp.option = (char)rb_reg_options(obj);
91289136
regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
9129-
IBF_WV(regexp);
9137+
IBF_W(&regexp, struct ibf_object_regexp, 1);
91309138
}
91319139

91329140
static VALUE
@@ -9146,7 +9154,7 @@ static void
91469154
ibf_dump_object_array(struct ibf_dump *dump, VALUE obj)
91479155
{
91489156
long i, len = (int)RARRAY_LEN(obj);
9149-
IBF_WV(len);
9157+
IBF_W(&len, long, 1);
91509158
for (i=0; i<len; i++) {
91519159
long index = (long)ibf_dump_object(dump, RARRAY_AREF(obj, i));
91529160
IBF_WV(index);
@@ -9174,18 +9182,18 @@ static int
91749182
ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
91759183
{
91769184
struct ibf_dump *dump = (struct ibf_dump *)ptr;
9177-
long key_index = (long)ibf_dump_object(dump, (VALUE)key);
9178-
long val_index = (long)ibf_dump_object(dump, (VALUE)val);
9179-
IBF_WV(key_index);
9180-
IBF_WV(val_index);
9185+
long keyval[2];
9186+
keyval[0] = (long)ibf_dump_object(dump, (VALUE)key);
9187+
keyval[1] = (long)ibf_dump_object(dump, (VALUE)val);
9188+
IBF_W(keyval, long, 2);
91819189
return ST_CONTINUE;
91829190
}
91839191

91849192
static void
91859193
ibf_dump_object_hash(struct ibf_dump *dump, VALUE obj)
91869194
{
91879195
long len = RHASH_SIZE(obj);
9188-
IBF_WV(len);
9196+
IBF_W(&len, long, 1);
91899197
if (len > 0) st_foreach(RHASH(obj)->ntbl, ibf_dump_object_hash_i, (st_data_t)dump);
91909198
}
91919199

@@ -9249,7 +9257,7 @@ ibf_dump_object_bignum(struct ibf_dump *dump, VALUE obj)
92499257
ssize_t slen = BIGNUM_SIGN(obj) > 0 ? len : len * -1;
92509258
BDIGIT *d = BIGNUM_DIGITS(obj);
92519259

9252-
IBF_WV(slen);
9260+
IBF_W(&slen, ssize_t, 1);
92539261
IBF_WP(d, BDIGIT, len);
92549262
}
92559263

@@ -9272,10 +9280,11 @@ ibf_dump_object_data(struct ibf_dump *dump, VALUE obj)
92729280
if (rb_data_is_encoding(obj)) {
92739281
rb_encoding *enc = rb_to_encoding(obj);
92749282
const char *name = rb_enc_name(enc);
9275-
enum ibf_object_data_type type = IBF_OBJECT_DATA_ENCODING;
92769283
long len = strlen(name) + 1;
9277-
IBF_WV(type);
9278-
IBF_WV(len);
9284+
long data[2];
9285+
data[0] = IBF_OBJECT_DATA_ENCODING;
9286+
data[1] = len;
9287+
IBF_W(data, long, 2);
92799288
IBF_WP(name, char, len);
92809289
}
92819290
else {
@@ -9304,11 +9313,11 @@ ibf_load_object_data(const struct ibf_load *load, const struct ibf_object_header
93049313
static void
93059314
ibf_dump_object_complex_rational(struct ibf_dump *dump, VALUE obj)
93069315
{
9307-
long real = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
9308-
long imag = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
9316+
long data[2];
9317+
data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
9318+
data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
93099319

9310-
IBF_WV(real);
9311-
IBF_WV(imag);
9320+
IBF_W(data, long, 2);
93129321
}
93139322

93149323
static VALUE
@@ -9330,7 +9339,7 @@ ibf_dump_object_symbol(struct ibf_dump *dump, VALUE obj)
93309339
{
93319340
VALUE str = rb_sym2str(obj);
93329341
long str_index = (long)ibf_dump_object(dump, str);
9333-
IBF_WV(str_index);
9342+
IBF_W(&str_index, long, 1);
93349343
}
93359344

93369345
static VALUE
@@ -9376,7 +9385,7 @@ static ibf_dump_object_function dump_object_functions[RUBY_T_MASK+1] = {
93769385
ibf_dump_object_unsupported, /* T_ICLASS 0x1c */
93779386
ibf_dump_object_unsupported, /* T_ZOMBIE 0x1d */
93789387
ibf_dump_object_unsupported, /* 0x1e */
9379-
ibf_dump_object_unsupported /* 0x1f */
9388+
ibf_dump_object_unsupported, /* 0x1f */
93809389
};
93819390

93829391
static ibf_offset_t
@@ -9397,7 +9406,7 @@ ibf_dump_object_object(struct ibf_dump *dump, VALUE obj)
93979406
obj_header.frozen = TRUE;
93989407
obj_header.internal = TRUE;
93999408
IBF_WV(obj_header);
9400-
IBF_WV(obj);
9409+
IBF_W(&obj, VALUE, 1);
94019410
}
94029411
else {
94039412
obj_header.internal = (RBASIC_CLASS(obj) == 0) ? TRUE : FALSE;
@@ -9444,7 +9453,7 @@ static ibf_load_object_function load_object_functions[RUBY_T_MASK+1] = {
94449453
ibf_load_object_unsupported, /* T_ICLASS 0x1c */
94459454
ibf_load_object_unsupported, /* T_ZOMBIE 0x1d */
94469455
ibf_load_object_unsupported, /* 0x1e */
9447-
ibf_load_object_unsupported /* 0x1f */
9456+
ibf_load_object_unsupported, /* 0x1f */
94489457
};
94499458

94509459
static VALUE
@@ -9489,7 +9498,7 @@ ibf_dump_object_list(struct ibf_dump *dump, struct ibf_header *header)
94899498
rb_ary_push(list, UINT2NUM(offset));
94909499
}
94919500
size = i;
9492-
ibf_dump_align(dump, sizeof(ibf_offset_t));
9501+
IBF_W_ALIGN(ibf_offset_t);
94939502
header->object_list_offset = ibf_dump_pos(dump);
94949503

94959504
for (i=0; i<size; i++) {
@@ -9609,10 +9618,6 @@ iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt)
96099618
static const ibf_offset_t *
96109619
ibf_iseq_list(const struct ibf_load *load)
96119620
{
9612-
if (load->header->iseq_list_offset % sizeof(ibf_offset_t)) {
9613-
rb_raise(rb_eArgError, "unaligned iseq list offset: %u",
9614-
load->header->iseq_list_offset);
9615-
}
96169621
return (ibf_offset_t *)(load->buff + load->header->iseq_list_offset);
96179622
}
96189623

@@ -9621,19 +9626,18 @@ ibf_load_iseq_complete(rb_iseq_t *iseq)
96219626
{
96229627
struct ibf_load *load = RTYPEDDATA_DATA(iseq->aux.loader.obj);
96239628
rb_iseq_t *prev_src_iseq = load->iseq;
9629+
const ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
96249630
load->iseq = iseq;
96259631
#if IBF_ISEQ_DEBUG
9626-
fprintf(stderr, "ibf_load_iseq_complete: load=%p iseq=%p prev=%p\n",
9627-
load, iseq, prev_src_iseq);
9628-
fprintf(stderr, "ibf_load_iseq_complete: list=%p(%p+%#x) index=%i/%u\n",
9629-
ibf_iseq_list(load),
9630-
load->buff, load->header->iseq_list_offset,
9631-
iseq->aux.loader.index, load->header->iseq_list_size);
9632-
fprintf(stderr, "ibf_load_iseq_complete: offset=%#x size=%#x\n",
9633-
ibf_iseq_list(load)[iseq->aux.loader.index],
9632+
fprintf(stderr, "ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
9633+
iseq->aux.loader.index, offset,
96349634
load->header->size);
96359635
#endif
9636-
ibf_load_iseq_each(load, iseq, ibf_iseq_list(load)[iseq->aux.loader.index]);
9636+
if (offset % sizeof(VALUE)) {
9637+
rb_raise(rb_eArgError, "unaligned iseq offset: %#x @ %u",
9638+
offset, iseq->aux.loader.index);
9639+
}
9640+
ibf_load_iseq_each(load, iseq, offset);
96379641
ISEQ_COMPILE_DATA_CLEAR(iseq);
96389642
FL_UNSET(iseq, ISEQ_NOT_LOADED_YET);
96399643
load->iseq = prev_src_iseq;
@@ -9730,6 +9734,18 @@ ibf_load_setup(struct ibf_load *load, VALUE loader_obj, VALUE str)
97309734
if (strcmp(load->buff + sizeof(struct ibf_header), RUBY_PLATFORM) != 0) {
97319735
rb_raise(rb_eRuntimeError, "unmatched platform");
97329736
}
9737+
if (load->header->iseq_list_offset % RUBY_ALIGNOF(ibf_offset_t)) {
9738+
rb_raise(rb_eArgError, "unaligned iseq list offset: %u",
9739+
load->header->iseq_list_offset);
9740+
}
9741+
if (load->header->id_list_offset % RUBY_ALIGNOF(long)) {
9742+
rb_raise(rb_eArgError, "unaligned ID list offset: %u",
9743+
load->header->id_list_offset);
9744+
}
9745+
if (load->header->object_list_offset % RUBY_ALIGNOF(ibf_offset_t)) {
9746+
rb_raise(rb_eArgError, "unaligned object list offset: %u",
9747+
load->header->object_list_offset);
9748+
}
97339749
}
97349750

97359751
static void

test/ruby/test_iseq.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ def hexdump(bin)
403403
end
404404

405405
def assert_iseq_to_binary(code, mesg = nil)
406-
skip "does not work on other than x86" unless /x(?:86|64)|i\d86/ =~ RUBY_PLATFORM
407406
iseq = RubyVM::InstructionSequence.compile(code)
408407
bin = assert_nothing_raised(mesg) do
409408
iseq.to_binary

0 commit comments

Comments
 (0)
0