3 * Copyright (C) 2014 Red Hat, Inc.
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
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;
36 typedef unsigned int uint32_t;
39 typedef unsigned long long uint64_t;
40 typedef long long int64_t;
43 typedef struct va_list_str
*va_list;
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
)
55 __coverity_negative_sink__(len
);
59 __coverity_writeall__(buf
);
62 static void __bufread(uint8_t *buf
, ssize_t len
)
64 __coverity_negative_sink__(len
);
67 int last
= buf
[len
-1];
70 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
72 uint8_t *buf
, int len
)
75 // TODO: investigate impact of treating reads as producing
76 // tainted data, with __coverity_tainted_data_argument__(buf).
81 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
83 const uint8_t *buf
, int len
)
93 typedef struct {} name2keysym_t
;
94 static int get_keysym(const name2keysym_t
*table
,
99 __coverity_tainted_string_sanitize_content__(name
);
106 /* Replay data is considered trusted. */
107 uint8_t replay_get_byte(void)
119 * GLib memory allocation functions.
121 * Note that we ignore the fact that g_malloc of 0 bytes returns NULL,
122 * and g_realloc of 0 bytes frees the pointer.
124 * Modeling this would result in Coverity flagging a lot of memory
125 * allocations as potentially returning NULL, and asking us to check
126 * whether the result of the allocation is NULL or not. However, the
127 * resulting pointer should never be dereferenced anyway, and in fact
128 * it is not in the vast majority of cases.
130 * If a dereference did happen, this would suppress a defect report
131 * for an actual null pointer dereference. But it's too unlikely to
132 * be worth wading through the false positives, and with some luck
133 * we'll get a buffer overflow reported anyway.
137 * Allocation primitives, cannot return NULL
138 * See also Coverity's library/generic/libc/all/all.c
141 void *g_malloc_n(size_t nmemb
, size_t size
)
146 __coverity_negative_sink__(nmemb
);
147 __coverity_negative_sink__(size
);
149 ptr
= __coverity_alloc__(sz
);
150 __coverity_mark_as_uninitialized_buffer__(ptr
);
151 __coverity_mark_as_afm_allocated__(ptr
, "g_free");
155 void *g_malloc0_n(size_t nmemb
, size_t size
)
160 __coverity_negative_sink__(nmemb
);
161 __coverity_negative_sink__(size
);
163 ptr
= __coverity_alloc__(sz
);
164 __coverity_writeall0__(ptr
);
165 __coverity_mark_as_afm_allocated__(ptr
, "g_free");
169 void *g_realloc_n(void *ptr
, size_t nmemb
, size_t size
)
173 __coverity_negative_sink__(nmemb
);
174 __coverity_negative_sink__(size
);
176 __coverity_escape__(ptr
);
177 ptr
= __coverity_alloc__(sz
);
179 * Memory beyond the old size isn't actually initialized. Can't
180 * model that. See Coverity's realloc() model
182 __coverity_writeall__(ptr
);
183 __coverity_mark_as_afm_allocated__(ptr
, "g_free");
187 void g_free(void *ptr
)
189 __coverity_free__(ptr
);
190 __coverity_mark_as_afm_freed__(ptr
, "g_free");
194 * Derive the g_try_FOO_n() from the g_FOO_n() by adding indeterminate
195 * out of memory conditions
198 void *g_try_malloc_n(size_t nmemb
, size_t size
)
205 return g_malloc_n(nmemb
, size
);
208 void *g_try_malloc0_n(size_t nmemb
, size_t size
)
215 return g_malloc0_n(nmemb
, size
);
218 void *g_try_realloc_n(void *ptr
, size_t nmemb
, size_t size
)
225 return g_realloc_n(ptr
, nmemb
, size
);
228 /* Trivially derive the g_FOO() from the g_FOO_n() */
230 void *g_malloc(size_t size
)
232 return g_malloc_n(1, size
);
235 void *g_malloc0(size_t size
)
237 return g_malloc0_n(1, size
);
240 void *g_realloc(void *ptr
, size_t size
)
242 return g_realloc_n(ptr
, 1, size
);
245 void *g_try_malloc(size_t size
)
247 return g_try_malloc_n(1, size
);
250 void *g_try_malloc0(size_t size
)
252 return g_try_malloc0_n(1, size
);
255 void *g_try_realloc(void *ptr
, size_t size
)
257 return g_try_realloc_n(ptr
, 1, size
);
260 /* Other memory allocation functions */
262 void *g_memdup(const void *ptr
, unsigned size
)
271 dup
= g_malloc(size
);
272 for (i
= 0; i
< size
; i
++)
273 dup
[i
] = ((unsigned char *)ptr
)[i
];
278 * GLib string allocation functions
281 char *g_strdup(const char *s
)
290 __coverity_string_null_sink__(s
);
291 __coverity_string_size_sink__(s
);
292 dup
= __coverity_alloc_nosize__();
293 __coverity_mark_as_afm_allocated__(dup
, "g_free");
294 for (i
= 0; (dup
[i
] = s
[i
]); i
++) ;
298 char *g_strndup(const char *s
, size_t n
)
303 __coverity_negative_sink__(n
);
309 dup
= g_malloc(n
+ 1);
310 for (i
= 0; i
< n
&& (dup
[i
] = s
[i
]); i
++) ;
315 char *g_strdup_printf(const char *format
, ...)
320 __coverity_string_null_sink__(format
);
321 __coverity_string_size_sink__(format
);
325 s
= __coverity_alloc_nosize__();
326 __coverity_writeall__(s
);
327 __coverity_mark_as_afm_allocated__(s
, "g_free");
331 char *g_strdup_vprintf(const char *format
, va_list ap
)
336 __coverity_string_null_sink__(format
);
337 __coverity_string_size_sink__(format
);
342 s
= __coverity_alloc_nosize__();
343 __coverity_writeall__(s
);
344 __coverity_mark_as_afm_allocated__(s
, "g_free");
349 char *g_strconcat(const char *s
, ...)
354 * Can't model: last argument must be null, the others
355 * null-terminated strings
358 s
= __coverity_alloc_nosize__();
359 __coverity_writeall__(s
);
360 __coverity_mark_as_afm_allocated__(s
, "g_free");
364 /* Other glib functions */
366 typedef struct pollfd GPollFD
;
370 int g_poll (GPollFD
*fds
, unsigned nfds
, int timeout
)
372 return poll(fds
, nfds
, timeout
);
375 typedef struct _GIOChannel GIOChannel
;
376 GIOChannel
*g_io_channel_unix_new(int fd
)
378 GIOChannel
*c
= g_malloc0(sizeof(GIOChannel
));
379 __coverity_escape__(fd
);
383 void g_assertion_message_expr(const char *domain
,
389 __coverity_panic__();