From 89a4945aaa706660430e0092f7ca6fb4f70538c5 Mon Sep 17 00:00:00 2001 From: TartaRikker <30320758+TartaRikker@users.noreply.github.com> Date: Tue, 31 Oct 2017 23:13:55 +0100 Subject: [PATCH 1/5] Update ks.cpp Fix wrong x86 jump address --- llvm/keystone/ks.cpp | 92 +++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/llvm/keystone/ks.cpp b/llvm/keystone/ks.cpp index d1819f0d..238ed98e 100644 --- a/llvm/keystone/ks.cpp +++ b/llvm/keystone/ks.cpp @@ -552,55 +552,65 @@ int ks_asm(ks_engine *ks, unsigned char *encoding; SmallString<1024> Msg; raw_svector_ostream OS(Msg); + uint64_t BaseAddr; + int MemSts = 0; *insn = NULL; *insn_size = 0; - MCContext Ctx(ks->MAI, ks->MRI, &ks->MOFI, &ks->SrcMgr, true, address); + //If no address is specified by the user then allocate memory + //and give it some space In order to enhance its chances + //of remaining in the same location after reallocation is done + BaseAddr = address ? address : [&](){ encoding = (unsigned char *)malloc(1024); return (uint64_t)encoding; }(); + if (!BaseAddr) + // memory insufficient + return KS_ERR_NOMEM; + + MCContext Ctx(ks->MAI, ks->MRI, &ks->MOFI, &ks->SrcMgr, true, BaseAddr); ks->MOFI.InitMCObjectFileInfo(Triple(ks->TripleName), Ctx); CE = ks->TheTarget->createMCCodeEmitter(*ks->MCII, *ks->MRI, Ctx); if (!CE) { // memory insufficient - return KS_ERR_NOMEM; + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } + Streamer = ks->TheTarget->createMCObjectStreamer( Triple(ks->TripleName), Ctx, *ks->MAB, OS, CE, *ks->STI, ks->MCOptions.MCRelaxAll, /*DWARFMustBeAtTheEnd*/ false); - if (!Streamer) { - // memory insufficient - delete CE; - return KS_ERR_NOMEM; + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } - // Tell SrcMgr about this buffer, which is what the parser will pick up. - ErrorOr> BufferPtr = MemoryBuffer::getMemBuffer(assembly); - if (BufferPtr.getError()) { - delete Streamer; - delete CE; - return KS_ERR_NOMEM; - } + { // Tell SrcMgr about this buffer, which is what the parser will pick up. + ErrorOr> BufferPtr = MemoryBuffer::getMemBuffer(assembly); + if (BufferPtr.getError()) { + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; + } - ks->SrcMgr.clearBuffers(); - ks->SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); + ks->SrcMgr.clearBuffers(); + ks->SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); + } Streamer->setSymResolver((void *)(ks->sym_resolver)); - + MCAsmParser *Parser = createMCAsmParser(ks->SrcMgr, Ctx, *Streamer, *ks->MAI); if (!Parser) { - delete Streamer; - delete CE; - // memory insufficient - return KS_ERR_NOMEM; + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } + MCTargetAsmParser *TAP = ks->TheTarget->createMCAsmParser(*ks->STI, *Parser, *ks->MCII, ks->MCOptions); - if (!TAP) { - // memory insufficient - delete Parser; - delete Streamer; - delete CE; - return KS_ERR_NOMEM; - } + if (!TAP) { + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; + } TAP->KsSyntax = ks->syntax; Parser->setTargetParser(*TAP); @@ -611,7 +621,7 @@ int ks_asm(ks_engine *ks, ks->MAI->setCommentString(";"); } - *stat_count = Parser->Run(false, address); + *stat_count = Parser->Run(false, BaseAddr); // PPC counts empty statement if (ks->arch == KS_ARCH_PPC) @@ -619,19 +629,29 @@ int ks_asm(ks_engine *ks, ks->errnum = Parser->KsError; - delete TAP; - delete Parser; - delete CE; - delete Streamer; +MemoryInsufficient: // Clean up + + if (!TAP) delete TAP; + if (!Parser) delete Parser; + if (!CE) delete CE; + if (!Streamer) delete Streamer; - if (ks->errnum >= KS_ERR_ASM) - return -1; + if (ks->errnum >= KS_ERR_ASM || MemSts == KS_ERR_NOMEM) + { + if (encoding != nullptr) + free(encoding); + return MemSts ? MemSts : -1; + } else { *insn_size = Msg.size(); - encoding = (unsigned char *)malloc(*insn_size); - if (!encoding) { + //Reallocate the correct amount of memory + unsigned char *ext_encoding = (unsigned car *) realloc(encoding, *insn_size); + if (!ext_encoding) { + if (encoding) + free(encoding); return KS_ERR_NOMEM; } + encoding = ext_encoding; memcpy(encoding, Msg.data(), *insn_size); *insn = encoding; return 0; From 3993fcc7a10850c02956e2e5e429eeef5eac952e Mon Sep 17 00:00:00 2001 From: TartaRikker <30320758+TartaRikker@users.noreply.github.com> Date: Tue, 31 Oct 2017 23:29:58 +0100 Subject: [PATCH 2/5] Update ks.cpp --- llvm/keystone/ks.cpp | 82 ++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/llvm/keystone/ks.cpp b/llvm/keystone/ks.cpp index 238ed98e..7a4458f7 100644 --- a/llvm/keystone/ks.cpp +++ b/llvm/keystone/ks.cpp @@ -552,64 +552,64 @@ int ks_asm(ks_engine *ks, unsigned char *encoding; SmallString<1024> Msg; raw_svector_ostream OS(Msg); - uint64_t BaseAddr; - int MemSts = 0; + uint64_t BaseAddr; + int MemSts = 0; *insn = NULL; *insn_size = 0; - //If no address is specified by the user then allocate memory - //and give it some space In order to enhance its chances - //of remaining in the same location after reallocation is done - BaseAddr = address ? address : [&](){ encoding = (unsigned char *)malloc(1024); return (uint64_t)encoding; }(); + //If no address is specified by the user then allocate memory + //and give it some space In order to enhance its chances + //of remaining in the same location after reallocation is done + BaseAddr = address ? address : [&](){ encoding = (unsigned char *)malloc(1024); return (uint64_t)encoding; }(); if (!BaseAddr) // memory insufficient - return KS_ERR_NOMEM; + return KS_ERR_NOMEM; - MCContext Ctx(ks->MAI, ks->MRI, &ks->MOFI, &ks->SrcMgr, true, BaseAddr); + MCContext Ctx(ks->MAI, ks->MRI, &ks->MOFI, &ks->SrcMgr, true, BaseAddr); ks->MOFI.InitMCObjectFileInfo(Triple(ks->TripleName), Ctx); CE = ks->TheTarget->createMCCodeEmitter(*ks->MCII, *ks->MRI, Ctx); if (!CE) { // memory insufficient - MemSts = KS_ERR_NOMEM; - goto MemoryInsufficient; + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } Streamer = ks->TheTarget->createMCObjectStreamer( Triple(ks->TripleName), Ctx, *ks->MAB, OS, CE, *ks->STI, ks->MCOptions.MCRelaxAll, /*DWARFMustBeAtTheEnd*/ false); if (!Streamer) { - // memory insufficient - MemSts = KS_ERR_NOMEM; - goto MemoryInsufficient; + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } + + { // Tell SrcMgr about this buffer, which is what the parser will pick up. + ErrorOr> BufferPtr = MemoryBuffer::getMemBuffer(assembly); + if (BufferPtr.getError()) { + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; + } - { // Tell SrcMgr about this buffer, which is what the parser will pick up. - ErrorOr> BufferPtr = MemoryBuffer::getMemBuffer(assembly); - if (BufferPtr.getError()) { - // memory insufficient - MemSts = KS_ERR_NOMEM; - goto MemoryInsufficient; - } - - ks->SrcMgr.clearBuffers(); - ks->SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); - } + ks->SrcMgr.clearBuffers(); + ks->SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); + } Streamer->setSymResolver((void *)(ks->sym_resolver)); MCAsmParser *Parser = createMCAsmParser(ks->SrcMgr, Ctx, *Streamer, *ks->MAI); if (!Parser) { - // memory insufficient - MemSts = KS_ERR_NOMEM; - goto MemoryInsufficient; + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } MCTargetAsmParser *TAP = ks->TheTarget->createMCAsmParser(*ks->STI, *Parser, *ks->MCII, ks->MCOptions); if (!TAP) { - // memory insufficient - MemSts = KS_ERR_NOMEM; - goto MemoryInsufficient; + // memory insufficient + MemSts = KS_ERR_NOMEM; + goto MemoryInsufficient; } TAP->KsSyntax = ks->syntax; @@ -621,7 +621,7 @@ int ks_asm(ks_engine *ks, ks->MAI->setCommentString(";"); } - *stat_count = Parser->Run(false, BaseAddr); + *stat_count = Parser->Run(false, BaseAddr); // PPC counts empty statement if (ks->arch == KS_ARCH_PPC) @@ -637,21 +637,21 @@ int ks_asm(ks_engine *ks, if (!Streamer) delete Streamer; if (ks->errnum >= KS_ERR_ASM || MemSts == KS_ERR_NOMEM) - { - if (encoding != nullptr) - free(encoding); - return MemSts ? MemSts : -1; + { + if (encoding != nullptr) + free(encoding); + return MemSts ? MemSts : -1; } else { *insn_size = Msg.size(); //Reallocate the correct amount of memory - unsigned char *ext_encoding = (unsigned car *) realloc(encoding, *insn_size); - if (!ext_encoding) { - if (encoding) - free(encoding); - return KS_ERR_NOMEM; + unsigned char *ext_encoding = (unsigned car *) realloc(encoding, *insn_size); + if (!ext_encoding) { + if (encoding != nullptr) + free(encoding); + return KS_ERR_NOMEM; } - encoding = ext_encoding; + encoding = ext_encoding; memcpy(encoding, Msg.data(), *insn_size); *insn = encoding; return 0; From 90d65a9941c85f4d9580cb54cf95e48656ecb8dd Mon Sep 17 00:00:00 2001 From: TartaRikker <30320758+TartaRikker@users.noreply.github.com> Date: Tue, 31 Oct 2017 23:34:04 +0100 Subject: [PATCH 3/5] Update ks.cpp --- llvm/keystone/ks.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llvm/keystone/ks.cpp b/llvm/keystone/ks.cpp index 7a4458f7..053c45d4 100644 --- a/llvm/keystone/ks.cpp +++ b/llvm/keystone/ks.cpp @@ -555,6 +555,8 @@ int ks_asm(ks_engine *ks, uint64_t BaseAddr; int MemSts = 0; + encoding = nullptr; + BaseAddr = 0; *insn = NULL; *insn_size = 0; From a9ce2ff7ae30e5309b5685a86290b6e2ebdf7590 Mon Sep 17 00:00:00 2001 From: TartaRikker <30320758+TartaRikker@users.noreply.github.com> Date: Thu, 2 Nov 2017 00:02:48 +0100 Subject: [PATCH 4/5] fix building failure Fix syntax error and make it able to digest 'goto' statement --- llvm/keystone/ks.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/llvm/keystone/ks.cpp b/llvm/keystone/ks.cpp index 053c45d4..2c229fba 100644 --- a/llvm/keystone/ks.cpp +++ b/llvm/keystone/ks.cpp @@ -547,13 +547,15 @@ int ks_asm(ks_engine *ks, unsigned char **insn, size_t *insn_size, size_t *stat_count) { - MCCodeEmitter *CE; - MCStreamer *Streamer; - unsigned char *encoding; - SmallString<1024> Msg; - raw_svector_ostream OS(Msg); - uint64_t BaseAddr; - int MemSts = 0; + MCCodeEmitter *CE; + MCStreamer *Streamer; + MCAsmParser *Parser; + MCTargetAsmParser *TAP; + unsigned char *encoding; + SmallString<1024> Msg; + raw_svector_ostream OS(Msg); + uint64_t BaseAddr; + int32_t MemSts = 0; encoding = nullptr; BaseAddr = 0; @@ -647,7 +649,7 @@ int ks_asm(ks_engine *ks, else { *insn_size = Msg.size(); //Reallocate the correct amount of memory - unsigned char *ext_encoding = (unsigned car *) realloc(encoding, *insn_size); + unsigned char *ext_encoding = (unsigned char *) realloc(encoding, *insn_size); if (!ext_encoding) { if (encoding != nullptr) free(encoding); From 824ea44d35b20dc8609d2fab76e98cc5a83fc6cd Mon Sep 17 00:00:00 2001 From: TartaRikker <30320758+TartaRikker@users.noreply.github.com> Date: Thu, 2 Nov 2017 00:55:33 +0100 Subject: [PATCH 5/5] Fix syntax error --- llvm/keystone/ks.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/keystone/ks.cpp b/llvm/keystone/ks.cpp index 2c229fba..02a85ae3 100644 --- a/llvm/keystone/ks.cpp +++ b/llvm/keystone/ks.cpp @@ -602,14 +602,14 @@ int ks_asm(ks_engine *ks, Streamer->setSymResolver((void *)(ks->sym_resolver)); - MCAsmParser *Parser = createMCAsmParser(ks->SrcMgr, Ctx, *Streamer, *ks->MAI); + Parser = createMCAsmParser(ks->SrcMgr, Ctx, *Streamer, *ks->MAI); if (!Parser) { // memory insufficient MemSts = KS_ERR_NOMEM; goto MemoryInsufficient; } - MCTargetAsmParser *TAP = ks->TheTarget->createMCAsmParser(*ks->STI, *Parser, *ks->MCII, ks->MCOptions); + TAP = ks->TheTarget->createMCAsmParser(*ks->STI, *Parser, *ks->MCII, ks->MCOptions); if (!TAP) { // memory insufficient MemSts = KS_ERR_NOMEM;