NETHostUnix.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. int NETHostUnix::ExecAssembly(const String& assemblyName, const Vector<String>& args)
  51. {
  52. //void* hostHandle,
  53. //unsigned int domainId,
  54. //int argc,
  55. //const char** argv,
  56. //const char* managedAssemblyPath,
  57. //unsigned int* exitCode);
  58. const char** _argv { 0 };
  59. PODVector<const char*> argv;
  60. for (unsigned i = 0; i < args.Size(); i++)
  61. argv.Push(args[i].CString());
  62. unsigned int exitCode = 0;
  63. ExecuteAssembly_(hostHandle_, domainId_, (int) argv.Size(), argv.Size() ? &argv[0] : _argv, assemblyName.CString(), &exitCode);
  64. return (int) exitCode;
  65. }
  66. bool NETHostUnix::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
  67. {
  68. if (!CreateDelegate_)
  69. return false;
  70. *funcOut = 0;
  71. // TODO: wrap in SharedPtr to control delegate lifetime?
  72. int st = CreateDelegate_(hostHandle_,
  73. domainId_,
  74. assemblyName.CString(),
  75. qualifiedClassName.CString(),
  76. methodName.CString(),
  77. funcOut);
  78. // ensure ptr isn't uninitialized
  79. if (st < 0)
  80. *funcOut = 0;
  81. return st >= 0;
  82. }
  83. void NETHostUnix::WaitForDebuggerConnect()
  84. {
  85. }
  86. void NETHostUnix::AddFilesFromDirectoryToTPAList(const String& targetPath, Vector<String>& trustedAssemblies)
  87. {
  88. String _targetPath = AddTrailingSlash(targetPath);
  89. FileSystem* fs = GetSubsystem<FileSystem>();
  90. Vector<String> results;
  91. fs->ScanDir(results, _targetPath, "*.dll", SCAN_FILES, true);
  92. for (unsigned i = 0; i < results.Size(); i++)
  93. {
  94. const String& assembly = results[i];
  95. // TODO: apply filtering to catch duplicates/handle native images, etc
  96. trustedAssemblies.Push(_targetPath + assembly);
  97. }
  98. }
  99. void NETHostUnix::GenerateTPAList(String& tpaList)
  100. {
  101. tpaList = String::EMPTY;
  102. Vector<String> trustedAssemblies;
  103. // Add the CoreCLR assemblies to TPA
  104. AddFilesFromDirectoryToTPAList(coreCLRFilesAbsPath_, trustedAssemblies);
  105. Vector<String> paths = coreCLRTPAPaths_.Split(';');
  106. for (unsigned i = 0; i < paths.Size(); i++)
  107. {
  108. AddFilesFromDirectoryToTPAList(paths[i], trustedAssemblies);
  109. }
  110. /*
  111. for (unsigned i = 0; i < trustedAssemblies.Size(); i++)
  112. {
  113. LOGINFOF("TPA: %s", trustedAssemblies[i].CString());
  114. }
  115. */
  116. // Note : instead of ; here
  117. tpaList.Join(trustedAssemblies, ":");
  118. }
  119. bool NETHostUnix::CreateAppDomain(String& errorMsg)
  120. {
  121. // Allowed property names:
  122. // APPBASE
  123. // - The base path of the application from which the exe and other assemblies will be loaded
  124. //
  125. // TRUSTED_PLATFORM_ASSEMBLIES
  126. // - The list of complete paths to each of the fully trusted assemblies
  127. //
  128. // APP_PATHS
  129. // - The list of paths which will be probed by the assembly loader
  130. //
  131. // APP_NI_PATHS
  132. // - The list of additional paths that the assembly loader will probe for ngen images
  133. //
  134. // NATIVE_DLL_SEARCH_DIRECTORIES
  135. // - The list of paths that will be probed for native DLLs called by PInvoke
  136. //
  137. const char *propertyKeys[] = {
  138. "TRUSTED_PLATFORM_ASSEMBLIES",
  139. "APP_PATHS",
  140. "APP_NI_PATHS",
  141. "NATIVE_DLL_SEARCH_DIRECTORIES",
  142. "AppDomainCompatSwitch"
  143. };
  144. String tpaList;
  145. GenerateTPAList(tpaList);
  146. String appPath = "";
  147. Vector<String> nativeSearch;
  148. nativeSearch.Push(coreCLRFilesAbsPath_);
  149. nativeSearch.Push("");
  150. String nativeDllSearchDirs;
  151. nativeDllSearchDirs.Join(nativeSearch, ";");
  152. const char *propertyValues[] = {
  153. // TRUSTED_PLATFORM_ASSEMBLIES
  154. tpaList.CString(),
  155. // APP_PATHS
  156. appPath.CString(),
  157. // APP_NI_PATHS
  158. appPath.CString(),
  159. // NATIVE_DLL_SEARCH_DIRECTORIES
  160. nativeDllSearchDirs.CString(),
  161. // AppDomainCompatSwitch
  162. "UseLatestBehaviorWhenTFMNotSpecified"
  163. };
  164. int st = InitializeCoreCLR_(
  165. "AtomicEditor",
  166. "AtomicNETDomain",
  167. sizeof(propertyKeys) / sizeof(propertyKeys[0]),
  168. propertyKeys,
  169. propertyValues,
  170. &hostHandle_,
  171. &domainId_);
  172. if (st < 0)
  173. {
  174. Shutdown();
  175. errorMsg = ToString("NETCore: coreclr_initialize failed - status: 0x%08x\n", st);
  176. return false;
  177. }
  178. return true;
  179. }
  180. bool NETHostUnix::LoadCLRDLL(String& errorMsg)
  181. {
  182. String coreClrDllPath = AddTrailingSlash(coreCLRFilesAbsPath_) + sCoreClrDll;
  183. coreCLRDLLHandle_ = SDL_LoadObject(coreClrDllPath.CString());
  184. if (coreCLRDLLHandle_ == NULL)
  185. {
  186. errorMsg = ToString("NETCore: Unable to load %s with error %s",
  187. coreClrDllPath.CString(),
  188. SDL_GetError());
  189. return false;
  190. }
  191. InitializeCoreCLR_ = (InitializeCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_initialize");
  192. if (!InitializeCoreCLR_)
  193. {
  194. errorMsg = ToString("NETCore: Unable to get coreclr_initialize entry point in %s", coreClrDllPath.CString());
  195. return false;
  196. }
  197. ShutdownCoreCLR_ = (ShutdownCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_shutdown");
  198. if (!ShutdownCoreCLR_)
  199. {
  200. errorMsg = ToString("NETCore: Unable to get coreclr_shutdown entry point in %s", coreClrDllPath.CString());
  201. return false;
  202. }
  203. CreateDelegate_ = (CreateDelegateFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_create_delegate");
  204. if (!CreateDelegate_)
  205. {
  206. errorMsg = ToString("NETCore: Unable to get coreclr_create_delegate entry point in %s", coreClrDllPath.CString());
  207. return false;
  208. }
  209. ExecuteAssembly_ = (ExecuteAssemblyFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_execute_assembly");
  210. if (!ExecuteAssembly_)
  211. {
  212. errorMsg = ToString("NETCore: Unable to get coreclr_execute_assembly entry point in %s", coreClrDllPath.CString());
  213. return false;
  214. }
  215. return true;
  216. }
  217. void NETHostUnix::Shutdown()
  218. {
  219. if (ShutdownCoreCLR_ && hostHandle_)
  220. {
  221. ShutdownCoreCLR_(hostHandle_, domainId_);
  222. }
  223. if (coreCLRDLLHandle_)
  224. {
  225. SDL_UnloadObject(coreCLRDLLHandle_);
  226. }
  227. coreCLRDLLHandle_ = 0;
  228. hostHandle_ = 0;
  229. domainId_ = 0;
  230. InitializeCoreCLR_ = 0;
  231. ExecuteAssembly_ = 0;
  232. CreateDelegate_ = 0;
  233. ShutdownCoreCLR_ = 0;
  234. }
  235. }
  236. /*
  237. static const char *dbg_channel_names[]=
  238. {
  239. "PAL",
  240. "LOADER",
  241. "HANDLE",
  242. "SHMEM",
  243. "THREAD",
  244. "EXCEPT",
  245. "CRT",
  246. "UNICODE",
  247. "ARCH",
  248. "SYNC",
  249. "FILE",
  250. "VIRTUAL",
  251. "MEM",
  252. "SOCKET",
  253. "DEBUG",
  254. "LOCALE",
  255. "MISC",
  256. "MUTEX",
  257. "CRITSEC",
  258. "POLL",
  259. "CRYPT",
  260. "SHFOLDER"
  261. #ifdef FEATURE_PAL_SXS
  262. , "SXS"
  263. #endif // FEATURE_PAL_SXS
  264. };
  265. static const char *dbg_level_names[]=
  266. {
  267. "ENTRY",
  268. "TRACE",
  269. "WARN",
  270. "ERROR",
  271. "ASSERT",
  272. "EXIT"
  273. };
  274. */