8000 compile.c: align ibf_object_header · willnet/ruby@0f8368c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f8368c

Browse files
committed
compile.c: align ibf_object_header
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 89a6a4e commit 0f8368c

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

compile.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8958,6 +8958,8 @@ struct ibf_object_header {
89588958
unsigned int frozen: 1;
89598959
unsigned int internal: 1;
89608960
};
8961+
static const size_t ibf_object_header_align =
8962+
RUBY_ALIGNOF(struct ibf_object_header);
89618963

89628964
enum ibf_object_class_index {
89638965
IBF_OBJECT_CLASS_OBJECT,
@@ -9011,11 +9013,21 @@ struct ibf_object_symbol {
90119013
long str;
90129014
};
90139015

9014-
#define IBF_OBJHEADER(offset) (struct ibf_object_header *)(load->buff + (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))
9016+
#define IBF_ALIGNED_OFFSET(align, offset) /* offset > 0 */ \
9017+
((((offset) - 1) / (align) + 1) * (align))
9018+
#define IBF_OBJHEADER(offset) (const struct ibf_object_header *)\
9019+
ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(ibf_object_header_align, offset))
9020+
#define IBF_OBJBODY(type, offset) (const type *)\
9021+
ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
9022+
9023+
static const void *
9024+
ibf_load_check_offset(const struct ibf_load *load, size_t offset)
9025+
{
9026+
if (offset >= (size_t)RSTRING_LEN(load->str)) {
9027+
rb_raise(rb_eIndexError, "object offset out of range: %"PRIdSIZE, offset);
9028+
}
9029+
return load->buff + offset;
9030+
}
90199031

90209032
NORETURN(static void ibf_dump_object_unsupported(struct ibf_dump *dump, VALUE obj));
90219033

@@ -9057,7 +9069,7 @@ ibf_dump_object_class(struct ibf_dump *dump, VALUE obj)
90579069
static VALUE
90589070
ibf_load_object_class(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
90599071
{
9060-
enum ibf_object_class_index *cindexp = IBF_OBJBODY(enum ibf_object_class_index, offset);
9072+
const enum ibf_object_class_index *cindexp = IBF_OBJBODY(enum ibf_object_class_index, offset);
90619073
enum ibf_object_class_index cindex = *cindexp;
90629074

90639075
switch (cindex) {
@@ -9083,7 +9095,7 @@ ibf_dump_object_float(struct ibf_dump *dump, VALUE obj)
90839095
static VALUE
90849096
ibf_load_object_float(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
90859097
{
9086-
double *dblp = IBF_OBJBODY(double, offset);
9098+
const double *dblp = IBF_OBJBODY(double, offset);
90879099
return DBL2NUM(*dblp);
90889100
}
90899101

@@ -9295,11 +9307,12 @@ ibf_dump_object_data(struct ibf_dump *dump, VALUE obj)
92959307
static VALUE
92969308
ibf_load_object_data(const struct ibf_load *load, const struct ibf_object_header *header, ibf_offset_t offset)
92979309
{
9298-
const enum ibf_object_data_type *typep = IBF_OBJBODY(enum ibf_object_data_type, offset);
9299-
/* const long *lenp = IBF_OBJBODY(long, offset + sizeof(enum ibf_object_data_type)); */
9300-
const char *data = IBF_OBJBODY(char, offset + sizeof(enum ibf_object_data_type) + sizeof(long));
9310+
const long *body = IBF_OBJBODY(long, offset);
9311+
const enum ibf_object_data_type type = (enum ibf_object_data_type)body[0];
9312+
/* const long len = body[1]; */
9313+
const char *data = (const char *)&body[2];
93019314

9302-
switch (*typep) {
9315+
switch (type) {
93039316
case IBF_OBJECT_DATA_ENCODING:
93049317
{
93059318
VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
@@ -9392,10 +9405,13 @@ static ibf_offset_t
93929405
ibf_dump_object_object(struct ibf_dump *dump, VALUE obj)
93939406
{
93949407
struct ibf_object_header obj_header;
9395-
ibf_offset_t current_offset = ibf_dump_pos(dump);
9408+
ibf_offset_t current_offset;
93969409
IBF_ZERO(obj_header);
93979410
obj_header.type = TYPE(obj);
93989411

9412+
ibf_dump_align(dump, sizeof(ibf_offset_t));
9413+
current_offset = ibf_dump_pos(dump);
9414+
93999415
if (SPECIAL_CONST_P(obj)) {
94009416
if (RB_TYPE_P(obj, T_SYMBOL) ||
94019417
RB_TYPE_P(obj, T_FLOAT)) {
@@ -9478,8 +9494,13 @@ ibf_load_object(const struct ibf_load *load, VALUE object_index)
94789494
fprintf(stderr, "ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
94799495
header->type, header->special_const, header->frozen, header->internal);
94809496
#endif
9497+
if ((const char *)(header + 1) - load->buff >= RSTRING_LEN(load->str)) {
9498+
rb_raise( 6C43 rb_eIndexError, "object offset out of range: %"PRIdSIZE, offset);
9499+
}
9500+
offset = (ibf_offset_t)((const char *)(header + 1) - load->buff);
9501+
94819502
if (header->special_const) {
9482-
VALUE *vp = IBF_OBJBODY(VALUE, offset);
9503+
const VALUE *vp = IBF_OBJBODY(VALUE, offset);
94839504
#if IBF_ISEQ_DEBUG
94849505
fprintf(stderr, "ibf_load_object: vp=%p\n", vp);
94859506
#endif

0 commit comments

Comments
 (0)
0