Skip to content

Commit b040913

Browse files
akihikodakihuth
authored andcommitted
qapi: Do not cast function pointers
Using -fsanitize=undefined with Clang v18 causes an error if function pointers are casted: qapi/qapi-clone-visitor.c:188:5: runtime error: call to function visit_type_SocketAddress through pointer to incorrect function type 'bool (*)(struct Visitor *, const char *, void **, struct Error **)' /tmp/qemu-ubsan/qapi/qapi-visit-sockets.c:487: note: visit_type_SocketAddress defined here #0 0x5642aa2f7f3b in qapi_clone qapi/qapi-clone-visitor.c:188:5 #1 0x5642aa2c8ce5 in qio_channel_socket_listen_async io/channel-socket.c:285:18 #2 0x5642aa2b8903 in test_io_channel_setup_async tests/unit/test-io-channel-socket.c:116:5 #3 0x5642aa2b8204 in test_io_channel tests/unit/test-io-channel-socket.c:179:9 #4 0x5642aa2b8129 in test_io_channel_ipv4 tests/unit/test-io-channel-socket.c:323:5 ... It also prevents enabling the strict mode of CFI which is currently disabled with -fsanitize-cfi-icall-generalize-pointers. The problematic casts are necessary to pass visit_type_T() and visit_type_T_members() as callbacks to qapi_clone() and qapi_clone_members(), respectively. Open-code these two functions to avoid the callbacks, and thus the type casts. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2346 Signed-off-by: Akihiko Odaki <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> Message-ID: <[email protected]> [thuth: Improve commit message according to Markus' suggestions] Signed-off-by: Thomas Huth <[email protected]>
1 parent a3b3ad7 commit b040913

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

include/qapi/clone-visitor.h

+24-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef QAPI_CLONE_VISITOR_H
1212
#define QAPI_CLONE_VISITOR_H
1313

14+
#include "qapi/error.h"
1415
#include "qapi/visitor.h"
1516

1617
/*
@@ -20,32 +21,42 @@
2021
*/
2122
typedef struct QapiCloneVisitor QapiCloneVisitor;
2223

23-
void *qapi_clone(const void *src, bool (*visit_type)(Visitor *, const char *,
24-
void **, Error **));
25-
void qapi_clone_members(void *dst, const void *src, size_t sz,
26-
bool (*visit_type_members)(Visitor *, void *,
27-
Error **));
24+
Visitor *qapi_clone_visitor_new(void);
25+
Visitor *qapi_clone_members_visitor_new(void);
2826

2927
/*
3028
* Deep-clone QAPI object @src of the given @type, and return the result.
3129
*
3230
* Not usable on QAPI scalars (integers, strings, enums), nor on a
3331
* QAPI object that references the 'any' type. Safe when @src is NULL.
3432
*/
35-
#define QAPI_CLONE(type, src) \
36-
((type *)qapi_clone(src, \
37-
(bool (*)(Visitor *, const char *, void **, \
38-
Error **))visit_type_ ## type))
33+
#define QAPI_CLONE(type, src) \
34+
({ \
35+
Visitor *v_; \
36+
type *dst_ = (type *) (src); /* Cast away const */ \
37+
\
38+
if (dst_) { \
39+
v_ = qapi_clone_visitor_new(); \
40+
visit_type_ ## type(v_, NULL, &dst_, &error_abort); \
41+
visit_free(v_); \
42+
} \
43+
dst_; \
44+
})
3945

4046
/*
4147
* Copy deep clones of @type members from @src to @dst.
4248
*
4349
* Not usable on QAPI scalars (integers, strings, enums), nor on a
4450
* QAPI object that references the 'any' type.
4551
*/
46-
#define QAPI_CLONE_MEMBERS(type, dst, src) \
47-
qapi_clone_members(dst, src, sizeof(type), \
48-
(bool (*)(Visitor *, void *, \
49-
Error **))visit_type_ ## type ## _members)
52+
#define QAPI_CLONE_MEMBERS(type, dst, src) \
53+
({ \
54+
Visitor *v_; \
55+
\
56+
v_ = qapi_clone_members_visitor_new(); \
57+
*(type *)(dst) = *(src); \
58+
visit_type_ ## type ## _members(v_, (type *)(dst), &error_abort); \
59+
visit_free(v_); \
60+
})
5061

5162
#endif

qapi/qapi-clone-visitor.c

+4-26
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static void qapi_clone_free(Visitor *v)
149149
g_free(v);
150150
}
151151

152-
static Visitor *qapi_clone_visitor_new(void)
152+
Visitor *qapi_clone_visitor_new(void)
153153
{
154154
QapiCloneVisitor *v;
155155

@@ -174,31 +174,9 @@ static Visitor *qapi_clone_visitor_new(void)
174174
return &v->visitor;
175175
}
176176

177-
void *qapi_clone(const void *src, bool (*visit_type)(Visitor *, const char *,
178-
void **, Error **))
177+
Visitor *qapi_clone_members_visitor_new(void)
179178
{
180-
Visitor *v;
181-
void *dst = (void *) src; /* Cast away const */
182-
183-
if (!src) {
184-
return NULL;
185-
}
186-
187-
v = qapi_clone_visitor_new();
188-
visit_type(v, NULL, &dst, &error_abort);
189-
visit_free(v);
190-
return dst;
191-
}
192-
193-
void qapi_clone_members(void *dst, const void *src, size_t sz,
194-
bool (*visit_type_members)(Visitor *, void *,
195-
Error **))
196-
{
197-
Visitor *v;
198-
199-
v = qapi_clone_visitor_new();
200-
memcpy(dst, src, sz);
179+
Visitor *v = qapi_clone_visitor_new();
201180
to_qcv(v)->depth++;
202-
visit_type_members(v, dst, &error_abort);
203-
visit_free(v);
181+
return v;
204182
}

0 commit comments

Comments
 (0)