Free closed IPC domain objects and give their ids out again, and keep a domain alive while its objects are.

A domain's objects[32] only grew: CloseVirtualHandle returned without freeing anything, so a client that opens and
closes sub-objects (the applet proxy getters, about 17 at boot) would get IPC_R_UNKNOWN_COMMAND for its 33rd, which the
SDK aborts on. The handlers took the new object's id from nobjects, the table had no lock (two threads could race on
nobjects), and a sub-object reached through its own handle (CopyFromCurrentDomain) kept a root pointer that the
domain's last CloseHandle had freed.

The domain table now has a lock in ipc.c (g_domains), taken only for sessions in a domain and never across a handler
or a close callback. ipc_domain_add gives out the first free slot and returns the id the handlers write back; the
table holds one reference on each object and each object one on its domain (held_root), counted in the domain's subs.
CloseVirtualHandle clears the slot and drops the table's reference; it never closes the domain's own id. A request
takes a reference on its target object under the lock for its length, and a cleared id is answered with
IPC_R_BAD_HANDLE instead of being dereferenced; CopyFromCurrentDomain answers the same. When a domain's remaining
references are all its objects', its client has closed it: the table lets the objects go, each object nothing else
holds is freed and lets go of the domain, and the domain is freed with the last one. Domain objects now count in
ipc_sessions_live from the moment they are added, since their last reference counts them out.

Not reproduced before the change: no log here has TRACE=ipc, so the getters per return to the title are not counted.
ipc_session_check gains a domain section: appletOE converted to a domain, 100 rounds of a getter and its
CloseVirtualHandle each get id 3 with the table at 3 slots, a closed id gets a bad-handle result, the domain's own id
survives a CloseVirtualHandle, a proxy with a handle of its own keeps the closed domain until that handle closes and
then both are freed, and closing a domain frees its proxy and five getters. The compile check of every runtime file
passes with no new warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:26:03 -05:00
parent d3a5c3c353
commit e9becc12f3
3 changed files with 181 additions and 27 deletions

View File

@@ -41,8 +41,11 @@ typedef struct {
typedef struct IpcSession IpcSession;
typedef void (*IpcHandler)(IpcSession *s, const IpcRequest *rq, IpcResponse *rs);
// One server object. Every kernel handle to it (the first one, CloneCurrentObject copies) holds a reference; the
// last CloseHandle calls close(state).
// One server object. Every kernel handle to it (the first one, CloneCurrentObject copies) holds a reference, and so
// does a request on it while it runs; the last reference calls close(state). A domain's table holds one on each of
// its objects, and each object one on its domain (held_root), until the client closes the object's id
// (CloseVirtualHandle) or its last handle to the domain itself. ipc.c's domain lock guards objects, nobjects, subs
// and dom_closed.
struct IpcSession {
const char *name;
IpcHandler handler;
@@ -51,12 +54,16 @@ struct IpcSession {
int32_t refs;
int is_domain; // ConvertCurrentObjectToDomain ran: requests carry a domain header
IpcSession *root; // the domain session every object id belongs to (objects share it)
IpcSession *objects[32]; // object 1 is this session, then what the service handed out
int nobjects;
IpcSession *objects[32]; // object 1 is this session, then what the service handed out; NULL: closed
int nobjects; // slots used so far (closed ones are given out again)
uint32_t event; // readable event handed to the client (applet state changes)
IpcSession *held_root; // the domain this object holds a reference on (ipc_domain_add)
int32_t subs; // as a domain: the references its objects hold on it
int dom_closed; // as a domain: its client closed it and its table let the objects go
};
// Register an object inside a domain (1-based id returned, 0 when full).
// Register an object inside a domain: the table takes the object's first reference (the caller's), the object one on
// the domain. The 1-based id, 0 when the table is full or the domain closed.
unsigned ipc_domain_add(IpcSession *domain, IpcSession *object);
uint32_t ipc_session_new(const char *name, IpcHandler handler, void *state, void (*close)(void *state)); // handle