coverity-model.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 uint64_t hwaddr;
  42. typedef uint32_t MemTxResult;
  43. typedef uint64_t MemTxAttrs;
  44. static void __bufwrite(uint8_t *buf, ssize_t len)
  45. {
  46. int first, last;
  47. __coverity_negative_sink__(len);
  48. if (len == 0) return;
  49. buf[0] = first;
  50. buf[len-1] = last;
  51. __coverity_writeall__(buf);
  52. }
  53. static void __bufread(uint8_t *buf, ssize_t len)
  54. {
  55. __coverity_negative_sink__(len);
  56. if (len == 0) return;
  57. int first = buf[0];
  58. int last = buf[len-1];
  59. }
  60. MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
  61. MemTxAttrs attrs,
  62. uint8_t *buf, int len)
  63. {
  64. MemTxResult result;
  65. // TODO: investigate impact of treating reads as producing
  66. // tainted data, with __coverity_tainted_data_argument__(buf).
  67. __bufwrite(buf, len);
  68. return result;
  69. }
  70. MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
  71. MemTxAttrs attrs,
  72. const uint8_t *buf, int len)
  73. {
  74. MemTxResult result;
  75. __bufread(buf, len);
  76. return result;
  77. }
  78. /* Tainting */
  79. typedef struct {} name2keysym_t;
  80. static int get_keysym(const name2keysym_t *table,
  81. const char *name)
  82. {
  83. int result;
  84. if (result > 0) {
  85. __coverity_tainted_string_sanitize_content__(name);
  86. return result;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. /* Replay data is considered trusted. */
  92. uint8_t replay_get_byte(void)
  93. {
  94. uint8_t byte;
  95. return byte;
  96. }
  97. /*
  98. * GLib memory allocation functions.
  99. *
  100. * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
  101. * and g_realloc of 0 bytes frees the pointer.
  102. *
  103. * Modeling this would result in Coverity flagging a lot of memory
  104. * allocations as potentially returning NULL, and asking us to check
  105. * whether the result of the allocation is NULL or not. However, the
  106. * resulting pointer should never be dereferenced anyway, and in fact
  107. * it is not in the vast majority of cases.
  108. *
  109. * If a dereference did happen, this would suppress a defect report
  110. * for an actual null pointer dereference. But it's too unlikely to
  111. * be worth wading through the false positives, and with some luck
  112. * we'll get a buffer overflow reported anyway.
  113. */
  114. /*
  115. * Allocation primitives, cannot return NULL
  116. * See also Coverity's library/generic/libc/all/all.c
  117. */
  118. void *g_malloc_n(size_t nmemb, size_t size)
  119. {
  120. size_t sz;
  121. void *ptr;
  122. __coverity_negative_sink__(nmemb);
  123. __coverity_negative_sink__(size);
  124. sz = nmemb * size;
  125. ptr = __coverity_alloc__(sz);
  126. __coverity_mark_as_uninitialized_buffer__(ptr);
  127. __coverity_mark_as_afm_allocated__(ptr, "g_free");
  128. return ptr;
  129. }
  130. void *g_malloc0_n(size_t nmemb, size_t size)
  131. {
  132. size_t sz;
  133. void *ptr;
  134. __coverity_negative_sink__(nmemb);
  135. __coverity_negative_sink__(size);
  136. sz = nmemb * size;
  137. ptr = __coverity_alloc__(sz);
  138. __coverity_writeall0__(ptr);
  139. __coverity_mark_as_afm_allocated__(ptr, "g_free");
  140. return ptr;
  141. }
  142. void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
  143. {
  144. size_t sz;
  145. __coverity_negative_sink__(nmemb);
  146. __coverity_negative_sink__(size);
  147. sz = nmemb * size;
  148. __coverity_escape__(ptr);
  149. ptr = __coverity_alloc__(sz);
  150. /*
  151. * Memory beyond the old size isn't actually initialized. Can't
  152. * model that. See Coverity's realloc() model
  153. */
  154. __coverity_writeall__(ptr);
  155. __coverity_mark_as_afm_allocated__(ptr, "g_free");
  156. return ptr;
  157. }
  158. void g_free(void *ptr)
  159. {
  160. __coverity_free__(ptr);
  161. __coverity_mark_as_afm_freed__(ptr, "g_free");
  162. }
  163. /*
  164. * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
  165. * out of memory conditions
  166. */
  167. void *g_try_malloc_n(size_t nmemb, size_t size)
  168. {
  169. int nomem;
  170. if (nomem) {
  171. return NULL;
  172. }
  173. return g_malloc_n(nmemb, size);
  174. }
  175. void *g_try_malloc0_n(size_t nmemb, size_t size)
  176. {
  177. int nomem;
  178. if (nomem) {
  179. return NULL;
  180. }
  181. return g_malloc0_n(nmemb, size);
  182. }
  183. void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
  184. {
  185. int nomem;
  186. if (nomem) {
  187. return NULL;
  188. }
  189. return g_realloc_n(ptr, nmemb, size);
  190. }
  191. /* Trivially derive the g_FOO() from the g_FOO_n() */
  192. void *g_malloc(size_t size)
  193. {
  194. return g_malloc_n(1, size);
  195. }
  196. void *g_malloc0(size_t size)
  197. {
  198. return g_malloc0_n(1, size);
  199. }
  200. void *g_realloc(void *ptr, size_t size)
  201. {
  202. return g_realloc_n(ptr, 1, size);
  203. }
  204. void *g_try_malloc(size_t size)
  205. {
  206. return g_try_malloc_n(1, size);
  207. }
  208. void *g_try_malloc0(size_t size)
  209. {
  210. return g_try_malloc0_n(1, size);
  211. }
  212. void *g_try_realloc(void *ptr, size_t size)
  213. {
  214. return g_try_realloc_n(ptr, 1, size);
  215. }
  216. /* Other memory allocation functions */
  217. void *g_memdup(const void *ptr, unsigned size)
  218. {
  219. unsigned char *dup;
  220. unsigned i;
  221. if (!ptr) {
  222. return NULL;
  223. }
  224. dup = g_malloc(size);
  225. for (i = 0; i < size; i++)
  226. dup[i] = ((unsigned char *)ptr)[i];
  227. return dup;
  228. }
  229. /*
  230. * GLib string allocation functions
  231. */
  232. char *g_strdup(const char *s)
  233. {
  234. char *dup;
  235. size_t i;
  236. if (!s) {
  237. return NULL;
  238. }
  239. __coverity_string_null_sink__(s);
  240. __coverity_string_size_sink__(s);
  241. dup = __coverity_alloc_nosize__();
  242. __coverity_mark_as_afm_allocated__(dup, "g_free");
  243. for (i = 0; (dup[i] = s[i]); i++) ;
  244. return dup;
  245. }
  246. char *g_strndup(const char *s, size_t n)
  247. {
  248. char *dup;
  249. size_t i;
  250. __coverity_negative_sink__(n);
  251. if (!s) {
  252. return NULL;
  253. }
  254. dup = g_malloc(n + 1);
  255. for (i = 0; i < n && (dup[i] = s[i]); i++) ;
  256. dup[i] = 0;
  257. return dup;
  258. }
  259. char *g_strdup_printf(const char *format, ...)
  260. {
  261. char ch, *s;
  262. size_t len;
  263. __coverity_string_null_sink__(format);
  264. __coverity_string_size_sink__(format);
  265. ch = *format;
  266. s = __coverity_alloc_nosize__();
  267. __coverity_writeall__(s);
  268. __coverity_mark_as_afm_allocated__(s, "g_free");
  269. return s;
  270. }
  271. char *g_strdup_vprintf(const char *format, va_list ap)
  272. {
  273. char ch, *s;
  274. size_t len;
  275. __coverity_string_null_sink__(format);
  276. __coverity_string_size_sink__(format);
  277. ch = *format;
  278. ch = *(char *)ap;
  279. s = __coverity_alloc_nosize__();
  280. __coverity_writeall__(s);
  281. __coverity_mark_as_afm_allocated__(s, "g_free");
  282. return len;
  283. }
  284. char *g_strconcat(const char *s, ...)
  285. {
  286. char *s;
  287. /*
  288. * Can't model: last argument must be null, the others
  289. * null-terminated strings
  290. */
  291. s = __coverity_alloc_nosize__();
  292. __coverity_writeall__(s);
  293. __coverity_mark_as_afm_allocated__(s, "g_free");
  294. return s;
  295. }
  296. /* Other glib functions */
  297. typedef struct pollfd GPollFD;
  298. int poll();
  299. int g_poll (GPollFD *fds, unsigned nfds, int timeout)
  300. {
  301. return poll(fds, nfds, timeout);
  302. }
  303. typedef struct _GIOChannel GIOChannel;
  304. GIOChannel *g_io_channel_unix_new(int fd)
  305. {
  306. GIOChannel *c = g_malloc0(sizeof(GIOChannel));
  307. __coverity_escape__(fd);
  308. return c;
  309. }
  310. void g_assertion_message_expr(const char *domain,
  311. const char *file,
  312. int line,
  313. const char *func,
  314. const char *expr)
  315. {
  316. __coverity_panic__();
  317. }