shim_utils.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_utils.h
  17. */
  18. #ifndef _SHIM_UTILITIES_H_
  19. #define _SHIM_UTILITIES_H_
  20. #include <shim_handle.h>
  21. #include <pal.h>
  22. #include <linux_list.h>
  23. #include <api.h>
  24. struct shim_handle;
  25. void sysparser_printf (const char * fmt, ...);
  26. int snprintf (char * buf, size_t n, const char * fmt, ...);
  27. /* string object */
  28. struct shim_str * get_str_obj (void);
  29. int free_str_obj (struct shim_str * str);
  30. int init_str_mgr (void);
  31. /* qstring object */
  32. #define QSTR_INIT { .len = 0, .oflow = NULL }
  33. static inline const char * qstrgetstr (const struct shim_qstr * qstr)
  34. {
  35. return qstr->oflow ? qstr->oflow->str : qstr->name;
  36. }
  37. static inline void qstrfree (struct shim_qstr * qstr)
  38. {
  39. if (qstr->oflow) {
  40. free_str_obj(qstr->oflow);
  41. qstr->oflow = NULL;
  42. }
  43. qstr->name[0] = 0;
  44. qstr->len = 0;
  45. }
  46. static inline char * qstrsetstr (struct shim_qstr * qstr,
  47. const char * str, size_t size)
  48. {
  49. if (!str) {
  50. qstrfree(qstr);
  51. return NULL;
  52. }
  53. if (size >= STR_SIZE)
  54. return NULL;
  55. char * buf = qstr->name;
  56. if (size >= QSTR_SIZE) {
  57. if (!qstr->oflow)
  58. qstr->oflow = get_str_obj();
  59. buf = qstr->oflow->str;
  60. } else {
  61. if (qstr->oflow) {
  62. free_str_obj(qstr->oflow);
  63. qstr->oflow = NULL;
  64. }
  65. }
  66. qstr->len = 0;
  67. if (str) {
  68. if (size)
  69. memcpy(buf, str, size);
  70. buf[size] = 0;
  71. qstr->len = size;
  72. }
  73. return buf;
  74. }
  75. static inline char * qstrsetstrs (struct shim_qstr * qstr,
  76. int nstrs,
  77. const char ** strs, size_t * sizes)
  78. {
  79. size_t total_size = 0;
  80. for (int i = 0 ; i < nstrs ; i++)
  81. total_size += sizes[i];
  82. if (total_size >= STR_SIZE)
  83. return NULL;
  84. char * buf = qstr->name;
  85. if (total_size >= QSTR_SIZE) {
  86. if (!qstr->oflow)
  87. qstr->oflow = get_str_obj();
  88. buf = qstr->oflow->str;
  89. }
  90. char * ptr = buf;
  91. qstr->len = 0;
  92. for (int i = 0 ; i < nstrs ; i++) {
  93. int size = sizes[i];
  94. memcpy(ptr, strs[i], size);
  95. ptr[size] = 0;
  96. qstr->len += size;
  97. ptr += size;
  98. }
  99. return buf;
  100. }
  101. static inline int qstrempty (const struct shim_qstr * qstr)
  102. {
  103. return qstr->len == 0;
  104. }
  105. static inline void qstrcopy (struct shim_qstr * to,
  106. const struct shim_qstr * from)
  107. {
  108. qstrsetstr(to, qstrgetstr(from), from->len);
  109. to->hash = from->hash;
  110. }
  111. static inline int qstrcmpstr (const struct shim_qstr * qstr,
  112. const char * str, size_t size)
  113. {
  114. if (qstr->len != size)
  115. return 1;
  116. return memcmp(qstrgetstr(qstr), str, size);
  117. }
  118. //#define SLAB_DEBUG_PRINT
  119. //#define SLAB_DEBUG_TRACE
  120. /* heap allocation functions */
  121. int init_slab (void);
  122. #if defined(SLAB_DEBUG_PRINT) || defined(SLAB_DEBUG_TRACE)
  123. void * __malloc_debug (size_t size, const char * file, int line);
  124. #define malloc(size) __malloc_debug((size), __FILE__, __LINE__)
  125. void __free_debug (void * mem, const char * file, int line);
  126. #define free(mem) __free_debug((mem), __FILE__, __LINE__)
  127. void * __remalloc_debug (const void * mem, size_t size,
  128. const char * file, int line);
  129. #define remalloc(mem, size) __remalloc_debug((mem), (size), __FILE__, __LINE__)
  130. #else
  131. void * malloc (size_t size);
  132. void free (void * mem);
  133. void * remalloc (const void * mem, size_t size);
  134. #endif
  135. static inline
  136. __attribute__((always_inline))
  137. char * qstrtostr (struct shim_qstr * qstr, bool on_stack)
  138. {
  139. int len = qstr->len;
  140. char * buf = on_stack ? __alloca(len + 1) : malloc(len + 1);
  141. if (!buf)
  142. return NULL;
  143. if (len)
  144. memcpy(buf, qstrgetstr(qstr), len);
  145. buf[len] = 0;
  146. return buf;
  147. }
  148. /* typedef a 32 bit type */
  149. # ifndef UINT4
  150. # define UINT4 uint32_t
  151. # endif
  152. /* Data structure for MD5 (Message Digest) computation */
  153. struct shim_md5_ctx {
  154. UINT4 i[2]; /* number of _bits_ handled mod 2^64 */
  155. UINT4 buf[4]; /* scratch buffer */
  156. unsigned char in[64]; /* input buffer */
  157. unsigned char digest[16]; /* actual digest after MD5Final call */
  158. };
  159. void md5_init (struct shim_md5_ctx * mdContext);
  160. void md5_update (struct shim_md5_ctx * mdContext, const void * buf,
  161. size_t len);
  162. void md5_final (struct shim_md5_ctx * mdContext);
  163. /* prompt user for confirmation */
  164. int message_confirm (const char * message, const char * options);
  165. /* get random number */
  166. int getrand (void * buffer, size_t size);
  167. /* ELF binary loading */
  168. int check_elf_object (struct shim_handle ** file);
  169. int load_elf_object (struct shim_handle * file, void * addr, size_t mapped);
  170. int load_elf_interp (struct shim_handle * exec);
  171. int free_elf_interp (void);
  172. int execute_elf_object (struct shim_handle * exec, int argc, const char ** argp,
  173. int nauxv, elf_auxv_t * auxp);
  174. int remove_loaded_libraries (void);
  175. /* gdb debugging support */
  176. void remove_r_debug (void * addr);
  177. void append_r_debug (const char * uri, void * addr, void * dyn_addr);
  178. void clean_link_map_list (void);
  179. /* create unique files/pipes */
  180. #define PIPE_URI_SIZE 40
  181. int create_pipe (IDTYPE * pipeid, char * uri, size_t size, PAL_HANDLE * hdl,
  182. struct shim_qstr * qstr);
  183. int create_dir (const char * prefix, char * path, size_t size,
  184. struct shim_handle ** hdl);
  185. int create_file (const char * prefix, char * path, size_t size,
  186. struct shim_handle ** hdl);
  187. int create_handle (const char * prefix, char * path, size_t size,
  188. PAL_HANDLE * hdl, unsigned int * id);
  189. /* Asynchronous event support */
  190. int init_async (void);
  191. int install_async_event (unsigned long time,
  192. void (*callback) (IDTYPE caller, void * arg),
  193. void * arg);
  194. int create_async_helper (void);
  195. int terminate_async_helper (void);
  196. extern struct config_store * root_config;
  197. #endif /* _SHIM_UTILITIES_H */