mirror of
https://github.com/ruby/ruby.git
synced 2026-10-29 00:46:26 +08:00
This commit renames rb_str_tmp_frozen_no_embed_acquire to rb_str_no_gvl_safe_acquire and makes it part of the public C API. There is currently no way for C extensions that use no GVL operations to safely read from a string because it has no way of guaranteeing that the string is not embedded. Embedded strings are not safe for no GVL operations because GC compaction can run, which will mprotect the memory region and will cause any read to SEGV. See https://github.com/ruby/ruby/pull/9817 for when rb_str_tmp_frozen_no_embed_acquire was introduced.
44 lines
1.1 KiB
C
44 lines
1.1 KiB
C
#include "ruby.h"
|
|
#include "internal/string.h"
|
|
|
|
static VALUE
|
|
bug_str_capacity(VALUE klass, VALUE str)
|
|
{
|
|
if (!STR_EMBED_P(str) && STR_SHARED_P(str)) {
|
|
return INT2FIX(0);
|
|
}
|
|
|
|
return LONG2FIX(rb_str_capacity(str));
|
|
}
|
|
|
|
static VALUE
|
|
bug_str_new_shared(VALUE klass, VALUE str)
|
|
{
|
|
return rb_str_new_shared(str);
|
|
}
|
|
|
|
static VALUE
|
|
bug_str_tmp_frozen_acquire_release(VALUE klass, VALUE str)
|
|
{
|
|
VALUE tmp = rb_str_tmp_frozen_acquire(str);
|
|
rb_str_tmp_frozen_release(str, tmp);
|
|
return str;
|
|
}
|
|
|
|
static VALUE
|
|
bug_str_no_gvl_safe_acquire_release(VALUE klass, VALUE str)
|
|
{
|
|
VALUE tmp = rb_str_no_gvl_safe_acquire(str);
|
|
rb_str_no_gvl_safe_release(str, tmp);
|
|
return str;
|
|
}
|
|
|
|
void
|
|
Init_string_capacity(VALUE klass)
|
|
{
|
|
rb_define_singleton_method(klass, "capacity", bug_str_capacity, 1);
|
|
rb_define_singleton_method(klass, "rb_str_new_shared", bug_str_new_shared, 1);
|
|
rb_define_singleton_method(klass, "tmp_frozen_acquire_release", bug_str_tmp_frozen_acquire_release, 1);
|
|
rb_define_singleton_method(klass, "no_gvl_safe_acquire_release", bug_str_no_gvl_safe_acquire_release, 1);
|
|
}
|