Syscalls - Changed SysGetMessage to provide a buffer size
authorJohn Hodge <[email protected]>
Thu, 4 Oct 2012 13:41:13 +0000 (21:41 +0800)
committerJohn Hodge <[email protected]>
Thu, 4 Oct 2012 13:41:13 +0000 (21:41 +0800)
KernelLand/Kernel/include/threads.h
KernelLand/Kernel/messages.c
KernelLand/Kernel/syscalls.c
Usermode/Applications/axwin3_src/WM/ipc.c
Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c
Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h

index 0362cdb..6382cf0 100644 (file)
@@ -37,6 +37,6 @@ extern char   **Threads_GetCWD(void);
 extern char    **Threads_GetChroot(void);
 
 extern int     Proc_SendMessage(Uint Dest, int Length, void *Data);
-extern int     Proc_GetMessage(Uint *Source, void *Buffer);
+extern int     Proc_GetMessage(Uint *Source, Uint BufSize, void *Buffer);
 
 #endif
index 09c8c14..c991978 100644 (file)
@@ -78,10 +78,11 @@ int Proc_SendMessage(Uint Dest, int Length, void *Data)
  * \fn int Proc_GetMessage(Uint *Source, void *Buffer)
  * \brief Gets a message
  * \param Source       Where to put the source TID
+ * \param BufSize      Size of \a Buffer, only this many bytes will be copied
  * \param Buffer       Buffer to place the message data (set to NULL to just get message length)
  * \return Message length
  */
-int Proc_GetMessage(Uint *Source, void *Buffer)
+int Proc_GetMessage(Uint *Source, Uint BufSize, void *Buffer)
 {
         int    ret;
        void    *tmp;
@@ -113,7 +114,7 @@ int Proc_GetMessage(Uint *Source, void *Buffer)
        // Get message
        if(Buffer != GETMSG_IGNORE)
        {
-               if( !CheckMem( Buffer, cur->Messages->Length ) )
+               if( !CheckMem( Buffer, BufSize ) )
                {
                        LOG("Invalid buffer");
                        errno = -EINVAL;
@@ -121,8 +122,15 @@ int Proc_GetMessage(Uint *Source, void *Buffer)
                        LEAVE('i', -1);
                        return -1;
                }
+               if( BufSize < cur->Messages->Length )
+                       Log_Notice("Threads", "Buffer of 0x%x passed, but 0x%x long message, truncated",
+                               BufSize, cur->Messages->Length);
+               else if( BufSize < cur->Messages->Length )
+                       BufSize = cur->Messages->Length;
+               else
+                       ;       // equal
                LOG("Copied to buffer");
-               memcpy(Buffer, cur->Messages->Data, cur->Messages->Length);
+               memcpy(Buffer, cur->Messages->Data, BufSize);
        }
        ret = cur->Messages->Length;
        
index 0348d57..bfd68bf 100644 (file)
@@ -135,13 +135,10 @@ void SyscallHandler(tSyscallRegs *Regs)
        // -- Check for messages
        case SYS_GETMSG:
                CHECK_NUM_NULLOK( (Uint*)Regs->Arg1, sizeof(Uint) );
-               // NOTE: Can't do range checking as we don't know the size
-               // - Should be done by Proc_GetMessage
-               if( Regs->Arg2 && Regs->Arg2 != -1 && !MM_IsUser(Regs->Arg2) ) {
-                       err = -EINVAL;  ret = -1;       break;
-               }
+               if( Regs->Arg3 != -1 )
+                       CHECK_NUM_NULLOK((void*)Regs->Arg3, Regs->Arg2);
                // *Source, *Data
-               ret = Proc_GetMessage((Uint*)Regs->Arg1, (void*)Regs->Arg2);
+               ret = Proc_GetMessage((Uint*)Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
                break;
        
        // -- Get the current timestamp
index cbed7fd..1d4ac33 100644 (file)
@@ -112,12 +112,12 @@ void IPC_HandleSelect(fd_set *set)
                }
        }
 
-       while(SysGetMessage(NULL, NULL))
+       size_t  len;
+       pid_t   tid;
+       while( (len = SysGetMessage(&tid, 0, NULL)) )
        {
-               pid_t   tid;
-                int    len = SysGetMessage(&tid, NULL);
                char    data[len];
-               SysGetMessage(NULL, data);
+               SysGetMessage(NULL, len, data);
 
                IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
 //             _SysDebug("IPC_HandleSelect: Message handled");
index 2b50167..ac51e27 100644 (file)
@@ -127,7 +127,7 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
                        pid_t   tid;
                
                        // Wait for a message to arrive 
-                       while( !(len = SysGetMessage(&tid, NULL)) )
+                       while( !(len = SysGetMessage(&tid, 0, NULL)) )
                        {
                                _SysWaitEvent(THREAD_EVENT_IPCMSG);
                        }
@@ -140,7 +140,7 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
                                if( gAxWin3_MessageCallback )
                                        gAxWin3_MessageCallback(tid, len);
                                else
-                                       SysGetMessage(NULL, GETMSG_IGNORE);
+                                       SysGetMessage(NULL, 0, GETMSG_IGNORE);
                                continue ;
                        }
                        
@@ -148,10 +148,10 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
                        ret = malloc(len);
                        if(ret == NULL) {
                                _SysDebug("malloc() failed, ignoring message");
-                               SysGetMessage(NULL, GETMSG_IGNORE);
+                               SysGetMessage(NULL, 0, GETMSG_IGNORE);
                                return NULL;
                        }
-                       SysGetMessage(NULL, ret);
+                       SysGetMessage(NULL, len, ret);
                        break;
                }
                break;
index 1a954d3..2452972 100644 (file)
@@ -85,7 +85,7 @@ extern int    unlink(const char *pathname);
 
 // --- IPC ---
 extern int     SysSendMessage(pid_t dest, uint length, const void *Data);
-extern int     SysGetMessage(pid_t *src, void *Data);
+extern int     SysGetMessage(pid_t *src, uint buflen, void *Data);
 
 // --- MEMORY ---
 uint64_t       _SysGetPhys(uint vaddr);

UCC git Repository :: git.ucc.asn.au