use qemu_memalign for all cpu structs

Some structs, specically CPUARMState is 16-bytes aligned.

This causes segment fault because gcc tends to vectorize

the assignment of the struct with infamous movaps tricks.

Without this patch, we fail on manylinux with 2.17 glibc

in release mode in i686.

qemu_memalign will ensure the alignment across platforms.
This commit is contained in:
mio
2024-10-17 13:34:31 +08:00
parent e8ca3cbea5
commit ffeddd7579
11 changed files with 22 additions and 12 deletions

View File

@@ -2102,10 +2102,12 @@ ARMCPU *cpu_arm_init(struct uc_struct *uc)
CPUClass *cc; CPUClass *cc;
CPUARMState *env; CPUARMState *env;
cpu = calloc(1, sizeof(*cpu)); // cpu->env is 16 bytes aligned
cpu = qemu_memalign(16, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
#if !defined(TARGET_AARCH64) #if !defined(TARGET_AARCH64)
if (uc->mode & UC_MODE_MCLASS) { if (uc->mode & UC_MODE_MCLASS) {

View File

@@ -325,10 +325,12 @@ ARMCPU *cpu_aarch64_init(struct uc_struct *uc)
CPUClass *cc; CPUClass *cc;
CPUARMState *env; CPUARMState *env;
cpu = calloc(1, sizeof(*cpu)); // cpu->env is 16 bytes alignment
cpu = qemu_memalign(16, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {
uc->cpu_model = UC_CPU_ARM64_A72; uc->cpu_model = UC_CPU_ARM64_A72;

View File

@@ -5076,10 +5076,11 @@ X86CPU *cpu_x86_init(struct uc_struct *uc)
CPUClass *cc; CPUClass *cc;
X86CPUClass *xcc; X86CPUClass *xcc;
cpu = calloc(1, sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {
#ifdef TARGET_X86_64 #ifdef TARGET_X86_64

View File

@@ -265,10 +265,11 @@ M68kCPU *cpu_m68k_init(struct uc_struct *uc)
CPUState *cs; CPUState *cs;
CPUClass *cc; CPUClass *cc;
cpu = calloc(1, sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {
uc->cpu_model = UC_CPU_M68K_CFV4E; // cfv4e uc->cpu_model = UC_CPU_M68K_CFV4E; // cfv4e

View File

@@ -157,10 +157,11 @@ MIPSCPU *cpu_mips_init(struct uc_struct *uc)
CPUClass *cc; CPUClass *cc;
CPUMIPSState *env; CPUMIPSState *env;
cpu = calloc(1, sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
#ifdef TARGET_MIPS64 #ifdef TARGET_MIPS64
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {

View File

@@ -11016,7 +11016,7 @@ PowerPCCPU *cpu_ppc_init(struct uc_struct *uc)
CPUClass *cc; CPUClass *cc;
PowerPCCPUClass *pcc; PowerPCCPUClass *pcc;
cpu = malloc(sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }

View File

@@ -335,10 +335,11 @@ RISCVCPU *cpu_riscv_init(struct uc_struct *uc)
CPUState *cs; CPUState *cs;
CPUClass *cc; CPUClass *cc;
cpu = calloc(1, sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
#ifdef TARGET_RISCV32 #ifdef TARGET_RISCV32
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {

View File

@@ -245,10 +245,11 @@ S390CPU *cpu_s390_init(struct uc_struct *uc, const char *cpu_model)
CPUClass *cc; CPUClass *cc;
// int i; // int i;
cpu = calloc(1, sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {
uc->cpu_model = UC_CPU_S390X_QEMU; // qemu-s390x-cpu uc->cpu_model = UC_CPU_S390X_QEMU; // qemu-s390x-cpu

View File

@@ -517,11 +517,11 @@ SPARCCPU *cpu_sparc_init(struct uc_struct *uc)
CPUClass *cc; CPUClass *cc;
SPARCCPUClass *scc; SPARCCPUClass *scc;
cpu = malloc(sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset(cpu, 0, sizeof(*cpu)); memset((void*)cpu, 0, sizeof(*cpu));
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {
#ifdef TARGET_SPARC64 #ifdef TARGET_SPARC64

View File

@@ -165,10 +165,11 @@ TriCoreCPU *cpu_tricore_init(struct uc_struct *uc)
CPUState *cs; CPUState *cs;
CPUClass *cc; CPUClass *cc;
cpu = calloc(1, sizeof(*cpu)); cpu = qemu_memalign(8, sizeof(*cpu));
if (cpu == NULL) { if (cpu == NULL) {
return NULL; return NULL;
} }
memset((void*)cpu, 0, sizeof(*cpu));
if (uc->cpu_model == INT_MAX) { if (uc->cpu_model == INT_MAX) {
uc->cpu_model = 2; // tc27x uc->cpu_model = 2; // tc27x

2
uc.c
View File

@@ -515,7 +515,7 @@ uc_err uc_close(uc_engine *uc)
g_free(uc->cpu->thread); g_free(uc->cpu->thread);
/* cpu */ /* cpu */
free(uc->cpu); qemu_vfree(uc->cpu);
/* flatviews */ /* flatviews */
g_hash_table_destroy(uc->flat_views); g_hash_table_destroy(uc->flat_views);