blob: 8d0839ef08f729e6b2be93d134d2bc4ab4a94cbd [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
43/* exec.c */
44
45typedef struct AddressSpace AddressSpace;
46typedef uint64_t hwaddr;
47
48static void __write(uint8_t *buf, ssize_t len)
49{
50 int first, last;
51 __coverity_negative_sink__(len);
52 if (len == 0) return;
53 buf[0] = first;
54 buf[len-1] = last;
55 __coverity_writeall__(buf);
56}
57
58static void __read(uint8_t *buf, ssize_t len)
59{
60 __coverity_negative_sink__(len);
61 if (len == 0) return;
62 int first = buf[0];
63 int last = buf[len-1];
64}
65
66bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
67 int len, bool is_write)
68{
69 bool result;
70
71 // TODO: investigate impact of treating reads as producing
72 // tainted data, with __coverity_tainted_data_argument__(buf).
73 if (is_write) __write(buf, len); else __read(buf, len);
74
75 return result;
76}
77
78/* Tainting */
79
80typedef struct {} name2keysym_t;
81static int get_keysym(const name2keysym_t *table,
82 const char *name)
83{
84 int result;
85 if (result > 0) {
86 __coverity_tainted_string_sanitize_content__(name);
87 return result;
88 } else {
89 return 0;
90 }
91}
92
Markus Armbruster9d7a4c62015-01-22 11:21:37 +010093/*
94 * GLib memory allocation functions.
Paolo Bonzinie40cdb02014-03-26 12:45:49 +010095 *
96 * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
97 * and g_realloc of 0 bytes frees the pointer.
98 *
99 * Modeling this would result in Coverity flagging a lot of memory
100 * allocations as potentially returning NULL, and asking us to check
101 * whether the result of the allocation is NULL or not. However, the
102 * resulting pointer should never be dereferenced anyway, and in fact
103 * it is not in the vast majority of cases.
104 *
105 * If a dereference did happen, this would suppress a defect report
106 * for an actual null pointer dereference. But it's too unlikely to
107 * be worth wading through the false positives, and with some luck
108 * we'll get a buffer overflow reported anyway.
109 */
110
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100111/*
112 * Allocation primitives, cannot return NULL
113 * See also Coverity's library/generic/libc/all/all.c
114 */
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100115
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100116void *g_malloc_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100117{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100118 size_t sz;
119 void *ptr;
120
121 __coverity_negative_sink__(nmemb);
122 __coverity_negative_sink__(size);
123 sz = nmemb * size;
124 ptr = __coverity_alloc__(size);
125 __coverity_mark_as_uninitialized_buffer__(ptr);
126 __coverity_mark_as_afm_allocated__(ptr, AFM_free);
127 return ptr;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100128}
129
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100130void *g_malloc0_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100131{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100132 size_t sz;
133 void *ptr;
134
135 __coverity_negative_sink__(nmemb);
136 __coverity_negative_sink__(size);
137 sz = nmemb * size;
138 ptr = __coverity_alloc__(size);
139 __coverity_writeall0__(ptr);
140 __coverity_mark_as_afm_allocated__(ptr, AFM_free);
141 return ptr;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100142}
143
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100144void *g_realloc_n(void *ptr, size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100145{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100146 size_t sz;
147
148 __coverity_negative_sink__(nmemb);
149 __coverity_negative_sink__(size);
150 sz = nmemb * size;
151 __coverity_escape__(ptr);
152 ptr = __coverity_alloc__(size);
153 /*
154 * Memory beyond the old size isn't actually initialized. Can't
155 * model that. See Coverity's realloc() model
156 */
157 __coverity_writeall__(ptr);
158 __coverity_mark_as_afm_allocated__(ptr, AFM_free);
159 return ptr;
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100160}
161
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100162void g_free(void *ptr)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100163{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100164 __coverity_free__(ptr);
165 __coverity_mark_as_afm_freed__(ptr, AFM_free);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100166}
167
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100168/*
169 * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
170 * out of memory conditions
171 */
172
173void *g_try_malloc_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100174{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100175 int nomem;
176
177 if (nomem) {
178 return NULL;
179 }
180 return g_malloc_n(nmemb, size);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100181}
182
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100183void *g_try_malloc0_n(size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100184{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100185 int nomem;
186
187 if (nomem) {
188 return NULL;
189 }
190 return g_malloc0_n(nmemb, size);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100191}
192
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100193void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size)
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100194{
Markus Armbruster9d7a4c62015-01-22 11:21:37 +0100195 int nomem;
196
197 if (nomem) {
198 return NULL;
199 }
200 return g_realloc_n(ptr, nmemb, size);
201}
202
203/* Trivially derive the g_FOO() from the g_FOO_n() */
204
205void *g_malloc(size_t size)
206{
207 return g_malloc_n(1, size);
208}
209
210void *g_malloc0(size_t size)
211{
212 return g_malloc0_n(1, size);
213}
214
215void *g_realloc(void *ptr, size_t size)
216{
217 return g_realloc_n(ptr, 1, size);
218}
219
220void *g_try_malloc(size_t size)
221{
222 return g_try_malloc_n(1, size);
223}
224
225void *g_try_malloc0(size_t size)
226{
227 return g_try_malloc0_n(1, size);
228}
229
230void *g_try_realloc(void *ptr, size_t size)
231{
232 return g_try_realloc_n(ptr, 1, size);
Paolo Bonzinie40cdb02014-03-26 12:45:49 +0100233}
234
235/* Other glib functions */
236
237typedef struct _GIOChannel GIOChannel;
238GIOChannel *g_io_channel_unix_new(int fd)
239{
240 GIOChannel *c = g_malloc0(sizeof(GIOChannel));
241 __coverity_escape__(fd);
242 return c;
243}
244
245void g_assertion_message_expr(const char *domain,
246 const char *file,
247 int line,
248 const char *func,
249 const char *expr)
250{
251 __coverity_panic__();
252}