qdev_get_machine() is the helper that QEMU heavily uses in most places to fetch the current machine object after it's created. It can only be called after the machine is created as of now, otherwise a container can be wrongly created at path "/machine", and that could crash QEMU later.
It's not an issue for now, because all code paths will currently make sure this helper won't be called too early, e.g., before the machine object is properly created and attached under the object root path. This patch makes this behavior more predictable, by never trying to wrongly create a container if the object is missing. This enables the helper to be used even before the machine is created, as long as the caller can properly handle a NULL return (which says, "machine is not yet created"). No functional change intended as of now, but will start to make use of it in later patches, where qdev_get_machine() can start to be use before machine creations. Signed-off-by: Peter Xu <pet...@redhat.com> --- hw/core/qdev.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 5c83f48b33..c867aed28a 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -840,7 +840,13 @@ Object *qdev_get_machine(void) static Object *dev; if (dev == NULL) { - dev = container_get(object_get_root(), "/machine"); + /* + * NOTE: dev can keep being NULL if machine is not yet created! + * In which case the function will properly return NULL. + * + * Whenever machine object is created and found once, we cache it. + */ + dev = object_resolve_path_component(object_get_root(), "machine"); } return dev; -- 2.45.0