2
0

coverity-model.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* exec.c */
  39. typedef struct AddressSpace AddressSpace;
  40. typedef uint64_t hwaddr;
  41. static void __write(uint8_t *buf, ssize_t len)
  42. {
  43. int first, last;
  44. __coverity_negative_sink__(len);
  45. if (len == 0) return;
  46. buf[0] = first;
  47. buf[len-1] = last;
  48. __coverity_writeall__(buf);
  49. }
  50. static void __read(uint8_t *buf, ssize_t len)
  51. {
  52. __coverity_negative_sink__(len);
  53. if (len == 0) return;
  54. int first = buf[0];
  55. int last = buf[len-1];
  56. }
  57. bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
  58. int len, bool is_write)
  59. {
  60. bool result;
  61. // TODO: investigate impact of treating reads as producing
  62. // tainted data, with __coverity_tainted_data_argument__(buf).
  63. if (is_write) __write(buf, len); else __read(buf, len);
  64. return result;
  65. }
  66. /* Tainting */
  67. typedef struct {} name2keysym_t;
  68. static int get_keysym(const name2keysym_t *table,
  69. const char *name)
  70. {
  71. int result;
  72. if (result > 0) {
  73. __coverity_tainted_string_sanitize_content__(name);
  74. return result;
  75. } else {
  76. return 0;
  77. }
  78. }
  79. /* glib memory allocation functions.
  80. *
  81. * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
  82. * and g_realloc of 0 bytes frees the pointer.
  83. *
  84. * Modeling this would result in Coverity flagging a lot of memory
  85. * allocations as potentially returning NULL, and asking us to check
  86. * whether the result of the allocation is NULL or not. However, the
  87. * resulting pointer should never be dereferenced anyway, and in fact
  88. * it is not in the vast majority of cases.
  89. *
  90. * If a dereference did happen, this would suppress a defect report
  91. * for an actual null pointer dereference. But it's too unlikely to
  92. * be worth wading through the false positives, and with some luck
  93. * we'll get a buffer overflow reported anyway.
  94. */
  95. void *malloc(size_t);
  96. void *calloc(size_t, size_t);
  97. void *realloc(void *, size_t);
  98. void free(void *);
  99. void *
  100. g_malloc(size_t n_bytes)
  101. {
  102. void *mem;
  103. __coverity_negative_sink__(n_bytes);
  104. mem = malloc(n_bytes == 0 ? 1 : n_bytes);
  105. if (!mem) __coverity_panic__();
  106. return mem;
  107. }
  108. void *
  109. g_malloc0(size_t n_bytes)
  110. {
  111. void *mem;
  112. __coverity_negative_sink__(n_bytes);
  113. mem = calloc(1, n_bytes == 0 ? 1 : n_bytes);
  114. if (!mem) __coverity_panic__();
  115. return mem;
  116. }
  117. void g_free(void *mem)
  118. {
  119. free(mem);
  120. }
  121. void *g_realloc(void * mem, size_t n_bytes)
  122. {
  123. __coverity_negative_sink__(n_bytes);
  124. mem = realloc(mem, n_bytes == 0 ? 1 : n_bytes);
  125. if (!mem) __coverity_panic__();
  126. return mem;
  127. }
  128. void *g_try_malloc(size_t n_bytes)
  129. {
  130. __coverity_negative_sink__(n_bytes);
  131. return malloc(n_bytes == 0 ? 1 : n_bytes);
  132. }
  133. void *g_try_malloc0(size_t n_bytes)
  134. {
  135. __coverity_negative_sink__(n_bytes);
  136. return calloc(1, n_bytes == 0 ? 1 : n_bytes);
  137. }
  138. void *g_try_realloc(void *mem, size_t n_bytes)
  139. {
  140. __coverity_negative_sink__(n_bytes);
  141. return realloc(mem, n_bytes == 0 ? 1 : n_bytes);
  142. }
  143. /* Other glib functions */
  144. typedef struct _GIOChannel GIOChannel;
  145. GIOChannel *g_io_channel_unix_new(int fd)
  146. {
  147. GIOChannel *c = g_malloc0(sizeof(GIOChannel));
  148. __coverity_escape__(fd);
  149. return c;
  150. }
  151. void g_assertion_message_expr(const char *domain,
  152. const char *file,
  153. int line,
  154. const char *func,
  155. const char *expr)
  156. {
  157. __coverity_panic__();
  158. }