Ejemplo n.º 1
0
        public static void ProcessLine(this ReadOnlySequence <byte> buffer, ReadOnlySpanAction <byte> action, bool readToEnd = false)
        {
            const byte newline        = (byte)'\n';
            const byte carriageReturn = (byte)'\r';

            if (buffer.IsSingleSegment)
            {
                var span = buffer.FirstSpan;
                while (span.Length > 0)
                {
                    var newLine = span.IndexOf(newline);

                    if (newLine == -1 && !readToEnd)
                    {
                        break;
                    }

                    // If there is no newline found, just read to the end.
                    var line = span.Slice(0, newLine == -1 ? span.Length : newLine);

                    // If we did find a newline character, then make sure we count it as a consume character
                    var consumed = line.Length;
                    if (newLine > -1)
                    {
                        consumed++;
                    }

                    // Trim carriage return from the end
                    if (line[^ 1] == carriageReturn)
Ejemplo n.º 2
0
 internal ReadOnlySpanWriter(ReadOnlySpanAction <byte, TArg> output, TArg arg, Action <TArg>?flush, Func <TArg, CancellationToken, Task>?flushAsync)
 {
     this.output     = output;
     this.arg        = arg;
     this.flush      = flush;
     this.flushAsync = flushAsync;
 }
Ejemplo n.º 3
0
        // writes file with index i to stream.
        private static void WriteToStream(Stream stream, int i, ReadOnlySpanAction <byte, Stream> write)
        {
            var fileName = $"File{i}.txt";
            var path     = Path.Combine(AppContext.BaseDirectory, fileName);

            using var fileStream = File.OpenRead(path);
            var readFromFile = fileStream.Length;
            //read from file
            Span <byte> buff = new byte[bufferSize];

            while (readFromFile > 0)
            {
                var left = buff.Length;
                var read = 0;
                int readOnce;
                do
                {
                    readOnce = fileStream.Read(buff);
                    left    -= readOnce;
                    read    += readOnce;
                } while (left > 0 && readOnce > 0);

                // if "read" is less than buff size take only "read" number of bytes
                ReadOnlySpan <byte> sp = buff.Slice(0, read);

                // write to stream
                write(sp, stream);
                // left to read more
                readFromFile -= read;
            }
        }
Ejemplo n.º 4
0
            /// <summary>
            /// Run method starts the libFuzzer runner. It repeatedly executes
            /// the passed action and reports the execution result to libFuzzer.
            /// If the executable that is calling it is not running under libFuzzer,
            /// the action will be executed normally, and will receive its input
            /// from the file specified in the first command line parameter.
            /// </summary>
            /// <param name="action">
            /// Some action that calls the instrumented library. The span argument
            /// passed to the action contains the input data. If an uncaught
            /// exception escapes the call, crash is reported to libFuzzer.
            /// </param>
            public static unsafe void Run(ReadOnlySpanAction action)
            {
                ThrowIfNull(action, nameof(action));

                try
                {
                    using (var ipc = new FuzzerIpc())
                    {
                        var sharedMem = ipc.InputPointer();
                        var trace     = new TraceWrapper(sharedMem);

                        ipc.SetStatus(0);

                        try
                        {
                            var status = Fault.None;

                            // The program instrumented with libFuzzer will exit
                            // after the first error, so we should do the same.
                            while (status != Fault.Crash)
                            {
                                trace.ResetPrevLocation();

                                var size = ipc.InputSize();
                                var data = new ReadOnlySpan <byte>(sharedMem + MapSize, size);

                                try
                                {
                                    action(data);
                                }
                                catch (Exception ex)
                                {
                                    Console.Error.WriteLine(ex);
                                    status = Fault.Crash;
                                }

                                ipc.SetStatus(status);
                            }
                        }
                        catch
                        {
                            // Error communicating with the parent process, most likely
                            // because it was terminated after the timeout expired, or
                            // it was killed by the user. In any case, the exception
                            // details don't matter here, so we can just exit silently.
                            return;
                        }
                    }
                }
                catch (FuzzerIpcEnvironmentException)
                {
                    // Error establishing IPC with the parent process due to missing or
                    // definitely-invalid environment variables. This may be intentional.
                    // Instead of persistent fuzzing, fall back on testing a single input.
                    RunWithoutLibFuzzer(action);
                    return;
                }
            }
Ejemplo n.º 5
0
            /// <summary>
            /// Run method starts the libFuzzer runner. It repeatedly executes
            /// the passed action and reports the execution result to libFuzzer.
            /// If the executable that is calling it is not running under libFuzzer,
            /// the action will be executed normally, and will receive its input
            /// from the file specified in the first command line parameter.
            /// </summary>
            /// <param name="action">
            /// Some action that calls the instrumented library. The span argument
            /// passed to the action contains the input data. If an uncaught
            /// exception escapes the call, crash is reported to libFuzzer.
            /// </param>
            public static unsafe void Run(ReadOnlySpanAction action)
            {
                ThrowIfNull(action, nameof(action));
                var s = Environment.GetEnvironmentVariable("__LIBFUZZER_SHM_ID");

                if (s is null || !Int32.TryParse(s, out var shmid))
                {
                    RunWithoutLibFuzzer(action);
                    return;
                }

                using (var shmaddr = Native.shmat(shmid, IntPtr.Zero, 0))
                    using (var r = new BinaryReader(new AnonymousPipeClientStream(PipeDirection.In, "198")))
                        using (var w = new BinaryWriter(new AnonymousPipeClientStream(PipeDirection.Out, "199")))
                        {
                            var sharedMem = (byte *)shmaddr.DangerousGetHandle();
                            var trace     = new TraceWrapper(sharedMem);

                            w.Write(0);

                            try
                            {
                                var status = Fault.None;

                                // The program instrumented with libFuzzer will exit
                                // after the first error, so we should do the same.
                                while (status != Fault.Crash)
                                {
                                    trace.ResetPrevLocation();

                                    var size = r.ReadInt32();
                                    var data = new ReadOnlySpan <byte>(sharedMem + MapSize, size);

                                    try
                                    {
                                        action(data);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.Error.WriteLine(ex);
                                        status = Fault.Crash;
                                    }

                                    w.Write(status);
                                }
                            }
                            catch
                            {
                                // Error communicating with the parent process, most likely
                                // because it was terminated after the timeout expired, or
                                // it was killed by the user. In any case, the exception
                                // details don't matter here, so we can just exit silently.
                                return;
                            }
                        }
            }
Ejemplo n.º 6
0
        public void Init()
        {
            _migoStateModel       = new MigoStateModel();
            _positionalSerializer = new PositionalSerializer <MigoStateModel>(';')
                                    .Field(x => x.BedTemp)
                                    .Field(x => x.HeadX);

            _intParser    = _positionalSerializer.GetParser(0).ParserAction;
            _doubleParser = _positionalSerializer.GetParser(1).ParserAction;
        }
Ejemplo n.º 7
0
 /// <inheritdoc />
 void IByteBufferWriter.CopyTo <TArg>(ReadOnlySpanAction <byte, TArg> callback, TArg arg)
 {
     if (pool is null)
     {
         throw new ObjectDisposedException(GetType().Name);
     }
     if (Position > 0L)
     {
         callback(WrittenSpan, arg);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Transfers written content to the specified callback.
        /// </summary>
        /// <typeparam name="TArg">The type of the argument to be passed to the callback.</typeparam>
        /// <param name="action">The callback accepting written content. Can be called more than once.</param>
        /// <param name="arg">The argument to be passed to the callback.</param>
        public readonly void CopyTo <TArg>(ReadOnlySpanAction <T, TArg> action, TArg arg)
        {
            GetSegments(out var head, out var tail);

            if (!head.IsEmpty)
            {
                action(head, arg);
                if (!tail.IsEmpty)
                {
                    action(tail, arg);
                }
            }
        }
Ejemplo n.º 9
0
        internal TextBufferWriter(TWriter writer, IFormatProvider?provider, Action <TWriter>?flush, Func <TWriter, CancellationToken, Task>?flushAsync)
            : base(provider ?? InvariantCulture)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            writeImpl = writer is IGrowableBuffer <char>?
                        new ReadOnlySpanAction <char, TWriter>(WriteToGrowableBuffer) :
                            new ReadOnlySpanAction <char, TWriter>(WriteToBuffer);
            this.writer     = writer;
            this.flush      = flush;
            this.flushAsync = flushAsync;
Ejemplo n.º 10
0
            private static unsafe void RunWithoutLibFuzzer(ReadOnlySpanAction action)
            {
                var args = Environment.GetCommandLineArgs();

                if (args.Length <= 1)
                {
                    Console.Error.WriteLine("You must specify the input path as the first command line argument when not running under libFuzzer.");
                }

                fixed(byte *sharedMem = new byte[MapSize])
                {
                    new TraceWrapper(sharedMem);
                    action(File.ReadAllBytes(args[1]));
                }
            }
Ejemplo n.º 11
0
            /// <summary>
            /// Run method starts the libFuzzer runner. It repeatedly executes
            /// the passed action and reports the execution result to libFuzzer.
            /// If the executable that is calling it is not running under libFuzzer,
            /// the action will be executed normally, and will receive its input
            /// from the file specified in the first command line parameter.
            /// </summary>
            /// <param name="action">
            /// Some action that calls the instrumented library. If an uncaught
            /// exception escapes the call, crash is reported to libFuzzer.
            /// </param>
            public static unsafe void Run(ReadOnlySpanAction action)
            {
                ThrowIfNull(action, nameof(action));
                var s = Environment.GetEnvironmentVariable("__LIBFUZZER_SHM_ID");

                if (s is null || !Int32.TryParse(s, out var shmid))
                {
                    RunWithoutLibFuzzer(action);
                    return;
                }

                using (var shmaddr = Native.shmat(shmid, IntPtr.Zero, 0))
                    using (var r = new BinaryReader(new AnonymousPipeClientStream(PipeDirection.In, "198")))
                        using (var w = new BinaryWriter(new AnonymousPipeClientStream(PipeDirection.Out, "199")))
                        {
                            byte *sharedMem = (byte *)shmaddr.DangerousGetHandle();
                            InitializeSharedMemory(sharedMem);
                            w.Write(0);

                            while (true)
                            {
                                var size = r.ReadInt32();
                                var data = new ReadOnlySpan <byte>(sharedMem + MapSize, size);

                                try
                                {
                                    action(data);
                                    w.Write(Fault.None);
                                }
                                catch (Exception ex)
                                {
                                    Console.Error.WriteLine(ex);
                                    w.Write(Fault.Crash);

                                    // The program instrumented with libFuzzer will exit
                                    // after the first error, so we should do the same.
                                    return;
                                }
                            }
                        }
            }
Ejemplo n.º 12
0
        public override void CopyTo(ReadOnlySpanAction <byte, object?> callback, object?state, int bufferSize)
        {
            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Read() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into Read) when we are not sure.
            if (GetType() != typeof(MemoryStream))
            {
                base.CopyTo(callback, state, bufferSize);
                return;
            }

            StreamHelpers.ValidateCopyToArgs(this, callback, bufferSize);

            // Retrieve a span until the end of the MemoryStream.
            ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(_buffer, _position, _length - _position);

            _position = _length;

            // Invoke the callback, using our internal span and avoiding any
            // intermediary allocations.
            callback(span, state);
        }
Ejemplo n.º 13
0
 Task IAsyncBinaryReader.CopyToAsync <TArg>(ReadOnlySpanAction <byte, TArg> consumer, TArg arg, CancellationToken token)
 => token.IsCancellationRequested ? Task.FromCanceled(token) : Task.CompletedTask;
Ejemplo n.º 14
0
 /// <summary>
 /// Returns writable stream that wraps the provided delegate for writing data.
 /// </summary>
 /// <param name="writer">The callback that is called automatically.</param>
 /// <param name="arg">The arg to be passed to the callback.</param>
 /// <param name="flush">Optional synchronous flush action.</param>
 /// <param name="flushAsync">Optiona asynchronous flush action.</param>
 /// <typeparam name="TArg">The type of the object that represents the state.</typeparam>
 /// <returns>The writable stream wrapping the callback.</returns>
 public static Stream AsStream <TArg>(this ReadOnlySpanAction <byte, TArg> writer, TArg arg, Action <TArg>?flush = null, Func <TArg, CancellationToken, Task>?flushAsync = null)
 => AsSynchronousStream <ReadOnlySpanWriter <TArg> >(new ReadOnlySpanWriter <TArg>(writer ?? throw new ArgumentNullException(nameof(writer)), arg, flush, flushAsync));
Ejemplo n.º 15
0
 /// <summary>
 /// Returns writable stream that wraps the provided delegate for writing data.
 /// </summary>
 /// <param name="writer">The callback that is called automatically.</param>
 /// <param name="arg">The arg to be passed to the callback.</param>
 /// <param name="flush">Optional synchronous flush action.</param>
 /// <param name="flushAsync">Optiona asynchronous flush action.</param>
 /// <typeparam name="TArg">The type of the object that represents the state.</typeparam>
 /// <returns>The writable stream wrapping the callback.</returns>
 public static Stream AsStream <TArg>(this ReadOnlySpanAction <byte, TArg> writer, TArg arg, Action <TArg>?flush = null, Func <TArg, CancellationToken, Task>?flushAsync = null)
 => new SpanWriterStream <TArg>(writer, arg, flush, flushAsync);
Ejemplo n.º 16
0
 internal SpanWriterStream(ReadOnlySpanAction <byte, TArg> writer, TArg arg, Action <TArg>?flush, Func <TArg, CancellationToken, Task>?flushAsync)
     : base(arg, flush, flushAsync)
     => this.writer = writer ?? throw new ArgumentNullException(nameof(writer));
Ejemplo n.º 17
0
 public PositionParser(ReadOnlySpanAction <char, T> parserAction, char delimiter)
 {
     ParserAction = parserAction;
     Delimiter    = delimiter;
 }
Ejemplo n.º 18
0
 Task IAsyncBinaryReader.CopyToAsync <TArg>(ReadOnlySpanAction <byte, TArg> reader, TArg arg, CancellationToken token)
 => input.ReadAsync(reader, arg, buffer, token);
Ejemplo n.º 19
0
 /// <summary>
 /// Wraps the delegate instance.
 /// </summary>
 /// <param name="action">The delegate instance.</param>
 /// <param name="arg">The argument to be passed to the function represented by the delegate.</param>
 /// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
 public DelegatingReadOnlySpanConsumer(ReadOnlySpanAction <T, TArg> action, TArg arg)
 {
     this.action = action ?? throw new ArgumentNullException(nameof(action));
     this.arg    = arg;
 }
Ejemplo n.º 20
0
 public void Print(ReadOnlySpan <char> message, ReadOnlySpan <char> relativePathTrim, ReadOnlySpanAction <char, object?> toDo)
 {
     foreach (var item in message.SplitLines())
     {
         if (!_applicableErrorLine.IsApplicable(item.Line))
         {
             continue;
         }
         toDo(_trimErrorMessage.Trim(item.Line, relativePathTrim), null);
     }
 }
Ejemplo n.º 21
0
 internal DelegatingConsumer(ReadOnlySpanAction <T, TArg> output, Action <int, TArg> rewriter, TArg state)
 {
     this.output   = output;
     this.rewriter = rewriter;
     this.state    = state;
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Calls the given callback with a span of the memory stream data
        /// </summary>
        /// <param name="callback">the callback to be called</param>
        /// <param name="state">A user-defined state, passed to the callback</param>
        /// <param name="bufferSize">the maximum size of the memory span</param>
        public override void CopyTo(ReadOnlySpanAction <byte, object?> callback, object?state, int bufferSize)
        {
            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Read() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into Read) when we are not sure.
            if (GetType() != typeof(UnmanagedMemoryStream))
            {
                base.CopyTo(callback, state, bufferSize);
                return;
            }

            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            EnsureNotClosed();
            EnsureReadable();

            // Use a local variable to avoid a race where another thread
            // changes our position after we decide we can read some bytes.
            long pos = Interlocked.Read(ref _position);
            long len = Interlocked.Read(ref _length);
            long n   = len - pos;

            if (n <= 0)
            {
                return;
            }

            int nInt = (int)n; // Safe because n <= count, which is an Int32

            if (nInt < 0)
            {
                return;  // _position could be beyond EOF
            }

            unsafe
            {
                if (_buffer != null)
                {
                    byte *pointer = null;

                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        _buffer.AcquirePointer(ref pointer);
                        ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(pointer + pos + _offset, nInt);
                        Interlocked.Exchange(ref _position, pos + n);
                        callback(span, state);
                    }
                    finally
                    {
                        if (pointer != null)
                        {
                            _buffer.ReleasePointer();
                        }
                    }
                }
                else
                {
                    ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(_mem + pos, nInt);
                    Interlocked.Exchange(ref _position, pos + n);
                    callback(span, state);
                }
            }
        }
Ejemplo n.º 23
0
 Task IAsyncBinaryReader.CopyToAsync <TArg>(ReadOnlySpanAction <byte, TArg> consumer, TArg arg, CancellationToken token)
 => GetCompletedOrCanceledTask(token);