qcow2: Rename BDRVQcowState to BDRVQcow2State
[qemu/ar7.git] / scripts / coverity-model.c
blob617f67d716914a737999c11b546ca77d1c5ba7a7
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_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
71 uint8_t *buf, int len, bool is_write)
73 MemTxResult result;
75 // TODO: investigate impact of treating reads as producing
76 // tainted data, with __coverity_tainted_data_argument__(buf).
77 if (is_write) __bufread(buf, len); else __bufwrite(buf, len);
79 return result;
82 /* Tainting */
84 typedef struct {} name2keysym_t;
85 static int get_keysym(const name2keysym_t *table,
86 const char *name)
88 int result;
89 if (result > 0) {
90 __coverity_tainted_string_sanitize_content__(name);
91 return result;
92 } else {
93 return 0;
98 * GLib memory allocation functions.
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.
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.
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.
116 * Allocation primitives, cannot return NULL
117 * See also Coverity's library/generic/libc/all/all.c
120 void *g_malloc_n(size_t nmemb, size_t size)
122 size_t sz;
123 void *ptr;
125 __coverity_negative_sink__(nmemb);
126 __coverity_negative_sink__(size);
127 sz = nmemb * size;
128 ptr = __coverity_alloc__(sz);
129 __coverity_mark_as_uninitialized_buffer__(ptr);
130 __coverity_mark_as_afm_allocated__(ptr, "g_free");
131 return ptr;
134 void *g_malloc0_n(size_t nmemb, size_t size)
136 size_t sz;
137 void *ptr;
139 __coverity_negative_sink__(nmemb);
140 __coverity_negative_sink__(size);
141 sz = nmemb * size;
142 ptr = __coverity_alloc__(sz);
143 __coverity_writeall0__(ptr);
144 __coverity_mark_as_afm_allocated__(ptr, "g_free");
145 return ptr;
148 void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
150 size_t sz;
152 __coverity_negative_sink__(nmemb);
153 __coverity_negative_sink__(size);
154 sz = nmemb * size;
155 __coverity_escape__(ptr);
156 ptr = __coverity_alloc__(sz);
158 * Memory beyond the old size isn't actually initialized. Can't
159 * model that. See Coverity's realloc() model
161 __coverity_writeall__(ptr);
162 __coverity_mark_as_afm_allocated__(ptr, "g_free");
163 return ptr;
166 void g_free(void *ptr)
168 __coverity_free__(ptr);
169 __coverity_mark_as_afm_freed__(ptr, "g_free");
173 * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
174 * out of memory conditions
177 void *g_try_malloc_n(size_t nmemb, size_t size)
179 int nomem;
181 if (nomem) {
182 return NULL;
184 return g_malloc_n(nmemb, size);
187 void *g_try_malloc0_n(size_t nmemb, size_t size)
189 int nomem;
191 if (nomem) {
192 return NULL;
194 return g_malloc0_n(nmemb, size);
197 void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
199 int nomem;
201 if (nomem) {
202 return NULL;
204 return g_realloc_n(ptr, nmemb, size);
207 /* Trivially derive the g_FOO() from the g_FOO_n() */
209 void *g_malloc(size_t size)
211 return g_malloc_n(1, size);
214 void *g_malloc0(size_t size)
216 return g_malloc0_n(1, size);
219 void *g_realloc(void *ptr, size_t size)
221 return g_realloc_n(ptr, 1, size);
224 void *g_try_malloc(size_t size)
226 return g_try_malloc_n(1, size);
229 void *g_try_malloc0(size_t size)
231 return g_try_malloc0_n(1, size);
234 void *g_try_realloc(void *ptr, size_t size)
236 return g_try_realloc_n(ptr, 1, size);
240 * GLib string allocation functions
243 char *g_strdup(const char *s)
245 char *dup;
246 size_t i;
248 if (!s) {
249 return NULL;
252 __coverity_string_null_sink__(s);
253 __coverity_string_size_sink__(s);
254 dup = __coverity_alloc_nosize__();
255 __coverity_mark_as_afm_allocated__(dup, "g_free");
256 for (i = 0; (dup[i] = s[i]); i++) ;
257 return dup;
260 char *g_strndup(const char *s, size_t n)
262 char *dup;
263 size_t i;
265 __coverity_negative_sink__(n);
267 if (!s) {
268 return NULL;
271 dup = g_malloc(n + 1);
272 for (i = 0; i < n && (dup[i] = s[i]); i++) ;
273 dup[i] = 0;
274 return dup;
277 char *g_strdup_printf(const char *format, ...)
279 char ch, *s;
280 size_t len;
282 __coverity_string_null_sink__(format);
283 __coverity_string_size_sink__(format);
285 ch = *format;
287 s = __coverity_alloc_nosize__();
288 __coverity_writeall__(s);
289 __coverity_mark_as_afm_allocated__(s, "g_free");
290 return s;
293 char *g_strdup_vprintf(const char *format, va_list ap)
295 char ch, *s;
296 size_t len;
298 __coverity_string_null_sink__(format);
299 __coverity_string_size_sink__(format);
301 ch = *format;
302 ch = *(char *)ap;
304 s = __coverity_alloc_nosize__();
305 __coverity_writeall__(s);
306 __coverity_mark_as_afm_allocated__(s, "g_free");
308 return len;
311 char *g_strconcat(const char *s, ...)
313 char *s;
316 * Can't model: last argument must be null, the others
317 * null-terminated strings
320 s = __coverity_alloc_nosize__();
321 __coverity_writeall__(s);
322 __coverity_mark_as_afm_allocated__(s, "g_free");
323 return s;
326 /* Other glib functions */
328 typedef struct _GIOChannel GIOChannel;
329 GIOChannel *g_io_channel_unix_new(int fd)
331 GIOChannel *c = g_malloc0(sizeof(GIOChannel));
332 __coverity_escape__(fd);
333 return c;
336 void g_assertion_message_expr(const char *domain,
337 const char *file,
338 int line,
339 const char *func,
340 const char *expr)
342 __coverity_panic__();