Why doesn't the VS 2015 debug visualizer work on this unsafe struct?

眉间皱痕 提交于 2019-12-14 02:29:55

问题


I have to hash a lot of stuff...and I keep the hash as a kind of content identity. I use these things everywhere. The hashes are 20-byte arrays and I recently changed them to a (seemingly) simple unsafe struct in a c# project that has a ToString() method. However, at runtime, the visualization is always the default value (all zero) - even after the content changes.

The only instance data in the struct is a fixed byte array, which gets written-to by a number of methods. Without the ToString() method, the visualizer showed some lame representation of the value - but it was (I think) the address of the fixed array.

None of the permuting methods results in the visualizer changing from the default visualization.

For example:

Even though the ToString() method produces the following:

...which is the expected value (and expected visualization).

I've tried [DebuggerDisplay( "{ToString()}" )] and making the thing serializable, but still get the same results. So, does this mean I'm out of luck on unsafe structs, or am I doing something wrong that I haven't pinpointed?

Edit:

My apologies for not putting in a complete, verifiable sample. Here's a complete console app that demonstrates the issue. Just replace the content of Class1.cs with this code and you'll see the problem.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main( string[ ] args )
    {
      // sample data...
      var bytes = new byte[ 20 ] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };

      var hash1 = Hash.FromBytes( bytes );  //deserailize a hash from array
      Debug.WriteLine( hash1 );

      var hash2 = new Hash( bytes, 0, 20 );  //computes a hash over array
      Debug.WriteLine( hash2 );

      using ( var stream = new MemoryStream( bytes ) )
      {
        var hash3 = new Hash( );// empty hash
        hash3.Read( stream );  // deserialize a hash from stream
        Debug.WriteLine( hash3 );

        stream.Position = 0;

        var hash4 = new Hash( stream ); //compute hash over stream
        Debug.WriteLine( hash4 );

        var hash5 = new Hash( "Compute the hash of a string" );
        Debug.WriteLine( hash5 );

        Debug.Assert( hash1 == hash3, "Oops!" );
        Debug.Assert( hash2 == hash4, "Nope!" );
        Debug.Assert( hash1 != hash2, "Golly!" );
        Debug.Assert( hash3 != hash4, "Shucks!" );

      }
    }
  }


  /// <summary>Represents a hash of a string or byte array</summary>
  [StructLayout( LayoutKind.Sequential )]
  public unsafe struct Hash: IComparable<Hash>
  {

    #region statics and constants
    /// <summary>Character map for byte array to string</summary>
    readonly static char[ ] hex = new char[ ] {
              '0', '1', '2', '3',
              '4', '5', '6', '7',
              '8', '9', 'a', 'b',
              'c', 'd', 'e', 'f' };

    /// <summary>Synchronization primitive</summary>
    readonly static object sync = new object( );

    /// <summary>Buffer for reading hashes from streams, strings, and arrays</summary>
    readonly static byte[ ] buffer = new byte[ 20 ];

    /// <summary>ToString workspace</summary>
    static char[ ] hexChars = new char[ Length * 2 ];

    /// <summary>Returns a hash that has no value</summary>
    public readonly static Hash EmptyHash = new Hash( );

    /// <summary>Retruns the length of any <see cref="Hash"/></summary>
    public const int Length = 20;


    /// <summary>Returns a <see cref="HashAlgorithm"/> that the system uses to compute hashes</summary>
    public static HashAlgorithm GetHasher( )
    {
      return new SHA1Managed( );
    }

    #endregion

    #region private data
    /// <summary>A pointer to the underlying data</summary>
    fixed byte value[ 20 ];
    #endregion

    #region construction

    /// <summary>Creates a hash from a string</summary>
    public Hash( string hashable )
    {
      fixed ( byte* bytes = value, sourceBytes = GetHasher( ).ComputeHash( Encoding.Unicode.GetBytes( hashable ) ) )
      {
        NativeMethods.CopyMemory( bytes, sourceBytes, Length );
      }
    }

    /// <summary>Creates a hash from a byte array</summary>
    public Hash( byte[ ] source, int index, int length )
    {
      fixed ( byte* bytes = value, sourceBytes = GetHasher( ).ComputeHash( source, index, length ) )
      {
        NativeMethods.CopyMemory( bytes, sourceBytes, Length );
      }
    }

    /// <summary>Creates a hash from a series of hashes</summary>
    public Hash( IEnumerable<Hash> hashes )
    {
      var hasher = GetHasher( );
      var buffer = new byte[ Length ];
      hashes.Do( key =>
      {
        key.CopyTo( buffer );
        hasher.TransformBlock( buffer, 0, Length, buffer, 0 );
      } );
      hasher.TransformFinalBlock( buffer, 0, 0 );
      fixed ( byte* bytes = value, source = hasher.Hash )
      {
        NativeMethods.CopyMemory( bytes, source, Length );
      }
    }


    /// <summary>Creates a hash over a stream from current position to end</summary>
    public Hash( Stream stream )
    {
      const int bufferSize = 4096;
      var hasher = GetHasher( );
      var bytesRead = 0;
      var buffer = new byte[ bufferSize ];
      while ( true )
      {
        bytesRead = stream.Read( buffer, 0, bufferSize );
        if ( bytesRead == 0 )
        {
          hasher.TransformFinalBlock( buffer, 0, 0 );
          break;
        }
        else
        {
          hasher.TransformBlock( buffer, 0, bytesRead, buffer, 0 );
        }
      }
      fixed ( byte* bytes = value, source = hasher.Hash )
      {
        NativeMethods.CopyMemory( bytes, source, Length );
      }
    }

    #endregion


    #region methods
    /// <summary>Copies the hash to the start of a byte array</summary>
    public void CopyTo( byte[ ] buffer )
    {
      CopyTo( buffer, 0 );
    }

    /// <summary>Copies the hash to a byte array</summary>
    public void CopyTo( byte[ ] buffer, int offset )
    {
      if ( buffer == null ) throw new ArgumentNullException( nameof( buffer ) );
      if ( buffer.Length < ( offset + Length ) ) throw new ArgumentOutOfRangeException( nameof( buffer ) );
      fixed ( byte* bytes = value, dest = buffer )
      {
        NativeMethods.CopyMemory( dest + offset, bytes, Length );
      }
    }

    /// <summary>Returns a byte-array representation of the <see cref="Hash"/></summary>
    /// <remarks>The returned value is a copy</remarks>
    public byte[ ] GetBytes( )
    {
      var results = new byte[ Length ];
      fixed ( byte* bytes = value, target = results )
      {
        NativeMethods.CopyMemory( target, bytes, Length );
      }
      return results;
    }

    /// <summary>Compares this hash to another</summary>
    public int CompareTo( Hash other )
    {
      var comparedByte = 0;
      fixed ( byte* bytes = value )
      {
        for ( int i = 0; i < Length; i++ )
        {
          comparedByte = ( *( bytes + i ) ).CompareTo( other.value[ i ] );
          if ( comparedByte != 0 ) break;
        }
        return comparedByte;
      }
    }

    /// <summary>Returns true if <paramref name="obj"/> is a <see cref="Hash"/> and it's value exactly matches</summary>
    /// <param name="obj">The <see cref="Hash"/> to compare to this one</param>
    /// <returns>true if the values match</returns>
    public override bool Equals( object obj )
    {
      if ( obj == null || !( obj is Hash ) ) return false;
      var other = ( Hash ) obj;
      return CompareTo( other ) == 0;
    }

    /// <summary>Returns a .Net hash code for this <see cref="Hash"/></summary>
    public override int GetHashCode( )
    {
      unchecked
      {
        int hashCode = 17;
        fixed ( byte* bytes = value )
        {
          for ( int i = 0; i < Length; i++ )
          {
            hashCode = hashCode * 31 + *( bytes + i );
          }
          return hashCode;
        }
      }
    }

    /// <summary>Returns a hex string representation of the hash</summary>
    public override string ToString( )
    {
      lock ( sync )
      {
        fixed ( char* hexFixed = hex, hexCharsFixed = hexChars )
        {
          fixed ( byte* bytes = value )
          {
            for ( int i = 0; i < Length; i++ )
            {
              *( hexCharsFixed + ( i * 2 ) ) = *( hexFixed + ( *( bytes + i ) >> 4 ) );
              *( hexCharsFixed + ( 1 + ( i * 2 ) ) ) = *( hexFixed + ( *( bytes + i ) & 0xf ) );
            }
            return new string( hexChars );
          }
        }
      }
    }

    /// <summary>Reads a <see cref="Hash"/> from the provided stream</summary>
    public void Read( Stream stream )
    {
      lock ( sync )
      {
        var retryCount = 0;
        var bytesRead = ReadStream( stream, buffer, 0, Length, ref retryCount );
        if ( bytesRead == Length )
        {
          fixed ( byte* bytes = value, sourceBytes = buffer )
          {
            NativeMethods.CopyMemory( bytes, sourceBytes, Length );
          }
        }
      }
    }

    /// <summary>Tries hard to populate a <see cref="Hash"/> from a stream - across multiple reads if necessary - up to a point</summary>
    int ReadStream( Stream stream, byte[ ] buffer, int offset, int length, ref int retryCount )
    {
      const int maxStreamReadRetries = 3;

      var bytesRead = stream.Read( buffer, offset, length );
      var done = bytesRead == 0 || bytesRead == length;  // eos, timeout, or success
      if ( !done )
      {
        if ( retryCount++ >= maxStreamReadRetries ) return 0;
        bytesRead += ReadStream( stream, buffer, bytesRead, length - bytesRead, ref retryCount );
      }
      return bytesRead;
    }

    /// <summary>Writes the hash to a stream</summary>
    public void Write( Stream stream )
    {
      lock ( sync )
      {
        fixed ( byte* bytes = value, targetBytes = buffer )
        {
          NativeMethods.CopyMemory( targetBytes, bytes, Length );
        }
        stream.Write( buffer, 0, Length );
      }
    }

    /// <summary>Returns true if the hash has no value</summary>
    public bool IsEmpty( )
    {
      return Equals( EmptyHash );
    }

    /// <summary>Returns the result of XORing two <see cref="Hash"/>es</summary>
    public static Hash Combine( Hash a, Hash b )
    {
      var results = new Hash( );
      for ( int i = 0; i < Length; i++ )
      {
        *( results.value + i ) = ( byte ) ( *( a.value + i ) ^ *( b.value + i ) );
      }
      return results;
    }

    /// <summary>Returns the first non-empty hash from a list</summary>
    public static Hash FirstNotEmpty( params Hash[ ] hashes )
    {
      foreach ( var hash in hashes ) if ( !hash.IsEmpty( ) ) return hash;
      throw new ArgumentOutOfRangeException( nameof( hashes ) );
    }

    /// <summary>Implements == operator</summary>
    public static bool operator ==( Hash a, Hash b )
    {
      return a.Equals( b );
    }

    /// <summary>Implements != operator</summary>
    public static bool operator !=( Hash a, Hash b )
    {
      return !a.Equals( b );
    }


    /// <summary>Converts a byte array to a <see cref="Hash"/></summary>
    public static Hash FromBytes( byte[ ] hashBytes, int offset = 0 )
    {
      if ( hashBytes == null ) throw new ArgumentNullException( nameof( hashBytes ) );
      if ( ( hashBytes.Length + offset ) < Length ) throw new ArgumentOutOfRangeException( nameof( hashBytes ) );
      var hash = new Hash( );
      fixed ( byte* sourceBytes = hashBytes )
        NativeMethods.CopyMemory( hash.value, sourceBytes + offset, Length );
      return hash;
    }
    #endregion

  }

  class NativeMethods
  {
    [DllImport( "Kernel32", SetLastError = true, EntryPoint = "CopyMemory" )]
    internal unsafe static extern void CopyMemory( void* destination, void* source, uint length );
  }


  static class Extensions
  {
    /// <summary>Applies action to each element of the collection.</summary>
    public static void Do<T>( this IEnumerable<T> enumerable, Action<T> action )
    {
      if ( enumerable == null ) throw new ArgumentNullException( "enumerable" );
      if ( action == null ) throw new ArgumentNullException( "action" );
      foreach ( var item in enumerable ) action( item );
    }
  }
}

Set a breakpoint near the end of the main method and let your cursor hover over any of the hash1 through hash5 variables after they've been created.

Note: You have to set allow unsafe code in the project properties.


回答1:


Under Tools->Options->Debugging->General->Enable "Use Managed Compatibility Mode". It would work well.



来源:https://stackoverflow.com/questions/41044890/why-doesnt-the-vs-2015-debug-visualizer-work-on-this-unsafe-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!