public void DirectionInput(int tick, GameObject SourceInstance, IElectricityIO ComingFrom, IElectricityIO PassOn = null) { //Logger.Log(SourceInstance.ToString() + " < SourceInstance " + ComingFrom.ToString() + " < ComingFrom " + this.name + " < this " ); if (connections.Count > 2) { ElectricityFunctions.DirectionInput(tick, SourceInstance, ComingFrom, this); FirstPresentInspector = FirstPresent; } else { int SourceInstanceID = SourceInstance.GetInstanceID(); if (!(Upstream.ContainsKey(SourceInstanceID))) { Upstream [SourceInstanceID] = new HashSet <IElectricityIO> (); } if (!(Downstream.ContainsKey(SourceInstanceID))) { Downstream [SourceInstanceID] = new HashSet <IElectricityIO> (); } if (FirstPresent == 0) { //Logger.Log ("to It's been claimed", Category.Electrical); FirstPresent = SourceInstanceID; //Thiswire.FirstPresentInspector = SourceInstanceID; } if (ComingFrom != null) { Upstream [SourceInstanceID].Add(ComingFrom); } CameFromMemory = PassOn; SourceInstance.GetComponent <IProvidePower> ().DirectionWorkOnNextList.Add(this); } }
public TypeEdgeHost(IConfigurationRoot configuration) { _deviceId = configuration.GetValue <string>("DeviceId"); _iotHubConnectionString = configuration.GetValue <string>("IotHubConnectionString"); if (string.IsNullOrEmpty(_iotHubConnectionString)) { throw new Exception("Missing \"IotHubConnectionString\" value in configuration"); } if (string.IsNullOrEmpty(_deviceId)) { throw new Exception("Missing \"DeviceId\"value in configuration"); } _containerBuilder = new ContainerBuilder(); _hub = new EdgeHub(); Upstream = new Upstream <JsonMessage>(_hub); _inContainer = File.Exists(@"/.dockerenv"); _externalModules = new ModuleCollection(); }
public async Task TestDefault() { using var defaultUpstream = Upstream.Create("default"); using var routeUpstream = Upstream.Create("route"); var config = @$ " hosts: localhost:
public override void Dump(TextWriter writer) { Single(writer, "Title", Title); Multi(writer, "Description", Description); Multi(writer, "Home", Home); Single(writer, "Name", Name); Single(writer, "Version", Version); Single(writer, "Upstream", Upstream.ToString()); Multi(writer, "Assets", Assets); Multi(writer, "Deps", Deps); if (Build != null) { Build.Dump(writer); } }
private static Upstream NewUpstreamObj() { Console.WriteLine("instantiating fake upstream"); Upstream upstream = new Upstream(); upstream.Structure = new FSeed(); upstream.Structure.Data = new ConfigEntry[1]; upstream.ResultMessage = ""; ConfigEntry entry = new ConfigEntry(); upstream.Structure.Data[0] = entry; entry.Tag = "stuff"; entry.Type = "string"; entry.Used = true; entry.Value = "<html><body><h1>h1</h1><p>p</p></body></html>"; return(upstream); }
public ChainSetup( Func <Flow <TIn, TIn, NotUsed>, Flow <TIn, TOut, TMat> > stream, ActorMaterializerSettings settings, ActorMaterializer materializer, Func <Source <TOut, NotUsed>, ActorMaterializer, IPublisher <TOut> > toPublisher, TestKitBase system) { Settings = settings; System = system; Upstream = system.CreateManualPublisherProbe <TIn>(); Downstream = system.CreateSubscriberProbe <TOut>(); var s = Source.FromPublisher(Upstream).Via(stream(Flow.Identity <TIn>().Select(x => x).Named("buh"))); Publisher = toPublisher(s, materializer); UpstreamSubscription = Upstream.ExpectSubscription(); Publisher.Subscribe(Downstream); DownstreamSubscription = Downstream.ExpectSubscription(); }
public bool Delete() { if (!Upstream.RemoveDownstream(this)) { return(false); } if (Downstream.RemoveUpstream(this)) { Deleted?.Invoke(this, EventArgs.Empty); return(true); } // Couldn't remove upstream, compensate by re-adding previously removed downstream if (!Upstream.AddDownstream(this)) { throw new Exception("unable to add downstream"); } return(false); }
public bool ShakeIt() { var oneMilli = TimeSpan.FromMilliseconds(1); var marker = new object(); var u = Upstream.ReceiveWhile(oneMilli, filter: msg => { if (msg is TestPublisher.RequestMore) { var more = (TestPublisher.RequestMore)msg; DebugLog($"Operation requests {more.NrOfElements}"); _pendingRequests += more.NrOfElements; return(marker); } return(null); }); var d = Downstream.ReceiveWhile(oneMilli, filter: msg => msg.Match() .With <TestSubscriber.OnNext <TOut> >(next => { DebugLog($"Operation produces [{next.Element}]"); if (_outstandingDemand == 0) { throw new Exception("operation produced while there was no demand"); } _outstandingDemand--; _currentScript = _currentScript.ConsumeOutput(next.Element); }) .With <TestSubscriber.OnComplete>(complete => { _currentScript = _currentScript.Complete(); }) .With <TestSubscriber.OnError>(error => { _currentScript = _currentScript.Error(error.Cause); }) .WasHandled ? marker : null); return(u.Concat(d).Any(x => x == marker)); }
private static void ReturnResult(MemoryMappedViewAccessor resultBufferAccessor, Stream outputStream, long res, long ns, long sonar, Upstream upstream) { byte[] resBuffer = new byte[8]; byte[] nsBuffer = new byte[8]; byte[] sonarBuffer = new byte[8]; Serialize56(resBuffer, res); Serialize56(nsBuffer, ns); Serialize56(sonarBuffer, sonar); Console.WriteLine("serializing upstream"); /*int maxReturnSize = FlatBufferSerializer.Default.GetMaxSize(upstream); * var returnBuffer = new byte[maxReturnSize]; * returnLength = FlatBufferSerializer.Default.Serialize(upstream, returnBuffer);*/ byte[] returnLengthBuffer = new byte[8]; var returnResultBuffer = JsonSerializer.SerializeToUtf8Bytes <Upstream>(upstream); var jsonString = Encoding.UTF8.GetString(returnResultBuffer); Console.WriteLine("upstream json: " + jsonString); Serialize56(returnLengthBuffer, returnResultBuffer.Length); for (int i = 0; i < 8; i++) { resultBufferAccessor.Write(i, returnLengthBuffer[i]); } for (int i = 0; i < returnResultBuffer.Length; i++) { resultBufferAccessor.Write(i + 8, returnResultBuffer[i]); } resultBufferAccessor.Flush(); Console.WriteLine("wrote to comm1Accessor"); byte[] outputBuffer = new byte[24]; for (int i = 0; i < 8; i++) { outputBuffer[i] = resBuffer[i]; outputBuffer[i + 8] = nsBuffer[i]; outputBuffer[i + 16] = sonarBuffer[i]; } outputStream.Write(outputBuffer, 0, outputBuffer.Length); outputStream.Flush(); Console.WriteLine("wrote outputbuffer"); }
/// <summary> /// Run method starts the .NET equivalent of SFZ fuzzer main loop. /// </summary> /// <param name="action"> /// Some action that calls the instrumented library. The stream /// argument passed to the action contains the serialized input data. /// </param> public static unsafe void RunSfz(string[] args, Action <byte[]> action) { Console.WriteLine("Starting Sfz Main"); int[] fileDescriptors = ReadFileDescriptors(args); int fdIn = fileDescriptors[0]; int fdOut = fileDescriptors[1]; Console.Write("GO_FUZZ_IN_FD: " + fdIn); Console.WriteLine(" GO_FUZZ_OUT_FD: " + fdOut); string[] commName = ReadSharedMemoryNames(args); using (SafeFileHandle inPipeHandle = new SafeFileHandle(new IntPtr(fdIn), true), outPipeHandle = new SafeFileHandle(new IntPtr(fdOut), true)) { // Use the filehandles Stream inStream = new FileStream(inPipeHandle, FileAccess.Read); Stream outStream = new FileStream(outPipeHandle, FileAccess.Write); Console.WriteLine("created inStream and outStream"); //MemoryMappedFileSecurity security = new MemoryMappedFileSecurity(); //security.AddAccessRule(new AccessRule<MemoryMappedFileRights>(("Everyone"), // MemoryMappedFileRights.FullControl, AccessControlType.Allow)); //Console.WriteLine("created security"); using ( MemoryMappedFile comm0 = MemoryMappedFile.OpenExisting(commName[0], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable), comm1 = MemoryMappedFile.OpenExisting(commName[1], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable), comm2 = MemoryMappedFile.OpenExisting(commName[2], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable), comm3 = MemoryMappedFile.OpenExisting(commName[3], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable)) { const int MaxInputSize = 1 << 24; const int ReturnResultSize = 1 << 25; const int CoverSize = 64 << 10; const int SonarRegionSize = 1 << 20; var cover = new byte[CoverSize]; fixed(byte *coverPtr = cover) { var trace = new TraceWrapper(coverPtr); Console.WriteLine("created commX objects"); var comm0Accessor = comm0.CreateViewAccessor(0, MaxInputSize); var comm1Accessor = comm1.CreateViewAccessor(0, ReturnResultSize); var comm2Accessor = comm2.CreateViewAccessor(0, CoverSize); var comm3Accessor = comm3.CreateViewAccessor(0, SonarRegionSize); Console.WriteLine("created commX accessors"); while (true) { int fnidx = 0; long inputLength = 0; (fnidx, inputLength) = ReadIndexAndLength(inStream); // read inputBuffer data from comm0 var inputBuffer = new byte[inputLength]; comm0Accessor.ReadArray(0, inputBuffer, 0, (int)inputLength); for (int i = 0; i < inputLength; i++) { inputBuffer[i] = comm0Accessor.ReadByte(i); } var inputString = Encoding.UTF8.GetString(inputBuffer); Console.WriteLine("downstream: " + Encoding.UTF8.GetString(inputBuffer)); var downstream = new Downstream(); try { downstream = JsonSerializer.Deserialize <Downstream>(inputString); } catch (Exception ex) { Console.WriteLine("downstream deserialization exception: " + ex.Message); } //var downstream = FlatBufferSerializer.Default.Parse<Downstream>(inputBuffer); Console.WriteLine("downstream deserialized"); Upstream upstream = NewUpstreamObj(); long res = 0; long ns = 0; long sonar = 0; var seed = downstream.Seed; if (seed != null && seed.Data != null && seed.Data.Length >= 1) { ConfigEntry entry = seed.Data[0]; var value = entry.Value; if (entry.Value != null) { Console.WriteLine("got entry value: " + value); // Start the clock var nsStart = DateTime.UtcNow.Ticks; try { // Actually run the function to fuzz byte[] buffer = Encoding.UTF8.GetBytes(value); action(buffer); Console.Write("exec fuzz method"); } catch (Exception exception) { ns = DateTime.UtcNow.Ticks - nsStart; Console.WriteLine("ns: " + ns); upstream.Crashed = true; upstream.HasFailed = true; upstream.ResultMessage = exception.ToString(); } } else { Console.WriteLine("null entry value!"); var nsStart = DateTime.UtcNow.Ticks; try { // Actually run the function to fuzz byte[] buffer = Encoding.UTF8.GetBytes("<html><body><h1>h1</h1><p>p</p></body></html>"); action(buffer); Console.Write("exec fuzz method"); } catch (Exception exception) { ns = DateTime.UtcNow.Ticks - nsStart; Console.WriteLine("ns: " + ns); upstream.Crashed = true; upstream.HasFailed = true; upstream.ResultMessage = exception.ToString(); } } } else { Console.WriteLine("zero entries!"); var nsStart = DateTime.UtcNow.Ticks; try { // Actually run the function to fuzz byte[] buffer = Encoding.UTF8.GetBytes("<html><body><h1>h1</h1><p>p</p></body></html>"); action(buffer); Console.Write("exec fuzz method"); } catch (Exception exception) { ns = DateTime.UtcNow.Ticks - nsStart; Console.WriteLine("ns: " + ns); upstream.Crashed = true; upstream.HasFailed = true; upstream.ResultMessage = exception.ToString(); } } for (int i = 0; i < cover.Length; i++) { if (cover[i] != 0) { Console.WriteLine("nonzero cover!"); } } // copy cover to shared memory for (int i = 0; i < cover.Length; i++) { comm2Accessor.Write(i, cover[i]); } comm2Accessor.Flush(); ReturnResult(comm1Accessor, outStream, res, ns, sonar, upstream); } } } } }
static void Main(string[] args) { Upstream up = new Upstream(); up.Structure = new FSeed(); up.Structure.Data = new ConfigEntry[1]; var e0 = new ConfigEntry(); up.Structure.Data[0] = e0; e0.Tag = "stuff"; e0.Type = "string"; e0.Used = true; Console.WriteLine("test serializing up"); var testBuffer = new byte[1000]; var serializer = new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Default)); var testLength = serializer.Serialize(up, testBuffer); Upstream up2 = serializer.Parse <Upstream>(testBuffer); Console.WriteLine("Starting Main"); // For some reason, I have to copy the args into a local array, or else they disappear int[] fileDescriptors = new int[2]; for (int i = 0; i < fileDescriptors.Length && i < args.Length; i++) { int n = ParsedFd(args[i]); Console.WriteLine(n); fileDescriptors[i] = n; } // Grab the FDs from the environment variables and translate to a IntPtr int fdNumIn = fileDescriptors[0]; SafeFileHandle inPipeHandle = new SafeFileHandle(new IntPtr(fdNumIn), true); int fdNumOut = fileDescriptors[1]; SafeFileHandle outPipeHandle = new SafeFileHandle(new IntPtr(fdNumOut), true); Console.WriteLine("GO_FUZZ_IN_FD: " + fdNumIn); Console.WriteLine("GO_FUZZ_OUT_FD: " + fdNumOut); string[] commName = new string[4]; for (int i = 2; i < args.Length; i++) { commName[i - 2] = args[i]; Console.WriteLine(("comm" + (i - 2)) + ": " + commName[(i - 2)]); } const int MaxInputSize = 1 << 24; const int ReturnResultSize = 1 << 25; const int CoverSize = 64 << 10; const int SonarRegionSize = 1 << 20; // Use the filehandles Stream inStream = new FileStream(inPipeHandle, FileAccess.Read); Console.WriteLine("created inStream"); Stream outStream = new FileStream(outPipeHandle, FileAccess.Write); Console.WriteLine("created outStream"); MemoryMappedFileSecurity security = new MemoryMappedFileSecurity(); security.AddAccessRule(new AccessRule <MemoryMappedFileRights>(("Everyone"), MemoryMappedFileRights.FullControl, AccessControlType.Allow)); Console.WriteLine("created security"); MemoryMappedFile comm0 = MemoryMappedFile.OpenExisting(commName[0], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); Console.WriteLine("created comm0"); var comm0Accessor = comm0.CreateViewAccessor(0, MaxInputSize); Console.WriteLine("created comm0Accessor"); MemoryMappedFile comm1 = MemoryMappedFile.OpenExisting(commName[1], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); Console.WriteLine("created comm1"); var comm1Accessor = comm1.CreateViewAccessor(0, ReturnResultSize); Console.WriteLine("created comm1Accessor"); MemoryMappedFile comm2 = MemoryMappedFile.OpenExisting(commName[2], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); Console.WriteLine("created comm2"); var comm2Accessor = comm2.CreateViewAccessor(0, CoverSize); Console.WriteLine("created comm2Accessor"); //var comm3Stream = new FileStream(commNames[3], FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); //Console.WriteLine("created comm3Stream"); MemoryMappedFile comm3 = MemoryMappedFile.OpenExisting(commName[3], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); Console.WriteLine("created comm3"); var comm3Accessor = comm3.CreateViewAccessor(0, SonarRegionSize); Console.WriteLine("created comm3Accessor"); char[] inPipeBuffer = new char[10]; char[] outputBuffer = new char[24]; char[] returnLengthBuffer = new char[8]; StreamReader inPipeReader = new StreamReader(inStream); StreamWriter outputWriter = new StreamWriter(outStream); while (true) { inPipeReader.Read(inPipeBuffer, 0, inPipeBuffer.Length); int fnidx = inPipeBuffer[0]; fnidx += inPipeBuffer[1] << 8; Console.WriteLine("fnidx: " + fnidx); char[] lengthBuffer = new char[8]; for (int i = 2; i < inPipeBuffer.Length; i++) { lengthBuffer[i - 2] = inPipeBuffer[i]; } Int64 inputLength = Deserialize64(lengthBuffer); Console.WriteLine("input length: " + inputLength); // read inputBuffer data from comm0 var inputBuffer = new byte[inputLength]; comm0Accessor.ReadArray(0, inputBuffer, 0, (int)inputLength); for (int i = 0; i < inputLength; i++) { inputBuffer[i] = comm0Accessor.ReadByte(i); } var inputString = Encoding.UTF8.GetString(inputBuffer); Console.WriteLine("downstream: "); Console.WriteLine(inputString); //var downstream = Downstream.Deserialize(inputString); var downstream = FlatBufferSerializer.Default.Parse <Downstream>(inputBuffer); Console.WriteLine("downstream deserialized"); var seed = downstream.Seed; var entries = seed.Data; ConfigEntry entry = null; if (entries.Count >= 1) { entry = entries[0]; } else { Console.WriteLine("zero entries!"); } var value = entry.Value; Console.WriteLine("got entry value: " + value); Int64 res = 0; Int64 ns; Int64 sonar = 0; Int64 returnLength = 0; // Start the clock var nsStart = DateTime.UtcNow.Ticks; // Actually run the function to fuzz Console.WriteLine("BrokenMethod()"); Console.WriteLine(BrokenMethod(value)); ns = DateTime.UtcNow.Ticks - nsStart; Console.WriteLine("ns: " + ns); char[] resBuffer = new char[8]; char[] nsBuffer = new char[8]; char[] sonarBuffer = new char[8]; Serialize56(resBuffer, res); Serialize56(nsBuffer, ns); Serialize56(sonarBuffer, sonar); Console.WriteLine("instantiating upstream"); Upstream upstream = new Upstream(); upstream.Structure = new FSeed(); upstream.Structure.Data = new ConfigEntry[1]; var upEntry = upstream.Structure.Data[0]; upEntry.Tag = "stuff"; upEntry.Type = "string"; upEntry.Used = true; Console.WriteLine("serializing upstream"); int maxReturnSize = FlatBufferSerializer.Default.GetMaxSize(upstream); var returnBuffer = new byte[maxReturnSize]; returnLength = FlatBufferSerializer.Default.Serialize(upstream, returnBuffer); Console.WriteLine("return length: " + returnLength); Serialize56(returnLengthBuffer, returnLength); for (int i = 0; i < 8; i++) { Console.WriteLine("returnLengthBuffer: " + (byte)returnLengthBuffer[i]); comm1Accessor.Write(i, returnLengthBuffer[i]); } for (int i = 0; i < returnLength; i++) { comm1Accessor.Write(i + 8, returnBuffer[i]); } comm1Accessor.Flush(); Console.WriteLine("wrote to comm1Accessor"); for (int i = 0; i < 8; i++) { outputBuffer[i] = resBuffer[i]; outputBuffer[i + 8] = nsBuffer[i]; outputBuffer[i + 16] = sonarBuffer[i]; } outputWriter.Write(outputBuffer, 0, outputBuffer.Length); outputWriter.Flush(); Console.WriteLine("wrote outputbuffer"); } }
public TOutput GetItem() => Upstream.GetItem();