Skip to contentSkip to conversation

Limit type names to alphanumerical and some few special characters

PATCH Superseded Thomas Huth 5 participants 13 messages Nov 16, 2023 Arm/Zynq architecture

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Limit type names to alphanumerical and some few special characters

QOM names currently don't have any enforced naming rules. This can be problematic, e.g. when they are used on the command line for the "-device" option (where the comma is used to separate properties). To avoid that such problematic type names come in again, let's restrict the set of acceptable characters during the type registration.

First four patches are clean-ups related to comma and colons in type names, and the final patch introduces the check for valid names.

v2:
- Include Markus' patches in the series
- Add patches to clean up colons in type names
- Add the check to type_register_internal() instead of type_new() so that we can disallow colons, too
- Only allow '+' in legacy names

Markus Armbruster (2):
docs/system/arm: Fix for rename of type "xlnx.bbram-ctrl"
hw: Replace anti-social QOM type names (again)

Thomas Huth (3):
memory: Remove "qemu:" prefix from the "qemu:ram-discard-manager" type
name
tests/unit/test-io-task: Rename "qemu:dummy" to avoid colon in the
name
qom/object: Limit type names to alphanumerical and some few special
characters

 docs/system/arm/xlnx-versal-virt.rst     |  4 +--
 include/exec/memory.h                    |  2 +-
 include/hw/misc/xlnx-versal-cframe-reg.h |  2 +-
 include/hw/misc/xlnx-versal-cfu.h        |  6 ++--
 include/hw/misc/xlnx-versal-crl.h        |  2 +-
 include/hw/nvram/xlnx-efuse.h            |  2 +-
 include/hw/nvram/xlnx-versal-efuse.h     |  4 +--
 include/hw/nvram/xlnx-zynqmp-efuse.h     |  2 +-
 qom/object.c                             | 41 ++++++++++++++++++++++++
 tests/unit/test-io-task.c                |  2 +-
 10 files changed, 54 insertions(+), 13 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/5 docs/system/arm: Fix for rename of type "xlnx.bbram-ctrl"

From: Markus Armbruster <[email protected]>

Fixes b65b4b7ae3c8 (xlnx-bbram: hw/nvram: Use dot in device type name)
[thuth: Use longhand syntax to avoid problems with the "." in the name]
---
 docs/system/arm/xlnx-versal-virt.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
docs/system/arm/xlnx-versal-virt.rst +1 -1
index d2d1b26692..9a4b2ff55f 100644
@@ -194,7 +194,7 @@ To use a different index value, N, from default of 0, add:
 
 .. code-block:: bash
 
-  -global xlnx,bbram-ctrl.drive-index=N
+  -global driver=xlnx.bbram-ctrl,property=drive-index,value=N
 
 eFUSE File Backend
 """"""""""""""""""
Signature · 1 line
2.41.0
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] docs/system/arm: Fix for rename of type "xlnx.bbram-ctrl"

Quoted 8 lines · Thomas Huth

From: Markus Armbruster <[email protected]>

Fixes b65b4b7ae3c8 (xlnx-bbram: hw/nvram: Use dot in device type name)
[thuth: Use longhand syntax to avoid problems with the "." in the name]

---

thanks
-- PMM

#3 lore raw

PATCH v2 2/5 hw: Replace anti-social QOM type names (again)

In reply to Thomas Huth

From: Markus Armbruster <[email protected]>

QOM type names containing ',' result in awful UI. We got rid of them in v6.0.0 (commit e178113ff64 hw: Replace anti-social QOM type names). A few have crept back since:

xlnx,cframe-reg
xlnx,efuse
xlnx,pmc-efuse-cache
xlnx,versal-cfu-apb
xlnx,versal-cfu-fdro
xlnx,versal-cfu-sfr
xlnx,versal-crl
xlnx,versal-efuse
xlnx,zynqmp-efuse

These are all device types. They can't be plugged with -device / device_add, except for "xlnx,efuse" (I'm not sure that one is intentional).

They *can* be used with -device / device_add to request help. Usability is poor, though: you have to double the comma, like this:

    $ qemu-system-aarch64 -device xlnx,,pmc-efuse-cache,help

They can also be used with -global, where you must *not* double the comma:

    $ qemu-system-aarch64 -global xlnx,efuse.drive-index=2

Trap for the unwary.

"xlnx,efuse", "xlnx,versal-efuse", "xlnx,pmc-efuse-cache", "xlnx-zynqmp-efuse" are from v6.2.0, "xlnx,versal-crl" is from v7.1.0, and the remainder are new.

Rename them all to "xlnx-FOO", like commit e178113ff64 did.

---
 docs/system/arm/xlnx-versal-virt.rst     | 2 +-
 include/hw/misc/xlnx-versal-cframe-reg.h | 2 +-
 include/hw/misc/xlnx-versal-cfu.h        | 6 +++---
 include/hw/misc/xlnx-versal-crl.h        | 2 +-
 include/hw/nvram/xlnx-efuse.h            | 2 +-
 include/hw/nvram/xlnx-versal-efuse.h     | 4 ++--
 include/hw/nvram/xlnx-zynqmp-efuse.h     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
docs/system/arm/xlnx-versal-virt.rst +1 -1
index 9a4b2ff55f..0bafc76469 100644
@@ -212,7 +212,7 @@ To use a different index value, N, from default of 1, add:
 
 .. code-block:: bash
 
-  -global xlnx,efuse.drive-index=N
+  -global xlnx-efuse.drive-index=N
 
 .. warning::
   In actual physical Versal, BBRAM and eFUSE contain sensitive data.
include/hw/misc/xlnx-versal-cframe-reg.h +1 -1
index a14fbd7fe4..f403b00e31 100644
@@ -23,7 +23,7 @@
 #include "hw/misc/xlnx-versal-cfu.h"
 #include "qemu/fifo32.h"
 
-#define TYPE_XLNX_VERSAL_CFRAME_REG "xlnx,cframe-reg"
+#define TYPE_XLNX_VERSAL_CFRAME_REG "xlnx-cframe-reg"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalCFrameReg, XLNX_VERSAL_CFRAME_REG)
 
 #define TYPE_XLNX_VERSAL_CFRAME_BCAST_REG "xlnx.cframe-bcast-reg"
include/hw/misc/xlnx-versal-cfu.h +3 -3
index 86fb841053..8c581c0797 100644
@@ -22,13 +22,13 @@
 #include "hw/misc/xlnx-cfi-if.h"
 #include "qemu/fifo32.h"
 
-#define TYPE_XLNX_VERSAL_CFU_APB "xlnx,versal-cfu-apb"
+#define TYPE_XLNX_VERSAL_CFU_APB "xlnx-versal-cfu-apb"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalCFUAPB, XLNX_VERSAL_CFU_APB)
 
-#define TYPE_XLNX_VERSAL_CFU_FDRO "xlnx,versal-cfu-fdro"
+#define TYPE_XLNX_VERSAL_CFU_FDRO "xlnx-versal-cfu-fdro"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalCFUFDRO, XLNX_VERSAL_CFU_FDRO)
 
-#define TYPE_XLNX_VERSAL_CFU_SFR "xlnx,versal-cfu-sfr"
+#define TYPE_XLNX_VERSAL_CFU_SFR "xlnx-versal-cfu-sfr"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalCFUSFR, XLNX_VERSAL_CFU_SFR)
 
 REG32(CFU_ISR, 0x0)
include/hw/misc/xlnx-versal-crl.h +1 -1
index 2857f4169a..dfb8dff197 100644
@@ -13,7 +13,7 @@
 #include "hw/register.h"
 #include "target/arm/cpu.h"
 
-#define TYPE_XLNX_VERSAL_CRL "xlnx,versal-crl"
+#define TYPE_XLNX_VERSAL_CRL "xlnx-versal-crl"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalCRL, XLNX_VERSAL_CRL)
 
 REG32(ERR_CTRL, 0x0)
include/hw/nvram/xlnx-efuse.h +1 -1
index 58414e468b..cff7924106 100644
@@ -30,7 +30,7 @@
 #include "sysemu/block-backend.h"
 #include "hw/qdev-core.h"
 
-#define TYPE_XLNX_EFUSE "xlnx,efuse"
+#define TYPE_XLNX_EFUSE "xlnx-efuse"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxEFuse, XLNX_EFUSE);
 
 struct XlnxEFuse {
include/hw/nvram/xlnx-versal-efuse.h +2 -2
index a873dc5cb0..86e2261b9a 100644
@@ -29,8 +29,8 @@
 
 #define XLNX_VERSAL_EFUSE_CTRL_R_MAX ((0x100 / 4) + 1)
 
-#define TYPE_XLNX_VERSAL_EFUSE_CTRL  "xlnx,versal-efuse"
-#define TYPE_XLNX_VERSAL_EFUSE_CACHE "xlnx,pmc-efuse-cache"
+#define TYPE_XLNX_VERSAL_EFUSE_CTRL  "xlnx-versal-efuse"
+#define TYPE_XLNX_VERSAL_EFUSE_CACHE "xlnx-pmc-efuse-cache"
 
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalEFuseCtrl, XLNX_VERSAL_EFUSE_CTRL);
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxVersalEFuseCache, XLNX_VERSAL_EFUSE_CACHE);
include/hw/nvram/xlnx-zynqmp-efuse.h +1 -1
index 6b051ec4f1..f5beacc2e6 100644
@@ -29,7 +29,7 @@
 
 #define XLNX_ZYNQMP_EFUSE_R_MAX ((0x10fc / 4) + 1)
 
-#define TYPE_XLNX_ZYNQMP_EFUSE "xlnx,zynqmp-efuse"
+#define TYPE_XLNX_ZYNQMP_EFUSE "xlnx-zynqmp-efuse"
 OBJECT_DECLARE_SIMPLE_TYPE(XlnxZynqMPEFuse, XLNX_ZYNQMP_EFUSE);
 
 struct XlnxZynqMPEFuse {
Signature · 1 line
2.41.0
#4 lore raw

PATCH v2 3/5 memory: Remove "qemu:" prefix from the "qemu:ram-discard-manager" type name

In reply to Thomas Huth

Type names should not contain special characters like ":". Let's remove the whole prefix here since it does not really seem to be helpful to have such a prefix here. The type name is only used internally for an interface, so the renaming should not affect the user interface or migration.

---
 include/exec/memory.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/exec/memory.h +1 -1
index 831f7c996d..f172e82ac9 100644
@@ -43,7 +43,7 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;
 DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass,
                      IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION)
 
-#define TYPE_RAM_DISCARD_MANAGER "qemu:ram-discard-manager"
+#define TYPE_RAM_DISCARD_MANAGER "ram-discard-manager"
 typedef struct RamDiscardManagerClass RamDiscardManagerClass;
 typedef struct RamDiscardManager RamDiscardManager;
 DECLARE_OBJ_CHECKERS(RamDiscardManager, RamDiscardManagerClass,
Signature · 1 line
2.41.0
Reviewed-by on v2 3/5
#5 lore raw

Re: [PATCH v2 3/5] memory: Remove "qemu:" prefix from the "qemu:ram-discard-manager" type name

Quoted 16 lines · Thomas Huth

Type names should not contain special characters like ":". Let's remove the whole prefix here since it does not really seem to be helpful to have such a prefix here. The type name is only used internally for an interface, so the renaming should not affect the user interface or migration.

---
  include/exec/memory.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/exec/memory.h +0 -0
index 831f7c996d..f172e82ac9 100644

include/exec/memory.h typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass; · line 49

Quoted 8 lines
include/exec/memory.h +1 -1
@@ -43,7 +43,7 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;
  DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass,
                       IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION)
 
-#define TYPE_RAM_DISCARD_MANAGER "qemu:ram-discard-manager"
+#define TYPE_RAM_DISCARD_MANAGER "ram-discard-manager"
  typedef struct RamDiscardManagerClass RamDiscardManagerClass;
  typedef struct RamDiscardManager RamDiscardManager;
  DECLARE_OBJ_CHECKERS(RamDiscardManager, RamDiscardManagerClass,
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 3/5
#6 lore raw

Re: [PATCH v2 3/5] memory: Remove "qemu:" prefix from the "qemu:ram-discard-manager" type name

In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

Type names should not contain special characters like ":". Let's remove the whole prefix here since it does not really seem to be helpful to have such a prefix here. The type name is only used internally for an interface, so the renaming should not affect the user interface or migration.

---
  include/exec/memory.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#7 lore raw

PATCH v2 4/5 tests/unit/test-io-task: Rename "qemu:dummy" to avoid colon in the name

In reply to Thomas Huth

Type names should not contain special characters like ":" (so that they are easier to use with QAPI and other parts). We are going to forbid such names in an upcoming patch. Thus let's replace the ":" here with a "-".

---
 tests/unit/test-io-task.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/unit/test-io-task.c +1 -1
index 953a50ae66..115dba8970 100644
@@ -25,7 +25,7 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
-#define TYPE_DUMMY "qemu:dummy"
+#define TYPE_DUMMY "qemu-dummy"
 
 typedef struct DummyObject DummyObject;
 typedef struct DummyObjectClass DummyObjectClass;
Signature · 1 line
2.41.0
Reviewed-by on v2 4/5
#8 lore raw

Re: [PATCH v2 4/5] tests/unit/test-io-task: Rename "qemu:dummy" to avoid colon in the name

Quoted 9 lines · Thomas Huth

Type names should not contain special characters like ":" (so that they are easier to use with QAPI and other parts). We are going to forbid such names in an upcoming patch. Thus let's replace the ":" here with a "-".

---
 tests/unit/test-io-task.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v2 4/5
#9 lore raw

Re: [PATCH v2 4/5] tests/unit/test-io-task: Rename "qemu:dummy" to avoid colon in the name

In reply to Thomas Huth
Quoted 9 lines · Thomas Huth

Type names should not contain special characters like ":" (so that they are easier to use with QAPI and other parts). We are going to forbid such names in an upcoming patch. Thus let's replace the ":" here with a "-".

---
  tests/unit/test-io-task.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#10 lore raw

PATCH v2 5/5 qom/object: Limit type names to alphanumerical and some few special characters

In reply to Thomas Huth

QOM names currently don't have any enforced naming rules. This can be problematic, e.g. when they are used on the command line for the "-device" option (where the comma is used to separate properties). To avoid that such problematic type names come in again, let's restrict the set of acceptable characters during the type registration.

Ideally, we'd apply here the same rules as for QAPI, i.e. all type names should begin with a letter, and contain only ASCII letters, digits, hyphen, and underscore. However, we already have so many pre-existing types like:

    486-x86_64-cpu
    cfi.pflash01
    power5+_v2.1-spapr-cpu-core
    virt-2.6-machine
    pc-i440fx-3.0-machine

... so that we have to allow "." and "+" for now, too. While the dot is used in a lot of places, the "+" can fortunately be limited to two classes of legacy names ("power" and "Sun-UltraSparc" CPUs).

We also cannot enforce the rule that names must start with a letter yet, since there are lot of types that start with a digit. Still, at least limiting the first characters to the alphanumerical range should be way better than nothing.

---
 qom/object.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
qom/object.c +41 -0
index 95c0dc8285..571ef68950 100644
@@ -138,9 +138,50 @@ static TypeImpl *type_new(const TypeInfo *info)
     return ti;
 }
 
+static bool type_name_is_valid(const char *name)
+{
+    const int slen = strlen(name);
+
+    g_assert(slen > 1);
+
+    /*
+     * Ideally, the name should start with a letter - however, we've got
+     * too many names starting with a digit already, so allow digits here,
+     * too (except '0' which is not used yet)
+     */
+    if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
+        return false;
+    }
+
+    for (int i = 1; i < slen; i++) {
+        if (name[i] != '-' && name[i] != '_' && name[i] != '.' &&
+            !g_ascii_isalnum(name[i])) {
+            if (name[i] == '+') {
+                if (i == 6 && !strncmp(name, "power", 5)) {
+                    /* It's a legacy name like "power5+" */
+                    continue;
+                }
+                if (i >= 17 && !strncmp(name, "Sun-UltraSparc", 14)) {
+                    /* It's a legacy name like "Sun-UltraSparc-IV+" */
+                    continue;
+                }
+            }
+            return false;
+        }
+    }
+
+    return true;
+}
+
 static TypeImpl *type_register_internal(const TypeInfo *info)
 {
     TypeImpl *ti;
+
+    if (!type_name_is_valid(info->name)) {
+        fprintf(stderr, "Registering '%s' with illegal type name\n", info->name);
+        abort();
+    }
+
     ti = type_new(info);
 
     type_table_add(ti);
Signature · 1 line
2.41.0
#11 lore raw

Re: [PATCH v2 5/5] qom/object: Limit type names to alphanumerical and some few special characters

Quoted 61 lines · Thomas Huth

QOM names currently don't have any enforced naming rules. This can be problematic, e.g. when they are used on the command line for the "-device" option (where the comma is used to separate properties). To avoid that such problematic type names come in again, let's restrict the set of acceptable characters during the type registration.

Ideally, we'd apply here the same rules as for QAPI, i.e. all type names should begin with a letter, and contain only ASCII letters, digits, hyphen, and underscore. However, we already have so many pre-existing types like:

    486-x86_64-cpu
    cfi.pflash01
    power5+_v2.1-spapr-cpu-core
    virt-2.6-machine
    pc-i440fx-3.0-machine

... so that we have to allow "." and "+" for now, too. While the dot is used in a lot of places, the "+" can fortunately be limited to two classes of legacy names ("power" and "Sun-UltraSparc" CPUs).

We also cannot enforce the rule that names must start with a letter yet, since there are lot of types that start with a digit. Still, at least limiting the first characters to the alphanumerical range should be way better than nothing.

---
 qom/object.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
qom/object.c +21 -0
index 95c0dc8285..571ef68950 100644
@@ -138,9 +138,50 @@ static TypeImpl *type_new(const TypeInfo *info)
     return ti;
 }
 
+static bool type_name_is_valid(const char *name)
+{
+    const int slen = strlen(name);
+
+    g_assert(slen > 1);
+
+    /*
+     * Ideally, the name should start with a letter - however, we've got
+     * too many names starting with a digit already, so allow digits here,
+     * too (except '0' which is not used yet)
+     */
+    if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
+        return false;
+    }
+
+    for (int i = 1; i < slen; i++) {
+        if (name[i] != '-' && name[i] != '_' && name[i] != '.' &&
+            !g_ascii_isalnum(name[i])) {
+            if (name[i] == '+') {
+                if (i == 6 && !strncmp(name, "power", 5)) {
+                    /* It's a legacy name like "power5+" */

qom/object.c static TypeImpl *type_new(const TypeInfo *info) · line 171

Quoted 10 lines
qom/object.c +10 -0
+                    continue;
+                }
+                if (i >= 17 && !strncmp(name, "Sun-UltraSparc", 14)) {
+                    /* It's a legacy name like "Sun-UltraSparc-IV+" */
+                    continue;
+                }
+            }
+            return false;
+        }
+    }

Replace this big loop with strspn, which has an asm optimized impl in glibc

      ALPHA_LC "abcdefghijklmnopqrstuvwxyz"
      ALPHA_UC "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      OTHER "0123456789-_."

      return (strspn(name, ALPHA_UC ALPHA_LC OTHER) == slen) ||
          (g_str_has_prefix(name, "power") && slen > 6 && name[6] == '+') ||
	  (g_str_has_prefix(name, "Sun-UltraSparc") && slen > 17 && name[17] == '+');

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#12 lore raw

Re: [PATCH v2 5/5] qom/object: Limit type names to alphanumerical and some few special characters

Quoted 76 lines · Daniel P. Berrangé
Quoted 61 lines · Thomas Huth

QOM names currently don't have any enforced naming rules. This can be problematic, e.g. when they are used on the command line for the "-device" option (where the comma is used to separate properties). To avoid that such problematic type names come in again, let's restrict the set of acceptable characters during the type registration.

Ideally, we'd apply here the same rules as for QAPI, i.e. all type names should begin with a letter, and contain only ASCII letters, digits, hyphen, and underscore. However, we already have so many pre-existing types like:

     486-x86_64-cpu
     cfi.pflash01
     power5+_v2.1-spapr-cpu-core
     virt-2.6-machine
     pc-i440fx-3.0-machine

... so that we have to allow "." and "+" for now, too. While the dot is used in a lot of places, the "+" can fortunately be limited to two classes of legacy names ("power" and "Sun-UltraSparc" CPUs).

We also cannot enforce the rule that names must start with a letter yet, since there are lot of types that start with a digit. Still, at least limiting the first characters to the alphanumerical range should be way better than nothing.

---
  qom/object.c | 41 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 41 insertions(+)
qom/object.c +21 -0
index 95c0dc8285..571ef68950 100644
@@ -138,9 +138,50 @@ static TypeImpl *type_new(const TypeInfo *info)
      return ti;
  }
 
+static bool type_name_is_valid(const char *name)
+{
+    const int slen = strlen(name);
+
+    g_assert(slen > 1);
+
+    /*
+     * Ideally, the name should start with a letter - however, we've got
+     * too many names starting with a digit already, so allow digits here,
+     * too (except '0' which is not used yet)
+     */
+    if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
+        return false;
+    }
+
+    for (int i = 1; i < slen; i++) {
+        if (name[i] != '-' && name[i] != '_' && name[i] != '.' &&
+            !g_ascii_isalnum(name[i])) {
+            if (name[i] == '+') {
+                if (i == 6 && !strncmp(name, "power", 5)) {
+                    /* It's a legacy name like "power5+" */

qom/object.c static TypeImpl *type_new(const TypeInfo *info) · line 171

Quoted 10 lines
qom/object.c +10 -0
+                    continue;
+                }
+                if (i >= 17 && !strncmp(name, "Sun-UltraSparc", 14)) {
+                    /* It's a legacy name like "Sun-UltraSparc-IV+" */
+                    continue;
+                }
+            }
+            return false;
+        }
+    }

Replace this big loop with strspn, which has an asm optimized impl in glibc

Quoted 7 lines
       ALPHA_LC "abcdefghijklmnopqrstuvwxyz"
       ALPHA_UC "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       OTHER "0123456789-_."

       return (strspn(name, ALPHA_UC ALPHA_LC OTHER) == slen) ||
           (g_str_has_prefix(name, "power") && slen > 6 && name[6] == '+') ||
	  (g_str_has_prefix(name, "Sun-UltraSparc") && slen > 17 && name[17] == '+');

It's quite hard to believe that a function that has to check each and every character in a string of acceptable characters is faster than a function that uses something like g_ascii_asalnum which can check range of characters in one go...

So I gave it a try, wrote two test programs, one with my implementation and one with yours, and looped on the function 1000000000 times. And indeed, for short strings (less than 30 characters), my function is about three times faster than the one with strspn() (mine takes ~ 13 seconds, the strspn() one takes ~ 39 seconds).

Interestingly, for larger strings (more than 140 characters), the strspn() impementation starts to perform better. They indeed must have an optimization that kicks in for larger strings.

Now while my implementation seems to be a little bit faster for the strings that we are using in QEMU, we certainly don't have 1000000000 different types in QEMU, but rather only 1300 or so, so the performance shouldn't really matter that much here. And I have to admit that your code is indeed more compact to read, so I'll give it a try.

Thomas