coverity-model.c 8.4 KB

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