Skip to content

Commit

Permalink
Fix object movement bugs (#638)
Browse files Browse the repository at this point in the history
* Move objects around to check for movement bugs

* Pin objects saved to C globals in init routines
  • Loading branch information
XrXr authored Jan 23, 2021
1 parent 7ee1236 commit 6786984
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/oj/mimic_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ oj_mimic_json_methods(VALUE json) {
}
// Pull in the JSON::State mimic file.
state_class = rb_const_get_at(generator, rb_intern("State"));
rb_gc_register_mark_object(state_class);

symbolize_names_sym = ID2SYM(rb_intern("symbolize_names")); rb_gc_register_address(&symbolize_names_sym);
}
Expand Down
8 changes: 8 additions & 0 deletions ext/oj/oj.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,13 +1649,21 @@ Init_oj() {
rb_require("oj/schandler");

oj_bag_class = rb_const_get_at(Oj, rb_intern("Bag"));
rb_gc_register_mark_object(oj_bag_class);
oj_bigdecimal_class = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
rb_gc_register_mark_object(oj_bigdecimal_class);
oj_date_class = rb_const_get(rb_cObject, rb_intern("Date"));
rb_gc_register_mark_object(oj_date_class);
oj_datetime_class = rb_const_get(rb_cObject, rb_intern("DateTime"));
rb_gc_register_mark_object(oj_datetime_class);
oj_enumerable_class = rb_const_get(rb_cObject, rb_intern("Enumerable"));
rb_gc_register_mark_object(oj_enumerable_class);
oj_parse_error_class = rb_const_get_at(Oj, rb_intern("ParseError"));
rb_gc_register_mark_object(oj_parse_error_class);
oj_stringio_class = rb_const_get(rb_cObject, rb_intern("StringIO"));
rb_gc_register_mark_object(oj_stringio_class);
oj_struct_class = rb_const_get(rb_cObject, rb_intern("Struct"));
rb_gc_register_mark_object(oj_struct_class);
oj_json_parser_error_class = rb_eEncodingError; // replaced if mimic is called
oj_json_generator_error_class = rb_eEncodingError; // replaced if mimic is called

Expand Down
10 changes: 10 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true
#
# Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
# required. That can be set in the RUBYOPT environment variable.
# export RUBYOPT=-w
Expand All @@ -16,6 +18,14 @@
require 'pp'
require 'oj'


if defined?(GC.verify_compaction_references) == 'method'
# This method was added in Ruby 3.0.0. Calling it this way asks the GC to
# move objects around, helping to find object movement bugs.
GC.verify_compaction_references(double_heap: true, toward: :empty)
end


$ruby = RUBY_DESCRIPTION.split(' ')[0]
$ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')

Expand Down
8 changes: 8 additions & 0 deletions test/json_gem/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$: << File.dirname(__FILE__)
$oj_dir = File.dirname(File.dirname(File.expand_path(File.dirname(__FILE__))))
%w(lib ext).each do |dir|
Expand All @@ -12,6 +14,12 @@
else
require 'oj'
Oj.mimic_JSON

if defined?(GC.verify_compaction_references) == 'method'
# This method was added in Ruby 3.0.0. Calling it this way asks the GC to
# move objects around, helping to find object movement bugs.
GC.verify_compaction_references(double_heap: true, toward: :empty)
end
end

NaN = JSON::NaN if defined?(JSON::NaN)
Expand Down

0 comments on commit 6786984

Please sign in to comment.