2
0

model.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /* Tainting */
  40. typedef struct {} name2keysym_t;
  41. static int get_keysym(const name2keysym_t *table,
  42. const char *name)
  43. {
  44. int result;
  45. if (result > 0) {
  46. __coverity_tainted_string_sanitize_content__(name);
  47. return result;
  48. } else {
  49. return 0;
  50. }
  51. }
  52. /* Replay data is considered trusted. */
  53. uint8_t replay_get_byte(void)
  54. {
  55. uint8_t byte;
  56. return byte;
  57. }
  58. /*
  59. * GLib memory allocation functions.
  60. *
  61. * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
  62. * and g_realloc of 0 bytes frees the pointer.
  63. *
  64. * Modeling this would result in Coverity flagging a lot of memory
  65. * allocations as potentially returning NULL, and asking us to check
  66. * whether the result of the allocation is NULL or not. However, the
  67. * resulting pointer should never be dereferenced anyway, and in fact
  68. * it is not in the vast majority of cases.
  69. *
  70. * If a dereference did happen, this would suppress a defect report
  71. * for an actual null pointer dereference. But it's too unlikely to
  72. * be worth wading through the false positives, and with some luck
  73. * we'll get a buffer overflow reported anyway.
  74. */
  75. /*
  76. * Allocation primitives, cannot return NULL
  77. * See also Coverity's library/generic/libc/all/all.c
  78. */
  79. void *g_malloc_n(size_t nmemb, size_t size)
  80. {
  81. void *ptr;
  82. __coverity_negative_sink__(nmemb);
  83. __coverity_negative_sink__(size);
  84. ptr = __coverity_alloc__(nmemb * size);
  85. if (!ptr) {
  86. __coverity_panic__();
  87. }
  88. __coverity_mark_as_uninitialized_buffer__(ptr);
  89. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  90. return ptr;
  91. }
  92. void *g_malloc0_n(size_t nmemb, size_t size)
  93. {
  94. void *ptr;
  95. __coverity_negative_sink__(nmemb);
  96. __coverity_negative_sink__(size);
  97. ptr = __coverity_alloc__(nmemb * size);
  98. if (!ptr) {
  99. __coverity_panic__();
  100. }
  101. __coverity_writeall0__(ptr);
  102. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  103. return ptr;
  104. }
  105. void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
  106. {
  107. __coverity_negative_sink__(nmemb);
  108. __coverity_negative_sink__(size);
  109. __coverity_escape__(ptr);
  110. ptr = __coverity_alloc__(nmemb * size);
  111. if (!ptr) {
  112. __coverity_panic__();
  113. }
  114. /*
  115. * Memory beyond the old size isn't actually initialized. Can't
  116. * model that. See Coverity's realloc() model
  117. */
  118. __coverity_writeall__(ptr);
  119. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  120. return ptr;
  121. }
  122. void g_free(void *ptr)
  123. {
  124. __coverity_free__(ptr);
  125. __coverity_mark_as_afm_freed__(ptr, AFM_free);
  126. }
  127. /*
  128. * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
  129. * out of memory conditions
  130. */
  131. void *g_try_malloc_n(size_t nmemb, size_t size)
  132. {
  133. int nomem;
  134. if (nomem) {
  135. return NULL;
  136. }
  137. return g_malloc_n(nmemb, size);
  138. }
  139. void *g_try_malloc0_n(size_t nmemb, size_t size)
  140. {
  141. int nomem;
  142. if (nomem) {
  143. return NULL;
  144. }
  145. return g_malloc0_n(nmemb, size);
  146. }
  147. void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
  148. {
  149. int nomem;
  150. if (nomem) {
  151. return NULL;
  152. }
  153. return g_realloc_n(ptr, nmemb, size);
  154. }
  155. /* Derive the g_FOO() from the g_FOO_n() */
  156. void *g_malloc(size_t size)
  157. {
  158. void *ptr;
  159. __coverity_negative_sink__(size);
  160. ptr = __coverity_alloc__(size);
  161. if (!ptr) {
  162. __coverity_panic__();
  163. }
  164. __coverity_mark_as_uninitialized_buffer__(ptr);
  165. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  166. return ptr;
  167. }
  168. void *g_malloc0(size_t size)
  169. {
  170. void *ptr;
  171. __coverity_negative_sink__(size);
  172. ptr = __coverity_alloc__(size);
  173. if (!ptr) {
  174. __coverity_panic__();
  175. }
  176. __coverity_writeall0__(ptr);
  177. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  178. return ptr;
  179. }
  180. void *g_realloc(void *ptr, size_t size)
  181. {
  182. __coverity_negative_sink__(size);
  183. __coverity_escape__(ptr);
  184. ptr = __coverity_alloc__(size);
  185. if (!ptr) {
  186. __coverity_panic__();
  187. }
  188. /*
  189. * Memory beyond the old size isn't actually initialized. Can't
  190. * model that. See Coverity's realloc() model
  191. */
  192. __coverity_writeall__(ptr);
  193. __coverity_mark_as_afm_allocated__(ptr, AFM_free);
  194. return ptr;
  195. }
  196. void *g_try_malloc(size_t size)
  197. {
  198. int nomem;
  199. if (nomem) {
  200. return NULL;
  201. }
  202. return g_malloc(size);
  203. }
  204. void *g_try_malloc0(size_t size)
  205. {
  206. int nomem;
  207. if (nomem) {
  208. return NULL;
  209. }
  210. return g_malloc0(size);
  211. }
  212. void *g_try_realloc(void *ptr, size_t size)
  213. {
  214. int nomem;
  215. if (nomem) {
  216. return NULL;
  217. }
  218. return g_realloc(ptr, size);
  219. }
  220. /* Other glib functions */
  221. typedef struct pollfd GPollFD;
  222. int poll();
  223. int g_poll (GPollFD *fds, unsigned nfds, int timeout)
  224. {
  225. return poll(fds, nfds, timeout);
  226. }
  227. typedef struct _GIOChannel GIOChannel;
  228. GIOChannel *g_io_channel_unix_new(int fd)
  229. {
  230. /* cannot use incomplete type, the actual struct is roughly this size. */
  231. GIOChannel *c = g_malloc0(20 * sizeof(void *));
  232. __coverity_escape__(fd);
  233. return c;
  234. }
  235. void g_assertion_message_expr(const char *domain,
  236. const char *file,
  237. int line,
  238. const char *func,
  239. const char *expr)
  240. {
  241. __coverity_panic__();
  242. }