Files
ruby/constant.h
Koichi SasadaandClaude Opus 5.5 5771bd7a39 Read another Ractor's constant entries under the VM lock
rb_const_remove deleted the entry from the constant table without the VM
lock and then freed it, while a Ractor that does not own the class looks
the table up under the VM lock but reads the entry after releasing it.
Reading a constant while its owner removes it could crash in the table
lookup or return garbage (an Integer, Float or false that was never
assigned).

Delete under the VM lock, and have the readers that may not own the class
copy the entry while holding it (rb_const_lookup_copy).  Class#dup and
the singleton class clone walk the original's table under the lock for
the same reason; the Class#dup check collects the candidates under the
lock and runs rb_ractor_shareable_p after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 05:49:20 +09:00

58 lines
1.8 KiB
C

#ifndef CONSTANT_H
#define CONSTANT_H
/**********************************************************************
constant.h -
$Author$
created at: Sun Nov 15 00:09:33 2009
Copyright (C) 2009 Yusuke Endoh
**********************************************************************/
#include "ruby/ruby.h"
#include "id_table.h"
typedef enum {
CONST_DEPRECATED = 0x100,
CONST_VISIBILITY_MASK = 0xff,
CONST_PUBLIC = 0x00,
CONST_PRIVATE,
CONST_VISIBILITY_MAX
} rb_const_flag_t;
#define RB_CONST_PRIVATE_P(ce) \
(((ce)->flag & CONST_VISIBILITY_MASK) == CONST_PRIVATE)
#define RB_CONST_PUBLIC_P(ce) \
(((ce)->flag & CONST_VISIBILITY_MASK) == CONST_PUBLIC)
#define RB_CONST_DEPRECATED_P(ce) \
((ce)->flag & CONST_DEPRECATED)
typedef struct rb_const_entry_struct {
rb_const_flag_t flag;
int line;
VALUE value; /* should be mark */
union { /* should be mark */
VALUE file; /* the file where the constant is defined */
VALUE autoload_const; /* the autoload registration if value is Qundef */
} u;
} rb_const_entry_t;
VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj);
VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj);
VALUE rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj);
void rb_free_const_table(struct rb_id_table *tbl);
VALUE rb_const_source_location(VALUE, ID);
int rb_autoloading_value(VALUE mod, ID id, VALUE *value, rb_const_flag_t *flag);
rb_const_entry_t *rb_const_lookup(VALUE klass, ID id);
bool rb_const_lookup_copy(VALUE klass, ID id, rb_const_entry_t *ce);
VALUE rb_public_const_get_at(VALUE klass, ID id);
VALUE rb_public_const_get_from(VALUE klass, ID id);
int rb_public_const_defined_from(VALUE klass, ID id);
VALUE rb_const_source_location_at(VALUE, ID);
#endif /* CONSTANT_H */