Ensure Fiber storage is only accessed from the Fiber it belongs to

This commit is contained in:
Benoit Daloze 2022-12-20 18:21:43 +01:00
Родитель d557f17974
Коммит 0efa36ac06
3 изменённых файлов: 35 добавлений и 13 удалений

10
cont.c
Просмотреть файл

@ -2069,6 +2069,14 @@ fiber_storage_get(rb_fiber_t *fiber)
return storage;
}
static void storage_access_must_be_from_same_fiber(VALUE self) {
rb_fiber_t *fiber = fiber_ptr(self);
rb_fiber_t *current = fiber_current();
if (fiber != current) {
rb_raise(rb_eArgError, "Fiber storage can only be accessed from the Fiber it belongs to");
}
}
/**
* call-seq: Fiber.current.storage -> hash (dup)
*
@ -2077,6 +2085,7 @@ fiber_storage_get(rb_fiber_t *fiber)
static VALUE
rb_fiber_storage_get(VALUE self)
{
storage_access_must_be_from_same_fiber(self);
return rb_obj_dup(fiber_storage_get(fiber_ptr(self)));
}
@ -2134,6 +2143,7 @@ rb_fiber_storage_set(VALUE self, VALUE value)
"Fiber#storage= is experimental and may be removed in the future!");
}
storage_access_must_be_from_same_fiber(self);
fiber_storage_validate(value);
fiber_ptr(self)->cont.saved_ec.storage = rb_obj_dup(value);

Просмотреть файл

@ -11,8 +11,7 @@ describe "Fiber.new(storage:)" do
end
it "creates a fiber with lazily initialized storage" do
fiber = Fiber.new(storage: nil) {}
fiber.storage.should == {}
Fiber.new(storage: nil) { Fiber.current.storage }.resume.should == {}
end
it "creates a fiber by inheriting the storage of the parent fiber" do
@ -28,33 +27,34 @@ describe "Fiber.new(storage:)" do
end
end
describe "Fiber#storage" do
describe "Fiber#storage=" do
ruby_version_is "3.2" do
it "can clear the storage of the fiber" do
fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage }
fiber.storage = nil
fiber = Fiber.new(storage: {life: 42}) {
Fiber.current.storage = nil
Fiber.current.storage
}
fiber.resume.should == {}
end
it "can set the storage of the fiber" do
fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage }
fiber.storage = {life: 43}
fiber = Fiber.new(storage: {life: 42}) {
Fiber.current.storage = {life: 43}
Fiber.current.storage
}
fiber.resume.should == {life: 43}
end
it "can't set the storage of the fiber to non-hash" do
fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage }
-> { fiber.storage = 42 }.should raise_error(TypeError)
-> { Fiber.current.storage = 42 }.should raise_error(TypeError)
end
it "can't set the storage of the fiber to a frozen hash" do
fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage }
-> { fiber.storage = {life: 43}.freeze }.should raise_error(FrozenError)
-> { Fiber.current.storage = {life: 43}.freeze }.should raise_error(FrozenError)
end
it "can't set the storage of the fiber to a hash with non-symbol keys" do
fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage }
-> { fiber.storage = {life: 43, Object.new => 44} }.should raise_error(TypeError)
-> { Fiber.current.storage = {life: 43, Object.new => 44} }.should raise_error(TypeError)
end
end
end

Просмотреть файл

@ -41,6 +41,18 @@ class TestFiberStorage < Test::Unit::TestCase
Warning[:experimental] = old
end
def test_storage_only_allow_access_from_same_fiber
old, Warning[:experimental] = Warning[:experimental], false
f = Fiber.new do
Fiber[:a] = 1
end
assert_raise(ArgumentError) { f.storage }
assert_raise(ArgumentError) { f.storage = {} }
ensure
Warning[:experimental] = old
end
def test_inherited_storage
Fiber.new(storage: {foo: :bar}) do
f = Fiber.new do