RJIT: Stop allowing leaked globals rjit_*

This commit is contained in:
Takashi Kokubun 2023-03-08 23:14:33 -08:00
parent a0918a4a80
commit f5909ac6d9
10 changed files with 74 additions and 74 deletions

4
cont.c
View File

@ -70,7 +70,7 @@ static VALUE rb_cFiberPool;
#define FIBER_POOL_ALLOCATION_FREE
#endif
#define jit_cont_enabled (rjit_enabled || rb_yjit_enabled_p())
#define jit_cont_enabled (rb_rjit_enabled || rb_yjit_enabled_p())
enum context_type {
CONTINUATION_CONTEXT = 0,
@ -2547,7 +2547,7 @@ rb_threadptr_root_fiber_setup(rb_thread_t *th)
fiber->blocking = 1;
fiber_status_set(fiber, FIBER_RESUMED); /* skip CREATED */
th->ec = &fiber->cont.saved_ec;
// When rb_threadptr_root_fiber_setup is called for the first time, rjit_enabled and
// When rb_threadptr_root_fiber_setup is called for the first time, rb_rjit_enabled and
// rb_yjit_enabled_p() are still false. So this does nothing and rb_jit_cont_init() that is
// called later will take care of it. However, you still have to call cont_init_jit_cont()
// here for other Ractors, which are not initialized by rb_jit_cont_init().

86
rjit.c
View File

@ -63,25 +63,25 @@
// A copy of RJIT portion of MRI options since RJIT initialization. We
// need them as RJIT threads still can work when the most MRI data were
// freed.
struct rjit_options rjit_opts;
struct rjit_options rb_rjit_opts;
// true if RJIT is enabled.
bool rjit_enabled = false;
bool rjit_stats_enabled = false;
bool rb_rjit_enabled = false;
bool rb_rjit_stats_enabled = false;
// true if JIT-ed code should be called. When `ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS`
// and `rjit_call_p == false`, any JIT-ed code execution is cancelled as soon as possible.
bool rjit_call_p = false;
// A flag to communicate that rjit_call_p should be disabled while it's temporarily false.
bool rjit_cancel_p = false;
// and `rb_rjit_call_p == false`, any JIT-ed code execution is cancelled as soon as possible.
bool rb_rjit_call_p = false;
// A flag to communicate that rb_rjit_call_p should be disabled while it's temporarily false.
bool rb_rjit_cancel_p = false;
void
rb_rjit_cancel_all(const char *reason)
{
if (!rjit_enabled)
if (!rb_rjit_enabled)
return;
rjit_call_p = false;
rjit_cancel_p = true;
rb_rjit_call_p = false;
rb_rjit_cancel_p = true;
}
void
@ -114,7 +114,7 @@ static VALUE rb_mRJITHooks = 0;
opt_match(s, l, name) && (*(s) ? 1 : (rb_raise(rb_eRuntimeError, "--rjit-" name " needs an argument"), 0))
void
rjit_setup_options(const char *s, struct rjit_options *rjit_opt)
rb_rjit_setup_options(const char *s, struct rjit_options *rjit_opt)
{
const size_t l = strlen(s);
if (l == 0) {
@ -140,7 +140,7 @@ rjit_setup_options(const char *s, struct rjit_options *rjit_opt)
}
#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
const struct ruby_opt_message rjit_option_messages[] = {
const struct ruby_opt_message rb_rjit_option_messages[] = {
#if RJIT_STATS
M("--rjit-stats", "", "Enable collecting RJIT statistics"),
#endif
@ -262,7 +262,7 @@ uint8_t *rb_rjit_mem_block = NULL;
// `rb_ec_ractor_hooks(ec)->events` is moved to this variable during compilation.
rb_event_flag_t rb_rjit_global_events = 0;
// Basically rjit_opts.stats, but this becomes false during RJIT compilation.
// Basically rb_rjit_opts.stats, but this becomes false during RJIT compilation.
static bool rjit_stats_p = false;
#if RJIT_STATS
@ -286,12 +286,12 @@ extern VALUE rb_gc_disable(void);
rb_hook_list_t *global_hooks = rb_ec_ractor_hooks(GET_EC()); \
rb_rjit_global_events = global_hooks->events; \
global_hooks->events = 0; \
bool original_call_p = rjit_call_p; \
bool original_call_p = rb_rjit_call_p; \
rjit_stats_p = false; \
rjit_call_p = false; \
rb_rjit_call_p = false; \
stmt; \
rjit_call_p = (rjit_cancel_p ? false : original_call_p); \
rjit_stats_p = rjit_opts.stats; \
rb_rjit_call_p = (rb_rjit_cancel_p ? false : original_call_p); \
rjit_stats_p = rb_rjit_opts.stats; \
global_hooks->events = rb_rjit_global_events; \
if (!was_disabled) rb_gc_enable(); \
} while (0);
@ -299,14 +299,14 @@ extern VALUE rb_gc_disable(void);
void
rb_rjit_bop_redefined(int redefined_flag, enum ruby_basic_operators bop)
{
if (!rjit_call_p) return;
rjit_call_p = false;
if (!rb_rjit_call_p) return;
rb_rjit_call_p = false;
}
static void
rjit_cme_invalidate(void *data)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
WITH_RJIT_ISOLATED({
rb_funcall(rb_mRJITHooks, rb_intern("on_cme_invalidate"), 1, SIZET2NUM((size_t)data));
});
@ -317,7 +317,7 @@ extern int rb_workqueue_register(unsigned flags, rb_postponed_job_func_t func, v
void
rb_rjit_cme_invalidate(rb_callable_method_entry_t *cme)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
// Asynchronously hook the Ruby code since running Ruby in the middle of cme invalidation is dangerous.
rb_workqueue_register(0, rjit_cme_invalidate, (void *)cme);
}
@ -325,14 +325,14 @@ rb_rjit_cme_invalidate(rb_callable_method_entry_t *cme)
void
rb_rjit_before_ractor_spawn(void)
{
if (!rjit_call_p) return;
rjit_call_p = false;
if (!rb_rjit_call_p) return;
rb_rjit_call_p = false;
}
static void
rjit_constant_state_changed(void *data)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
RB_VM_LOCK_ENTER();
rb_vm_barrier();
@ -346,7 +346,7 @@ rjit_constant_state_changed(void *data)
void
rb_rjit_constant_state_changed(ID id)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
// Asynchronously hook the Ruby code since this is hooked during a "Ruby critical section".
rb_workqueue_register(0, rjit_constant_state_changed, (void *)id);
}
@ -354,7 +354,7 @@ rb_rjit_constant_state_changed(ID id)
void
rb_rjit_constant_ic_update(const rb_iseq_t *const iseq, IC ic, unsigned insn_idx)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
RB_VM_LOCK_ENTER();
rb_vm_barrier();
@ -370,7 +370,7 @@ rb_rjit_constant_ic_update(const rb_iseq_t *const iseq, IC ic, unsigned insn_idx
void
rb_rjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
WITH_RJIT_ISOLATED({
rb_funcall(rb_mRJITHooks, rb_intern("on_tracing_invalidate_all"), 1, UINT2NUM(new_iseq_events));
});
@ -379,7 +379,7 @@ rb_rjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events)
static void
rjit_iseq_update_references(void *data)
{
if (!rjit_enabled || !rjit_call_p || !rb_mRJITHooks) return;
if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks) return;
WITH_RJIT_ISOLATED({
rb_funcall(rb_mRJITHooks, rb_intern("on_update_references"), 0);
});
@ -388,7 +388,7 @@ rjit_iseq_update_references(void *data)
void
rb_rjit_iseq_update_references(struct rb_iseq_constant_body *const body)
{
if (!rjit_enabled) return;
if (!rb_rjit_enabled) return;
if (body->rjit_blocks) {
body->rjit_blocks = rb_gc_location(body->rjit_blocks);
@ -403,7 +403,7 @@ rb_rjit_iseq_update_references(struct rb_iseq_constant_body *const body)
void
rb_rjit_iseq_mark(VALUE rjit_blocks)
{
if (!rjit_enabled) return;
if (!rb_rjit_enabled) return;
// Note: This wasn't enough for some reason.
// We actually rely on RubyVM::RJIT::GC_REFS to mark this.
@ -459,9 +459,9 @@ rb_rjit_branch_stub_hit(VALUE branch_stub, int sp_offset, int target0_p)
// Called by rb_vm_mark()
void
rjit_mark(void)
rb_rjit_mark(void)
{
if (!rjit_enabled)
if (!rb_rjit_enabled)
return;
RUBY_MARK_ENTER("rjit");
@ -475,10 +475,10 @@ rjit_mark(void)
}
void
rjit_init(const struct rjit_options *opts)
rb_rjit_init(const struct rjit_options *opts)
{
VM_ASSERT(rjit_enabled);
rjit_opts = *opts;
VM_ASSERT(rb_rjit_enabled);
rb_rjit_opts = *opts;
rb_rjit_mem_block = rb_rjit_reserve_addr_space(RJIT_CODE_SIZE);
@ -486,7 +486,7 @@ rjit_init(const struct rjit_options *opts)
rb_mRJIT = rb_const_get(rb_cRubyVM, rb_intern("RJIT"));
if (!rb_const_defined(rb_mRJIT, rb_intern("Compiler"))) {
rb_warn("Disabling RJIT because RubyVM::RJIT::Compiler is not defined");
rjit_enabled = false;
rb_rjit_enabled = false;
return;
}
rb_mRJITC = rb_const_get(rb_mRJIT, rb_intern("C"));
@ -497,14 +497,14 @@ rjit_init(const struct rjit_options *opts)
rb_cRJITCfpPtr = rb_funcall(rb_mRJITC, rb_intern("rb_control_frame_t"), 0);
rb_mRJITHooks = rb_const_get(rb_mRJIT, rb_intern("Hooks"));
rjit_call_p = true;
rjit_stats_p = rjit_opts.stats;
rb_rjit_call_p = true;
rjit_stats_p = rb_rjit_opts.stats;
// Normalize options
if (rjit_opts.call_threshold == 0)
rjit_opts.call_threshold = DEFAULT_CALL_THRESHOLD;
if (rb_rjit_opts.call_threshold == 0)
rb_rjit_opts.call_threshold = DEFAULT_CALL_THRESHOLD;
#ifndef HAVE_LIBCAPSTONE
if (rjit_opts.dump_disasm)
if (rb_rjit_opts.dump_disasm)
rb_warn("libcapstone has not been linked. Ignoring --rjit-dump-disasm.");
#endif
}
@ -513,14 +513,14 @@ rjit_init(const struct rjit_options *opts)
static VALUE
rjit_stats_enabled_p(rb_execution_context_t *ec, VALUE self)
{
return RBOOL(rjit_stats_enabled);
return RBOOL(rb_rjit_stats_enabled);
}
// Disable anything that could impact stats. It ends up disabling JIT calls as well.
static VALUE
rjit_stop_stats(rb_execution_context_t *ec, VALUE self)
{
rjit_call_p = false;
rb_rjit_call_p = false;
rjit_stats_p = false;
return Qnil;
}

22
rjit.h
View File

@ -87,19 +87,19 @@ struct rb_rjit_compile_info {
};
RUBY_SYMBOL_EXPORT_BEGIN
RUBY_EXTERN struct rjit_options rjit_opts;
RUBY_EXTERN bool rjit_call_p;
RUBY_EXTERN struct rjit_options rb_rjit_opts;
RUBY_EXTERN bool rb_rjit_call_p;
#define rb_rjit_call_threshold() rjit_opts.call_threshold
#define rb_rjit_call_threshold() rb_rjit_opts.call_threshold
extern void rb_rjit_compile(const rb_iseq_t *iseq);
RUBY_SYMBOL_EXPORT_END
extern void rb_rjit_cancel_all(const char *reason);
extern void rjit_init(const struct rjit_options *opts);
extern void rb_rjit_init(const struct rjit_options *opts);
extern void rb_rjit_free_iseq(const rb_iseq_t *iseq);
extern void rb_rjit_iseq_update_references(struct rb_iseq_constant_body *const body);
extern void rjit_mark(void);
extern void rb_rjit_mark(void);
extern void rb_rjit_iseq_mark(VALUE rjit_blocks);
extern void rjit_notify_waitpid(int exit_code);
@ -115,8 +115,8 @@ extern void rb_rjit_before_ractor_spawn(void);
extern void rb_rjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events);
extern void rb_rjit_collect_vm_usage_insn(int insn);
extern bool rjit_enabled;
extern bool rjit_stats_enabled;
extern bool rb_rjit_enabled;
extern bool rb_rjit_stats_enabled;
# else // USE_RJIT
@ -124,7 +124,7 @@ static inline void rb_rjit_compile(const rb_iseq_t *iseq){}
static inline void rb_rjit_cancel_all(const char *reason){}
static inline void rb_rjit_free_iseq(const rb_iseq_t *iseq){}
static inline void rjit_mark(void){}
static inline void rb_rjit_mark(void){}
static inline void rb_rjit_bop_redefined(int redefined_flag, enum ruby_basic_operators bop) {}
static inline void rb_rjit_cme_invalidate(rb_callable_method_entry_t *cme) {}
@ -133,9 +133,9 @@ static inline void rb_rjit_constant_state_changed(ID id) {}
static inline void rb_rjit_constant_ic_update(const rb_iseq_t *const iseq, IC ic, unsigned insn_idx) {}
static inline void rb_rjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events) {}
#define rjit_enabled false
#define rjit_call_p false
#define rjit_stats_enabled false
#define rb_rjit_enabled false
#define rb_rjit_call_p false
#define rb_rjit_stats_enabled false
#define rb_rjit_call_threshold() UINT_MAX

View File

@ -1,7 +1,7 @@
module RubyVM::RJIT
# Return true if RJIT is enabled.
def self.enabled?
Primitive.cexpr! 'RBOOL(rjit_enabled)'
Primitive.cexpr! 'RBOOL(rb_rjit_enabled)'
end
# Stop generating JITed code.

View File

@ -165,7 +165,7 @@ dump_disasm(rb_execution_context_t *ec, VALUE self, VALUE from, VALUE to)
static VALUE
rjit_enabled_p(rb_execution_context_t *ec, VALUE self)
{
return RBOOL(rjit_enabled);
return RBOOL(rb_rjit_enabled);
}
static int

View File

@ -511,7 +511,7 @@ module RubyVM::RJIT # :nodoc: all
end
def rjit_opts
addr = Primitive.cexpr! 'PTR2NUM((VALUE)&rjit_opts)'
addr = Primitive.cexpr! 'PTR2NUM((VALUE)&rb_rjit_opts)'
rjit_options.new(addr)
end

20
ruby.c
View File

@ -342,7 +342,7 @@ usage(const char *name, int help, int highlight, int columns)
};
#endif
#if USE_RJIT
extern const struct ruby_opt_message rjit_option_messages[];
extern const struct ruby_opt_message rb_rjit_option_messages[];
#endif
int i;
const char *sb = highlight ? esc_standout+1 : esc_none;
@ -377,8 +377,8 @@ usage(const char *name, int help, int highlight, int columns)
#endif
#if USE_RJIT
printf("%s""RJIT options (experimental):%s\n", sb, se);
for (i = 0; rjit_option_messages[i].str; ++i)
SHOW(rjit_option_messages[i]);
for (i = 0; rb_rjit_option_messages[i].str; ++i)
SHOW(rb_rjit_option_messages[i]);
#endif
}
@ -1494,9 +1494,9 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt)
}
else if (is_option_with_optarg("rjit", '-', true, false, false)) {
#if USE_RJIT
extern void rjit_setup_options(const char *s, struct rjit_options *rjit_opt);
extern void rb_rjit_setup_options(const char *s, struct rjit_options *rjit_opt);
FEATURE_SET(opt->features, FEATURE_BIT(rjit));
rjit_setup_options(s, &opt->rjit);
rb_rjit_setup_options(s, &opt->rjit);
#else
rb_warn("RJIT support is disabled.");
#endif
@ -1614,9 +1614,9 @@ ruby_opt_init(ruby_cmdline_options_t *opt)
#if USE_RJIT
// rb_call_builtin_inits depends on RubyVM::RJIT.enabled?
if (opt->rjit.on)
rjit_enabled = true;
rb_rjit_enabled = true;
if (opt->rjit.stats)
rjit_stats_enabled = true;
rb_rjit_stats_enabled = true;
#endif
Init_ext(); /* load statically linked extensions before rubygems */
@ -1626,9 +1626,9 @@ ruby_opt_init(ruby_cmdline_options_t *opt)
// Initialize JITs after prelude because JITing prelude is typically not optimal.
#if USE_RJIT
// Also, rjit_init is safe only after rb_call_builtin_inits() defines RubyVM::RJIT::Compiler.
// Also, rb_rjit_init is safe only after rb_call_builtin_inits() defines RubyVM::RJIT::Compiler.
if (opt->rjit.on)
rjit_init(&opt->rjit);
rb_rjit_init(&opt->rjit);
#endif
#if USE_YJIT
if (opt->yjit)
@ -1941,7 +1941,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
#if USE_RJIT
if (FEATURE_SET_P(opt->features, rjit)) {
opt->rjit.on = true; // set opt->rjit.on for Init_ruby_description() and calling rjit_init()
opt->rjit.on = true; // set opt->rjit.on for Init_ruby_description() and calling rb_rjit_init()
}
#endif
#if USE_YJIT

View File

@ -57,7 +57,7 @@ IO.foreach("|#{NM} #{no_llvm} #{ARGV.join(' ')}") do |line|
next unless /[A-TV-Z]/ =~ t
next unless n.sub!(/^#{SYMBOL_PREFIX}/o, "")
next if n.include?(".")
next if /\A(?:Init_|InitVM_|RUBY_|ruby_|rb_|[Oo]nig|dln_|rjit_|coroutine_)/ =~ n
next if /\A(?:Init_|InitVM_|RUBY_|ruby_|rb_|[Oo]nig|dln_|coroutine_)/ =~ n
next if REPLACE.include?(n)
puts col.fail("leaked") if count.zero?
count += 1

6
vm.c
View File

@ -379,7 +379,7 @@ jit_exec(rb_execution_context_t *ec)
const rb_iseq_t *iseq = ec->cfp->iseq;
struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
bool yjit_enabled = rb_yjit_enabled_p();
if (yjit_enabled || rjit_call_p) {
if (yjit_enabled || rb_rjit_call_p) {
body->total_calls++;
}
else {
@ -402,7 +402,7 @@ jit_exec(rb_execution_context_t *ec)
return Qundef;
}
}
else { // rjit_call_p
else { // rb_rjit_call_p
if (body->total_calls == rb_rjit_call_threshold()) {
rb_rjit_compile(iseq);
}
@ -2822,7 +2822,7 @@ rb_vm_mark(void *ptr)
}
}
rjit_mark();
rb_rjit_mark();
}
RUBY_MARK_LEAVE("vm");

View File

@ -204,7 +204,7 @@ clear_method_cache_by_id_in_class(VALUE klass, ID mid)
if (rb_yjit_enabled_p() && rb_id_table_lookup(cm_tbl, mid, &cme)) {
rb_yjit_cme_invalidate((rb_callable_method_entry_t *)cme);
}
if (rjit_enabled && rb_id_table_lookup(cm_tbl, mid, &cme)) {
if (rb_rjit_enabled && rb_id_table_lookup(cm_tbl, mid, &cme)) {
rb_rjit_cme_invalidate((rb_callable_method_entry_t *)cme);
}
rb_id_table_delete(cm_tbl, mid);