From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alperen Erkan <erkanalperen54@gmail.com>
Date: Sat, 25 Jul 2026 00:00:00 +0300
Subject: [PATCH] Revert erroneous changes to ipc_mqueue_send

The prior patch to ipc_mqueue.c introduced no genuine fix and several
regressions, as pointed out in review:

 - Added an author/copyright claim for a handful of modified lines,
   which does not establish authorship.
 - Replaced assert(IP_VALID(port)) with a soft runtime check; the
   port is expected to already be validated by the caller, so this
   only hid a caller-side bug instead of catching it.
 - Added a NULL-kmsg guard for a value that is never NULL at this
   call site, for the same reason.
 - Introduced IPC_MQUEUE_HARD_LIMIT and used it to let
   MACH_SEND_ALWAYS sends past port->ip_qlimit, silently weakening
   the existing queue-limit enforcement.
 - Reformatted large parts of the function from tabs to spaces,
   mangling the diff and making the change hard to review.
 - Added a no-op reassignment (self->ith_state = MACH_MSG_SUCCESS)
   and a dead-code state clear before the switch, neither of which
   changes behavior.
 - Left a non-English comment in the code.

This restores ipc_mqueue_send to its original, correct form. No
functional change is being proposed for this file.
---
 gnumach/ipc/ipc_mqueue.c | 88 ++++++++++++++++-------------------------------
 1 file changed, 30 insertions(+), 58 deletions(-)

--- a/gnumach/ipc/ipc_mqueue.c	2026-07-25 08:14:49.874356085 +0000
+++ b/gnumach/ipc/ipc_mqueue.c	2026-07-25 08:14:49.875967700 +0000
@@ -27,11 +27,11 @@
  * the rights to redistribute these changes.
  */
 /*
- *  File:   ipc/ipc_mqueue.c
- *  Author: Rich Draves / Alperen ERKAN 
- *  Date:   2026 
+ *	File:	ipc/ipc_mqueue.c
+ *	Author:	Rich Draves
+ *	Date:	2026
  *
- *  Functions to manipulate IPC message queues.
+ *	Functions to manipulate IPC message queues.
  */
 
 #include <mach/port.h>
@@ -51,12 +51,6 @@
 #include <ipc/ipc_space.h>
 #include <ipc/ipc_marequest.h>
 
-/* 
- * An absolute message queue upper limit to prevent OOM and memory exhaustion.
- */
-#define IPC_MQUEUE_HARD_LIMIT 65536
-
-
 /*
  *	Routine:	ipc_mqueue_init
  *	Purpose:
@@ -159,8 +153,6 @@
 
 /*
  *	Routine:	ipc_mqueue_send
- *  Author :    Alperen ERKAN 
- * 						 2026
  *	Purpose:
  *		Send a message to a port.  The message holds a reference
  *		for the destination port in the msgh_remote_port field.
@@ -185,16 +177,8 @@
 {
 	ipc_port_t port;
 
-	/* Defensive C: Checking the incoming message object and destination port */
-	if (kmsg == IKM_NULL) {
-    	return MACH_SEND_INVALID_DATA;
-	}
-
 	port = (ipc_port_t) kmsg->ikm_header.msgh_remote_port;
-
-	if (!IP_VALID(port)) {
-   	   return MACH_SEND_INVALID_DEST;
-	}
+	assert(IP_VALID(port));
 
 	ip_lock(port);
 
@@ -243,17 +227,17 @@
 		}
 
 		/*
-         *  Don't block if:
-         *  1) We're under the queue limit.
-         *  2) Caller used MACH_SEND_ALWAYS but we are under the hard safety limit.
-         *  3) Message is sent to a send-once right.
-         */
-
-        if ((port->ip_msgcount < port->ip_qlimit) ||
-            ((option & MACH_SEND_ALWAYS) && (port->ip_msgcount < IPC_MQUEUE_HARD_LIMIT)) ||
-            (MACH_MSGH_BITS_REMOTE(kmsg->ikm_header.msgh_bits) ==
-                        MACH_MSG_TYPE_PORT_SEND_ONCE))
-            break;
+		 *  Don't block if:
+		 *	1) We're under the queue limit.
+		 *	2) Caller used the MACH_SEND_ALWAYS internal option.
+		 *	3) Message is sent to a send-once right.
+		 */
+
+		if ((port->ip_msgcount < port->ip_qlimit) ||
+		    (option & MACH_SEND_ALWAYS) ||
+		    (MACH_MSGH_BITS_REMOTE(kmsg->ikm_header.msgh_bits) ==
+						MACH_MSG_TYPE_PORT_SEND_ONCE))
+			break;
 
 		/* must block waiting for queue to clear */
 
@@ -272,53 +256,49 @@
 		ipc_thread_enqueue(&port->ip_blocked, self);
 		self->ith_state = MACH_SEND_IN_PROGRESS;
 
-	 	ip_unlock(port);
+		ip_unlock(port);
 		counter(c_ipc_mqueue_send_block++);
 		thread_block(thread_no_continuation);
 		ip_lock(port);
-  
+
 		/* why did we wake up? */
 
-        if (self->ith_state == MACH_MSG_SUCCESS) {
-            self->ith_state = MACH_MSG_SUCCESS; // veya temiz durum sıfırlaması
-            continue;
-        }
-        assert(self->ith_state == MACH_SEND_IN_PROGRESS);
-
-        /* take ourselves off blocked queue under port lock */
-        ipc_thread_rmqueue(&port->ip_blocked, self);
-        
-        /* [DEFENSIVE]: Clear state to prevent stale status reuse */
-        self->ith_state = MACH_MSG_SUCCESS;
-
-        /*
-         *  Thread wakeup-reason field tells us why
-         *  the wait was interrupted.
-         */
-
-        switch (self->ith_wait_result) {
-            case THREAD_INTERRUPTED:
-            /* send was interrupted - give up */
-
-            ip_unlock(port);
-            return MACH_SEND_INTERRUPTED;
-
-            case THREAD_TIMED_OUT:
-            /* timeout expired */
-
-            assert(option & MACH_SEND_TIMEOUT);
-            time_out = 0;
-            break;
+		if (self->ith_state == MACH_MSG_SUCCESS)
+			continue;
+		assert(self->ith_state == MACH_SEND_IN_PROGRESS);
+
+		/* take ourselves off blocked queue */
+
+		ipc_thread_rmqueue(&port->ip_blocked, self);
+
+		/*
+		 *	Thread wakeup-reason field tells us why
+		 *	the wait was interrupted.
+		 */
+
+		switch (self->ith_wait_result) {
+		    case THREAD_INTERRUPTED:
+			/* send was interrupted - give up */
+
+			ip_unlock(port);
+			return MACH_SEND_INTERRUPTED;
+
+		    case THREAD_TIMED_OUT:
+			/* timeout expired */
+
+			assert(option & MACH_SEND_TIMEOUT);
+			time_out = 0;
+			break;
 
-            case THREAD_RESTART:
-            default:
+		    case THREAD_RESTART:
+		    default:
 #if MACH_ASSERT
-            assert(!"ipc_mqueue_send: unexpected thread wakeup result");
+			assert(!"ipc_mqueue_send");
 #else
-            panic("ipc_mqueue_send");
+			panic("ipc_mqueue_send");
 #endif
-        }
-    }
+		}
+	}
 
 	if (kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
 		ip_unlock(port);
-- 
2.43.0
