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)
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; }
// 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; } }
/// <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; } }
/// <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; } } }
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; }
/// <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); } }
/// <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); } } }
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;
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])); } }
/// <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; } } } }
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); }
Task IAsyncBinaryReader.CopyToAsync <TArg>(ReadOnlySpanAction <byte, TArg> consumer, TArg arg, CancellationToken token) => token.IsCancellationRequested ? Task.FromCanceled(token) : Task.CompletedTask;
/// <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));
/// <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);
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));
public PositionParser(ReadOnlySpanAction <char, T> parserAction, char delimiter) { ParserAction = parserAction; Delimiter = delimiter; }
Task IAsyncBinaryReader.CopyToAsync <TArg>(ReadOnlySpanAction <byte, TArg> reader, TArg arg, CancellationToken token) => input.ReadAsync(reader, arg, buffer, token);
/// <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; }
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); } }
internal DelegatingConsumer(ReadOnlySpanAction <T, TArg> output, Action <int, TArg> rewriter, TArg state) { this.output = output; this.rewriter = rewriter; this.state = state; }
/// <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); } } }
Task IAsyncBinaryReader.CopyToAsync <TArg>(ReadOnlySpanAction <byte, TArg> consumer, TArg arg, CancellationToken token) => GetCompletedOrCanceledTask(token);