Пример #1
0
        public override Task AddTraceAsync(TraceEntity traceEntity)
        {
            // Allocate dictionary for mapping instruction addresses to memory access hashes
            Dictionary <ulong, byte[]> instructionHashes = new Dictionary <ulong, byte[]>();

            // Hash all memory access instructions
            foreach (var traceEntry in traceEntity.PreprocessedTraceFile.Entries)
            {
                // Extract instruction and memory address
                ulong instructionAddress;
                ulong memoryAddress;
                switch (traceEntry.EntryType)
                {
                case TraceEntry.TraceEntryTypes.HeapMemoryAccess:
                {
                    var heapMemoryAccess = (TraceEntryTypes.HeapMemoryAccess)traceEntry;
                    instructionAddress = ((ulong)heapMemoryAccess.InstructionImageId << 32) | heapMemoryAccess.InstructionRelativeAddress;
                    memoryAddress      = ((ulong)heapMemoryAccess.MemoryAllocationBlockId << 32) | heapMemoryAccess.MemoryRelativeAddress;
                    break;
                }

                case TraceEntry.TraceEntryTypes.ImageMemoryAccess:
                {
                    var imageMemoryAccess = (TraceEntryTypes.ImageMemoryAccess)traceEntry;
                    instructionAddress = ((ulong)imageMemoryAccess.InstructionImageId << 32) | imageMemoryAccess.InstructionRelativeAddress;
                    memoryAddress      = ((ulong)imageMemoryAccess.MemoryImageId << 32) | imageMemoryAccess.MemoryRelativeAddress;
                    break;
                }

                case TraceEntry.TraceEntryTypes.StackMemoryAccess:
                {
                    var stackMemoryAccess = (TraceEntryTypes.StackMemoryAccess)traceEntry;
                    instructionAddress = ((ulong)stackMemoryAccess.InstructionImageId << 32) | stackMemoryAccess.InstructionRelativeAddress;
                    memoryAddress      = stackMemoryAccess.MemoryRelativeAddress;
                    break;
                }

                default:
                    continue;
                }

                // Update hash
                byte[] hash;
                if (!instructionHashes.TryGetValue(instructionAddress, out hash))
                {
                    hash = new byte[32];
                    instructionHashes.Add(instructionAddress, hash);
                }
                var hashSpan = hash.AsSpan();
                MemoryMarshal.Write(hashSpan, ref memoryAddress); // Will overwrite first 8 bytes of hash
                Blake2s.ComputeAndWriteHash(hashSpan, hashSpan);
            }

            // Store instruction hashes
            _testcaseInstructionHashes.AddOrUpdate(traceEntity.Id, instructionHashes, (t, h) => h);

            // Done
            return(Task.CompletedTask);
        }
Пример #2
0
    private static byte[] blake2sNoAllocSelfTest()
    {
        Span <byte> buff = stackalloc byte[Blake2b.DefaultDigestLength];
        var         inc  = Blake2s.CreateIncrementalHasher(blake2sCheck.Length);

        foreach (int diglen in new[] { 16, 20, 28, 32 })
        {
            foreach (int msglen in new[] { 0, 3, 64, 65, 255, 1024 })
            {
                var msg = getTestSequence(msglen);
                var key = getTestSequence(diglen);

                Blake2s.ComputeAndWriteHash(diglen, msg, buff);
                inc.Update(buff.Slice(0, diglen));

                Blake2s.ComputeAndWriteHash(diglen, key, msg, buff);
                inc.Update(buff.Slice(0, diglen));
            }
        }

        return(inc.TryFinish(buff, out int len) ? buff.Slice(0, len).ToArray() : Array.Empty <byte>());
    }