Skip to content

NSConnection: honor bycopy and byref on returned objects - #910

Open
probonopd wants to merge 1 commit into
gnustep:masterfrom
probonopd:fix-nsconnection-bycopy-return
Open

probonopd wants to merge 1 commit into
gnustep:masterfrom
probonopd:fix-nsconnection-bycopy-return

Conversation

@probonopd

Copy link
Copy Markdown
Contributor

A method declared to return (bycopy NSArray *) still sends its result back over a connection as a proxy: -[NSConnection _service_forwardForProxy:] encodes the return value with -encodeValueOfObjCType:at: and drops the qualifier, while arguments already go through -encodeBycopyObject: / -encodeByrefObject: (cifframe_dissect_call()). The caller then pays one round trip per message to the result, and a for ... in loop over it crashes, because -countByEnumeratingWithState:objects:count: cannot pass its C array over the connection.

Reproducer (server and client in one process, two local ports):

#import <Foundation/Foundation.h>
@protocol P
- (bycopy NSArray*) items;
@end
@interface S : NSObject <P>
@end
@implementation S
- (bycopy NSArray*) items
{
  return [NSArray arrayWithObjects: @"a", @"b", @"c", nil];
}
@end
int main()
{
  NSAutoreleasePool *arp = [NSAutoreleasePool new];
  NSPort *sp = [NSPort port];
  NSConnection *s = [[NSConnection alloc] initWithReceivePort: sp sendPort: nil];
  NSConnection *c;
  id proxy;
  id items;
  [s setRootObject: [[S new] autorelease]];
  c = [[NSConnection alloc] initWithReceivePort: [NSPort port] sendPort: sp];
  proxy = [c rootProxy];
  [proxy setProtocolForProxy: @protocol(P)];
  items = [proxy items];
  printf("items is %s\n", [items isProxy] ? "a proxy" : "a copy");
  for (id o in items)
    {
      printf("%s\n", [o UTF8String]);
    }
  [arp release];
  return 0;
}
master:            items is a proxy
                   Segmentation fault (exit 139)
with this change:  items is a copy
                   a
                   b
                   c

The change encodes a returned object with -encodeBycopyObject: or -encodeByrefObject: when the return type carries the qualifier, as is done for arguments; unqualified return values are encoded as before. The new test Tests/base/NSConnection/bycopyReturn.m checks bycopy, byref and unqualified returns:

$ cd Tests && gnustep-tests base/NSConnection base/NSMessagePort base/NSProxy
master:            Failed test: bycopyReturn.m:63 ... a bycopy return value arrives as a copy, not as a proxy
                   106 Passed tests, 1 Failed test
with this change:  107 Passed tests

cc @pkgdemon

@probonopd
probonopd requested a review from rfm as a code owner November 28, 2026 09:12
@probonopd
probonopd force-pushed the fix-nsconnection-bycopy-return branch from a0cd7ae to 9f353a3 Compare November 28, 2026 09:56
@probonopd

Copy link
Copy Markdown
Contributor Author

The failing job "Windows arm64 MSVC Clang gnustep-2.2" is not caused by this change. It fails on master with the same test and message: basic.m:54 ... call stack addresses is not empty (one failed test in each run).

This PR only touches Source/NSConnection.m and adds Tests/base/NSConnection/bycopyReturn.m.

A method declared to return (bycopy NSArray *) still sent its result
back over a connection as a proxy. The server side encoded the return
value with -encodeValueOfObjCType:at: and ignored the qualifier, while
arguments already went through -encodeBycopyObject: and
-encodeByrefObject:. The caller then paid one round trip for every
message to the result, and fast enumeration of it crashed, because
-countByEnumeratingWithState:objects:count: cannot pass its C array
over the connection.

The returned object is now encoded with -encodeBycopyObject: or
-encodeByrefObject: when the method's return type carries the
qualifier, in the same way as for arguments. Unqualified return values
are encoded as before. Tests/base/NSConnection/bycopyReturn.m checks
all three cases over a pair of local ports.
@probonopd
probonopd force-pushed the fix-nsconnection-bycopy-return branch from 9f353a3 to be6cb2c Compare November 28, 2026 10:36
@probonopd

Copy link
Copy Markdown
Contributor Author

The GCC job aborts of Tests/base/NSTask/posix_spawn_multithread.m (Ubuntu x64 GCC https://github.com/gnustep/libs-base/actions/runs/46129340154/job/140930868162 and GCC-4.9.4 https://github.com/gnustep/libs-base/actions/runs/46132795847/job/140941455958) are an intermittent hang in NSTask that does not depend on this change. The same jobs passed on the other runs of this branch, and the test does not use NSConnection.

I ran that test binary 120 times (6 in parallel, 40 second limit) against a libgnustep-base built from master (this PR's Source/NSConnection.m change reverted) and 120 times against one built with this PR. Both built the same way (same tree, only NSConnection.m differs, local Linux build, not GCC).

  • master: 1 hang, 3 segfaults
  • with this PR: 1 hang, 1 segfault

gdb on the hung master process shows thread 2 in -[TaskRunner runTask:] -> -[NSTask waitUntilExit] (NSTask.m:896) -> -[NSRunLoop runMode:beforeDate:] -> poll(), and the main thread in main() at posix_spawn_multithread.m:130 spinning the run loop. No NSConnection code is on either stack.

@rfm

rfm commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This fix is obviously correct, and the fast enumeration issue is important, and the change really looks to be safe, but I think it's just too late to be included in the next release. We should do a bug fix release in January with it.

@rfm

rfm commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

PS the info about NSTask is really interesting. A Google search reveals a lot of people have similar issues with Apple's implementation not working properly when you wait for task exit outside the main thread too. I have some ideas about fixing that, but again that's not something we can address right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants