Skip to content

Commit

Permalink
Declare global arch arrays with contents (capstone-engine#1171)
Browse files Browse the repository at this point in the history
This eliminates the need for archs_enable() and eliminates the racey
initialization.

This makes the architecture-specific init, option, and destroy functions
non-static so that they may be called from a different file.
  • Loading branch information
tmfink authored and aquynh committed Jun 21, 2018
1 parent 7723175 commit 853a287
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 156 deletions.
18 changes: 4 additions & 14 deletions arch/AArch64/AArch64Module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include "AArch64Disassembler.h"
#include "AArch64InstPrinter.h"
#include "AArch64Mapping.h"
#include "AArch64Module.h"

static cs_err init(cs_struct *ud)
cs_err AArch64_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));
Expand All @@ -28,7 +29,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err AArch64_option(cs_struct *handle, cs_opt_type type, size_t value)
{
if (type == CS_OPT_MODE) {
handle->mode = (cs_mode)value;
Expand All @@ -37,19 +38,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void AArch64_destroy(cs_struct *handle)
{
}

void AArch64_enable(void)
{
cs_arch_init[CS_ARCH_ARM64] = init;
cs_arch_option[CS_ARCH_ARM64] = option;
cs_arch_destroy[CS_ARCH_ARM64] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_ARM64] = ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN);

// support this arch
all_arch |= (1 << CS_ARCH_ARM64);
}

#endif
13 changes: 13 additions & 0 deletions arch/AArch64/AArch64Module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_AARCH64_MODULE_H
#define CS_AARCH64_MODULE_H

#include "../../utils.h"

cs_err AArch64_global_init(cs_struct *ud);
cs_err AArch64_option(cs_struct *handle, cs_opt_type type, size_t value);
void AArch64_destroy(cs_struct *handle);

#endif
20 changes: 4 additions & 16 deletions arch/ARM/ARMModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include "ARMDisassembler.h"
#include "ARMInstPrinter.h"
#include "ARMMapping.h"
#include "ARMModule.h"

static cs_err init(cs_struct *ud)
cs_err ARM_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));
Expand All @@ -33,7 +34,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err ARM_option(cs_struct *handle, cs_opt_type type, size_t value)
{
switch(type) {
case CS_OPT_MODE:
Expand All @@ -56,21 +57,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void ARM_destroy(cs_struct *handle)
{
}

void ARM_enable(void)
{
cs_arch_init[CS_ARCH_ARM] = init;
cs_arch_option[CS_ARCH_ARM] = option;
cs_arch_destroy[CS_ARCH_ARM] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_ARM] = ~(CS_MODE_LITTLE_ENDIAN |
CS_MODE_ARM | CS_MODE_V8 | CS_MODE_MCLASS | CS_MODE_THUMB |
CS_MODE_BIG_ENDIAN);

// support this arch
all_arch |= (1 << CS_ARCH_ARM);
}

#endif
13 changes: 13 additions & 0 deletions arch/ARM/ARMModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_ARM_MODULE_H
#define CS_ARM_MODULE_H

#include "../../utils.h"

cs_err ARM_global_init(cs_struct *ud);
cs_err ARM_option(cs_struct *handle, cs_opt_type type, size_t value);
void ARM_destroy(cs_struct *handle);

#endif
20 changes: 4 additions & 16 deletions arch/Mips/MipsModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "MipsDisassembler.h"
#include "MipsInstPrinter.h"
#include "MipsMapping.h"
#include "MipsModule.h"

// Returns mode value with implied bits set
static inline cs_mode updated_mode(cs_mode mode)
Expand All @@ -19,7 +20,7 @@ static inline cs_mode updated_mode(cs_mode mode)
return mode;
}

static cs_err init(cs_struct *ud)
cs_err Mips_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));
Expand All @@ -42,7 +43,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err Mips_option(cs_struct *handle, cs_opt_type type, size_t value)
{
if (type == CS_OPT_MODE) {
value = updated_mode((cs_mode)value);
Expand All @@ -56,21 +57,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void Mips_destroy(cs_struct *handle)
{
}

void Mips_enable(void)
{
cs_arch_init[CS_ARCH_MIPS] = init;
cs_arch_option[CS_ARCH_MIPS] = option;
cs_arch_destroy[CS_ARCH_MIPS] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_MIPS] = ~(CS_MODE_LITTLE_ENDIAN |
CS_MODE_32 | CS_MODE_64 | CS_MODE_MICRO | CS_MODE_MIPS32R6 |
CS_MODE_MIPSGP64 | CS_MODE_BIG_ENDIAN);

// support this arch
all_arch |= (1 << CS_ARCH_MIPS);
}

#endif
13 changes: 13 additions & 0 deletions arch/Mips/MipsModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_MIPS_MODULE_H
#define CS_MIPS_MODULE_H

#include "../../utils.h"

cs_err Mips_global_init(cs_struct *ud);
cs_err Mips_option(cs_struct *handle, cs_opt_type type, size_t value);
void Mips_destroy(cs_struct *handle);

#endif
19 changes: 4 additions & 15 deletions arch/PowerPC/PPCModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include "PPCDisassembler.h"
#include "PPCInstPrinter.h"
#include "PPCMapping.h"
#include "PPCModule.h"

static cs_err init(cs_struct *ud)
cs_err PPC_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = (MCRegisterInfo *) cs_mem_malloc(sizeof(*mri));
Expand All @@ -29,7 +30,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err PPC_option(cs_struct *handle, cs_opt_type type, size_t value)
{
if (type == CS_OPT_SYNTAX)
handle->syntax = (int) value;
Expand All @@ -41,20 +42,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void PPC_destroy(cs_struct *handle)
{
}

void PPC_enable(void)
{
cs_arch_init[CS_ARCH_PPC] = init;
cs_arch_option[CS_ARCH_PPC] = option;
cs_arch_destroy[CS_ARCH_PPC] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_PPC] = ~(CS_MODE_LITTLE_ENDIAN |
CS_MODE_32 | CS_MODE_64 | CS_MODE_BIG_ENDIAN);

// support this arch
all_arch |= (1 << CS_ARCH_PPC);
}

#endif
13 changes: 13 additions & 0 deletions arch/PowerPC/PPCModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_POWERPC_MODULE_H
#define CS_POWERPC_MODULE_H

#include "../../utils.h"

cs_err PPC_global_init(cs_struct *ud);
cs_err PPC_option(cs_struct *handle, cs_opt_type type, size_t value);
void PPC_destroy(cs_struct *handle);

#endif
19 changes: 4 additions & 15 deletions arch/Sparc/SparcModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include "SparcDisassembler.h"
#include "SparcInstPrinter.h"
#include "SparcMapping.h"
#include "SparcModule.h"

static cs_err init(cs_struct *ud)
cs_err Sparc_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));
Expand All @@ -29,7 +30,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err Sparc_option(cs_struct *handle, cs_opt_type type, size_t value)
{
if (type == CS_OPT_SYNTAX)
handle->syntax = (int) value;
Expand All @@ -41,20 +42,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void Sparc_destroy(cs_struct *handle)
{
}

void Sparc_enable(void)
{
cs_arch_init[CS_ARCH_SPARC] = init;
cs_arch_option[CS_ARCH_SPARC] = option;
cs_arch_destroy[CS_ARCH_SPARC] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_SPARC] =
~(CS_MODE_BIG_ENDIAN | CS_MODE_V9);

// support this arch
all_arch |= (1 << CS_ARCH_SPARC);
}

#endif
13 changes: 13 additions & 0 deletions arch/Sparc/SparcModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_SPARC_MODULE_H
#define CS_SPARC_MODULE_H

#include "../../utils.h"

cs_err Sparc_global_init(cs_struct *ud);
cs_err Sparc_option(cs_struct *handle, cs_opt_type type, size_t value);
void Sparc_destroy(cs_struct *handle);

#endif
18 changes: 4 additions & 14 deletions arch/SystemZ/SystemZModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include "SystemZDisassembler.h"
#include "SystemZInstPrinter.h"
#include "SystemZMapping.h"
#include "SystemZModule.h"

static cs_err init(cs_struct *ud)
cs_err SystemZ_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));
Expand All @@ -29,7 +30,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err SystemZ_option(cs_struct *handle, cs_opt_type type, size_t value)
{
if (type == CS_OPT_SYNTAX)
handle->syntax = (int) value;
Expand All @@ -40,19 +41,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void SystemZ_destroy(cs_struct *handle)
{
}

void SystemZ_enable(void)
{
cs_arch_init[CS_ARCH_SYSZ] = init;
cs_arch_option[CS_ARCH_SYSZ] = option;
cs_arch_destroy[CS_ARCH_SYSZ] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_SYSZ] = ~CS_MODE_BIG_ENDIAN;

// support this arch
all_arch |= (1 << CS_ARCH_SYSZ);
}

#endif
13 changes: 13 additions & 0 deletions arch/SystemZ/SystemZModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_SYSTEMZ_MODULE_H
#define CS_SYSTEMZ_MODULE_H

#include "../../utils.h"

cs_err SystemZ_global_init(cs_struct *ud);
cs_err SystemZ_option(cs_struct *handle, cs_opt_type type, size_t value);
void SystemZ_destroy(cs_struct *handle);

#endif
19 changes: 4 additions & 15 deletions arch/X86/X86Module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include "X86Disassembler.h"
#include "X86InstPrinter.h"
#include "X86Mapping.h"
#include "X86Module.h"

static cs_err init(cs_struct *ud)
cs_err X86_global_init(cs_struct *ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));
Expand All @@ -35,7 +36,7 @@ static cs_err init(cs_struct *ud)
return CS_ERR_OK;
}

static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
cs_err X86_option(cs_struct *handle, cs_opt_type type, size_t value)
{
switch(type) {
default:
Expand Down Expand Up @@ -82,20 +83,8 @@ static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

static void destroy(cs_struct *handle)
void X86_destroy(cs_struct *handle)
{
}

void X86_enable(void)
{
cs_arch_init[CS_ARCH_X86] = init;
cs_arch_option[CS_ARCH_X86] = option;
cs_arch_destroy[CS_ARCH_X86] = destroy;
cs_arch_disallowed_mode_mask[CS_ARCH_X86] = ~(CS_MODE_LITTLE_ENDIAN |
CS_MODE_32 | CS_MODE_64 | CS_MODE_16);

// support this arch
all_arch |= (1 << CS_ARCH_X86);
}

#endif
13 changes: 13 additions & 0 deletions arch/X86/X86Module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Capstone Disassembly Engine */
/* By Travis Finkenauer <[email protected]>, 2018 */

#ifndef CS_X86_MODULE_H
#define CS_X86_MODULE_H

#include "../../utils.h"

cs_err X86_global_init(cs_struct *ud);
cs_err X86_option(cs_struct *handle, cs_opt_type type, size_t value);
void X86_destroy(cs_struct *handle);

#endif
Loading

0 comments on commit 853a287

Please sign in to comment.