Пример #1
0
 public void Write(ByteBufferRef buf, int offset)
 {
     Contract.Assert(offset >= 0 && offset + Size <= buf.Length);
     Deserializer.WriteInt(fd, buf, offset);
     Deserializer.WriteShort(events, buf, offset + sizeof(int));
     Deserializer.WriteShort(revents, buf, offset + sizeof(int) + sizeof(short));
 }
Пример #2
0
 public void Write(ByteBufferRef buf, int offset)
 {
     Contract.Assert(offset >= 0 && offset + Size <= buf.Length);
     Deserializer.WriteInt(fd, buf, offset);
     Deserializer.WriteShort(events, buf, offset + sizeof(int));
     Deserializer.WriteShort(revents, buf, offset + sizeof(int) + sizeof(short));
 }
Пример #3
0
        public static unsafe void CalculateHMAC(int pg_offset, uint filesize, byte[] page_signatures, byte *result)
        {
            var b   = CalculateHMAC(pg_offset, filesize, page_signatures);
            var buf = new ByteBufferRef(new IntPtr(result), (int)SHA1Managed.SHA1HashSize);

            buf.CopyFrom(0, b);
        }
Пример #4
0
 public static void DumpUserBuf(Thread current, UserPtr writeBuf, int size)
 {
     var buf = new byte[(size + 3) / 4];
     var buf_ref = new ByteBufferRef(buf);
     writeBuf.Read(current, buf, size);
     DumpBuf(new Pointer(buf_ref.Location), size);
 }
Пример #5
0
        internal int Write(Thread current, ref Arch.ExceptionRegisters regs, ref ByteBufferRef buf, int len)
        {
            Contract.Requires(GhostOwner == current.Parent);
            var ret = inode.Write(current, ref regs, ref buf, len, position, this);

            return(ret);
        }
Пример #6
0
        private static int ReadUserBuffer(Thread current, ByteBufferRef src, int buf_offset, CachePage page, int page_offset, int len)
        {
            var dst = new ByteBufferRef(page.Buffer.Location + page_offset, len);

            dst.CopyFrom(buf_offset, src);
            return(0);
        }
Пример #7
0
 internal ThreadCompletionEntryWithBuffer(Thread current, Kind kind, ByteBufferRef buf)
     : base(current, kind)
 {
     Contract.Ensures(this.buf.Length == buf.Length);
     Contract.Ensures(this.buf.Location == buf.Location);
     this.buf = buf;
 }
Пример #8
0
        internal int TranslateToUserFdlist(Thread current, ByteBufferRef buf, int poll_ret, int maxfds, UserPtr userPtr, short event_type)
        {
            Contract.Requires(poll_ret * pollfd.Size < buf.Length);

            if (userPtr == UserPtr.Zero)
                return 0;

            var res = 0;
            var len = (maxfds + 7) / 8;
            var vec = new FixedSizeBitVector(maxfds);

            for (int i = 0; i < poll_ret; i++)
            {
                var poll_struct = pollfd.Deserialize(buf, i * pollfd.Size);
                var linux_fd = poll_struct.fd;
                var node = Lookup(linux_fd);
                if (node == null)
                    return -ErrorCode.EBADF;

                if ((poll_struct.revents & event_type & node.event_type) != 0)
                {
                    vec.Set(node.expressos_fd);
                    ++res;
                }
            }

            if (userPtr.Write(current, vec.Buffer) != 0)
                return -ErrorCode.EFAULT;

            return res;
        }
Пример #9
0
        internal void WriteString16(string s)
        {
            var r = new ByteBufferRef(Buffer);

            if (cursor + sizeof(int) > Buffer.Length)
            {
                return;
            }

            WriteInt32(s.Length);

            foreach (var c in s)
            {
                if (cursor + sizeof(short) > r.Length)
                {
                    return;
                }
                Deserializer.WriteShort((short)c, r, cursor);
                cursor += sizeof(short);
            }

            if (cursor + sizeof(short) > r.Length)
            {
                return;
            }

            Deserializer.WriteShort(0, r, cursor);
            cursor += sizeof(short);
            Pad();
        }
Пример #10
0
 public void CopyFrom(int offset, ByteBufferRef src)
 {
     Contract.Requires(offset >= 0 && offset < Length);
     for (var i = 0; i < src.Length && i + offset < length; ++i)
     {
         Set(i + offset, src[i]);
     }
 }
Пример #11
0
        private unsafe int Read(Thread current, void *dst, int length)
        {
            var process   = current.Parent;
            var buf       = new ByteBufferRef(new IntPtr(dst), length);
            var bytesRead = Read(process, buf, false);

            return(length - bytesRead);
        }
Пример #12
0
        public static void WriteInt(int val, ByteBufferRef buf, int offset)
        {
            Contract.Requires(offset >= 0);
            Contract.Requires(buf.Length >= offset + sizeof(int));
            var v = (uint)val;

            WriteUInt(v, buf, offset);
        }
Пример #13
0
        public short revents; /* returned events */

        #endregion Fields

        #region Methods

        public static pollfd Deserialize(ByteBufferRef buf, int offset)
        {
            Contract.Assert(offset >= 0 && offset + Size <= buf.Length);
            pollfd res;
            res.fd = Deserializer.ReadInt(buf, offset);
            res.events = Deserializer.ReadShort(buf, offset + sizeof(int));
            res.revents = Deserializer.ReadShort(buf, offset + sizeof(int) + sizeof(short));
            return res;
        }
Пример #14
0
        private int Write(Thread current, ByteBufferRef buf, int len, uint pos)
        {
            var       writtenBytes     = 0;
            var       remainedBytes    = len;
            var       currentPageIndex = (int)Arch.ArchDefinition.PageIndex(pos);
            CachePage page             = null;

            while (remainedBytes > 0)
            {
                page = Pages.Lookup(currentPageIndex / Arch.ArchDefinition.PageSize);
                if (page == null)
                {
                    page = CachePage.Allocate(current.Parent, currentPageIndex / Arch.ArchDefinition.PageSize);

                    int currentBlockId = currentPageIndex / Arch.ArchDefinition.PageSize;

                    if (currentBlockId < OnDiskBlock)
                    {
                        // Case (1)
                        var succeed = page.Load(this);
                        Contract.Assert(page.Next == null);
                        if (!succeed)
                        {
                            return(-ErrorCode.EIO);
                        }
                    }
                    else
                    {
                        // Case (2) / (3)
                        // assert cachedPage.Empty();
                    }
                    Pages.Add(page);
                }

                // Copying
                int pageCursor = Arch.ArchDefinition.PageOffset((int)(pos + writtenBytes));
                int chunkLen   = Arch.ArchDefinition.PageSize - pageCursor < remainedBytes ? Arch.ArchDefinition.PageSize - pageCursor : remainedBytes;

                var left = ReadUserBuffer(current, buf, writtenBytes, page, pageCursor, chunkLen);

                writtenBytes += chunkLen - left;
                if (left != 0)
                {
                    return(-ErrorCode.EFAULT);
                }

                remainedBytes -= chunkLen;
                // Update FileSize
                if (pos + writtenBytes > FileSize)
                {
                    FileSize = (uint)(pos + writtenBytes);
                }

                currentPageIndex += Arch.ArchDefinition.PageSize;
            }
            return(writtenBytes);
        }
Пример #15
0
 public int Read(ByteBufferRef buffer, int offset, int count, ref uint pos)
 {
     switch (kind)
     {
     case INodeKind.ArchINodeKind:
         return(ArchINode.ReadImpl(buffer, offset, count, ref pos));
     }
     return(-ErrorCode.EINVAL);
 }
Пример #16
0
 public VBinderMessage(Thread from, Thread target, int label, ByteBufferRef payload, int length)
 {
     Contract.Ensures(GhostTarget == target);
     this.from = from;
     this.label = label;
     this.payload = payload;
     this.GhostTarget = target;
     this.Length = length;
 }
Пример #17
0
 public VBinderMessage(Thread from, Thread target, int label, ByteBufferRef payload, int length)
 {
     Contract.Ensures(GhostTarget == target);
     this.from        = from;
     this.label       = label;
     this.payload     = payload;
     this.GhostTarget = target;
     this.Length      = length;
 }
Пример #18
0
        private static void DumpUserBuf(Thread current, UserPtr writeBuf, int size)
        {
            var buf = new byte[(size + 3) / 4];

            writeBuf.Read(current, buf, size);

            var buf_ref = new ByteBufferRef(buf);

            DumpBuf(new Pointer(buf_ref.Location), size);
        }
Пример #19
0
        public static pollfd Deserialize(ByteBufferRef buf, int offset)
        {
            Contract.Assert(offset >= 0 && offset + Size <= buf.Length);
            pollfd res;

            res.fd      = Deserializer.ReadInt(buf, offset);
            res.events  = Deserializer.ReadShort(buf, offset + sizeof(int));
            res.revents = Deserializer.ReadShort(buf, offset + sizeof(int) + sizeof(short));
            return(res);
        }
Пример #20
0
 public static void WriteULong(ulong val, ByteBufferRef buf, int offset)
 {
     Contract.Requires(offset >= 0);
     Contract.Requires(buf.Length >= offset + sizeof(long));
     for (var i = 0; i < sizeof(long); ++i)
     {
         Contract.Assert(offset + i < buf.Length);
         buf.Set(offset + i, (byte)((val >> (8 * i)) & 0xff));
     }
 }
Пример #21
0
        internal void WriteInt32(int v)
        {
            // Fail sliently
            if (cursor + sizeof(int) > Buffer.Length)
                return;

            var r = new ByteBufferRef(Buffer);
            Deserializer.WriteInt(v, r, cursor);
            cursor += sizeof(int);
        }
Пример #22
0
        internal BinderIPCMarshaler(Thread current, ByteBufferRef buf)
        {
            Contract.Requires(buf.Length >= kPatchTableSize * sizeof(int));

            this.buf               = buf;
            this.ReadCursor        = 0;
            this.WriteCursor       = 0;
            this.current           = current;
            this.patchTable        = new int[kPatchTableSize];
            this.CurrentPatchEntry = 0;
        }
Пример #23
0
        public PollCompletion(Thread current, UserPtr fds, int nfds, ByteBufferRef buf)
            : base(current, Kind.PollCompletionKind, buf)
        {
            Contract.Requires(nfds >= 0);
            Contract.Requires(buf.Length >= nfds * pollfd.Size);
            Contract.Ensures(fdMaps.Length == nfds);
            Contract.Ensures(this.buf.Length >= fdMaps.Length * pollfd.Size);

            this.userFdBuf = fds;
            this.fdMaps    = new int[nfds];
        }
Пример #24
0
        internal BinderIPCMarshaler(Thread current, ByteBufferRef buf)
        {
            Contract.Requires(buf.Length >= kPatchTableSize * sizeof(int));

            this.buf = buf;
            this.ReadCursor = 0;
            this.WriteCursor = 0;
            this.current = current;
            this.patchTable = new int[kPatchTableSize];
            this.CurrentPatchEntry = 0;
        }
Пример #25
0
        private int Write(Process process, Pointer dst, int length)
        {
            var bytesLeft = length;

            var src     = _value;
            var dst_buf = new ByteBufferRef(dst.ToIntPtr(), length);
            var cursor  = 0;

            while (bytesLeft > 0)
            {
                var region = process.Space.Find(src);

                // Invalid mapping
                if (region == null || region.IsFixed)
                {
                    return(bytesLeft);
                }

                var off         = Arch.ArchDefinition.PageOffset(src.ToUInt32());
                var virtualAddr = process.Space.UserToVirt(new UserPtr(src));

                if (virtualAddr == Pointer.Zero)
                {
                    // Page isn't present, try to bring it in.
                    uint permission;

                    Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, src, Pointer.Zero, out virtualAddr, out permission);

                    if (virtualAddr == Pointer.Zero)
                    {
                        break;
                    }
                }

                var virtual_page = Arch.ArchDefinition.PageIndex(virtualAddr.ToUInt32());
                var page_buf     = new ByteBufferRef(new IntPtr(virtual_page), Arch.ArchDefinition.PageSize);

                var b = Arch.ArchDefinition.PageSize - off;
                var bytesTobeCopied = b > bytesLeft ? bytesLeft : b;

                var dst_buf_page = page_buf.Slice(off, bytesTobeCopied);
                for (var i = 0; i < bytesTobeCopied; ++i)
                {
                    dst_buf_page.Set(i, dst_buf.Get(cursor + i));
                }

                bytesLeft -= bytesTobeCopied;
                src       += bytesTobeCopied;
                cursor    += bytesTobeCopied;
            }

            return(bytesLeft);
        }
Пример #26
0
        internal int SFSWrite(Thread current, ref Arch.ExceptionRegisters regs, ByteBufferRef buf, int len, uint pos, File file)
        {
            var writtenBytes = Write(current, buf, len, pos);

            if (writtenBytes <= 0)
            {
                return(writtenBytes);
            }

            file.position = (uint)(pos + writtenBytes);
            return(writtenBytes);
        }
Пример #27
0
 public static uint ReadUInt(ByteBufferRef buf, int offset)
 {
     Contract.Requires(offset >= 0);
     Contract.Requires(buf.Length >= offset + sizeof(uint));
     uint res = 0;
     for (var i = 0; i < sizeof(uint); ++i)
     {
         Contract.Assert(offset + i < buf.Length);
         res += (uint)buf.Get(offset + i) << (8 * i);
     }
     return res;
 }
Пример #28
0
        public static short ReadShort(ByteBufferRef buf, int offset)
        {
            Contract.Requires(offset >= 0);
            Contract.Requires(buf.Length >= offset + sizeof(ushort));
            uint res = 0;

            for (var i = 0; i < sizeof(short); ++i)
            {
                Contract.Assert(offset + i < buf.Length);
                res += (uint)buf.Get(offset + i) << (8 * i);
            }
            return((short)res);
        }
Пример #29
0
        // like strncpy, but return the number of bytes copied.
        // It never add 0 to terminate the string
        public static int CopyString(ref ByteBufferRef dst, int cursor, ByteBufferRef src)
        {
            Contract.Requires(cursor >= 0 && cursor < dst.Length);

            var i = 0;
            while (i + cursor < dst.Length && i < src.Length && src.Get(i) != 0)
            {
                Contract.Assert(i < src.Length);
                dst.Set(i + cursor, src.Get(i));
                ++i;
            }
            return i;
        }
Пример #30
0
        public static ulong ReadUlong(ByteBufferRef buf, int offset)
        {
            Contract.Requires(offset >= 0);
            Contract.Requires(buf.Length >= offset + sizeof(long));
            ulong res = 0;

            for (var i = 0; i < sizeof(long); ++i)
            {
                Contract.Assert(offset + i < buf.Length);
                res += (ulong)buf.Get(offset + i) << (8 * i);
            }
            return(res);
        }
Пример #31
0
        internal void WriteInt32(int v)
        {
            // Fail sliently
            if (cursor + sizeof(int) > Buffer.Length)
            {
                return;
            }

            var r = new ByteBufferRef(Buffer);

            Deserializer.WriteInt(v, r, cursor);
            cursor += sizeof(int);
        }
Пример #32
0
        private int PrepareBuffer(ByteBufferRef buf, CachePage[] sealed_page)
        {
            var cursor = 0;

            // Metadata
            for (var i = 0; i < DataPageOffset; ++i)
            {
                Deserializer.WriteInt(i, buf, cursor);
                cursor += sizeof(int);
            }

            for (var i = 0; i < sealed_page.Length; ++i)
            {
                var pgoffset = sealed_page[i].Location;
                Deserializer.WriteInt(pgoffset + DataPageOffset, buf, cursor);
                cursor += sizeof(int);
                if (pgoffset > MaximumPageOffset())
                {
                    Arch.Console.WriteLine("SecureFSINode::FlushAndCloseAsync, file too big");
                    Utils.Panic();
                    return(-1);
                }
            }

            var metadata_cursor = cursor;

            cursor += DataPageOffset * Arch.ArchDefinition.PageSize;

            // Copy data
            var sha1 = new SHA1Managed();

            for (var i = 0; i < sealed_page.Length; ++i)
            {
                var page = sealed_page[i];
                buf.CopyFrom(cursor, page.Buffer);
                sha1.Reset();
                sha1.Input(page.Buffer);
                var r = sha1.GetResult();

                for (var j = 0; j < r.Length; ++j)
                {
                    Signatures[page.Location * HMACSize + j] = r[j];
                }

                page.Dispose();
                cursor += Arch.ArchDefinition.PageSize;
            }

            SerializeMetadata(buf, metadata_cursor);
            return(0);
        }
Пример #33
0
        private CachePage(Process owner, int location, ByteBufferRef buf)
        {
            Contract.Requires(location >= 0);
            Contract.Ensures(Owner == owner);
            Contract.Ensures(Location == location);
            Contract.Ensures(CurrentState == State.Empty);
            Contract.Ensures(Next == null);

            this.Owner        = owner;
            this.Location     = location;
            this.Buffer       = buf;
            this.Next         = null;
            this.CurrentState = State.Empty;
        }
Пример #34
0
        // Duplicated from Input(byte[])
        public int Input(ByteBufferRef message_array)
        {
            if (message_array.Length == 0)
            {
                return(0);
            }

            var length = message_array.Length;

            if (Computed)
            {
                Corrupted = true;
                return(-1);
            }

            if (Corrupted)
            {
                return(-1);
            }

            int i = 0;

            Contract.Assert(i + length == message_array.Length);
            while (length > 0 && !Corrupted)
            {
                Contract.Assert(i >= 0);
                Contract.Assert(i + length == message_array.Length);
                Message_Block[Message_Block_Index++] = message_array.Get(i);

                Length_Low += 8;
                if (Length_Low == 0)
                {
                    Length_High++;
                    if (Length_High == 0)
                    {
                        /* Message is too long */
                        Corrupted = true;
                    }
                }

                if (Message_Block_Index == 64)
                {
                    SHA1ProcessMessageBlock();
                }

                i++;
                length--;
            }
            return(0);
        }
Пример #35
0
        private CachePage(Process owner, int location, ByteBufferRef buf)
        {
            Contract.Requires(location >= 0);
            Contract.Ensures(Owner == owner);
            Contract.Ensures(Location == location);
            Contract.Ensures(CurrentState == State.Empty);
            Contract.Ensures(Next == null);

            this.Owner = owner;
            this.Location = location;
            this.Buffer = buf;
            this.Next = null;
            this.CurrentState = State.Empty;
        }
Пример #36
0
        // like strncpy, but return the number of bytes copied.
        // It never add 0 to terminate the string
        public static int CopyString(ref ByteBufferRef dst, int cursor, ByteBufferRef src)
        {
            Contract.Requires(cursor >= 0 && cursor < dst.Length);

            var i = 0;

            while (i + cursor < dst.Length && i < src.Length && src.Get(i) != 0)
            {
                Contract.Assert(i < src.Length);
                dst.Set(i + cursor, src.Get(i));
                ++i;
            }
            return(i);
        }
Пример #37
0
        public static int Strnlen(ByteBufferRef buf, int max_len)
        {
            var i = 0;

            while (i < max_len && i < buf.Length)
            {
                if (buf.Get(i) == 0)
                {
                    return(i);
                }

                ++i;
            }
            return(i);
        }
Пример #38
0
        public void Write(ByteBufferRef buf, int offset)
        {
            Contract.Requires(offset >= 0);
            Contract.Requires(offset + Size <= buf.Length);

            Deserializer.WriteUInt(HandleOrPtr.Value.ToUInt32(), buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(cookie.Value.ToUInt32(), buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(code, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(flags, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(sender_pid, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(sender_euid, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(data_size, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(offsets_size, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(data_buffer.Value.ToUInt32(), buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(data_offsets.Value.ToUInt32(), buf, offset); offset += sizeof(uint);
        }
Пример #39
0
        private void SerializeMetadata(ByteBufferRef buf, int cursor)
        {
            Deserializer.WriteULong(HeaderMagic, buf, cursor);
            cursor += sizeof(ulong);
            var hmac = CalculateHMAC(DataPageOffset, FileSize, Signatures);

            buf.CopyFrom(cursor, hmac);
            cursor += HMACSize;
            Deserializer.WriteInt(DataPageOffset, buf, cursor);
            cursor += sizeof(int);
            Deserializer.WriteUInt(FileSize, buf, cursor);
            cursor += sizeof(uint);
            Deserializer.WriteInt(Signatures.Length, buf, cursor);
            cursor += sizeof(int);
            buf.CopyFrom(cursor, Signatures);
        }
Пример #40
0
        public void Write(ByteBufferRef buf, int offset)
        {
            Contract.Requires(offset >= 0);
            Contract.Requires(offset + Size <= buf.Length);

            Deserializer.WriteUInt(HandleOrPtr.Value.ToUInt32(), buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(cookie.Value.ToUInt32(), buf, offset); offset      += sizeof(uint);
            Deserializer.WriteUInt(code, buf, offset); offset         += sizeof(uint);
            Deserializer.WriteUInt(flags, buf, offset); offset        += sizeof(uint);
            Deserializer.WriteUInt(sender_pid, buf, offset); offset   += sizeof(uint);
            Deserializer.WriteUInt(sender_euid, buf, offset); offset  += sizeof(uint);
            Deserializer.WriteUInt(data_size, buf, offset); offset    += sizeof(uint);
            Deserializer.WriteUInt(offsets_size, buf, offset); offset += sizeof(uint);
            Deserializer.WriteUInt(data_buffer.Value.ToUInt32(), buf, offset); offset  += sizeof(uint);
            Deserializer.WriteUInt(data_offsets.Value.ToUInt32(), buf, offset); offset += sizeof(uint);
        }
Пример #41
0
        private static int UnmarshalDataEntries(Thread current, ByteBufferRef buf, ref sys_binder_write_desc desc)
        {
            var cursor = desc.read_consumed;
            var i      = 0;

            while (i < desc.patch_table_entries && cursor < buf.Length)
            {
                if (cursor + 2 * sizeof(uint) > buf.Length)
                {
                    return(-1);
                }

                var offset = Deserializer.ReadUInt(buf, cursor);
                cursor += sizeof(uint);
                var length = (int)Deserializer.ReadUInt(buf, cursor);
                cursor += sizeof(int);

                if (cursor + length > buf.Length)
                {
                    return(-1);
                }

                //Arch.Console.Write("UnmarshalDataEntries: offset=");
                //Arch.Console.Write(offset);
                //Arch.Console.Write(" length=");
                //Arch.Console.Write(length);
                //Arch.Console.WriteLine();

                //BinderIPCMarshaler.DumpBuf(marshaledPtr, length);
                var b = buf.Slice(cursor, length);

                if (b.Length + offset > current.Parent.binderVMSize)
                {
                    return(-1);
                }

                Contract.Assert(offset + b.Length <= current.Parent.binderVMSize);
                if ((current.Parent.binderVMStart + offset).Write(current, b) != 0)
                {
                    return(-1);
                }

                cursor += length;
                i++;
            }
            return(0);
        }
Пример #42
0
        public static binder_transaction_data Deserialize(ByteBufferRef buf, int offset)
        {
            Contract.Requires(offset >= 0);
            Contract.Requires(offset + Size <= buf.Length);

            binder_transaction_data r;
            r.HandleOrPtr = new UserPtr(Deserializer.ReadUInt(buf, offset)); offset += sizeof(uint);
            r.cookie = new UserPtr(Deserializer.ReadUInt(buf, offset)); offset += sizeof(uint);
            r.code = Deserializer.ReadUInt(buf, offset); offset += sizeof(uint);
            r.flags = Deserializer.ReadUInt(buf, offset); offset += sizeof(uint);
            r.sender_pid = Deserializer.ReadUInt(buf, offset); offset += sizeof(uint);
            r.sender_euid = Deserializer.ReadUInt(buf, offset); offset += sizeof(uint);
            r.data_size = Deserializer.ReadUInt(buf, offset); offset += sizeof(uint);
            r.offsets_size = Deserializer.ReadUInt(buf, offset); offset += sizeof(uint);
            r.data_buffer = new UserPtr(Deserializer.ReadUInt(buf, offset)); offset += sizeof(uint);
            r.data_offsets = new UserPtr(Deserializer.ReadUInt(buf, offset)); offset += sizeof(uint);
            return r;
        }
Пример #43
0
        public static int UnmarshalReadBuffer(Thread thr, ByteBufferRef completionBuf, ref sys_binder_write_desc desc, UserPtr readBuffer, int readBufferSize)
        {
            var proc = thr.Parent;
            var marshaledPtr = new Pointer(completionBuf.Location);

            //Arch.Console.Write("read_consumed:");
            //Arch.Console.Write(desc.read_consumed);
            //BinderIPCMarshaler.DumpBuf(new Pointer(completionBuf.Location), (int)desc.read_consumed);

            if (proc.binderVMStart == UserPtr.Zero)
            {
                Arch.Console.WriteLine("proc.binderVMStart == UserPtr.Zero");
                return -ErrorCode.EFAULT;
            }

            if (UnmarshalDataEntries(thr, completionBuf, ref desc) != 0)
            {
                Arch.Console.WriteLine("UnmarshalDataEntries failed");
                return -ErrorCode.ENOMEM;
            }

            if (desc.read_consumed > completionBuf.Length)
            {
                Arch.Console.WriteLine("UnmarshalReadBuffer: bad input");
                return -ErrorCode.ENOMEM;
            }

            // Patch pointers and convert file descriptors
            var b = completionBuf.Slice(0, desc.read_consumed);
            if (PatchReadBuffer(thr, b) != 0)
            {
                Arch.Console.WriteLine("Failed to patch read buffer");
                return -ErrorCode.EINVAL;
            }

            if (readBuffer.Write(thr, marshaledPtr, desc.read_consumed) != 0)
            {
                Arch.Console.WriteLine("readBuffer.Write failed");
                return -ErrorCode.ENOMEM;
            }

            return 0;
        }
Пример #44
0
        public ByteBufferRef AllocPages(int pages)
        {
            Contract.Ensures(!Contract.Result<ByteBufferRef>().isValid ||
                Contract.Result<ByteBufferRef>().Length == pages * Arch.ArchDefinition.PageSize);

            var size = pages * Arch.ArchDefinition.PageSize;
            var p = NativeMethods.sel4_alloc_alloc(this.handle, size);

            if (p == Pointer.Zero)
            {
                // Post-condition of ByteBufferRef.Empty
                Contract.Assume(!ByteBufferRef.Empty.isValid);
                return ByteBufferRef.Empty;
            }

            var r = new ByteBufferRef(p.ToIntPtr(), size);
            // Post-condition of ByteBufferRef
            Contract.Assume(r.Length == size);

            Contract.Assert(r.Length == pages * Arch.ArchDefinition.PageSize);
            return r;
        }
Пример #45
0
        internal void WriteString16(string s)
        {
            var r = new ByteBufferRef(Buffer);

            if (cursor + sizeof(int) > Buffer.Length)
                return;

            WriteInt32(s.Length);

            foreach (var c in s)
            {
                if (cursor + sizeof(short) > r.Length)
                    return;
                Deserializer.WriteShort((short)c, r, cursor);
                cursor += sizeof(short);
            }

            if (cursor + sizeof(short) > r.Length)
                return;

            Deserializer.WriteShort(0, r, cursor);
            cursor += sizeof(short);
            Pad();
        }
Пример #46
0
 public bool Contains(ByteBufferRef buf)
 {
     return Start <= new Pointer(buf.Location) && new Pointer(buf.Location) + buf.Length < End;
 }
Пример #47
0
 public static bool StatIsDir(ByteBufferRef buf)
 {
     var mode = Deserializer.ReadInt(buf, OFFSET_OF_MODE_IN_STAT64);
     return ((mode & S_IFMT) & S_IFDIR) != 0;
 }
Пример #48
0
        private int Write(Process process, Pointer dst, int length)
        {
            var bytesLeft = length;

            var src = _value;
            var dst_buf = new ByteBufferRef(dst.ToIntPtr(), length);
            var cursor = 0;
            while (bytesLeft > 0)
            {
                var region = process.Space.Find(src);

                // Invalid mapping
                if (region == null || region.IsFixed)
                {
                    return bytesLeft;
                }

                var off = Arch.ArchDefinition.PageOffset(src.ToUInt32());
                var virtualAddr = process.Space.UserToVirt(new UserPtr(src));

                if (virtualAddr == Pointer.Zero)
                {
                    // Page isn't present, try to bring it in.
                    uint permission;

                    Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, src, Pointer.Zero, out virtualAddr, out permission);

                    if (virtualAddr == Pointer.Zero)
                        break;
                }

                var virtual_page = Arch.ArchDefinition.PageIndex(virtualAddr.ToUInt32());
                var page_buf = new ByteBufferRef(new IntPtr(virtual_page), Arch.ArchDefinition.PageSize);

                var b = Arch.ArchDefinition.PageSize - off;
                var bytesTobeCopied = b > bytesLeft ? bytesLeft : b;

                var dst_buf_page = page_buf.Slice(off, bytesTobeCopied);
                for (var i = 0; i < bytesTobeCopied; ++i)
                {
                    dst_buf_page.Set(i, dst_buf.Get(cursor + i));
                }

                bytesLeft -= bytesTobeCopied;
                src += bytesTobeCopied;
                cursor += bytesTobeCopied;
            }

            return bytesLeft;
        }
Пример #49
0
 int Write(Process proc, ByteBufferRef val)
 {
     return Write(proc, new Pointer(val.Location), val.Length);
 }
Пример #50
0
 private unsafe int Read(Thread current, void* dst, int length)
 {
     var process = current.Parent;
     var buf = new ByteBufferRef(new IntPtr(dst), length);
     var bytesRead = Read(process, buf, false);
     return length - bytesRead;
 }
Пример #51
0
        private int Read(Process process, ByteBufferRef buffer, bool is_string)
        {
            var bytesLeft = is_string ? buffer.Length - 1 : buffer.Length;
            var bytesRead = 0;

            var src = _value;

            while (bytesLeft > 0)
            {
                var region = process.Space.Find(src);

                // Invalid mapping
                if (region == null || region.IsFixed)
                    break;

                var off = Arch.ArchDefinition.PageOffset(src.ToUInt32());
                var virtualAddr = process.Space.UserToVirt(new UserPtr(src));

                if (virtualAddr == Pointer.Zero)
                {
                    uint permission;

                    Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, _value, Pointer.Zero, out virtualAddr, out permission);

                    if (virtualAddr == Pointer.Zero)
                        break;
                }

                var virtual_page = Arch.ArchDefinition.PageIndex(virtualAddr.ToUInt32());
                var page_buf = new ByteBufferRef(new IntPtr(virtual_page), Arch.ArchDefinition.PageSize);

                var b = Arch.ArchDefinition.PageSize - off;
                var bytesTobeCopied = b > bytesLeft ? bytesLeft : b;

                var src_buf = page_buf.Slice(off, bytesTobeCopied);

                if (is_string)
                {
                    var res = Util.CopyString(ref buffer, bytesRead, src_buf);
                    bytesRead += res;

                    if (res < bytesTobeCopied)
                    {
                        // We're done.
                        break;
                    }
                }
                else
                {
                    for (var i = 0; i < bytesTobeCopied; ++i)
                    {
                        buffer.Set(i + bytesRead, src_buf.Get(i));
                    }
                    bytesRead += bytesTobeCopied;
                }

                bytesLeft -= bytesTobeCopied;
                src += bytesTobeCopied;
            }

            if (is_string)
            {
                buffer.Set(bytesRead, 0);
            }
            return bytesRead;
        }
Пример #52
0
 internal int Write(Process proc, byte[] val)
 {
     var b = new ByteBufferRef(val);
     return Write(proc, b);
 }
Пример #53
0
 internal int Write(Thread current, ByteBufferRef val)
 {
     return Write(current.Parent, val);
 }
Пример #54
0
 public int ReadString(Thread current, byte[] buf)
 {
     var b = new ByteBufferRef(buf);
     return ReadString(current, b);
 }
Пример #55
0
 internal unsafe int Read(Thread current, ByteBufferRef buf, int desired_length)
 {
     Contract.Requires(buf.Length >= desired_length);
     return Read(current, buf.Location.ToPointer(), desired_length);
 }
Пример #56
0
 public int ReadString(Thread current, ByteBufferRef buf)
 {
     return Read(current.Parent, buf, true);
 }
Пример #57
0
 public static void SetSizeFromStat64(ByteBufferRef buf, ulong size)
 {
     Deserializer.WriteULong(size, buf, OFFSET_OF_SIZE_IN_STAT64);
 }
Пример #58
0
 public static long GetSizeFromStat64(ByteBufferRef buf)
 {
     return Deserializer.ReadLong(buf, OFFSET_OF_SIZE_IN_STAT64);
 }
Пример #59
0
        /*
         * Write a segment buffer.
         *
         * The buffer itself is passed as a reference, because some inode might take the ownership
         * of the buffer and put it as a part of its completion. In this case the buf is set to empty
         */
        internal int Write(Thread current, ref Arch.ExceptionRegisters regs, ref ByteBufferRef buf, int len, uint pos, File file)
        {
            switch (kind)
            {
                case INodeKind.ConsoleINodeKind:
                    {
                        uint dummy = 0;
                        return ConsoleINode.WriteImpl(current, buf, len, ref dummy);
                    }
                case INodeKind.ArchINodeKind:
                case INodeKind.SocketINodeKind:
                    return ArchINode.ArchWrite(current, ref regs, ref buf, len, pos, file);

                case INodeKind.SecureFSINodeKind:
                    return SFSINode.SFSWrite(current, ref regs, buf, len, pos, file);
            }
            return -ErrorCode.EINVAL;
        }
Пример #60
0
 public int Read(ByteBufferRef buffer, int offset, int count, ref uint pos)
 {
     switch (kind)
     {
         case INodeKind.ArchINodeKind:
             return ArchINode.ReadImpl(buffer, offset, count, ref pos);
     }
     return -ErrorCode.EINVAL;
 }