db_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_main.c
  15. *
  16. * This file contains the main function of the PAL loader, which loads and
  17. * processes environment, arguments and manifest.
  18. */
  19. #include "pal_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_debug.h"
  23. #include "pal_error.h"
  24. #include "pal_rtld.h"
  25. #include "api.h"
  26. #include <sysdeps/generic/ldsodefs.h>
  27. #include <elf/elf.h>
  28. PAL_CONTROL __pal_control;
  29. PAL_CONTROL * pal_control_addr (void)
  30. {
  31. return &__pal_control;
  32. }
  33. struct pal_internal_state pal_state;
  34. static void load_libraries (void)
  35. {
  36. /* we will not make any assumption for where the libraries are loaded */
  37. char cfgbuf[CONFIG_MAX];
  38. ssize_t len, ret = 0;
  39. /* loader.preload:
  40. any other libraries to preload. The can be multiple URIs,
  41. seperated by commas */
  42. len = get_config(pal_state.root_config, "loader.preload", cfgbuf,
  43. sizeof(cfgbuf));
  44. if (len <= 0)
  45. return;
  46. char * c = cfgbuf, * library_name = c;
  47. for (;; c++)
  48. if (*c == ',' || !(*c)) {
  49. if (c > library_name) {
  50. #if PROFILING == 1
  51. unsigned long before_load_library = _DkSystemTimeQuery();
  52. #endif
  53. *c = 0;
  54. if ((ret = load_elf_object(library_name, OBJECT_PRELOAD)) < 0)
  55. INIT_FAIL(-ret, "Unable to load preload library");
  56. #if PROFILING == 1
  57. pal_state.linking_time +=
  58. _DkSystemTimeQuery() - before_load_library;
  59. #endif
  60. }
  61. if (c == cfgbuf + len)
  62. break;
  63. library_name = c + 1;
  64. }
  65. }
  66. static void read_environments (const char *** envpp)
  67. {
  68. struct config_store * store = pal_state.root_config;
  69. const char ** envp = *envpp;
  70. /* loader.env.*: rewriting host environment variables */
  71. struct setenv {
  72. const char * str;
  73. int len, idx;
  74. } * setenvs = NULL;
  75. int nsetenvs = 0;
  76. if (!pal_state.root_config)
  77. return;
  78. ssize_t cfgsize_envs = get_config_entries_size(store, "loader.env");
  79. /* XXX Propagate this error? */
  80. if (cfgsize_envs < 0)
  81. return;
  82. char * cfgbuf_envs = malloc(cfgsize_envs);
  83. assert(cfgbuf_envs);
  84. nsetenvs = get_config_entries(store, "loader.env", cfgbuf_envs, cfgsize_envs);
  85. if (nsetenvs <= 0) {
  86. free(cfgbuf_envs);
  87. return;
  88. }
  89. setenvs = __alloca(sizeof(struct setenv) * nsetenvs);
  90. char * cfg = cfgbuf_envs;
  91. for (int i = 0 ; i < nsetenvs ; i++) {
  92. size_t len = strlen(cfg);
  93. char * str = __alloca(len + 1);
  94. setenvs[i].str = str;
  95. setenvs[i].len = len;
  96. setenvs[i].idx = -1;
  97. memcpy(str, cfg, len + 1);
  98. cfg += len + 1;
  99. }
  100. free(cfgbuf_envs);
  101. int nenvs = 0, noverwrite = 0;
  102. for (const char ** e = envp ; *e ; e++, nenvs++)
  103. for (int i = 0 ; i < nsetenvs ; i++)
  104. if (!memcmp(setenvs[i].str, *e, setenvs[i].len) &&
  105. (*e)[setenvs[i].len] == '=') {
  106. setenvs[i].idx = nenvs;
  107. noverwrite++;
  108. break;
  109. }
  110. /* TODO: This code appears to rely on the memory buffer being zero-
  111. * initialized, so we use calloc here to get zeroed memory. We should
  112. * audit this code to verify that it's correct. */
  113. const char ** new_envp =
  114. calloc((nenvs + nsetenvs - noverwrite + 1), sizeof(const char *));
  115. memcpy(new_envp, envp, sizeof(const char *) * nenvs);
  116. envp = new_envp;
  117. char key[CONFIG_MAX] = "loader.env.";
  118. int prefix_len = static_strlen("loader.env.");
  119. const char ** ptr;
  120. char cfgbuf[CONFIG_MAX];
  121. for (int i = 0 ; i < nsetenvs ; i++) {
  122. const char * str = setenvs[i].str;
  123. int len = setenvs[i].len;
  124. int idx = setenvs[i].idx;
  125. ssize_t bytes;
  126. ptr = &envp[(idx == -1) ? nenvs++ : idx];
  127. memcpy(key + prefix_len, str, len + 1);
  128. if ((bytes = get_config(store, key, cfgbuf, sizeof(cfgbuf))) > 0) {
  129. char * e = malloc(len + bytes + 2);
  130. memcpy(e, str, len);
  131. e[len] = '=';
  132. memcpy(e + len + 1, cfgbuf, bytes + 1);
  133. *ptr = e;
  134. } else {
  135. char * e = malloc(len + 2);
  136. memcpy(e, str, len);
  137. e[len] = '=';
  138. e[len + 1] = 0;
  139. *ptr = e;
  140. }
  141. }
  142. *envpp = envp;
  143. }
  144. static void set_debug_type (void)
  145. {
  146. char cfgbuf[CONFIG_MAX];
  147. ssize_t ret = 0;
  148. if (!pal_state.root_config)
  149. return;
  150. ret = get_config(pal_state.root_config, "loader.debug_type",
  151. cfgbuf, sizeof(cfgbuf));
  152. if (ret <= 0)
  153. return;
  154. PAL_HANDLE handle = NULL;
  155. if (!strcmp_static(cfgbuf, "inline")) {
  156. ret = _DkStreamOpen(&handle, URI_PREFIX_DEV "tty", PAL_ACCESS_RDWR, 0, 0, 0);
  157. } else if (!strcmp_static(cfgbuf, "file")) {
  158. ret = get_config(pal_state.root_config, "loader.debug_file",
  159. cfgbuf, sizeof(cfgbuf));
  160. if (ret <= 0)
  161. INIT_FAIL(PAL_ERROR_INVAL, "debug file not specified");
  162. ret = _DkStreamOpen(&handle, cfgbuf,
  163. PAL_ACCESS_RDWR,
  164. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  165. PAL_CREATE_TRY, 0);
  166. } else if (!strcmp_static(cfgbuf, "none")) {
  167. ret = 0;
  168. } else {
  169. INIT_FAIL(PAL_ERROR_INVAL, "unknown debug type");
  170. }
  171. if (ret < 0)
  172. INIT_FAIL(-ret, "cannot open debug stream");
  173. __pal_control.debug_stream = handle;
  174. }
  175. static int loader_filter (const char * key, int len)
  176. {
  177. /* try to do this as fast as possible */
  178. return (len > 7 && key[0] == 'l' && key[1] == 'o' && key[2] == 'a' && key[3] == 'd' &&
  179. key[4] == 'e' && key[5] == 'r' && key[6] == '.') ? 0 : 1;
  180. }
  181. /* 'pal_main' must be called by the host-specific bootloader */
  182. noreturn void pal_main (
  183. PAL_NUM instance_id, /* current instance id */
  184. PAL_HANDLE manifest_handle, /* manifest handle if opened */
  185. PAL_HANDLE exec_handle, /* executable handle if opened */
  186. PAL_PTR exec_loaded_addr, /* executable addr if loaded */
  187. PAL_HANDLE parent_process, /* parent process if it's a child */
  188. PAL_HANDLE first_thread, /* first thread handle */
  189. PAL_STR * arguments, /* application arguments */
  190. PAL_STR * environments /* environment variables */
  191. )
  192. {
  193. #if PROFILING == 1
  194. __pal_control.host_specific_startup_time =
  195. _DkSystemTimeQuery() - pal_state.start_time;
  196. #endif
  197. pal_state.instance_id = instance_id;
  198. pal_state.alloc_align = _DkGetAllocationAlignment();
  199. assert(IS_POWER_OF_2(pal_state.alloc_align));
  200. init_slab_mgr(pal_state.alloc_align);
  201. pal_state.parent_process = parent_process;
  202. char uri_buf[URI_MAX];
  203. char * manifest_uri = NULL, * exec_uri = NULL;
  204. ssize_t ret;
  205. if (exec_handle) {
  206. ret = _DkStreamGetName(exec_handle, uri_buf, URI_MAX);
  207. if (ret < 0)
  208. INIT_FAIL(-ret, "cannot get executable name");
  209. exec_uri = malloc_copy(uri_buf, ret + 1);
  210. }
  211. if (manifest_handle) {
  212. ret = _DkStreamGetName(manifest_handle, uri_buf, URI_MAX);
  213. if (ret < 0)
  214. INIT_FAIL(-ret, "cannot get manifest name");
  215. manifest_uri = malloc_copy(uri_buf, ret + 1);
  216. } else {
  217. if (!exec_handle)
  218. INIT_FAIL(PAL_ERROR_INVAL, "Must have manifest or executable");
  219. #if PROFILING == 1
  220. unsigned long before_find_manifest = _DkSystemTimeQuery();
  221. #endif
  222. /* try open "<execname>.manifest" */
  223. size_t len = sizeof(uri_buf);
  224. ret = get_norm_path(exec_uri, uri_buf, &len);
  225. if (ret < 0) {
  226. INIT_FAIL(-ret, "cannot normalize exec_uri");
  227. }
  228. strcpy_static(uri_buf + len, ".manifest", sizeof(uri_buf) - len);
  229. ret = _DkStreamOpen(&manifest_handle, uri_buf, PAL_ACCESS_RDONLY, 0, 0, 0);
  230. if (ret) {
  231. /* try open "file:manifest" */
  232. manifest_uri = URI_PREFIX_FILE "manifest";
  233. ret = _DkStreamOpen(&manifest_handle, manifest_uri, PAL_ACCESS_RDONLY,
  234. 0, 0, 0);
  235. if (ret) {
  236. #if PROFILING == 1
  237. pal_state.manifest_loading_time +=
  238. _DkSystemTimeQuery() - before_find_manifest;
  239. #endif
  240. /* well, there is no manifest file, leave it alone */
  241. printf("Can't find any manifest, will run without one.\n");
  242. }
  243. }
  244. }
  245. /* load manifest if there is one */
  246. if (!pal_state.root_config && manifest_handle) {
  247. #if PROFILING == 1
  248. unsigned long before_load_manifest = _DkSystemTimeQuery();
  249. #endif
  250. PAL_STREAM_ATTR attr;
  251. ret = _DkStreamAttributesQueryByHandle(manifest_handle, &attr);
  252. if (ret < 0)
  253. INIT_FAIL(-ret, "cannot open manifest file");
  254. void * cfg_addr = NULL;
  255. int cfg_size = attr.pending_size;
  256. ret = _DkStreamMap(manifest_handle, &cfg_addr,
  257. PAL_PROT_READ, 0,
  258. ALLOC_ALIGN_UP(cfg_size));
  259. if (ret < 0)
  260. INIT_FAIL(-ret, "cannot open manifest file");
  261. struct config_store * root_config = malloc(sizeof(struct config_store));
  262. root_config->raw_data = cfg_addr;
  263. root_config->raw_size = cfg_size;
  264. root_config->malloc = malloc;
  265. root_config->free = free;
  266. const char * errstring = NULL;
  267. if ((ret = read_config(root_config, loader_filter, &errstring)) < 0) {
  268. if (_DkStreamGetName(manifest_handle, uri_buf, URI_MAX) > 0)
  269. printf("reading manifest \"%s\" failed\n", uri_buf);
  270. INIT_FAIL(-ret, errstring);
  271. }
  272. pal_state.root_config = root_config;
  273. #if PROFILING == 1
  274. pal_state.manifest_loading_time +=
  275. _DkSystemTimeQuery() - before_load_manifest;
  276. #endif
  277. }
  278. /* if there is no executable, try to find one in the manifest */
  279. if (!exec_handle && pal_state.root_config) {
  280. ret = get_config(pal_state.root_config, "loader.exec",
  281. uri_buf, URI_MAX);
  282. if (ret > 0) {
  283. exec_uri = malloc_copy(uri_buf, ret + 1);
  284. ret = _DkStreamOpen(&exec_handle, exec_uri, PAL_ACCESS_RDONLY,
  285. 0, 0, 0);
  286. if (ret < 0)
  287. INIT_FAIL(-ret, "cannot open executable");
  288. }
  289. }
  290. /* If we still don't have an exec in the manifest, but we have a manifest
  291. * try implicitly from the manifest name */
  292. if ((!exec_handle) && manifest_uri) {
  293. size_t manifest_strlen = strlen(manifest_uri);
  294. size_t exec_strlen = manifest_strlen - 9;
  295. int success = 0;
  296. // Try .manifest
  297. if (!strcmp_static(&manifest_uri[exec_strlen], ".manifest")) {
  298. success = 1;
  299. } else {
  300. exec_strlen -= 4;
  301. if (!strcmp_static(&manifest_uri[exec_strlen], ".manifest.sgx")) {
  302. success = 1;
  303. }
  304. }
  305. if (success) {
  306. exec_uri = malloc(exec_strlen + 1);
  307. if (!exec_uri)
  308. INIT_FAIL(-PAL_ERROR_NOMEM, "Cannot allocate URI buf");
  309. memcpy (exec_uri, manifest_uri, exec_strlen);
  310. exec_uri[exec_strlen] = '\0';
  311. ret = _DkStreamOpen(&exec_handle, exec_uri, PAL_ACCESS_RDONLY,
  312. 0, 0, 0);
  313. // DEP 3/20/17: There are cases where we want to let
  314. // the PAL start up without a main executable. Don't
  315. // die here, just free the exec_uri buffer.
  316. if (ret < 0) {
  317. free(exec_uri);
  318. exec_uri = NULL;
  319. }
  320. }
  321. }
  322. /* must be an ELF */
  323. if (exec_handle) {
  324. if (exec_loaded_addr) {
  325. if (check_elf_magic(exec_loaded_addr, sizeof(ElfW(Ehdr))))
  326. INIT_FAIL(PAL_ERROR_INVAL, "executable is not an ELF binary");
  327. } else {
  328. if (check_elf_object(exec_handle) < 0)
  329. INIT_FAIL(PAL_ERROR_INVAL, "executable is not an ELF binary");
  330. }
  331. }
  332. pal_state.manifest = manifest_uri;
  333. pal_state.manifest_handle = manifest_handle;
  334. pal_state.exec = exec_uri;
  335. pal_state.exec_handle = exec_handle;
  336. if (pal_state.root_config && *arguments
  337. && (strendswith(*arguments, ".manifest") || strendswith(*arguments, ".manifest.sgx"))) {
  338. /* Run as a manifest file,
  339. * replace argv[0] with the contents of the manifest's loader.execname */
  340. char cfgbuf[CONFIG_MAX];
  341. ret = get_config(pal_state.root_config, "loader.execname", cfgbuf, sizeof(cfgbuf));
  342. if (ret > 0)
  343. *arguments = malloc_copy(cfgbuf, ret + 1);
  344. }
  345. read_environments(&environments);
  346. if (pal_state.root_config)
  347. load_libraries();
  348. if (exec_handle) {
  349. #if PROFILING == 1
  350. unsigned long before_load_exec = _DkSystemTimeQuery();
  351. #endif
  352. if (exec_loaded_addr) {
  353. ret = add_elf_object(exec_loaded_addr, exec_handle, OBJECT_EXEC);
  354. } else {
  355. ret = load_elf_object_by_handle(exec_handle, OBJECT_EXEC);
  356. }
  357. if (ret < 0)
  358. INIT_FAIL(ret, pal_strerror(ret));
  359. #if PROFILING == 1
  360. pal_state.linking_time += _DkSystemTimeQuery() - before_load_exec;
  361. #endif
  362. }
  363. #if PROFILING == 1
  364. unsigned long before_tail = _DkSystemTimeQuery();
  365. #endif
  366. set_debug_type();
  367. __pal_control.host_type = XSTRINGIFY(HOST_TYPE);
  368. __pal_control.process_id = _DkGetProcessId();
  369. __pal_control.host_id = _DkGetHostId();
  370. __pal_control.manifest_handle = manifest_handle;
  371. __pal_control.executable = exec_uri;
  372. __pal_control.parent_process = parent_process;
  373. __pal_control.first_thread = first_thread;
  374. _DkGetAvailableUserAddressRange(&__pal_control.user_address.start,
  375. &__pal_control.user_address.end,
  376. &__pal_control.user_address_hole.start,
  377. &__pal_control.user_address_hole.end);
  378. __pal_control.alloc_align = pal_state.alloc_align;
  379. if (_DkGetCPUInfo(&__pal_control.cpu_info) < 0) {
  380. goto out_fail;
  381. }
  382. __pal_control.mem_info.mem_total = _DkMemoryQuota();
  383. #if PROFILING == 1
  384. pal_state.tail_startup_time += _DkSystemTimeQuery() - before_tail;
  385. __pal_control.relocation_time = pal_state.relocation_time;
  386. __pal_control.linking_time = pal_state.linking_time;
  387. __pal_control.manifest_loading_time
  388. = pal_state.manifest_loading_time;
  389. __pal_control.allocation_time = pal_state.slab_time;
  390. __pal_control.child_creation_time = (parent_process == NULL) ? 0 : pal_state.start_time -
  391. pal_state.process_create_time;
  392. #endif
  393. /* Now we will start the execution */
  394. start_execution(arguments, environments);
  395. out_fail:
  396. /* We wish we will never reached here */
  397. INIT_FAIL(PAL_ERROR_DENIED, "unexpected termination");
  398. }
  399. void write_log (int nstrs, ...)
  400. {
  401. const char ** strs = __alloca(sizeof(const char *) * nstrs);
  402. int len = 0;
  403. va_list ap;
  404. va_start(ap, nstrs);
  405. for (int i = 0 ; i < nstrs ; i++) {
  406. strs[i] = va_arg(ap, char *);
  407. len += strlen(strs[i]);
  408. }
  409. va_end(ap);
  410. char * buf = __alloca(len);
  411. int cnt = 0;
  412. for (int i = 0 ; i < nstrs ; i++) {
  413. int l = strlen(strs[i]);
  414. memcpy(buf + cnt, strs[i], l);
  415. cnt += l;
  416. }
  417. _DkStreamWrite(pal_state.log_stream, 0, cnt, buf, NULL, 0);
  418. }