@@ -8272,7 +8272,9 @@ ibf_load_alloc(const struct ibf_load *load, ibf_offset_t offset, int size)
8272
8272
return buff ;
8273
8273
}
8274
8274
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))
8276
8278
#define IBF_WV (variable ) ibf_dump_write(dump, &(variable), sizeof(variable))
8277
8279
#define IBF_WP (b , type , n ) ibf_dump_write(dump, (b), sizeof(type) * (n))
8278
8280
#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)
8798
8800
dump_body .variable .coverage = Qnil ;
8799
8801
dump_body .variable .original_iseq = Qnil ;
8800
8802
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 );
8802
8805
}
8803
8806
8804
8807
static VALUE
@@ -9009,7 +9012,10 @@ struct ibf_object_symbol {
9009
9012
};
9010
9013
9011
9014
#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))
9013
9019
9014
9020
NORETURN (static void ibf_dump_object_unsupported (struct ibf_dump * dump , VALUE obj ));
9015
9021
@@ -9071,7 +9077,7 @@ static void
9071
9077
ibf_dump_object_float (struct ibf_dump * dump , VALUE obj )
9072
9078
{
9073
9079
double dbl = RFLOAT_VALUE (obj );
9074
- ibf_dump_write ( dump , & dbl , sizeof ( dbl ) );
9080
+ IBF_W ( & dbl , double , 1 );
9075
9081
}
9076
9082
9077
9083
static VALUE
@@ -9087,15 +9093,17 @@ ibf_dump_object_string(struct ibf_dump *dump, VALUE obj)
9087
9093
long encindex = (long )rb_enc_get_index (obj );
9088
9094
long len = RSTRING_LEN (obj );
9089
9095
const char * ptr = RSTRING_PTR (obj );
9096
+ long buff [2 ];
9090
9097
9091
9098
if (encindex > RUBY_ENCINDEX_BUILTIN_MAX ) {
9092
9099
rb_encoding * enc = rb_enc_from_index ((int )encindex );
9093
9100
const char * enc_name = rb_enc_name (enc );
<
E7EE
/tr>9094
9101
encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object (dump , rb_str_new2 (enc_name ));
9095
9102
}
9096
9103
9097
- IBF_WV (encindex );
9098
- IBF_WV (len );
9104
+ buff [0 ] = encindex ;
9105
+ buff [1 ] = len ;
9106
+ IBF_W (buff , long , 2 );
9099
9107
IBF_WP (ptr , char , len );
9100
9108
}
9101
9109
@@ -9126,7 +9134,7 @@ ibf_dump_object_regexp(struct ibf_dump *dump, VALUE obj)
9126
9134
IBF_ZERO (regexp );
9127
9135
regexp .option = (char )rb_reg_options (obj );
9128
9136
regexp .srcstr = (long )ibf_dump_object (dump , srcstr );
9129
- IBF_WV ( regexp );
9137
+ IBF_W ( & regexp , struct ibf_object_regexp , 1 );
9130
9138
}
9131
9139
9132
9140
static VALUE
@@ -9146,7 +9154,7 @@ static void
9146
9154
ibf_dump_object_array (struct ibf_dump * dump , VALUE obj )
9147
9155
{
9148
9156
long i , len = (int )RARRAY_LEN (obj );
9149
- IBF_WV ( len );
9157
+ IBF_W ( & len , long , 1 );
9150
9158
for (i = 0 ; i < len ; i ++ ) {
9151
9159
long index = (long )ibf_dump_object (dump , RARRAY_AREF (obj , i ));
9152
9160
IBF_WV (index );
@@ -9174,18 +9182,18 @@ static int
9174
9182
ibf_dump_object_hash_i (st_data_t key , st_data_t val , st_data_t ptr )
9175
9183
{
9176
9184
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 );
9181
9189
return ST_CONTINUE ;
9182
9190
}
9183
9191
9184
9192
static void
9185
9193
ibf_dump_object_hash (struct ibf_dump * dump , VALUE obj )
9186
9194
{
9187
9195
long len = RHASH_SIZE (obj );
9188
- IBF_WV ( len );
9196
+ IBF_W ( & len , long , 1 );
9189
9197
if (len > 0 ) st_foreach (RHASH (obj )-> ntbl , ibf_dump_object_hash_i , (st_data_t )dump );
9190
9198
}
9191
9199
@@ -9249,7 +9257,7 @@ ibf_dump_object_bignum(struct ibf_dump *dump, VALUE obj)
9249
9257
ssize_t slen = BIGNUM_SIGN (obj ) > 0 ? len : len * -1 ;
9250
9258
BDIGIT * d = BIGNUM_DIGITS (obj );
9251
9259
9252
- IBF_WV ( slen );
9260
+ IBF_W ( & slen , ssize_t , 1 );
9253
9261
IBF_WP (d , BDIGIT , len );
9254
9262
}
9255
9263
@@ -9272,10 +9280,11 @@ ibf_dump_object_data(struct ibf_dump *dump, VALUE obj)
9272
9280
if (rb_data_is_encoding (obj )) {
9273
9281
rb_encoding * enc = rb_to_encoding (obj );
9274
9282
const char * name = rb_enc_name (enc );
9275
- enum ibf_object_data_type type = IBF_OBJECT_DATA_ENCODING ;
9276
9283
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 );
9279
9288
IBF_WP (name , char , len );
9280
9289
}
9281
9290
else {
@@ -9304,11 +9313,11 @@ ibf_load_object_data(const struct ibf_load *load, const struct ibf_object_header
9304
9313
static void
9305
9314
ibf_dump_object_complex_rational (struct ibf_dump * dump , VALUE obj )
9306
9315
{
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 );
9309
9319
9310
- IBF_WV (real );
9311
- IBF_WV (imag );
9320
+ IBF_W (data , long , 2 );
9312
9321
}
9313
9322
9314
9323
static VALUE
@@ -9330,7 +9339,7 @@ ibf_dump_object_symbol(struct ibf_dump *dump, VALUE obj)
9330
9339
{
9331
9340
VALUE str = rb_sym2str (obj );
9332
9341
long str_index = (long )ibf_dump_object (dump , str );
9333
- IBF_WV ( str_index );
9342
+ IBF_W ( & str_index , long , 1 );
9334
9343
}
9335
9344
9336
9345
static VALUE
@@ -9376,7 +9385,7 @@ static ibf_dump_object_function dump_object_functions[RUBY_T_MASK+1] = {
9376
9385
ibf_dump_object_unsupported , /* T_ICLASS 0x1c */
9377
9386
ibf_dump_object_unsupported , /* T_ZOMBIE 0x1d */
9378
9387
ibf_dump_object_unsupported , /* 0x1e */
9379
- ibf_dump_object_unsupported /* 0x1f */
9388
+ ibf_dump_object_unsupported , /* 0x1f */
9380
9389
};
9381
9390
9382
9391
static ibf_offset_t
@@ -9397,7 +9406,7 @@ ibf_dump_object_object(struct ibf_dump *dump, VALUE obj)
9397
9406
obj_header .frozen = TRUE;
9398
9407
obj_header .internal = TRUE;
9399
9408
IBF_WV (obj_header );
9400
- IBF_WV ( obj );
9409
+ IBF_W ( & obj , VALUE , 1 );
9401
9410
}
9402
9411
else {
9403
9412
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] = {
9444
9453
ibf_load_object_unsupported , /* T_ICLASS 0x1c */
9445
9454
ibf_load_object_unsupported , /* T_ZOMBIE 0x1d */
9446
9455
ibf_load_object_unsupported , /* 0x1e */
9447
- ibf_load_object_unsupported /* 0x1f */
9456
+ ibf_load_object_unsupported , /* 0x1f */
9448
9457
};
9449
9458
9450
9459
static VALUE
@@ -9489,7 +9498,7 @@ ibf_dump_object_list(struct ibf_dump *dump, struct ibf_header *header)
9489
9498
rb_ary_push (list , UINT2NUM (offset ));
9490
9499
}
9491
9500
size = i ;
9492
- ibf_dump_align ( dump , sizeof ( ibf_offset_t ) );
9501
+ IBF_W_ALIGN ( ibf_offset_t );
9493
9502
header -> object_list_offset = ibf_dump_pos (dump );
9494
9503
9495
9504
for (i = 0 ; i < size ; i ++ ) {
@@ -9609,10 +9618,6 @@ iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt)
9609
9618
static const ibf_offset_t *
9610
9619
ibf_iseq_list (const struct ibf_load * load )
9611
9620
{
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
- }
9616
9621
return (ibf_offset_t * )(load -> buff + load -> header -> iseq_list_offset );
9617
9622
}
9618
9623
@@ -9621,19 +9626,18 @@ ibf_load_iseq_complete(rb_iseq_t *iseq)
9621
9626
{
9622
9627
struct ibf_load * load = RTYPEDDATA_DATA (iseq-> aux .loader .obj );
9623
9628
rb_iseq_t * prev_src_iseq = load -> iseq ;
9629
+ const ibf_offset_t offset = ibf_iseq_list (load )[iseq -> aux .loader .index ];
9624
9630
load -> iseq = iseq ;
9625
9631
#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 ,
9634
9634
load -> header -> size );
9635
9635
#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 );
9637
9641
ISEQ_COMPILE_DATA_CLEAR (iseq );
9638
9642
FL_UNSET (iseq , ISEQ_NOT_LOADED_YET );
9639
9643
load -> iseq = prev_src_iseq ;
@@ -9730,6 +9734,18 @@ ibf_load_setup(struct ibf_load *load, VALUE loader_obj, VALUE str)
9730
9734
if (strcmp (load -> buff + sizeof (struct ibf_header ), RUBY_PLATFORM ) != 0 ) {
9731
9735
rb_raise (rb_eRuntimeError , "unmatched platform" );
9732
9736
}
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
+ }
9733
9749
}
9734
9750
9735
9751
static void
0 commit comments