ui: add trace events related to VNC client throttling
[qemu/ar7.git] / scripts / coverity-model.c
blobc702804f41c17c1cf5cf6e09aa1ec8516ebfed52
1 /* Coverity Scan model
3 * Copyright (C) 2014 Red Hat, Inc.
5 * Authors:
6 * Markus Armbruster <armbru@redhat.com>
7 * Paolo Bonzini <pbonzini@redhat.com>
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.
15 * This is the source code for our Coverity user model file. The
16 * purpose of user models is to increase scanning accuracy by explaining
17 * code Coverity can't see (out of tree libraries) or doesn't
18 * sufficiently understand. Better accuracy means both fewer false
19 * positives and more true defects. Memory leaks in particular.
21 * - A model file can't import any header files. Some built-in primitives are
22 * available but not wchar_t, NULL etc.
23 * - Modeling doesn't need full structs and typedefs. Rudimentary structs
24 * and similar types are sufficient.
25 * - An uninitialized local variable signifies that the variable could be
26 * any value.
28 * The model file must be uploaded by an admin in the analysis settings of
29 * http://scan.coverity.com/projects/378
32 #define NULL ((void *)0)
34 typedef unsigned char uint8_t;
35 typedef char int8_t;
36 typedef unsigned int uint32_t;
37 typedef int int32_t;
38 typedef long ssize_t;
39 typedef unsigned long long uint64_t;
40 typedef long long int64_t;
41 typedef _Bool bool;
43 typedef struct va_list_str *va_list;
45 /* exec.c */
47 typedef struct AddressSpace AddressSpace;
48 typedef uint64_t hwaddr;
49 typedef uint32_t MemTxResult;
50 typedef uint64_t MemTxAttrs;
52 static void __bufwrite(uint8_t *buf, ssize_t len)
54 int first, last;
55 __coverity_negative_sink__(len);
56 if (len == 0) return;
57 buf[0] = first;
58 buf[len-1] = last;
59 __coverity_writeall__(buf);
62 static void __bufread(uint8_t *buf, ssize_t len)
64 __coverity_negative_sink__(len);
65 if (len == 0) return;
66 int first = buf[0];
67 int last = buf[len-1];
70 MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
71 MemTxAttrs attrs,
72 uint8_t *buf, int len)
74 MemTxResult result;
75 // TODO: investigate impact of treating reads as producing
76 // tainted data, with __coverity_tainted_data_argument__(buf).
77 __bufwrite(buf, len);
78 return result;
81 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
82 MemTxAttrs attrs,
83 const uint8_t *buf, int len)
85 MemTxResult result;
86 __bufread(buf, len);
87 return result;
91 /* Tainting */
93 typedef struct {} name2keysym_t;
94 static int get_keysym(const name2keysym_t *table,
95 const char *name)
97 int result;
98 if (result > 0) {
99 __coverity_tainted_string_sanitize_content__(name);
100 return result;
101 } else {
102 return 0;
107 * GLib memory allocation functions.
109 * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
110 * and g_realloc of 0 bytes frees the pointer.
112 * Modeling this would result in Coverity flagging a lot of memory
113 * allocations as potentially returning NULL, and asking us to check
114 * whether the result of the allocation is NULL or not. However, the
115 * resulting pointer should never be dereferenced anyway, and in fact
116 * it is not in the vast majority of cases.
118 * If a dereference did happen, this would suppress a defect report
119 * for an actual null pointer dereference. But it's too unlikely to
120 * be worth wading through the false positives, and with some luck
121 * we'll get a buffer overflow reported anyway.
125 * Allocation primitives, cannot return NULL
126 * See also Coverity's library/generic/libc/all/all.c
129 void *g_malloc_n(size_t nmemb, size_t size)
131 size_t sz;
132 void *ptr;
134 __coverity_negative_sink__(nmemb);
135 __coverity_negative_sink__(size);
136 sz = nmemb * size;
137 ptr = __coverity_alloc__(sz);
138 __coverity_mark_as_uninitialized_buffer__(ptr);
139 __coverity_mark_as_afm_allocated__(ptr, "g_free");
140 return ptr;
143 void *g_malloc0_n(size_t nmemb, size_t size)
145 size_t sz;
146 void *ptr;
148 __coverity_negative_sink__(nmemb);
149 __coverity_negative_sink__(size);
150 sz = nmemb * size;
151 ptr = __coverity_alloc__(sz);
152 __coverity_writeall0__(ptr);
153 __coverity_mark_as_afm_allocated__(ptr, "g_free");
154 return ptr;
157 void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
159 size_t sz;
161 __coverity_negative_sink__(nmemb);
162 __coverity_negative_sink__(size);
163 sz = nmemb * size;
164 __coverity_escape__(ptr);
165 ptr = __coverity_alloc__(sz);
167 * Memory beyond the old size isn't actually initialized. Can't
168 * model that. See Coverity's realloc() model
170 __coverity_writeall__(ptr);
171 __coverity_mark_as_afm_allocated__(ptr, "g_free");
172 return ptr;
175 void g_free(void *ptr)
177 __coverity_free__(ptr);
178 __coverity_mark_as_afm_freed__(ptr, "g_free");
182 * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
183 * out of memory conditions
186 void *g_try_malloc_n(size_t nmemb, size_t size)
188 int nomem;
190 if (nomem) {
191 return NULL;
193 return g_malloc_n(nmemb, size);
196 void *g_try_malloc0_n(size_t nmemb, size_t size)
198 int nomem;
200 if (nomem) {
201 return NULL;
203 return g_malloc0_n(nmemb, size);
206 void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
208 int nomem;
210 if (nomem) {
211 return NULL;
213 return g_realloc_n(ptr, nmemb, size);
216 /* Trivially derive the g_FOO() from the g_FOO_n() */
218 void *g_malloc(size_t size)
220 return g_malloc_n(1, size);
223 void *g_malloc0(size_t size)
225 return g_malloc0_n(1, size);
228 void *g_realloc(void *ptr, size_t size)
230 return g_realloc_n(ptr, 1, size);
233 void *g_try_malloc(size_t size)
235 return g_try_malloc_n(1, size);
238 void *g_try_malloc0(size_t size)
240 return g_try_malloc0_n(1, size);
243 void *g_try_realloc(void *ptr, size_t size)
245 return g_try_realloc_n(ptr, 1, size);
248 /* Other memory allocation functions */
250 void *g_memdup(const void *ptr, unsigned size)
252 unsigned char *dup;
253 unsigned i;
255 if (!ptr) {
256 return NULL;
259 dup = g_malloc(size);
260 for (i = 0; i < size; i++)
261 dup[i] = ((unsigned char *)ptr)[i];
262 return dup;
266 * GLib string allocation functions
269 char *g_strdup(const char *s)
271 char *dup;
272 size_t i;
274 if (!s) {
275 return NULL;
278 __coverity_string_null_sink__(s);
279 __coverity_string_size_sink__(s);
280 dup = __coverity_alloc_nosize__();
281 __coverity_mark_as_afm_allocated__(dup, "g_free");
282 for (i = 0; (dup[i] = s[i]); i++) ;
283 return dup;
286 char *g_strndup(const char *s, size_t n)
288 char *dup;
289 size_t i;
291 __coverity_negative_sink__(n);
293 if (!s) {
294 return NULL;
297 dup = g_malloc(n + 1);
298 for (i = 0; i < n && (dup[i] = s[i]); i++) ;
299 dup[i] = 0;
300 return dup;
303 char *g_strdup_printf(const char *format, ...)
305 char ch, *s;
306 size_t len;
308 __coverity_string_null_sink__(format);
309 __coverity_string_size_sink__(format);
311 ch = *format;
313 s = __coverity_alloc_nosize__();
314 __coverity_writeall__(s);
315 __coverity_mark_as_afm_allocated__(s, "g_free");
316 return s;
319 char *g_strdup_vprintf(const char *format, va_list ap)
321 char ch, *s;
322 size_t len;
324 __coverity_string_null_sink__(format);
325 __coverity_string_size_sink__(format);
327 ch = *format;
328 ch = *(char *)ap;
330 s = __coverity_alloc_nosize__();
331 __coverity_writeall__(s);
332 __coverity_mark_as_afm_allocated__(s, "g_free");
334 return len;
337 char *g_strconcat(const char *s, ...)
339 char *s;
342 * Can't model: last argument must be null, the others
343 * null-terminated strings
346 s = __coverity_alloc_nosize__();
347 __coverity_writeall__(s);
348 __coverity_mark_as_afm_allocated__(s, "g_free");
349 return s;
352 /* Other glib functions */
354 typedef struct pollfd GPollFD;
356 int poll();
358 int g_poll (GPollFD *fds, unsigned nfds, int timeout)
360 return poll(fds, nfds, timeout);
363 typedef struct _GIOChannel GIOChannel;
364 GIOChannel *g_io_channel_unix_new(int fd)
366 GIOChannel *c = g_malloc0(sizeof(GIOChannel));
367 __coverity_escape__(fd);
368 return c;
371 void g_assertion_message_expr(const char *domain,
372 const char *file,
373 int line,
374 const char *func,
375 const char *expr)
377 __coverity_panic__();