model.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* Coverity Scan model
  2. *
  3. * Copyright (C) 2014 Red Hat, Inc.
  4. *
  5. * Authors:
  6. * Markus Armbruster <armbru@redhat.com>
  7. * Paolo Bonzini <pbonzini@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or, at your
  10. * option, any later version. See the COPYING file in the top-level directory.
  11. */
  12. /*
  13. * This is the source code for our Coverity user model file. The
  14. * purpose of user models is to increase scanning accuracy by explaining
  15. * code Coverity can't see (out of tree libraries) or doesn't
  16. * sufficiently understand. Better accuracy means both fewer false
  17. * positives and more true defects. Memory leaks in particular.
  18. *
  19. * - A model file can't import any header files. Some built-in primitives are
  20. * available but not wchar_t, NULL etc.
  21. * - Modeling doesn't need full structs and typedefs. Rudimentary structs
  22. * and similar types are sufficient.
  23. * - An uninitialized local variable signifies that the variable could be
  24. * any value.
  25. *
  26. * The model file must be uploaded by an admin in the analysis settings of
  27. * http://scan.coverity.com/projects/378
  28. */
  29. #define NULL ((void *)0)
  30. typedef unsigned char uint8_t;
  31. typedef char int8_t;
  32. typedef unsigned int uint32_t;
  33. typedef int int32_t;
  34. typedef long ssize_t;
  35. typedef unsigned long long uint64_t;
  36. typedef long long int64_t;
  37. typedef _Bool bool;
  38. typedef struct va_list_str *va_list;
  39. /* exec.c */
  40. typedef struct AddressSpace AddressSpace;
  41. typedef struct MemoryRegionCache MemoryRegionCache;
  42. typedef uint64_t hwaddr;
  43. typedef uint32_t MemTxResult;
  44. typedef struct MemTxAttrs {} MemTxAttrs;
  45. static void __bufwrite(uint8_t *buf, ssize_t len)
  46. {
  47. int first, last;
  48. __coverity_negative_sink__(len);
  49. if (len == 0) return;
  50. buf[0] = first;
  51. buf[len-1] = last;
  52. __coverity_writeall__(buf);
  53. }
  54. static void __bufread(uint8_t *buf, ssize_t len)
  55. {
  56. __coverity_negative_sink__(len);
  57. if (len == 0) return;
  58. int first = buf[0];
  59. int last = buf[len-1];
  60. }
  61. MemTxResult address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
  62. MemTxAttrs attrs,
  63. void *buf, int len)
  64. {
  65. MemTxResult result;
  66. // TODO: investigate impact of treating reads as producing
  67. // tainted data, with __coverity_tainted_data_argument__(buf).
  68. __bufwrite(buf, len);
  69. return result;
  70. }
  71. MemTxResult address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
  72. MemTxAttrs attrs,
  73. const void *buf, int len)
  74. {
  75. MemTxResult result;
  76. __bufread(buf, len);
  77. return result;
  78. }
  79. MemTxResult address_space_rw_cached(MemoryRegionCache *cache, hwaddr addr,
  80. MemTxAttrs attrs,
  81. void *buf, int len, bool is_write)
  82. {
  83. if (is_write) {
  84. return address_space_write_cached(cache, addr, attrs, buf, len);
  85. } else {
  86. return address_space_read_cached(cache, addr, attrs, buf, len);
  87. }
  88. }
  89. MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
  90. MemTxAttrs attrs,
  91. void *buf, int len)
  92. {
  93. MemTxResult result;
  94. // TODO: investigate impact of treating reads as producing
  95. // tainted data, with __coverity_tainted_data_argument__(buf).
  96. __bufwrite(buf, len);
  97. return result;
  98. }
  99. MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
  100. MemTxAttrs attrs,
  101. const void *buf, int len)
  102. {
  103. MemTxResult result;
  104. __bufread(buf, len);
  105. return result;
  106. }
  107. MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
  108. MemTxAttrs attrs,
  109. void *buf, int len, bool is_write)
  110. {
  111. if (is_write) {
  112. return address_space_write(as, addr, attrs, buf, len);
  113. } else {
  114. return address_space_read(as, addr, attrs, buf, len);
  115. }
  116. }
  117. /* Tainting */
  118. typedef struct {} name2keysym_t;
  119. static int get_keysym(const name2keysym_t *table,
  120. const char *name)
  121. {
  122. int result;
  123. if (result > 0) {
  124. __coverity_tainted_string_sanitize_content__(name);
  125. return result;
  126. } else {
  127. return 0;
  128. }
  129. }
  130. /* Replay data is considered trusted. */
  131. uint8_t replay_get_byte(void)
  132. {
  133. uint8_t byte;
  134. return byte;
  135. }
  136. /*
  137. * GLib memory allocation functions.
  138. *
  139. * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
  140. * and g_realloc of 0 bytes frees the pointer.
  141. *
  142. * Modeling this would result in Coverity flagging a lot of memory
  143. * allocations as potentially returning NULL, and asking us to check
  144. * whether the result of the allocation is NULL or not. However, the
  145. * resulting pointer should never be dereferenced anyway, and in fact
  146. * it is not in the vast majority of cases.
  147. *
  148. * If a dereference did happen, this would suppress a defect report
  149. * for an actual null pointer dereference. But it's too unlikely to
  150. * be worth wading through the false positives, and with some luck
  151. * we'll get a buffer overflow reported anyway.
  152. */
  153. /*
  154. * Allocation primitives, cannot return NULL
  155. * See also Coverity's library/generic/libc/all/all.c
  156. */
  157. void *g_malloc_n(size_t nmemb, size_t size)
  158. {
  159. void *ptr;
  160. __coverity_negative_sink__(nmemb);
  161. __coverity_negative_sink__(size);
  162. ptr = __coverity_alloc__(nmemb * size);
  163. if (!ptr) {
  164. __coverity_panic__();
  165. }
  166. __coverity_mark_as_uninitialized_buffer__(ptr);
  167. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  168. return ptr;
  169. }
  170. void *g_malloc0_n(size_t nmemb, size_t size)
  171. {
  172. void *ptr;
  173. __coverity_negative_sink__(nmemb);
  174. __coverity_negative_sink__(size);
  175. ptr = __coverity_alloc__(nmemb * size);
  176. if (!ptr) {
  177. __coverity_panic__();
  178. }
  179. __coverity_writeall0__(ptr);
  180. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  181. return ptr;
  182. }
  183. void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
  184. {
  185. __coverity_negative_sink__(nmemb);
  186. __coverity_negative_sink__(size);
  187. __coverity_escape__(ptr);
  188. ptr = __coverity_alloc__(nmemb * size);
  189. if (!ptr) {
  190. __coverity_panic__();
  191. }
  192. /*
  193. * Memory beyond the old size isn't actually initialized. Can't
  194. * model that. See Coverity's realloc() model
  195. */
  196. __coverity_writeall__(ptr);
  197. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  198. return ptr;
  199. }
  200. void g_free(void *ptr)
  201. {
  202. __coverity_free__(ptr);
  203. __coverity_mark_as_afm_freed__(ptr, AFM_free);
  204. }
  205. /*
  206. * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
  207. * out of memory conditions
  208. */
  209. void *g_try_malloc_n(size_t nmemb, size_t size)
  210. {
  211. int nomem;
  212. if (nomem) {
  213. return NULL;
  214. }
  215. return g_malloc_n(nmemb, size);
  216. }
  217. void *g_try_malloc0_n(size_t nmemb, size_t size)
  218. {
  219. int nomem;
  220. if (nomem) {
  221. return NULL;
  222. }
  223. return g_malloc0_n(nmemb, size);
  224. }
  225. void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
  226. {
  227. int nomem;
  228. if (nomem) {
  229. return NULL;
  230. }
  231. return g_realloc_n(ptr, nmemb, size);
  232. }
  233. /* Derive the g_FOO() from the g_FOO_n() */
  234. void *g_malloc(size_t size)
  235. {
  236. void *ptr;
  237. __coverity_negative_sink__(size);
  238. ptr = __coverity_alloc__(size);
  239. if (!ptr) {
  240. __coverity_panic__();
  241. }
  242. __coverity_mark_as_uninitialized_buffer__(ptr);
  243. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  244. return ptr;
  245. }
  246. void *g_malloc0(size_t size)
  247. {
  248. void *ptr;
  249. __coverity_negative_sink__(size);
  250. ptr = __coverity_alloc__(size);
  251. if (!ptr) {
  252. __coverity_panic__();
  253. }
  254. __coverity_writeall0__(ptr);
  255. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  256. return ptr;
  257. }
  258. void *g_realloc(void *ptr, size_t size)
  259. {
  260. __coverity_negative_sink__(size);
  261. __coverity_escape__(ptr);
  262. ptr = __coverity_alloc__(size);
  263. if (!ptr) {
  264. __coverity_panic__();
  265. }
  266. /*
  267. * Memory beyond the old size isn't actually initialized. Can't
  268. * model that. See Coverity's realloc() model
  269. */
  270. __coverity_writeall__(ptr);
  271. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  272. return ptr;
  273. }
  274. void *g_try_malloc(size_t size)
  275. {
  276. int nomem;
  277. if (nomem) {
  278. return NULL;
  279. }
  280. return g_malloc(size);
  281. }
  282. void *g_try_malloc0(size_t size)
  283. {
  284. int nomem;
  285. if (nomem) {
  286. return NULL;
  287. }
  288. return g_malloc0(size);
  289. }
  290. void *g_try_realloc(void *ptr, size_t size)
  291. {
  292. int nomem;
  293. if (nomem) {
  294. return NULL;
  295. }
  296. return g_realloc(ptr, size);
  297. }
  298. /* Other glib functions */
  299. typedef struct pollfd GPollFD;
  300. int poll();
  301. int g_poll (GPollFD *fds, unsigned nfds, int timeout)
  302. {
  303. return poll(fds, nfds, timeout);
  304. }
  305. typedef struct _GIOChannel GIOChannel;
  306. GIOChannel *g_io_channel_unix_new(int fd)
  307. {
  308. /* cannot use incomplete type, the actual struct is roughly this size. */
  309. GIOChannel *c = g_malloc0(20 * sizeof(void *));
  310. __coverity_escape__(fd);
  311. return c;
  312. }
  313. void g_assertion_message_expr(const char *domain,
  314. const char *file,
  315. int line,
  316. const char *func,
  317. const char *expr)
  318. {
  319. __coverity_panic__();
  320. }