Exemple #1
0
        private bool Sys_Read()
        {
            // get fd index
            UInt64 fd_index = RBX;

            if (fd_index >= (UInt64)FDCount)
            {
                Terminate(ErrorCode.OutOfBounds); return(false);
            }

            // get fd
            IFileWrapper fd = FileDescriptors[fd_index];

            if (fd == null)
            {
                Terminate(ErrorCode.FDNotInUse); return(false);
            }

            // make sure we can read from it
            if (!fd.CanRead())
            {
                Terminate(ErrorCode.FilePermissions); return(false);
            }

            // make sure we're in bounds
            if (RCX >= MemorySize || RDX >= MemorySize || RCX + RDX > MemorySize)
            {
                Terminate(ErrorCode.OutOfBounds); return(false);
            }
            // make sure we're not in the readonly segment
            if (RCX < ReadonlyBarrier)
            {
                Terminate(ErrorCode.AccessViolation); return(false);
            }

            // read from the file
            try
            {
                Int64 n = fd.Read(Memory, (int)RCX, (int)RDX);

                // if we got nothing but it's interactive
                if (n == 0 && fd.IsInteractive())
                {
                    --RIP;                // await further data by repeating the syscall
                    SuspendedRead = true; // suspend execution until there's more data
                }
                // otherwise success - return num chars read from file
                else
                {
                    RAX = (UInt64)n;
                }
            }
            // errors are failures - return -1
            catch (Exception) { RAX = ~(UInt64)0; }

            return(true);
        }