diff --git a/internal/vm/images.go b/internal/vm/images.go index 3464f2a0..032ed070 100644 --- a/internal/vm/images.go +++ b/internal/vm/images.go @@ -89,6 +89,22 @@ func (vm *VM) registerImagesImage(mod *RClass) { vm.consts["Images::Image"] = cls // Images::Image.open(path) decodes the image file at path. + // + // ⛔ NOT socketOpenWithBlock, AND IT MUST NOT BECOME IT. Two reasons, and the + // second is the dangerous one: + // + // 1. MiniMagick — what go-ruby-images' README says it replaces — declares + // `def self.open(path_or_url, ext = nil, **options)` with no block + // parameter at all, so there is no block form to honour (#950). + // 2. That helper closes by sending the class's OWN `close`, and on this + // class `close` is the MORPHOLOGICAL CLOSING operator — dilate then + // erode, taking a radius (see registerImagesTransforms). So the wiring + // would call an image filter on the way out of the block, believing it + // was releasing a resource. `Images::Image#open` is the morphological + // opening for the same reason; the collision is on both names. + // + // TestImagesImageOpenTakesNoBlock and testdata/open-doors.txt both go red if + // this is "fixed". cls.smethods["open"] = &Method{name: "open", owner: cls, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { imagesArity(args, 1) diff --git a/internal/vm/images_internal_test.go b/internal/vm/images_internal_test.go index b12abc7f..c083eb2c 100644 --- a/internal/vm/images_internal_test.go +++ b/internal/vm/images_internal_test.go @@ -177,6 +177,63 @@ puts r.join("|") } } +// TestImagesImageOpenTakesNoBlock pins a NON-defect, which is the only reason it +// exists. +// +// #950 asked whether Images::Image.open discarding its block was a gap, the way +// the socket doors' discarded blocks were (#931/#932: 27 hung spec files) and +// the way Augeas.open's was (#952). Reading the reference settles it the other +// way. go-ruby-images' README calls itself "a MiniMagick / ruby-vips +// replacement", and MiniMagick's is: +// +// def self.open(path_or_url, ext = nil, **options) +// +// with no block parameter — no `yield`, no `block_given?`, no `ensure`. The only +// blocks in its body are internal, handed to `uri.open` / `pathname.open`. +// ruby-vips has no `Image.open` at all; it spells this `Vips::Image +// .new_from_file`. A Ruby method that takes no block and is handed one ignores +// it, so rbgo discarding it IS the contract. +// +// ⛔ WITHOUT THIS TEST THE CORRECT BEHAVIOUR IS INDISTINGUISHABLE FROM THE +// DEFECT IT RESEMBLES. The three other `open` doors that discarded a block were +// all real bugs — #931/#932 for the sockets, #952 for Augeas — so the next +// reader to notice this one has every reason to assume the same and to "fix" it +// into socketOpenWithBlock. +// +// ⛔⛔ AND THAT FIX WOULD BE WORSE THAN POINTLESS, which is what the mutation +// showed rather than reasoned: socketOpenWithBlock closes through the CLASS'S +// OWN `close`, and on Images::Image `close` is the MORPHOLOGICAL CLOSING +// operator — dilate then erode, taking a radius — just as `#open` is +// morphological opening. Wiring the door makes the program die with +// +// ArgumentError: wrong number of arguments (given 0, expected 1) +// +// from that operator, called with no radius on the way out of the block. The +// helper's "close whatever the class calls close" contract is silent about +// classes where that name means something else, and this is one. +func TestImagesImageOpenTakesNoBlock(t *testing.T) { + dir := t.TempDir() + src := fmt.Sprintf(` +require "images" +Images::Image.read(Images::Canvas.new(2, 2, Images::Color::WHITE).to_blob).write(%q) + +ran = false +got = Images::Image.open(%q) { |i| ran = true; "the block's value" } + +# The block is not called, and open answers the IMAGE -- not the block's value, +# which is the half a yield-only "fix" would still get wrong. +puts ran.inspect +puts got.class +puts got.dimensions.inspect +`, dir+"/in.png", dir+"/in.png") + want := "false\nImages::Image\n[2, 2]" + if got := runSrc(t, src); got != want { + t.Errorf("Image.open with a block:\n got=%q\nwant=%q\n"+ + "a block that RAN, or a return value that is not the image, means this door was wired to "+ + "socketOpenWithBlock -- see testdata/open-doors.txt: MiniMagick's open takes no block", got, want) + } +} + // TestImagesImageErrors covers every Images::Image error branch: a missing file // and a corrupt blob (imagesWrap -> Images::Error), a non-positive resize and an // unencodable convert (also imagesWrap), a bad rotation, an out-of-bounds pixel diff --git a/internal/vm/socket.go b/internal/vm/socket.go index 0de8ef39..4edf4f90 100644 --- a/internal/vm/socket.go +++ b/internal/vm/socket.go @@ -233,6 +233,21 @@ func (vm *VM) installSocketNonblock() { // connection, so there is one close per class instead of a second spelling of // it; each of those is already idempotent, so a block that closed the socket // itself does not get it closed twice. +// +// ⛔ THAT CARRIES AN UNSTATED ASSUMPTION: that on the class being wrapped, +// `close` MEANS "release the resource". It is not true of every class in this +// package. Images::Image has a `close` — the morphological closing operator, +// dilate then erode, taking a radius — and an `open` that is morphological +// opening, so wrapping Images::Image.open here would run an image filter on the +// way out of the block believing it had released something; measured, the +// program dies with `wrong number of arguments (given 0, expected 1)` from that +// operator (#950, images.go). +// +// Nothing here can tell the two meanings apart — a native checks its own +// arguments, so even arity is not introspectable. The check that can is +// testdata/open-doors.txt plus TestEveryOpenDoorIsAccountedFor: every `open` +// door is either wrapped here or carries a row saying why not. Before wrapping +// a new class, read what ITS `close` does. func socketOpenWithBlock(cls *RClass, newFn NativeFn) NativeFn { return func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { v := newFn(vm, self, args, nil) diff --git a/internal/vm/testdata/open-doors.txt b/internal/vm/testdata/open-doors.txt index 46a5779d..f6180133 100644 --- a/internal/vm/testdata/open-doors.txt +++ b/internal/vm/testdata/open-doors.txt @@ -27,9 +27,11 @@ net_ftp.go:ftp Net::FTP.open yields the connected client through ftpWithClose, w sqlite3.go:cls SQLite3::Database.open yields and closes the database in a defer (sqlite3.go:102); SQLite3::Database.new shares the same func. bolt.go:cls The bbolt database's open yields and closes the handle (bolt.go:76), returning the block's value. -# ───────── does NOT honour its block. #950 -# Takes `_ *Proc` and discards it. Listed here rather than fixed so the count is -# visible: one door, on a binding surface, not in MRI core. +# ───────── discards its block, and that is the CONTRACT. #950 +# One door, on a binding surface, not in MRI core. It was listed here as +# "UNSPECIFIED, not a known defect" while nobody had read the reference. The +# reference has now been read and it settles the question: there is no block +# form to honour, so a discarded block is Ruby behaving normally, not a gap. # # ⛔ augeas.go:cls WAS THE OTHER ONE, and its row said the fix was "blocked" # because rbgo's Augeas had no #close for socketOpenWithBlock to call and @@ -39,4 +41,4 @@ bolt.go:cls The bbolt database's open yields and closes the handle (bolt.go:76), # guard, needing nothing from the dependency. The door now goes through # socketOpenWithBlock, so it is correct by construction and owes this file no # row at all. -images.go:cls UNSPECIFIED, not a known defect. Images::Image.open discards its block (images.go:93). go-ruby-images' README documents no Ruby `Image.open` and no block form at all — it documents the Go library (`images.Open("photo.jpg")`) — and ruby-vips and MiniMagick spell this operation without a block, so "no block form" may be the contract to DOCUMENT rather than a gap to fill. See #950. +images.go:cls ⛔ NOT A DEFECT, reference READ. Images::Image.open discards its block (images.go:93), and MiniMagick — which go-ruby-images' README names as what it replaces, "a MiniMagick / ruby-vips replacement" — defines `def self.open(path_or_url, ext = nil, **options)` with NO block parameter: no `yield`, no `block_given?`, no `ensure`. The only blocks in its body are internal, passed to `uri.open` / `pathname.open`. ruby-vips has no `Image.open` at all (`Vips::Image.new_from_file`). A Ruby method that takes no block and is given one simply ignores it, so this door is correct as written. ⛔⛔ AND WIRING IT ANYWAY WOULD BE WORSE THAN POINTLESS: socketOpenWithBlock closes through the CLASS'S OWN `close`, and on Images::Image `close` is the MORPHOLOGICAL CLOSING operator — dilate then erode, taking a radius — just as `#open` is morphological opening. The helper would run an image filter on the way out of the block believing it had released something. Measured, not reasoned: wiring it makes the program die with `ArgumentError: wrong number of arguments (given 0, expected 1)` from that operator. ⛔ The row stays BECAUSE the door is correct — it is what keeps the next reader from "fixing" a door that looks exactly like the three real bugs (#931/#932 sockets, #952 Augeas). (MiniMagick's `self.create(ext, &block)` does yield a Tempfile to write into, but rbgo binds no `Images::Image.create`, so that is not a door here.) Settles #950.