System.Data.SQLite
Check-in [6546019824]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:More work in progress on making the native library preloader more portable.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | netStandard20
Files: files | file ages | folders
SHA1:6546019824c355058d5d37095b05205742beca6c
User & Date: mistachkin 2018-04-06 19:18:01
Context
2018-04-06
19:21
Map the 'x86_64' processor architecture to the 'x64' platform. check-in: b90add92d7 user: mistachkin tags: netStandard20
19:18
More work in progress on making the native library preloader more portable. check-in: 6546019824 user: mistachkin tags: netStandard20
18:29
Fix comment. check-in: 695e80e91f user: mistachkin tags: netStandard20
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

933
934
935
936
937
938
939




































































940
941
942
943
944
945
946
...
961
962
963
964
965
966
967


968
969
970
971
972
973
974
...
988
989
990
991
992
993
994
995




















































































996
997
998
999
1000
1001
1002
....
1165
1166
1167
1168
1169
1170
1171








1172
1173
1174
1175
1176
1177
1178
....
2606
2607
2608
2609
2610
2611
2612
















2613
2614
2615
2616
2617
2618
2619
#if !PLATFORM_COMPACTFRAMEWORK
  /// <summary>
  /// This class declares P/Invoke methods to call native POSIX APIs.
  /// </summary>
  [SuppressUnmanagedCodeSecurity]
  internal static class UnsafeNativeMethodsPosix
  {




































































      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// This is the P/Invoke method that wraps the native Unix dlopen
      /// function.  See the POSIX documentation for full details on what it
      /// does.
      /// </summary>
      /// <param name="fileName">
................................................................................
          EntryPoint = "dlopen",
          CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi,
          BestFitMapping = false, ThrowOnUnmappableChar = true,
          SetLastError = true)]
      internal static extern IntPtr dlopen(string fileName, int mode);

      /////////////////////////////////////////////////////////////////////////


      /// <summary>
      /// For use with dlopen(), bind function calls lazily.
      /// </summary>
      internal const int RTLD_LAZY = 0x1;

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
................................................................................
      /// </summary>
      internal const int RTLD_LOCAL = 0x000;

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// For use with dlopen(), the defaults used by this class.
      /// </summary>
      internal  const int RTLD_DEFAULT = RTLD_NOW | RTLD_GLOBAL;




















































































  }
#endif
  #endregion

  /////////////////////////////////////////////////////////////////////////////

  #region Unmanaged Interop Methods Static Class (Win32)
................................................................................

      #region Private Data
      /// <summary>
      /// This lock is used to protect the static _SQLiteNativeModuleFileName,
      /// _SQLiteNativeModuleHandle, and processorArchitecturePlatforms fields.
      /// </summary>
      private static readonly object staticSyncRoot = new object();









      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// This dictionary stores the mappings between processor architecture
      /// names and platform names.  These mappings are now used for two
      /// purposes.  First, they are used to determine if the assembly code
      /// base should be used instead of the location, based upon whether one
................................................................................
              }
              catch
              {
                  // do nothing.
              }
#endif
          }
















#else
          if (processorArchitecture == null)
          {
              //
              // NOTE: On the .NET Compact Framework, attempt to use the native
              //       Win32 API function (via P/Invoke) that can provide us
              //       with the processor architecture.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
....
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
....
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
....
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
....
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
#if !PLATFORM_COMPACTFRAMEWORK
  /// <summary>
  /// This class declares P/Invoke methods to call native POSIX APIs.
  /// </summary>
  [SuppressUnmanagedCodeSecurity]
  internal static class UnsafeNativeMethodsPosix
  {
      /// <summary>
      /// This structure is used when running on POSIX operating systems
      /// to store information about the current machine, including the
      /// human readable name of the operating system as well as that of
      /// the underlying hardware.
      /// </summary>
      internal struct utsname
      {
          public string sysname;  /* Name of this implementation of
                                   * the operating system. */
          public string nodename; /* Name of this node within the
                                   * communications network to which
                                   * this node is attached, if any. */
          public string release;  /* Current release level of this
                                   * implementation. */
          public string version;  /* Current version level of this
                                   * release. */
          public string machine;  /* Name of the hardware type on
                                   * which the system is running. */
      }

      /////////////////////////////////////////////////////////////////////////

      /// <summary>
      /// This structure is passed directly to the P/Invoke method to
      /// obtain the information about the current machine, including
      /// the human readable name of the operating system as well as
      /// that of the underlying hardware.
      /// </summary>
      [StructLayout(LayoutKind.Sequential)]
      private struct utsname_interop
      {
          //
          // NOTE: The following string fields should be present in
          //       this buffer, all of which will be zero-terminated:
          //
          //                      sysname
          //                      nodename
          //                      release
          //                      version
          //                      machine
          //
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4096)]
          public byte[] buffer;
      }

      /////////////////////////////////////////////////////////////////////////

      /// <summary>
      /// This is the P/Invoke method that wraps the native Unix uname
      /// function.  See the POSIX documentation for full details on what it
      /// does.
      /// </summary>
      /// <param name="name">
      /// Structure containing a preallocated byte buffer to fill with the
      /// requested information.
      /// </param>
      /// <returns>
      /// Zero for success and less than zero upon failure.
      /// </returns>
#if NET_STANDARD_20
      [DllImport("libc",
#else
      [DllImport("__Internal",
#endif
          CallingConvention = CallingConvention.Cdecl)]
      private static extern int uname(out utsname_interop name);

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// This is the P/Invoke method that wraps the native Unix dlopen
      /// function.  See the POSIX documentation for full details on what it
      /// does.
      /// </summary>
      /// <param name="fileName">
................................................................................
          EntryPoint = "dlopen",
          CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi,
          BestFitMapping = false, ThrowOnUnmappableChar = true,
          SetLastError = true)]
      internal static extern IntPtr dlopen(string fileName, int mode);

      /////////////////////////////////////////////////////////////////////////

      #region Private Constants
      /// <summary>
      /// For use with dlopen(), bind function calls lazily.
      /// </summary>
      internal const int RTLD_LAZY = 0x1;

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
................................................................................
      /// </summary>
      internal const int RTLD_LOCAL = 0x000;

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// For use with dlopen(), the defaults used by this class.
      /// </summary>
      internal const int RTLD_DEFAULT = RTLD_NOW | RTLD_GLOBAL;
      #endregion

      /////////////////////////////////////////////////////////////////////////

      #region Private Data
      /// <summary>
      /// These are the characters used to separate the string fields within
      /// the raw buffer returned by the <see cref="uname" /> P/Invoke method.
      /// </summary>
      private static readonly char[] utsNameSeparators = { '\0' };
      #endregion

      /////////////////////////////////////////////////////////////////////////

      #region Private Methods
      /// <summary>
      /// This method is a wrapper around the <see cref="uname" /> P/Invoke
      /// method that extracts and returns the human readable strings from
      /// the raw buffer.
      /// </summary>
      /// <param name="utsName">
      /// This structure, which contains strings, will be filled based on the
      /// data placed in the raw buffer returned by the <see cref="uname" />
      /// P/Invoke method.
      /// </param>
      /// <returns>
      /// Non-zero upon success; otherwise, zero.
      /// </returns>
      internal static bool GetOsVersionInfo(
          ref utsname utsName
          )
      {
          try
          {
              utsname_interop utfNameInterop;

              if (uname(out utfNameInterop) < 0)
                  return false;

              if (utfNameInterop.buffer == null)
                  return false;

              string bufferAsString = Encoding.UTF8.GetString(
                  utfNameInterop.buffer);

              if ((bufferAsString == null) || (utsNameSeparators == null))
                  return false;

              bufferAsString = bufferAsString.Trim(utsNameSeparators);

              string[] parts = bufferAsString.Split(
                  utsNameSeparators, StringSplitOptions.RemoveEmptyEntries);

              if (parts == null)
                  return false;

              utsname localUtsName = new utsname();

              if (parts.Length >= 1)
                  localUtsName.sysname = parts[0];

              if (parts.Length >= 2)
                  localUtsName.nodename = parts[1];

              if (parts.Length >= 3)
                  localUtsName.release = parts[2];

              if (parts.Length >= 4)
                  localUtsName.version = parts[3];

              if (parts.Length >= 5)
                  localUtsName.machine = parts[4];

              utsName = localUtsName;
              return true;
          }
          catch
          {
              // do nothing.
          }

          return false;
      }
      #endregion
  }
#endif
  #endregion

  /////////////////////////////////////////////////////////////////////////////

  #region Unmanaged Interop Methods Static Class (Win32)
................................................................................

      #region Private Data
      /// <summary>
      /// This lock is used to protect the static _SQLiteNativeModuleFileName,
      /// _SQLiteNativeModuleHandle, and processorArchitecturePlatforms fields.
      /// </summary>
      private static readonly object staticSyncRoot = new object();

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// This structure is used to cache the human readable strings extracted
      /// from the raw buffer passed to the uname P/Invoke method.  It is only
      /// used on POSIX operating systems.
      /// </summary>
      private static UnsafeNativeMethodsPosix.utsname utsName;

      /////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// This dictionary stores the mappings between processor architecture
      /// names and platform names.  These mappings are now used for two
      /// purposes.  First, they are used to determine if the assembly code
      /// base should be used instead of the location, based upon whether one
................................................................................
              }
              catch
              {
                  // do nothing.
              }
#endif
          }

          //
          // NOTE: When running on POSIX (non-Windows), attempt to query the
          //       machine from the operating system via uname().
          //
          if ((processorArchitecture == null) && !HelperMethods.IsWindows())
          {
              lock (staticSyncRoot)
              {
                  if ((utsName.machine != null) ||
                      UnsafeNativeMethodsPosix.GetOsVersionInfo(ref utsName))
                  {
                      processorArchitecture = utsName.machine;
                  }
              }
          }
#else
          if (processorArchitecture == null)
          {
              //
              // NOTE: On the .NET Compact Framework, attempt to use the native
              //       Win32 API function (via P/Invoke) that can provide us
              //       with the processor architecture.