blob: c702804f41c17c1cf5cf6e09aa1ec8516ebfed52 [file] [log] [blame]
Paolo Bonzinie40cdb02014-03-26 12:45:49 +01001/* 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
14/*
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.
20 *
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.
27 *
28 * The model file must be uploaded by an admin in the analysis settings of
29 * http://scan.coverity.com/projects/378
30 */
31
32#define NULL ((void *)0)
33
34typedef unsigned char uint8_t;
35typedef char int8_t;
36typedef unsigned int uint32_t;
37typedef int int32_t;
38typedef long ssize_t;
39typedef unsigned long long uint64_t;
40typedef long long int64_t;
41typedef _Bool bool;
42
Markus Armbrustere4b77da2015-01-26 15:05:11 +010043typedef struct va_list_str *va_list;
44
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010045/* exec.c */
46
47typedef struct AddressSpace AddressSpace;
48typedef uint64_t hwaddr;
Peter Maydell5c9eb022015-04-26 16:49:24 +010049typedef uint32_t MemTxResult;
50typedef uint64_t MemTxAttrs;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010051
Paolo Bonzini2e1c92d2015-05-04 14:18:09 +020052static void __bufwrite(uint8_t *buf, ssize_t len)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010053{
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);
60}
61
Paolo Bonzini2e1c92d2015-05-04 14:18:09 +020062static void __bufread(uint8_t *buf, ssize_t len)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010063{
64 __coverity_negative_sink__(len);
65 if (len == 0) return;
66 int first = buf[0];
67 int last = buf[len-1];
68}
69
Paolo Bonzini4d0e7232017-03-15 09:16:41 +010070MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
71 MemTxAttrs attrs,
72 uint8_t *buf, int len)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010073{
Peter Maydell5c9eb022015-04-26 16:49:24 +010074 MemTxResult result;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010075 // TODO: investigate impact of treating reads as producing
76 // tainted data, with __coverity_tainted_data_argument__(buf).
Paolo Bonzini4d0e7232017-03-15 09:16:41 +010077 __bufwrite(buf, len);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010078 return result;
79}
80
Paolo Bonzini4d0e7232017-03-15 09:16:41 +010081MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
82 MemTxAttrs attrs,
83 const uint8_t *buf, int len)
84{
85 MemTxResult result;
86 __bufread(buf, len);
87 return result;
88}
89
90
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010091/* Tainting */
92
93typedef struct {} name2keysym_t;
94static int get_keysym(const name2keysym_t *table,
95 const char *name)
96{
97 int result;
98 if (result > 0) {
99 __coverity_tainted_string_sanitize_content__(name);
100 return result;
101 } else {
102 return 0;
103 }
104}
105
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100106/*
107 * GLib memory allocation functions.
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100108 *
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.
111 *
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.
117 *
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.
122 */
123
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100124/*
125 * Allocation primitives, cannot return NULL
126 * See also Coverity's library/generic/libc/all/all.c
127 */
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100128
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100129void *g_malloc_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100130{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100131 size_t sz;
132 void *ptr;
133
134 __coverity_negative_sink__(nmemb);
135 __coverity_negative_sink__(size);
136 sz = nmemb * size;
Jan Kiszka906b8ba2015-03-12 12:24:26 +0100137 ptr = __coverity_alloc__(sz);
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100138 __coverity_mark_as_uninitialized_buffer__(ptr);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100139 __coverity_mark_as_afm_allocated__(ptr, "g_free");
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100140 return ptr;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100141}
142
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100143void *g_malloc0_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100144{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100145 size_t sz;
146 void *ptr;
147
148 __coverity_negative_sink__(nmemb);
149 __coverity_negative_sink__(size);
150 sz = nmemb * size;
Jan Kiszka906b8ba2015-03-12 12:24:26 +0100151 ptr = __coverity_alloc__(sz);
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100152 __coverity_writeall0__(ptr);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100153 __coverity_mark_as_afm_allocated__(ptr, "g_free");
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100154 return ptr;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100155}
156
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100157void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100158{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100159 size_t sz;
160
161 __coverity_negative_sink__(nmemb);
162 __coverity_negative_sink__(size);
163 sz = nmemb * size;
164 __coverity_escape__(ptr);
Jan Kiszka906b8ba2015-03-12 12:24:26 +0100165 ptr = __coverity_alloc__(sz);
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100166 /*
167 * Memory beyond the old size isn't actually initialized. Can't
168 * model that. See Coverity's realloc() model
169 */
170 __coverity_writeall__(ptr);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100171 __coverity_mark_as_afm_allocated__(ptr, "g_free");
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100172 return ptr;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100173}
174
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100175void g_free(void *ptr)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100176{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100177 __coverity_free__(ptr);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100178 __coverity_mark_as_afm_freed__(ptr, "g_free");
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100179}
180
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100181/*
182 * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
183 * out of memory conditions
184 */
185
186void *g_try_malloc_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100187{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100188 int nomem;
189
190 if (nomem) {
191 return NULL;
192 }
193 return g_malloc_n(nmemb, size);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100194}
195
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100196void *g_try_malloc0_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100197{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100198 int nomem;
199
200 if (nomem) {
201 return NULL;
202 }
203 return g_malloc0_n(nmemb, size);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100204}
205
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100206void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100207{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100208 int nomem;
209
210 if (nomem) {
211 return NULL;
212 }
213 return g_realloc_n(ptr, nmemb, size);
214}
215
216/* Trivially derive the g_FOO() from the g_FOO_n() */
217
218void *g_malloc(size_t size)
219{
220 return g_malloc_n(1, size);
221}
222
223void *g_malloc0(size_t size)
224{
225 return g_malloc0_n(1, size);
226}
227
228void *g_realloc(void *ptr, size_t size)
229{
230 return g_realloc_n(ptr, 1, size);
231}
232
233void *g_try_malloc(size_t size)
234{
235 return g_try_malloc_n(1, size);
236}
237
238void *g_try_malloc0(size_t size)
239{
240 return g_try_malloc0_n(1, size);
241}
242
243void *g_try_realloc(void *ptr, size_t size)
244{
245 return g_try_realloc_n(ptr, 1, size);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100246}
247
Markus Armbruster29cd81f2015-11-30 17:32:32 +0100248/* Other memory allocation functions */
249
250void *g_memdup(const void *ptr, unsigned size)
251{
252 unsigned char *dup;
253 unsigned i;
254
255 if (!ptr) {
256 return NULL;
257 }
258
259 dup = g_malloc(size);
260 for (i = 0; i < size; i++)
261 dup[i] = ((unsigned char *)ptr)[i];
262 return dup;
263}
264
Markus Armbrustere4b77da2015-01-26 15:05:11 +0100265/*
266 * GLib string allocation functions
267 */
268
269char *g_strdup(const char *s)
270{
271 char *dup;
272 size_t i;
273
274 if (!s) {
275 return NULL;
276 }
277
278 __coverity_string_null_sink__(s);
279 __coverity_string_size_sink__(s);
280 dup = __coverity_alloc_nosize__();
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100281 __coverity_mark_as_afm_allocated__(dup, "g_free");
Markus Armbrustere4b77da2015-01-26 15:05:11 +0100282 for (i = 0; (dup[i] = s[i]); i++) ;
283 return dup;
284}
285
286char *g_strndup(const char *s, size_t n)
287{
288 char *dup;
289 size_t i;
290
291 __coverity_negative_sink__(n);
292
293 if (!s) {
294 return NULL;
295 }
296
297 dup = g_malloc(n + 1);
298 for (i = 0; i < n && (dup[i] = s[i]); i++) ;
299 dup[i] = 0;
300 return dup;
301}
302
303char *g_strdup_printf(const char *format, ...)
304{
305 char ch, *s;
306 size_t len;
307
308 __coverity_string_null_sink__(format);
309 __coverity_string_size_sink__(format);
310
311 ch = *format;
312
313 s = __coverity_alloc_nosize__();
314 __coverity_writeall__(s);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100315 __coverity_mark_as_afm_allocated__(s, "g_free");
Markus Armbrustere4b77da2015-01-26 15:05:11 +0100316 return s;
317}
318
319char *g_strdup_vprintf(const char *format, va_list ap)
320{
321 char ch, *s;
322 size_t len;
323
324 __coverity_string_null_sink__(format);
325 __coverity_string_size_sink__(format);
326
327 ch = *format;
328 ch = *(char *)ap;
329
330 s = __coverity_alloc_nosize__();
331 __coverity_writeall__(s);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100332 __coverity_mark_as_afm_allocated__(s, "g_free");
Markus Armbrustere4b77da2015-01-26 15:05:11 +0100333
334 return len;
335}
336
337char *g_strconcat(const char *s, ...)
338{
339 char *s;
340
341 /*
342 * Can't model: last argument must be null, the others
343 * null-terminated strings
344 */
345
346 s = __coverity_alloc_nosize__();
347 __coverity_writeall__(s);
Markus Armbruster7ad4c722015-01-26 21:37:15 +0100348 __coverity_mark_as_afm_allocated__(s, "g_free");
Markus Armbrustere4b77da2015-01-26 15:05:11 +0100349 return s;
350}
351
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100352/* Other glib functions */
353
Markus Armbruster1e819692015-12-17 08:20:33 +0100354typedef struct pollfd GPollFD;
355
356int poll();
357
358int g_poll (GPollFD *fds, unsigned nfds, int timeout)
359{
360 return poll(fds, nfds, timeout);
361}
362
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100363typedef struct _GIOChannel GIOChannel;
364GIOChannel *g_io_channel_unix_new(int fd)
365{
366 GIOChannel *c = g_malloc0(sizeof(GIOChannel));
367 __coverity_escape__(fd);
368 return c;
369}
370
371void g_assertion_message_expr(const char *domain,
372 const char *file,
373 int line,
374 const char *func,
375 const char *expr)
376{
377 __coverity_panic__();
378}