arm64: remove asprintf.c and use static buffer instead for SysRegMapper_toString(). this fixes the bug on MSR insn reported by Patroklos Argyroudis
diff --git a/Makefile b/Makefile
index b8088df..7740893 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@
LIBNAME = capstone
LIBOBJ =
-LIBOBJ += cs.o asprintf.o utils.o SStream.o MCInstrDesc.o MCRegisterInfo.o
+LIBOBJ += cs.o utils.o SStream.o MCInstrDesc.o MCRegisterInfo.o
LIBOBJ += arch/ARM/ARMDisassembler.o arch/ARM/ARMInstPrinter.o arch/ARM/mapping.o
LIBOBJ += arch/X86/X86DisassemblerDecoder.o arch/X86/X86Disassembler.o arch/X86/X86IntelInstPrinter.o arch/X86/X86ATTInstPrinter.o arch/X86/mapping.o
LIBOBJ += arch/Mips/MipsDisassembler.o arch/Mips/MipsInstPrinter.o arch/Mips/mapping.o
diff --git a/arch/AArch64/AArch64BaseInfo.c b/arch/AArch64/AArch64BaseInfo.c
index bbed5a8..1b9ead0 100644
--- a/arch/AArch64/AArch64BaseInfo.c
+++ b/arch/AArch64/AArch64BaseInfo.c
@@ -14,9 +14,9 @@
/* Capstone Disassembler Engine */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
-#include "../../asprintf.h"
#include "../../utils.h"
+#include <stdio.h>
#include <stdlib.h>
#include "AArch64BaseInfo.h"
@@ -568,21 +568,23 @@
{"ich_lr15_el2", A64SysReg_ICH_LR15_EL2}
};
-// NOTE: caller must free() the result itself
-char *SysRegMapper_toString(SysRegMapper *S, uint32_t Bits, bool *Valid)
+// result must be a big enough buffer: 128 bytes is more than enough
+void SysRegMapper_toString(SysRegMapper *S, uint32_t Bits, bool *Valid, char *result)
{
unsigned i;
for (i = 0; i < ARR_SIZE(SysRegPairs); ++i) {
if (SysRegPairs[i].Value == Bits) {
*Valid = true;
- return strdup(SysRegPairs[i].Name);
+ strcpy(result, SysRegPairs[i].Name);
+ return;
}
}
for (i = 0; i < S->NumInstPairs; ++i) {
if (S->InstPairs[i].Value == Bits) {
*Valid = true;
- return strdup(S->InstPairs[i].Name);
+ strcpy(result, S->InstPairs[i].Name);
+ return;
}
}
@@ -596,31 +598,27 @@
// name.
if (Op0 != 3 || (CRn != 11 && CRn != 15)) {
*Valid = false;
- return NULL;
+ return;
}
//assert(Op0 == 3 && (CRn == 11 || CRn == 15) && "Invalid generic sysreg");
*Valid = true;
- //return "s3_" + utostr(Op1) + "_c" + utostr(CRn) + "_c" + utostr(CRm) + "_" + utostr(Op2);
-
char *Op1S, *CRnS, *CRmS, *Op2S;
Op1S = utostr(Op1, false);
CRnS = utostr(CRn, false);
CRmS = utostr(CRm, false);
Op2S = utostr(Op2, false);
- char *result;
- int dummy = asprintf(&result, "s3_%s_c%s_c%s_%s", Op1S, CRnS, CRmS, Op2S);
+ //printf("Op1S: %s, CRnS: %s, CRmS: %s, Op2S: %s\n", Op1S, CRnS, CRmS, Op2S);
+ int dummy = sprintf(result, "s3_%s_c%s_c%s_%s", Op1S, CRnS, CRmS, Op2S);
(void)dummy;
free(Op1S);
free(CRnS);
free(CRmS);
free(Op2S);
-
- return result;
}
static NamedImmMapper_Mapping TLBIPairs[] = {
diff --git a/arch/AArch64/AArch64BaseInfo.h b/arch/AArch64/AArch64BaseInfo.h
index d7a4504..5f527d0 100644
--- a/arch/AArch64/AArch64BaseInfo.h
+++ b/arch/AArch64/AArch64BaseInfo.h
@@ -858,6 +858,6 @@
bool NamedImmMapper_validImm(NamedImmMapper *N, uint32_t Value);
-char *SysRegMapper_toString(SysRegMapper *S, uint32_t Bits, bool *Valid);
+void SysRegMapper_toString(SysRegMapper *S, uint32_t Bits, bool *Valid, char *result);
#endif
diff --git a/arch/AArch64/AArch64Disassembler.c b/arch/AArch64/AArch64Disassembler.c
index e9e883d..e849050 100644
--- a/arch/AArch64/AArch64Disassembler.c
+++ b/arch/AArch64/AArch64Disassembler.c
@@ -772,8 +772,8 @@
void *Decoder)
{
bool ValidNamed;
- char *str = SysRegMapper_toString(Mapper, Val, &ValidNamed);
- free(str);
+ char result[128];
+ SysRegMapper_toString(Mapper, Val, &ValidNamed, result);
MCInst_addOperand(Inst, MCOperand_CreateImm(Val));
diff --git a/arch/AArch64/AArch64InstPrinter.c b/arch/AArch64/AArch64InstPrinter.c
index 1bdc14d..dca077a 100644
--- a/arch/AArch64/AArch64InstPrinter.c
+++ b/arch/AArch64/AArch64InstPrinter.c
@@ -372,14 +372,15 @@
static void printSysRegOperand(SysRegMapper *Mapper,
MCInst *MI, unsigned OpNum, SStream *O)
{
+ bool ValidName;
+ char Name[128];
+
MCOperand *MO = MCInst_getOperand(MI, OpNum);
- bool ValidName;
- char *Name = SysRegMapper_toString(Mapper, MCOperand_getImm(MO), &ValidName);
+ SysRegMapper_toString(Mapper, MCOperand_getImm(MO), &ValidName, Name);
if (ValidName) {
SStream_concat(O, Name);
}
- free(Name);
}
#define GET_REGINFO_ENUM
diff --git a/asprintf.c b/asprintf.c
deleted file mode 100644
index dc3ead8..0000000
--- a/asprintf.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (c) 2007, The xFTPd Project.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * * Neither the name of the xFTPd Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * * Redistributions of this project or parts of this project in any form
- * must retain the following aknowledgment:
- * "This product includes software developed by the xFTPd Project.
- * http://www.xftpd.com/ - http://www.xftpd.org/"
- *
- * THIS SOFTWARE IS PROVIDED BY THE xFTPd PROJECT ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE xFTPd PROJECT BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/* MinGW lacks asprintf */
-
-#include <stdio.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
-
-int vasprintf(char **strp, const char *fmt, va_list ap)
-{
- FILE *dev_null;
- int arg_len;
-
- dev_null = fopen("nul", "w");
- arg_len = vfprintf(dev_null, fmt, ap);
- if(arg_len != -1) {
- *strp = (char *)malloc((size_t)arg_len + 1);
- arg_len = vsprintf(*strp, fmt, ap);
- } else *strp = NULL;
- fclose(dev_null);
- return arg_len;
-}
-
-int asprintf(char **strp, const char *fmt, ...)
-{
- int result;
-
- va_list args;
- va_start(args, fmt);
- result = vasprintf(strp, fmt, args);
- va_end(args);
- return result;
-}
diff --git a/asprintf.h b/asprintf.h
deleted file mode 100644
index 0000db1..0000000
--- a/asprintf.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2007, The xFTPd Project.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * * Neither the name of the xFTPd Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * * Redistributions of this project or parts of this project in any form
- * must retain the following aknowledgment:
- * "This product includes software developed by the xFTPd Project.
- * http://www.xftpd.com/ - http://www.xftpd.org/"
- *
- * THIS SOFTWARE IS PROVIDED BY THE xFTPd PROJECT ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE xFTPd PROJECT BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/* MinGW lacks asprintf */
-
-#ifndef __ASPRINTF_H
-#define __ASPRINTF_H
-
-#ifdef WIN32
-#include <windows.h>
-#else
-#include <stdarg.h>
-#endif
-
-int asprintf(char **ret, const char *fmt, ...);
-int vasprintf(char **strp, const char *fmt, va_list ap);
-
-#endif /* __ASPRINTF_H */
diff --git a/tests/test_arm64.c b/tests/test_arm64.c
index 993df04..29d1449 100644
--- a/tests/test_arm64.c
+++ b/tests/test_arm64.c
@@ -133,6 +133,7 @@
//#define ARM64_CODE "\x20\x00\x02\xab" // adds x0, x1, x2 (alias of adds x0, x1, x2, lsl #0)
//#define ARM64_CODE "\x20\xf4\x18\x9e" // fcvtzs x0, s1, #3
//#define ARM64_CODE "\x20\xfc\x02\x9b" // mneg x0, x1, x2
+//#define ARM64_CODE "\xd0\xb6\x1e\xd5" // msr s3_6_c11_c6_6, x16
#define ARM64_CODE "\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x20\x04\x81\xda\x20\x08\x02\x8b"
struct platform platforms[] = {