NETHostUnix.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include <ThirdParty/SDL/include/SDL.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/Core/StringUtils.h>
  4. #include "NETHostUnix.h"
  5. /*
  6. mcs /nostdlib /noconfig /r:System.Console.dll /r:System.Runtime.dll /r:System.IO.dll /r:System.IO.FileSystem.dll /r:mscorlib.dll HelloWorld.cs
  7. export PAL_DBG_CHANNELS="+all.all"
  8. export PAL_DBG_CHANNELS="+PAL.TRACE:+LOADER.TRACE:+ARCH.TRACE:+EXCEPT.ENTRY:+DEBUG.TRACE:+LOCALE.TRACE:+MISC.WARN:+HANDLE.TRACE:+UNICODE.TRACE:-FILE.ERROR"
  9. // see bottom of file for channels
  10. */
  11. #ifdef ATOMIC_PLATFORM_OSX
  12. static const char * const sCoreClrDll = "libcoreclr.dylib";
  13. #elif ATOMIC_PLATFORM_WINDOWS
  14. static const char * const sCoreClrDll = "coreclr.dll";
  15. #else
  16. static const char * const sCoreClrDll = "libcoreclr.so";
  17. #endif
  18. namespace Atomic
  19. {
  20. /// Construct.
  21. NETHostUnix::NETHostUnix(Context* context) : NETHost(context),
  22. InitializeCoreCLR_(0),
  23. ExecuteAssembly_(0),
  24. CreateDelegate_(0),
  25. ShutdownCoreCLR_(0),
  26. coreCLRDLLHandle_(0),
  27. hostHandle_(0),
  28. domainId_(0)
  29. {
  30. }
  31. /// Destruct.
  32. NETHostUnix::~NETHostUnix()
  33. {
  34. }
  35. bool NETHostUnix::Initialize()
  36. {
  37. if (!coreCLRFilesAbsPath_.Length())
  38. return false;
  39. if (!coreCLRTPAPaths_.Length())
  40. return false;
  41. if (!coreCLRAssemblyLoadPaths_.Length())
  42. return false;
  43. String errorMsg;
  44. if (!LoadCLRDLL(errorMsg))
  45. return false;
  46. if (!CreateAppDomain(errorMsg))
  47. return false;
  48. return true;
  49. }
  50. bool NETHostUnix::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
  51. {
  52. if (!CreateDelegate_)
  53. return false;
  54. *funcOut = 0;
  55. // TODO: wrap in SharedPtr to control delegate lifetime?
  56. int st = CreateDelegate_(hostHandle_,
  57. domainId_,
  58. assemblyName.CString(),
  59. qualifiedClassName.CString(),
  60. methodName.CString(),
  61. funcOut);
  62. // ensure ptr isn't uninitialized
  63. if (st < 0)
  64. *funcOut = 0;
  65. return st >= 0;
  66. }
  67. void NETHostUnix::WaitForDebuggerConnect()
  68. {
  69. }
  70. void NETHostUnix::AddFilesFromDirectoryToTPAList(const String& targetPath, Vector<String>& trustedAssemblies)
  71. {
  72. String _targetPath = AddTrailingSlash(targetPath);
  73. FileSystem* fs = GetSubsystem<FileSystem>();
  74. Vector<String> results;
  75. fs->ScanDir(results, _targetPath, "*.dll", SCAN_FILES, true);
  76. for (unsigned i = 0; i < results.Size(); i++)
  77. {
  78. const String& assembly = results[i];
  79. // TODO: apply filtering to catch duplicates/handle native images, etc
  80. trustedAssemblies.Push(_targetPath + assembly);
  81. }
  82. }
  83. void NETHostUnix::GenerateTPAList(String& tpaList)
  84. {
  85. tpaList = String::EMPTY;
  86. Vector<String> trustedAssemblies;
  87. // Add the CoreCLR assemblies to TPA
  88. AddFilesFromDirectoryToTPAList(coreCLRFilesAbsPath_, trustedAssemblies);
  89. Vector<String> paths = coreCLRTPAPaths_.Split(';');
  90. for (unsigned i = 0; i < paths.Size(); i++)
  91. {
  92. AddFilesFromDirectoryToTPAList(paths[i], trustedAssemblies);
  93. }
  94. /*
  95. for (unsigned i = 0; i < trustedAssemblies.Size(); i++)
  96. {
  97. LOGINFOF("TPA: %s", trustedAssemblies[i].CString());
  98. }
  99. */
  100. // Note : instead of ; here
  101. tpaList.Join(trustedAssemblies, ":");
  102. }
  103. bool NETHostUnix::CreateAppDomain(String& errorMsg)
  104. {
  105. // Allowed property names:
  106. // APPBASE
  107. // - The base path of the application from which the exe and other assemblies will be loaded
  108. //
  109. // TRUSTED_PLATFORM_ASSEMBLIES
  110. // - The list of complete paths to each of the fully trusted assemblies
  111. //
  112. // APP_PATHS
  113. // - The list of paths which will be probed by the assembly loader
  114. //
  115. // APP_NI_PATHS
  116. // - The list of additional paths that the assembly loader will probe for ngen images
  117. //
  118. // NATIVE_DLL_SEARCH_DIRECTORIES
  119. // - The list of paths that will be probed for native DLLs called by PInvoke
  120. //
  121. const char *propertyKeys[] = {
  122. "TRUSTED_PLATFORM_ASSEMBLIES",
  123. "APP_PATHS",
  124. "APP_NI_PATHS",
  125. "NATIVE_DLL_SEARCH_DIRECTORIES",
  126. "AppDomainCompatSwitch"
  127. };
  128. String tpaList;
  129. GenerateTPAList(tpaList);
  130. String appPath = "";
  131. Vector<String> nativeSearch;
  132. nativeSearch.Push(coreCLRFilesAbsPath_);
  133. nativeSearch.Push("");
  134. String nativeDllSearchDirs;
  135. nativeDllSearchDirs.Join(nativeSearch, ";");
  136. const char *propertyValues[] = {
  137. // TRUSTED_PLATFORM_ASSEMBLIES
  138. tpaList.CString(),
  139. // APP_PATHS
  140. appPath.CString(),
  141. // APP_NI_PATHS
  142. appPath.CString(),
  143. // NATIVE_DLL_SEARCH_DIRECTORIES
  144. nativeDllSearchDirs.CString(),
  145. // AppDomainCompatSwitch
  146. "UseLatestBehaviorWhenTFMNotSpecified"
  147. };
  148. int st = InitializeCoreCLR_(
  149. "AtomicEditor",
  150. "AtomicNETDomain",
  151. sizeof(propertyKeys) / sizeof(propertyKeys[0]),
  152. propertyKeys,
  153. propertyValues,
  154. &hostHandle_,
  155. &domainId_);
  156. if (st < 0)
  157. {
  158. Shutdown();
  159. errorMsg = ToString("NETCore: coreclr_initialize failed - status: 0x%08x\n", st);
  160. return false;
  161. }
  162. return true;
  163. }
  164. bool NETHostUnix::LoadCLRDLL(String& errorMsg)
  165. {
  166. String coreClrDllPath = AddTrailingSlash(coreCLRFilesAbsPath_) + sCoreClrDll;
  167. coreCLRDLLHandle_ = SDL_LoadObject(coreClrDllPath.CString());
  168. if (coreCLRDLLHandle_ == NULL)
  169. {
  170. errorMsg = ToString("NETCore: Unable to load %s with error %s",
  171. coreClrDllPath.CString(),
  172. SDL_GetError());
  173. return false;
  174. }
  175. InitializeCoreCLR_ = (InitializeCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_initialize");
  176. if (!InitializeCoreCLR_)
  177. {
  178. errorMsg = ToString("NETCore: Unable to get coreclr_initialize entry point in %s", coreClrDllPath.CString());
  179. return false;
  180. }
  181. ShutdownCoreCLR_ = (ShutdownCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_shutdown");
  182. if (!ShutdownCoreCLR_)
  183. {
  184. errorMsg = ToString("NETCore: Unable to get coreclr_shutdown entry point in %s", coreClrDllPath.CString());
  185. return false;
  186. }
  187. CreateDelegate_ = (CreateDelegateFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_create_delegate");
  188. if (!CreateDelegate_)
  189. {
  190. errorMsg = ToString("NETCore: Unable to get coreclr_create_delegate entry point in %s", coreClrDllPath.CString());
  191. return false;
  192. }
  193. ExecuteAssembly_ = (ExecuteAssemblyFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_execute_assembly");
  194. if (!ExecuteAssembly_)
  195. {
  196. errorMsg = ToString("NETCore: Unable to get coreclr_execute_assembly entry point in %s", coreClrDllPath.CString());
  197. return false;
  198. }
  199. return true;
  200. }
  201. void NETHostUnix::Shutdown()
  202. {
  203. if (ShutdownCoreCLR_ && hostHandle_)
  204. {
  205. ShutdownCoreCLR_(hostHandle_, domainId_);
  206. }
  207. if (coreCLRDLLHandle_)
  208. {
  209. SDL_UnloadObject(coreCLRDLLHandle_);
  210. }
  211. coreCLRDLLHandle_ = 0;
  212. hostHandle_ = 0;
  213. domainId_ = 0;
  214. InitializeCoreCLR_ = 0;
  215. ExecuteAssembly_ = 0;
  216. CreateDelegate_ = 0;
  217. ShutdownCoreCLR_ = 0;
  218. }
  219. }
  220. /*
  221. static const char *dbg_channel_names[]=
  222. {
  223. "PAL",
  224. "LOADER",
  225. "HANDLE",
  226. "SHMEM",
  227. "THREAD",
  228. "EXCEPT",
  229. "CRT",
  230. "UNICODE",
  231. "ARCH",
  232. "SYNC",
  233. "FILE",
  234. "VIRTUAL",
  235. "MEM",
  236. "SOCKET",
  237. "DEBUG",
  238. "LOCALE",
  239. "MISC",
  240. "MUTEX",
  241. "CRITSEC",
  242. "POLL",
  243. "CRYPT",
  244. "SHFOLDER"
  245. #ifdef FEATURE_PAL_SXS
  246. , "SXS"
  247. #endif // FEATURE_PAL_SXS
  248. };
  249. static const char *dbg_level_names[]=
  250. {
  251. "ENTRY",
  252. "TRACE",
  253. "WARN",
  254. "ERROR",
  255. "ASSERT",
  256. "EXIT"
  257. };
  258. */