Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build CDHASH properly when loading iseq from binary #4511

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -10758,7 +10758,6 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod
/* operands */
for (op_index=0; types[op_index]; op_index++, code_index++) {
switch (types[op_index]) {
case TS_CDHASH:
case TS_VALUE:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
Expand All @@ -10770,6 +10769,20 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod
}
break;
}
case TS_CDHASH:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
VALUE v = ibf_load_object(load, op);
v = rb_hash_dup(v); // hash dumped as frozen
RHASH_TBL_RAW(v)->type = &cdhash_type;
rb_hash_rehash(v); // hash function changed
freeze_hide_obj(v);

code[code_index] = v;
RB_OBJ_WRITTEN(iseqv, Qundef, v);
FL_SET(iseqv, ISEQ_MARKABLE_ISEQ);
break;
}
case TS_ISEQ:
{
VALUE op = (VALUE)ibf_load_small_value(load, &reading_pos);
Expand Down
13 changes: 13 additions & 0 deletions test/ruby/test_iseq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ def bug(kw)
end;
end if defined?(RubyVM::InstructionSequence.load)

def test_cdhash_after_roundtrip
# CDHASH was not built properly when loading from binary and
# was causing opt_case_dispatch to clobber its stack canary
# for its "leaf" instruction attribute.
iseq = compile(<<~EOF)
case Class.new(String).new("foo")
when "foo"
42
end
EOF
assert_equal(42, ISeq.load_from_binary(iseq.to_binary).eval)
end

def test_disasm_encoding
src = "\u{3042} = 1; \u{3042}; \u{3043}"
asm = compile(src).disasm
Expand Down