]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
xml: use long long internally, to centralize overflow checks
authorEric Blake <eblake@redhat.com>
Sat, 3 Mar 2012 00:47:16 +0000 (17:47 -0700)
committerEric Blake <eblake@redhat.com>
Thu, 8 Mar 2012 01:24:43 +0000 (18:24 -0700)
On 64-bit platforms, unsigned long and unsigned long long are
identical, so we don't have to worry about overflow checks.
On 32-bit platforms, anywhere we narrow unsigned long long back
to unsigned long, we have to worry about overflow; it's easier
to do this in one place by having most of the code use the same
or wider types, and only doing the narrowing at the last minute.
Therefore, the memory set commands remain unsigned long, and
the memory get command now centralizes the overflow check into
libvirt.c, so that drivers don't have to repeat the work.

This also fixes a bug where xen returned the wrong value on
failure (most APIs return -1 on failure, but getMaxMemory
must return 0 on failure).

* src/driver.h (virDrvDomainGetMaxMemory): Use long long.
* src/libvirt.c (virDomainGetMaxMemory): Raise overflow.
* src/test/test_driver.c (testGetMaxMemory): Fix driver.
* src/rpc/gendispatch.pl (name_to_ProcName): Likewise.
* src/xen/xen_hypervisor.c (xenHypervisorGetMaxMemory): Likewise.
* src/xen/xen_driver.c (xenUnifiedDomainGetMaxMemory): Likewise.
* src/xen/xend_internal.c (xenDaemonDomainGetMaxMemory):
Likewise.
* src/xen/xend_internal.h (xenDaemonDomainGetMaxMemory):
Likewise.
* src/xen/xm_internal.c (xenXMDomainGetMaxMemory): Likewise.
* src/xen/xm_internal.h (xenXMDomainGetMaxMemory): Likewise.
* src/xen/xs_internal.c (xenStoreDomainGetMaxMemory): Likewise.
* src/xen/xs_internal.h (xenStoreDomainGetMaxMemory): Likewise.
* src/xenapi/xenapi_driver.c (xenapiDomainGetMaxMemory):
Likewise.
* src/esx/esx_driver.c (esxDomainGetMaxMemory): Likewise.
* src/libxl/libxl_driver.c (libxlDomainGetMaxMemory): Likewise.
* src/qemu/qemu_driver.c (qemudDomainGetMaxMemory): Likewise.
* src/lxc/lxc_driver.c (lxcDomainGetMaxMemory): Likewise.
* src/uml/uml_driver.c (umlDomainGetMaxMemory): Likewise.

18 files changed:
src/driver.h
src/esx/esx_driver.c
src/libvirt.c
src/libxl/libxl_driver.c
src/lxc/lxc_driver.c
src/qemu/qemu_driver.c
src/rpc/gendispatch.pl
src/test/test_driver.c
src/uml/uml_driver.c
src/xen/xen_driver.c
src/xen/xen_hypervisor.c
src/xen/xend_internal.c
src/xen/xend_internal.h
src/xen/xm_internal.c
src/xen/xm_internal.h
src/xen/xs_internal.c
src/xen/xs_internal.h
src/xenapi/xenapi_driver.c

index 212d2f5e76975d4d50c243bf99007131a72030a7..03d249b7aea5b0ee88d357ebf0b1cb750cb571a7 100644 (file)
@@ -142,7 +142,7 @@ typedef int
                                          unsigned int flags);
 typedef char *
         (*virDrvDomainGetOSType)       (virDomainPtr domain);
-typedef unsigned long
+typedef unsigned long long
         (*virDrvDomainGetMaxMemory)    (virDomainPtr domain);
 typedef int
         (*virDrvDomainSetMaxMemory)    (virDomainPtr domain,
index b6b22f8d42764bd25d7a816c3fcdc83d46af0356..69435344be13fdd80284ecc85552da3c7dcc2713 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * esx_driver.c: core driver functions for managing VMware ESX hosts
  *
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2009-2011 Matthias Bolte <matthias.bolte@googlemail.com>
  * Copyright (C) 2009 Maximilian Wilhelm <max@rfc2324.org>
  *
@@ -2073,7 +2073,7 @@ esxDomainGetOSType(virDomainPtr domain ATTRIBUTE_UNUSED)
 
 
 
-static unsigned long
+static unsigned long long
 esxDomainGetMaxMemory(virDomainPtr domain)
 {
     esxPrivate *priv = domain->conn->privateData;
index 21b47660880ba04c91571f04b5283137fbb9cbbc..e916aa04bd0b5f9d477fc2a2b47e9e7e28f430e6 100644 (file)
@@ -3603,10 +3603,15 @@ virDomainGetMaxMemory(virDomainPtr domain)
     conn = domain->conn;
 
     if (conn->driver->domainGetMaxMemory) {
-        unsigned long ret;
+        unsigned long long ret;
         ret = conn->driver->domainGetMaxMemory (domain);
         if (ret == 0)
             goto error;
+        if ((unsigned long) ret != ret) {
+            virLibDomainError(VIR_ERR_OVERFLOW, _("result too large: %llu"),
+                              ret);
+            goto error;
+        }
         return ret;
     }
 
index d5fa64a87da2c0ae1e0c22375403cd3c731e9896..a7bb75100a96106121e7faebd48222da485d6998 100644 (file)
@@ -1,7 +1,7 @@
 /*---------------------------------------------------------------------------*/
-/*  Copyright (c) 2011 SUSE LINUX Products GmbH, Nuernberg, Germany.
+/*  Copyright (C) 2006-2012 Red Hat, Inc.
+ *  Copyright (c) 2011 SUSE LINUX Products GmbH, Nuernberg, Germany.
  *  Copyright (C) 2011 Univention GmbH.
- *  Copyright (C) 2006-2011 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -1592,12 +1592,12 @@ cleanup:
     return type;
 }
 
-static unsigned long
+static unsigned long long
 libxlDomainGetMaxMemory(virDomainPtr dom)
 {
     libxlDriverPrivatePtr driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
 
     libxlDriverLock(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
index d9cbd9e90f0e7fd18916fb2011a66767fd53a64f..1a0e4586fe2cd83098bfa4a59c64e1f7a5441ab5 100644 (file)
@@ -670,10 +670,12 @@ cleanup:
 }
 
 /* Returns max memory in kb, 0 if error */
-static unsigned long lxcDomainGetMaxMemory(virDomainPtr dom) {
+static unsigned long long
+lxcDomainGetMaxMemory(virDomainPtr dom)
+{
     lxc_driver_t *driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
 
     lxcDriverLock(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
index b364b2c3c1e9dfd25c0ba713606f5cb126f37dd9..6546d0b17d81a3f6cb6048d4fa3d2983b34d30c5 100644 (file)
@@ -1877,10 +1877,12 @@ cleanup:
 }
 
 /* Returns max memory in kb, 0 if error */
-static unsigned long qemudDomainGetMaxMemory(virDomainPtr dom) {
+static unsigned long long
+qemuDomainGetMaxMemory(virDomainPtr dom)
+{
     struct qemud_driver *driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
 
     qemuDriverLock(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
@@ -12417,7 +12419,7 @@ static virDriver qemuDriver = {
     .domainDestroy = qemuDomainDestroy, /* 0.2.0 */
     .domainDestroyFlags = qemuDomainDestroyFlags, /* 0.9.4 */
     .domainGetOSType = qemudDomainGetOSType, /* 0.2.2 */
-    .domainGetMaxMemory = qemudDomainGetMaxMemory, /* 0.4.2 */
+    .domainGetMaxMemory = qemuDomainGetMaxMemory, /* 0.4.2 */
     .domainSetMaxMemory = qemudDomainSetMaxMemory, /* 0.4.2 */
     .domainSetMemory = qemudDomainSetMemory, /* 0.4.2 */
     .domainSetMemoryFlags = qemudDomainSetMemoryFlags, /* 0.9.0 */
index 3f37d58420ed5a34c5813ccfc0863aecebd30738..ef369b980b45977acf7fe2a6b5d62804e107843a 100755 (executable)
@@ -201,7 +201,6 @@ close(PROTOCOL);
 # this list is fixed. new procedures and public APIs have to map [unsigned]
 # hyper to [unsigned] long long
 my $long_legacy = {
-    DomainGetMaxMemory          => { ret => { memory => 1 } },
     DomainGetInfo               => { ret => { maxMem => 1, memory => 1 } },
     DomainMigrate               => { arg => { flags => 1, resource => 1 } },
     DomainMigrate2              => { arg => { flags => 1, resource => 1 } },
index 39fd63dea7d366ceb53dc75f36d83ac6a9f895fe..f03b2fdcc5bbe4287c4485d502439de080e0744b 100644 (file)
@@ -2021,10 +2021,10 @@ static char *testGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) {
     return ret;
 }
 
-static unsigned long testGetMaxMemory(virDomainPtr domain) {
+static unsigned long long testGetMaxMemory(virDomainPtr domain) {
     testConnPtr privconn = domain->conn->privateData;
     virDomainObjPtr privdom;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
 
     testDriverLock(privconn);
     privdom = virDomainFindByName(&privconn->domains,
index 378dffcc3e73b80d5814280bf1127d1660442d22..16818cdc18cf65898a2a2579abad465478b7db1c 100644 (file)
@@ -1625,10 +1625,12 @@ cleanup:
 }
 
 /* Returns max memory in kb, 0 if error */
-static unsigned long umlDomainGetMaxMemory(virDomainPtr dom) {
+static unsigned long long
+umlDomainGetMaxMemory(virDomainPtr dom)
+{
     struct uml_driver *driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
 
     umlDriverLock(driver);
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
index 19ce7dae654370845d4a3c12044623bc6231f6a2..0238ed740d6e478e026efe62b9d47633d6ec894d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xen_driver.c: Unified Xen driver.
  *
- * Copyright (C) 2007-2011 Red Hat, Inc.
+ * Copyright (C) 2007-2012 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -937,12 +937,12 @@ xenUnifiedDomainGetOSType (virDomainPtr dom)
     return NULL;
 }
 
-static unsigned long
+static unsigned long long
 xenUnifiedDomainGetMaxMemory (virDomainPtr dom)
 {
     GET_PRIVATE(dom->conn);
     int i;
-    unsigned long ret;
+    unsigned long long ret;
 
     for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
         if (priv->opened[i] && drivers[i]->xenDomainGetMaxMemory) {
index b5b2328c22ccffb17f7710b3ed6064907d744215..4401b68747efc6646e9f10588dd65ab87a42e73d 100644 (file)
@@ -855,7 +855,7 @@ typedef struct xen_op_v2_dom xen_op_v2_dom;
 # error "unsupported platform"
 #endif
 
-static unsigned long xenHypervisorGetMaxMemory(virDomainPtr domain);
+static unsigned long long xenHypervisorGetMaxMemory(virDomainPtr domain);
 
 struct xenUnifiedDriver xenHypervisorDriver = {
     .xenClose = xenHypervisorClose,
@@ -3157,7 +3157,7 @@ xenHypervisorGetDomMaxMemory(virConnectPtr conn, int id)
  *
  * Returns the memory size in kilobytes or 0 in case of error.
  */
-static unsigned long ATTRIBUTE_NONNULL (1)
+static unsigned long long ATTRIBUTE_NONNULL (1)
 xenHypervisorGetMaxMemory(virDomainPtr domain)
 {
     xenUnifiedPrivatePtr priv;
index 83bfac0f272b68e33811c9b66d9b0895d43e2691..a19d055125ddb2bdd5b3253d4bd7d2366aa70f23 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xend_internal.c: access to Xen though the Xen Daemon interface
  *
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
  *
  *  This file is subject to the terms and conditions of the GNU Lesser General
@@ -1638,32 +1638,32 @@ xenDaemonDomainRestore(virConnectPtr conn, const char *filename)
  *
  * Returns the memory size in kilobytes or 0 in case of error.
  */
-unsigned long
+unsigned long long
 xenDaemonDomainGetMaxMemory(virDomainPtr domain)
 {
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
     struct sexpr *root;
     xenUnifiedPrivatePtr priv;
 
     if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
         virXendError(VIR_ERR_INVALID_ARG, __FUNCTION__);
-        return(-1);
+        return 0;
     }
 
     priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
 
     if (domain->id < 0 && priv->xendConfigVersion < XEND_CONFIG_VERSION_3_0_4)
-        return(-1);
+        return 0;
 
     /* can we ask for a subset ? worth it ? */
     root = sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name);
     if (root == NULL)
-        return(0);
+        return 0;
 
-    ret = (unsigned long) sexpr_u64(root, "domain/memory") << 10;
+    ret = sexpr_u64(root, "domain/memory") << 10;
     sexpr_free(root);
 
-    return(ret);
+    return ret;
 }
 
 
index 3f0b63d6ab05b11b9fb3929eeb4cc0fc9815254c..59427881036ee1b4ffa5becdf35bd2ca4fd33f35 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xend_internal.h
  *
- * Copyright (C) 2006-2008, 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2006-2008, 2010-2012 Red Hat, Inc.
  * Copyright (C) 2005,2006
  *
  *      Anthony Liguori <aliguori@us.ibm.com>
@@ -119,7 +119,7 @@ int xenDaemonDomainGetState(virDomainPtr domain,
                             unsigned int flags);
 char *xenDaemonDomainGetXMLDesc(virDomainPtr domain, unsigned int flags,
                                 const char *cpus);
-unsigned long xenDaemonDomainGetMaxMemory(virDomainPtr domain);
+unsigned long long xenDaemonDomainGetMaxMemory(virDomainPtr domain);
 char **xenDaemonListDomainsOld(virConnectPtr xend);
 
 virDomainPtr xenDaemonDomainDefineXML(virConnectPtr xend, const char *sexpr);
index 5acac8b703c7ae78ff6d70aadb7fe2440798bf75..e966e637d5ea592ee9ab99a5059e9b329f617d7b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xm_internal.h: helper routines for dealing with inactive domains
  *
- * Copyright (C) 2006-2007, 2009-2011 Red Hat, Inc.
+ * Copyright (C) 2006-2007, 2009-2012 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -646,11 +646,11 @@ cleanup:
 /*
  * Get max memory limit from config
  */
-unsigned long xenXMDomainGetMaxMemory(virDomainPtr domain) {
+unsigned long long xenXMDomainGetMaxMemory(virDomainPtr domain) {
     xenUnifiedPrivatePtr priv;
     const char *filename;
     xenXMConfCachePtr entry;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
 
     if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
         xenXMError(VIR_ERR_INVALID_ARG, __FUNCTION__);
index 89af23ee455b0836fab8ca9d978c648c676ec19a..3e1f886e0cd95dff5e37cf7a99dca1fc883f8670 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xm_internal.h: helper routines for dealing with inactive domains
  *
- * Copyright (C) 2006-2007, 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2006-2007, 2010-2012 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -48,7 +48,7 @@ int xenXMDomainGetState(virDomainPtr domain,
 char *xenXMDomainGetXMLDesc(virDomainPtr domain, unsigned int flags);
 int xenXMDomainSetMemory(virDomainPtr domain, unsigned long memory);
 int xenXMDomainSetMaxMemory(virDomainPtr domain, unsigned long memory);
-unsigned long xenXMDomainGetMaxMemory(virDomainPtr domain);
+unsigned long long xenXMDomainGetMaxMemory(virDomainPtr domain);
 int xenXMDomainSetVcpus(virDomainPtr domain, unsigned int vcpus);
 int xenXMDomainSetVcpusFlags(virDomainPtr domain, unsigned int vcpus,
                              unsigned int flags);
index 86e5519b45d765424d136911deb0810b957d9e57..729873fcd5d8e4bd932585611bee33d4638ecb65 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xs_internal.c: access to Xen Store
  *
- * Copyright (C) 2006, 2009-2011 Red Hat, Inc.
+ * Copyright (C) 2006, 2009-2012 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -495,23 +495,23 @@ xenStoreDomainSetMemory(virDomainPtr domain, unsigned long memory)
  *
  * Returns the memory size in kilobytes or 0 in case of error.
  */
-unsigned long
+unsigned long long
 xenStoreDomainGetMaxMemory(virDomainPtr domain)
 {
     char *tmp;
-    unsigned long ret = 0;
+    unsigned long long ret = 0;
     xenUnifiedPrivatePtr priv;
 
     if (!VIR_IS_CONNECTED_DOMAIN(domain))
         return (ret);
     if (domain->id == -1)
-        return(-1);
+        return 0;
 
     priv = domain->conn->privateData;
     xenUnifiedLock(priv);
     tmp = virDomainDoStoreQuery(domain->conn, domain->id, "memory/target");
     if (tmp != NULL) {
-        ret = (unsigned long) atol(tmp);
+        ret = atol(tmp);
         VIR_FREE(tmp);
     }
     xenUnifiedUnlock(priv);
index f7e487b3be2b023973e9e53fcc02d88641615392..1ada0f3a152e0de003ea936d36d0902833723ba1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * xs_internal.h: internal API for access to XenStore
  *
- * Copyright (C) 2006, 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2006, 2010-2012 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -36,7 +36,7 @@ virDomainPtr  xenStoreLookupByName(virConnectPtr conn,
 unsigned long  xenStoreGetMaxMemory    (virDomainPtr domain);
 int            xenStoreDomainSetMemory (virDomainPtr domain,
                                          unsigned long memory);
-unsigned long  xenStoreDomainGetMaxMemory(virDomainPtr domain);
+unsigned long long xenStoreDomainGetMaxMemory(virDomainPtr domain);
 int            xenStoreDomainShutdown  (virDomainPtr domain);
 int            xenStoreDomainReboot    (virDomainPtr domain,
                                          unsigned int flags);
index 94644ae025cd75bf6ad413a9c7a4f59b5fd75bdf..298db1220f1b498c3e094badf851be38efa2f0d8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * xenapi_driver.c: Xen API driver.
- * Copyright (C) 2011 Red Hat, Inc.
+ * Copyright (C) 2011-2012 Red Hat, Inc.
  * Copyright (C) 2009, 2010 Citrix Ltd.
  *
  * This library is free software; you can redistribute it and/or
@@ -955,7 +955,7 @@ xenapiDomainGetOSType (virDomainPtr dom)
  * Returns maximum static memory for VM on success
  * or 0 in case of error
  */
-static unsigned long
+static unsigned long long
 xenapiDomainGetMaxMemory (virDomainPtr dom)
 {
     int64_t mem_static_max = 0;
@@ -972,7 +972,7 @@ xenapiDomainGetMaxMemory (virDomainPtr dom)
         vm = vms->contents[0];
         xen_vm_get_memory_static_max(session, &mem_static_max, vm);
         xen_vm_set_free(vms);
-        return (unsigned long)(mem_static_max / 1024);
+        return (mem_static_max / 1024);
     } else {
         if (vms) xen_vm_set_free(vms);
         xenapiSessionErrorHandler(dom->conn, VIR_ERR_NO_DOMAIN, NULL);