Pharo test defects

Cog: SocketPlugin: an integer option value of each length, and SO_LINGER

State
Fixed in a Cog patch (not sent)
Defect in
stock Cog VM v12.0.6
Tests
0
Fix
cog-socket-option-values.patch
Upstream repository
pharo-project/pharo-vm

Record

Test

A probe that prints the same lines on a correct VM. Run it with Pharo.image eval or file it in:

socket_options.st (download)
"socket_options.st -- the socket options of the image, on a UDP socket and on
 a TCP socket.  The answer has one line for each call, so a diff of the
 output of two VMs shows each option that the VMs handle in different ways.

   scripts/vmrun.sh eval cog  scripts/cogdiff/socket_options.st
   scripts/vmrun.sh eval ours scripts/cogdiff/socket_options.st

 The names are the table of the stock VM
 (plugins/SocketPlugin/src/common/SocketPluginImpl.c, socketOptions[]), two
 names that the table does not have (SO_PRIORITY, NO_SUCH_OPTION), and the
 calls that a multicast socket makes.

 Measured 2026-11-17 (Pharo 14 build 791, macOS 27): 72 lines.  Stock Cog and
 our VM give the same answer for 68 of them.  The 4 lines that differ are two
 defects of the stock VM, each on the UDP and on the TCP socket:
   set SO_RCVBUF := 65536   stock Cog answers #(0 0), and the option then
                            reads 8388608.  The stock VM reads an integer
                            only from a value of 4 characters or less, and
                            gives '65536' to setsockopt as 5 characters.
   set SO_LINGER := 5       stock Cog answers nil (the primitive fails): it
                            gives setsockopt one int, and the system needs a
                            struct linger.
 Before the repair of 2026-11-17 our VM knew 6 names and answered #(0 0) for
 each other name."
| names out try run udp tcp |
names := #( 'SO_DEBUG' 'SO_REUSEADDR' 'SO_DONTROUTE' 'SO_BROADCAST' 'SO_SNDBUF' 'SO_RCVBUF' 'SO_KEEPALIVE'
	'SO_OOBINLINE' 'SO_LINGER' 'IP_TTL' 'IP_HDRINCL' 'IP_MULTICAST_IF' 'IP_MULTICAST_TTL' 'IP_MULTICAST_LOOP'
	'IP_ADD_MEMBERSHIP' 'TCP_MAXSEG' 'TCP_NODELAY' 'SO_REUSEPORT' 'SO_PRIORITY' 'NO_SUCH_OPTION' ).
out := WriteStream on: String new.
try := [ :block | [ block value printString ] on: Error do: [ :e | 'error ' , e class name ] ].
run := [ :label :socket |
	names do: [ :each |
		out nextPutAll: label; nextPutAll: ' get '; nextPutAll: each; nextPutAll: ' -> ';
			nextPutAll: (try value: [ socket getOption: each ]); lf ].
	{ 'SO_REUSEADDR' -> true. 'SO_REUSEPORT' -> true. 'SO_BROADCAST' -> true. 'SO_KEEPALIVE' -> true.
	  'IP_TTL' -> 7. 'IP_MULTICAST_TTL' -> 3. 'IP_MULTICAST_LOOP' -> false.
	  'IP_MULTICAST_IF' -> #[ 127 0 0 1 ].
	  'IP_ADD_MEMBERSHIP' -> #[ 224 0 0 251 127 0 0 1 ].
	  'IP_DROP_MEMBERSHIP' -> #[ 224 0 0 251 127 0 0 1 ].
	  'TCP_NODELAY' -> 1. 'SO_SNDBUF' -> 8192. 'SO_RCVBUF' -> 65536. 'SO_LINGER' -> 5.
	  'SO_PRIORITY' -> 1. 'NO_SUCH_OPTION' -> 1 } do: [ :each |
		out nextPutAll: label; nextPutAll: ' set '; nextPutAll: each key; nextPutAll: ' := '; print: each value;
			nextPutAll: ' -> '; nextPutAll: (try value: [ socket setOption: each key value: each value ]);
			nextPutAll: '  then get -> '; nextPutAll: (try value: [ socket getOption: each key ]); lf ] ].
udp := Socket newUDP.
tcp := Socket newTCP.
[ run value: 'udp' value: udp.
  run value: 'tcp' value: tcp ] ensure: [ udp closeAndDestroy. tcp closeAndDestroy ].
out contents

Names such as scripts/vmrun.sh and scripts/cogdiff/ are scripts of the iospharo repository, which runs one VM at a time. cog is stock Cog, cogp is Cog with the change, ours is iospharo.

Fix

A change of the Cog sources (pharo-project/pharo-vm, branch pharo-12), on the local branch fix/socket-option-values. State: committed on the branch, tested. Not sent upstream.

cog-socket-option-values.patch (download)
From 7e941fc475a4484bbdc4f8b603de3cd7889abdde Mon Sep 17 00:00:00 2001
From: Aaron Wohl
Date: Tue, 17 Nov 2026 11:40:56 -0500
Subject: [PATCH] SocketPlugin: an integer option value of each length, and
 SO_LINGER as a struct linger

sqSocketSetOptions read an integer only from a value of sizeof(int)
characters or less.  Socket>>setOption: 'SO_RCVBUF' value: 65536 went to
setsockopt as the 5 characters '65536', and the system read the first 4 of
them as the int: the buffer became 8 MB on macOS.  A value that reads as an
integer is now an int of each length.

SO_LINGER is in the option table, but setsockopt got one int for it and
answered EINVAL, so the option could not be set.  The value is now the
linger time in a struct linger (0 sets the option off), and the get
primitive answers the linger time.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
 .../src/common/SocketPluginImpl.c             | 31 +++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/plugins/SocketPlugin/src/common/SocketPluginImpl.c b/plugins/SocketPlugin/src/common/SocketPluginImpl.c
index 34a69938f..40ad60c93 100644
--- a/plugins/SocketPlugin/src/common/SocketPluginImpl.c
+++ b/plugins/SocketPlugin/src/common/SocketPluginImpl.c
@@ -1576,13 +1576,27 @@ sqInt sqSocketSetOptionsoptionNameStartoptionNameSizeoptionValueStartoptionValue
 
 	  memset((void *)buf, 0, sizeof(buf));
 	  memcpy((void *)buf, optionValue, optionValueSize);
-	  if (optionValueSize <= sizeof(int)
-	   && (strtol(buf, &endptr, 0),
+	  /* A value that reads as an integer is an int of each length.  With a
+	     limit of sizeof(int) characters here, '65536' for SO_RCVBUF went to
+	     setsockopt as 5 characters, and the system read the first 4 of them
+	     as the int (a buffer of 8 MB on macOS). */
+	  if ((strtol(buf, &endptr, 0),
 	       endptr - buf == optionValueSize)) /* are all option chars digits? */
 	    {
 	      val= strtol(buf, &endptr, 0);
 		  memcpy((void *)buf, (void *)&val, sizeof(val));
 		  optionValueSize= sizeof(val);
+		  /* SO_LINGER takes a struct linger.  The value is the linger
+		     time in seconds, and 0 sets the option off. */
+		  if (opt->optlevel == SOL_SOCKET && opt->optname == SO_LINGER)
+		    {
+		      struct linger lin;
+		      memset(&lin, 0, sizeof(lin));
+		      lin.l_onoff= val > 0;
+		      lin.l_linger= val;
+		      memcpy((void *)buf, (void *)&lin, sizeof(lin));
+		      optionValueSize= sizeof(lin);
+		    }
 	    }
 	  if ((setsockopt(PSP(s)->s, opt->optlevel, opt->optname,
 			  (const void *)buf, optionValueSize)) < 0)
@@ -1624,6 +1638,19 @@ sqInt sqSocketGetOptionsoptionNameStartoptionNameSizereturnedValue(SocketPtr s,
 		  int optval;	/* NOT sqInt */
 		  socklen_t optlen= sizeof(optval);
 
+		  if (opt->optlevel == SOL_SOCKET && opt->optname == SO_LINGER) {
+			  /* The answer is the linger time, or 0 when the option is off. */
+			  struct linger lin;
+			  socklen_t linlen= sizeof(lin);
+
+			  if (getsockopt(PSP(s)->s, SOL_SOCKET, SO_LINGER, (void *)&lin, &linlen) < 0) {
+				  success(false);
+				  return getLastSocketError();
+			  }
+			  *result = lin.l_onoff ? lin.l_linger : 0;
+			  return 0;
+		  }
+
 		  if (((getsockopt(PSP(s)->s, opt->optlevel, opt->optname, (void *)&optval, &optlen)) < 0) || optlen != sizeof(optval)){
 			  success(false);
 			  return getLastSocketError();
-- 
2.54.0 (Apple Git-157)

State of 2026-11-20. Made from the test records of the iospharo project (a second Pharo VM). The fixes are proposals for review.