Skip to content

Commit 1fa307d

Browse files
committed
Add lock_file and lock_line to VM lock in debug mode
1 parent dc94d3c commit 1fa307d

File tree

10 files changed

+62
-26
lines changed

10 files changed

+62
-26
lines changed

encoding.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ int
422422
rb_enc_register(const char *name, rb_encoding *encoding)
423423
{
424424
int index;
425+
bool set_const = false;
425426

426427
GLOBAL_ENC_TABLE_LOCKING(enc_table) {
427428
index = enc_registered(enc_table, name);
@@ -440,9 +441,12 @@ rb_enc_register(const char *name, rb_encoding *encoding)
440441
}
441442
else {
442443
index = enc_register(enc_table, name, encoding);
443-
set_encoding_const(name, rb_enc_from_index(index));
444+
set_const = true;
444445
}
445446
}
447+
if (set_const) {
448+
set_encoding_const(name, rb_enc_from_index(index));
449+
}
446450
return index;
447451
}
448452

@@ -473,13 +477,14 @@ rb_enc_registered(const char *name)
473477
void
474478
rb_encdb_declare(const char *name)
475479
{
480+
int idx;
476481
GLOBAL_ENC_TABLE_LOCKING(enc_table) {
477-
int idx = enc_registered(enc_table, name);
482+
idx = enc_registered(enc_table, name);
478483
if (idx < 0) {
479484
idx = enc_register(enc_table, name, 0);
480485
}
481-
set_encoding_const(name, rb_enc_from_index(idx));
482486
}
487+
set_encoding_const(name, rb_enc_from_index(idx));
483488
}
484489

485490
static void
@@ -538,7 +543,6 @@ enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encodi
538543
idx = enc_register(enc_table, name, encoding);
539544
if (idx < 0) rb_raise(rb_eArgError, "invalid encoding name: %s", name);
540545
set_base_encoding(enc_table, idx, encoding);
541-
set_encoding_const(name, rb_enc_from_index(idx));
542546
return idx;
543547
}
544548

@@ -553,7 +557,6 @@ enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encod
553557
}
554558
if (idx >= 0) {
555559
set_base_encoding(enc_table, idx, origenc);
556-
set_encoding_const(name, rb_enc_from_index(idx));
557560
}
558561
else {
559562
rb_raise(rb_eArgError, "failed to replicate encoding");
@@ -576,6 +579,8 @@ rb_encdb_replicate(const char *name, const char *orig)
576579
r = enc_replicate_with_index(enc_table, name, rb_enc_from_index(origidx), idx);
577580
}
578581

582+
set_encoding_const(name, rb_enc_from_index(r));
583+
579584
return r;
580585
}
581586

@@ -589,6 +594,7 @@ rb_define_dummy_encoding(const char *name)
589594
rb_encoding *enc = enc_table->list[index].enc;
590595
ENC_SET_DUMMY((rb_raw_encoding *)enc);
591596
}
597+
set_encoding_const(name, rb_enc_from_index(index));
592598

593599
return index;
594600
}
@@ -606,6 +612,8 @@ rb_encdb_dummy(const char *name)
606612
ENC_SET_DUMMY((rb_raw_encoding *)enc);
607613
}
608614

615+
set_encoding_const(name, rb_enc_from_index(index));
616+
609617
return index;
610618
}
611619

@@ -672,18 +680,20 @@ enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
672680
}
673681

674682
static int
675-
enc_alias(struct enc_table *enc_table, const char *alias, int idx)
683+
enc_alias(struct enc_table *enc_table, const char *alias, int idx, bool *set_const)
676684
{
677685
if (!valid_encoding_name_p(alias)) return -1;
678-
if (!enc_alias_internal(enc_table, alias, idx))
679-
set_encoding_const(alias, enc_from_index(enc_table, idx));
686+
if (!enc_alias_internal(enc_table, alias, idx)) {
687+
*set_const = true;
688+
}
680689
return idx;
681690
}
682691

683692
int
684693
rb_enc_alias(const char *alias, const char *orig)
685694
{
686695
int idx, r;
696+
bool set_const = false;
687697
GLOBAL_ENC_TABLE_LOCKING(enc_table) {
688698
enc_check_addable(enc_table, alias); // can raise
689699
}
@@ -692,7 +702,11 @@ rb_enc_alias(const char *alias, const char *orig)
692702
if (idx < 0) return -1;
693703

694704
GLOBAL_ENC_TABLE_LOCKING(enc_table) {
695-
r = enc_alias(enc_table, alias, idx);
705+
r = enc_alias(enc_table, alias, idx, &set_const);
706+
}
707+
708+
if (set_const) {
709+
set_encoding_const(alias, rb_enc_from_index(idx));
696710
}
697711

698712
return r;
@@ -702,14 +716,18 @@ int
702716
rb_encdb_alias(const char *alias, const char *orig)
703717
{
704718
int r;
719+
bool set_const = false;
705720

706721
GLOBAL_ENC_TABLE_LOCKING(enc_table) {
707722
int idx = enc_registered(enc_table, orig);
708723

709724
if (idx < 0) {
710725
idx = enc_register(enc_table, orig, 0);
711726
}
712-
r = enc_alias(enc_table, alias, idx);
727+
r = enc_alias(enc_table, alias, idx, &set_const);
728+
}
729+
if (set_const) {
730+
set_encoding_const(alias, rb_enc_from_index(r));
713731
}
714732

715733
return r;
@@ -1818,6 +1836,7 @@ set_default_internal(VALUE klass, VALUE encoding)
18181836
static void
18191837
set_encoding_const(const char *name, rb_encoding *enc)
18201838
{
1839+
ASSERT_vm_unlocking();
18211840
VALUE encoding = rb_enc_from_encoding(enc);
18221841
char *s = (char *)name;
18231842
int haslower = 0, hasupper = 0, valid = 0;

thread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ rb_thread_sleep(int sec)
14721472
static void
14731473
rb_thread_schedule_limits(uint32_t limits_us)
14741474
{
1475-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
1475+
ASSERT_vm_unlocking();
14761476
if (!rb_thread_alone()) {
14771477
rb_thread_t *th = GET_THREAD();
14781478
RUBY_DEBUG_LOG("us:%u", (unsigned int)limits_us);
@@ -2596,6 +2596,7 @@ rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
25962596
int ret = FALSE;
25972597

25982598
VM_ASSERT(GET_THREAD() == th);
2599+
ASSERT_vm_unlocking();
25992600

26002601
if (th->ec->raised_flag) return ret;
26012602

thread_pthread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ thread_sched_to_waiting_common(struct rb_thread_sched *sched, rb_thread_t *th)
10251025
static void
10261026
thread_sched_to_waiting(struct rb_thread_sched *sched, rb_thread_t *th)
10271027
{
1028-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR()); // never context switch with VM lock held
1028+
ASSERT_vm_unlocking();
10291029
thread_sched_lock(sched, th);
10301030
{
10311031
thread_sched_to_waiting_common(sched, th);

variable.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,6 +3857,7 @@ static void
38573857
const_added(VALUE klass, ID const_name)
38583858
{
38593859
if (GET_VM()->running) {
3860+
ASSERT_vm_unlocking();
38603861
VALUE name = ID2SYM(const_name);
38613862
rb_funcallv(klass, idConst_added, 1, &name);
38623863
}

vm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,7 @@ invoke_block_from_c_bh(rb_execution_context_t *ec, VALUE block_handler,
16751675
int kw_splat, VALUE passed_block_handler, const rb_cref_t *cref,
16761676
int is_lambda, int force_blockarg)
16771677
{
1678-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
1678+
ASSERT_vm_unlocking();
16791679
again:
16801680
switch (vm_block_handler_type(block_handler)) {
16811681
case block_handler_type_iseq:
@@ -1718,7 +1718,7 @@ check_block_handler(rb_execution_context_t *ec)
17181718
static VALUE
17191719
vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat, const rb_cref_t *cref, int is_lambda)
17201720
{
1721-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
1721+
ASSERT_vm_unlocking();
17221722
return invoke_block_from_c_bh(ec, check_block_handler(ec),
17231723
argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
17241724
cref, is_lambda, FALSE);

vm_core.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ typedef struct rb_vm_struct {
675675
rb_nativethread_lock_t lock;
676676
struct rb_ractor_struct *lock_owner;
677677
unsigned int lock_rec;
678+
#if VM_CHECK_MODE > 0
679+
const char *lock_file;
680+
int lock_line;
681+
#endif
678682

679683
// join at exit
680684
rb_nativethread_cond_t terminate_cond;
@@ -687,7 +691,7 @@ typedef struct rb_vm_struct {
687691
rb_nativethread_cond_t barrier_complete_cond;
688692
rb_nativethread_cond_t barrier_release_cond;
689693
#endif
690-
} sync;
694+
} sync; // VM lock
691695

692696
#ifdef RUBY_THREAD_PTHREAD_H
693697
// ractor scheduling

vm_exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ rb_vm_get_insns_address_table(void)
129129
static VALUE
130130
vm_exec_core(rb_execution_context_t *ec)
131131
{
132-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
132+
ASSERT_vm_unlocking();
133133
register rb_control_frame_t *reg_cfp = ec->cfp;
134134
rb_thread_t *th;
135135

vm_sync.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ void
2727
RUBY_ASSERT_vm_unlocking(void)
2828
{
2929
rb_vm_t *vm = GET_VM();
30-
VM_ASSERT(!vm_locked(vm));
30+
if (vm_locked(vm)) {
31+
const char *file = vm->ractor.sync.lock_file;
32+
int line = vm->ractor.sync.lock_line;
33+
if (file) {
34+
fprintf(stderr, "Last locked at %s:%d\n", file, line);
35+
}
36+
VM_ASSERT(!vm_locked(vm));
37+
}
3138
}
3239
#endif
3340

@@ -75,6 +82,11 @@ vm_lock_enter(rb_ractor_t *cr, rb_vm_t *vm, bool locked, bool no_barrier, unsign
7582
VM_ASSERT(vm->ractor.sync.lock_owner == NULL);
7683
VM_ASSERT(vm->ractor.sync.lock_rec == 0);
7784

85+
#if VM_CHECK_MODE > 0
86+
vm->ractor.sync.lock_file = file;
87+
vm->ractor.sync.lock_line = line;
88+
#endif
89+
7890
// barrier
7991
if (vm_need_barrier(no_barrier, cr, vm)) {
8092
rb_execution_context_t *ec = GET_EC();

vm_sync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "vm_debug.h"
55
#include "debug_counter.h"
66

7-
#if USE_RUBY_DEBUG_LOG
7+
#if USE_RUBY_DEBUG_LOG || RUBY_DEBUG > 0 || VM_CHECK_MODE > 0
88
#define LOCATION_ARGS const char *file, int line
99
#define LOCATION_PARAMS file, line
1010
#define APPEND_LOCATION_ARGS , const char *file, int line

yjit/src/yjit.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,13 @@ pub extern "C" fn rb_yjit_enable(_ec: EcPtr, _ruby_self: VALUE, gen_stats: VALUE
240240

241241
yjit_init();
242242

243-
// Add "+YJIT" to RUBY_DESCRIPTION
244-
extern "C" {
245-
fn ruby_set_yjit_description();
246-
}
247-
unsafe { ruby_set_yjit_description(); }
248-
249-
Qtrue
250-
})
243+
});
244+
// Add "+YJIT" to RUBY_DESCRIPTION
245+
extern "C" {
246+
fn ruby_set_yjit_description();
247+
}
248+
unsafe { ruby_set_yjit_description(); }
249+
Qtrue
251250
}
252251

253252
/// Simulate a situation where we are out of executable memory

0 commit comments

Comments
 (0)