Git Product home page Git Product logo

swojtasiak / fcml-lib Goto Github PK

View Code? Open in Web Editor NEW
83.0 8.0 23.0 23.5 MB

A general purpose machine code manipulation library for x86-32 (IA-32) and x86-64 (AMD64) architectures (Assembler, Disassembler, Library).

Home Page: http://www.fcml-lib.com

License: GNU Lesser General Public License v2.1

C++ 13.85% C 84.81% Lex 0.61% Yacc 0.37% Makefile 0.26% M4 0.10% Dockerfile 0.01%
disassembler assembler code-generator shared-library avx avx2 sse sse2 ssse3 sse3

fcml-lib's People

Contributors

deni90 avatar skitt avatar stonedreamforest avatar swojtasiak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fcml-lib's Issues

Failed to generate segment override prefix

I used fcml-asm to assemble such code:

mov %rax, %gs:0x44

However, it generated wrong instruction: 6748a344000000, whose disassembly result is mov %rax, 0x44. The segment register %gs is lost. In the wrong assembly result, there's no segment override prefix 0x65 which represents "%gs" at the beginning of the machine code.

There may be some BUGs in function operand_encoder_segment_relative_offset in fcml_encoding.c I guess, because after I added the following code (which comes from operand_encoder_rm function) to operand_encoder_segment_relative_offset, it can generate correct result.

const fcml_st_register *base =
        &(args->operand->address.effective_address.base);
const fcml_st_segment_selector *segment_selector =
        &(args->operand->address.segment_selector);
const fcml_st_register *segment_register =
        &(segment_selector->segment_selector);

if (segment_register->type == FCML_REG_SEG) {
    if (!(args->addr_mode_def->instruction_group
          & FCML_AMT_BRANCH)
        && !(base->type == FCML_REG_GPR
             && (base->reg == FCML_REG_BP
                 || base->reg == FCML_REG_SP))) {
        if (segment_register->reg != FCML_REG_DS) {
            args->context->segment_override = *segment_register;
        }
    }
}

there is too many build errors when i use fcml

image

#include <fcml_intel_mnemonics.hpp>

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>


void test_fcml::fcml_main(int argc , char **argv) {
// Notice that it is an array of strings.
	const fcml_string instructions[] = {
		"start:      mov ebx, 1" ,
		"loop_big:   inc ebx" ,
		"            cmp ebx, 10" ,
		"            je  finish" ,
		"loop_small: mov eax, 1" ,
		"increment:  inc eax" ,
		"            cmp eax, 10" ,
		"            je  finish_small" ,
		"            jmp increment" ,
		"finish_small:" ,
		"            jmp loop_big" ,
		"finish:     ret" ,
		NULL
	};
	using namespace fcml;
	using namespace fcml::intel;
	fcml::IntelDialect dialect;
	//ParserContext ctx(0x401000);
	//Parser parser(dialect);
	//ParserResult result;
	//parser.parse(ctx , _FT("mov eax, dword [0x0401000]") , result);
	//const Instruction &instruction = result.getInstruction();
}

any sample step to add header file? just like only add fcml_header.hpp

syntax error on complex mov commands

Example:

./fcml_asm -asa32 -osa32 -m32 "mov ecx,dword ptr [eax*4+4]"

result:

Can not parse the instruction mnemonic, error: 2
ERROR: 1008: syntax error

But

./fcml_disasm -m32 -asa32 "8B0C8504000000"

works fine and return command:
mov ecx,dword ptr [eax*4+4]

Got incorrect result when disassembling an AVX2 instruction

I used FCML library to disassemble a machine instruction c5fdd7c1,whose correct disassembly result is
vpmovmskb %ymm1, %eax

But FCML disassembler gives vpmovmskb %ymm0,%rax, it seems that both the source operand register, and the destination operand register size are incorrect.

I found that in fcml_def.c, the addr_mode_desc of vpmovmskb instruction is

   {FCML_AMT_AVX_SIMD, FCML_NO_DETAILS, F_L0 | F_VEX | F_IGNORE_L | F_66, IS_MODRM | MODE3264 | EOSA64_FORCE | OPCODE_NUM(2) | PRIMARY_OPCODE(1), {0xf, 0xd7, 0x22}, {FCML_OP_MODRM_R_W, FCML_OP_VEX_VVVV_REG(FCML_REG_SIMD, FCML_EOS_XWORD), FCML_NA, FCML_NA, FCML_NA}, FCML_AM_UNKNOWN, FCML_HINT_NO_HINTS},
    {FCML_AMT_AVX2_SIMD, FCML_NO_DETAILS, F_L1 | F_VEX | F_IGNORE_L | F_66, IS_MODRM | MODE3264 | EOSA64_FORCE | OPCODE_NUM(2) | PRIMARY_OPCODE(1), {0xf, 0xd7, 0x22}, {FCML_OP_MODRM_R_W, FCML_OP_VEX_VVVV_REG(FCML_REG_SIMD, FCML_EOS_YWORD), FCML_NA, FCML_NA, FCML_NA}, FCML_AM_UNKNOWN, FCML_HINT_NO_HINTS}

It seems that this addr_mode_desc uses VEX.vvvv to specify the source operand register. However, the Intel Architectures Software Developer's Manual said that in VPMOVMSKB instruction, VEX.vvvv is reserved and must be 1111b, and ModRM should be used to encode the source operand. That's the reason for the first problem, I guess.

Look like incorrect result in assembling push numeric value in intel assembler

command from example dir: ./fcml_asm -asa32 -osa32 -m32 "push 80h"

Return this:

Number of the assembled instructions: 2
Instruction: 1
 Code: 6880000000
 Code length: 5
Instruction: 2
 Code: 666a80
 Code length: 3
Best instruction chosen by the assembler: 2

But 666a80 look like wrong.
This is bug? or exists way to give to push information about argument size?

Got wrong result when disassembling an instruction

I used FCML library to disassemble a machine instruction 0x488d148500000000,whose correct disassembly result is
lea 0x00(, %rax, 4), %rdx

However, when I use FCML-Disassembler, FCML generates result like this: lea 0x0000000000000000,%rdx

$ ./fcml_disasm -gas -m64 488d148500000000
Basic information:
 Disassembled instruction: lea 0x0000000000000000,%rdx

I also check that FCML-Assembler will produce right result

$ ./fcml_asm -gas -m64 "lea 0(,%rax,4), %rdx" 
Number of the assembled instructions: 1
Instruction: 1
 Code: 488d148500000000
 Code length: 8
Best instruction chosen by the assembler: 1

I found that the BUG locates in fcml_modrm_decoder.c,in function decode_sib,below the comment /* Base register and displacement.*/。When f_mod == 0 && FCML_MODRM_SIB_BASE(sib) == 5,the address_from will be set to FCML_AF_OFFSET directly. However, although lea 0x00(, %rax, 4), %rdx satisfies f_mod == 0 and it does not have base register(so that FCML_MODRM_SIB_BASE(sib) == 5), it still has the index register, and can not be categorized to FCML_AF_OFFSET form. So I modified the source code to

/* Base register and displacement.*/
    if (f_mod == 0 && FCML_MODRM_SIB_BASE(sib) == 5) {

        address->address_form = FCML_MODRM_SIB_INDEX(sib) == 4 ? FCML_AF_OFFSET : FCML_AF_COMBINED; /* ------------------ changed ------------------- */

        /* In this case base register doesn't exist.*/
        error = decode_displacement(context,
                stream, &(effective_address->displacement),
                &(modrm_details->displacement), &(address->offset), FCML_DS_32,
                effective_address_size, 0);

    }

And this BUG seems to be solved, and all unit tests pass.

FCML-based HSDIS crashes when used with WinPerfAsmProfiler

Contents of hs_err_pid8384.log:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffca7034404, pid=8384, tid=8588
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b15) (build 1.8.0_45-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [hsdis-amd64.dll+0x4404]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

---------------  T H R E A D  ---------------

Current thread (0x000000003acbd000):  JavaThread "C1 CompilerThread11" daemon [_thread_in_vm, id=8588, stack(0x000000003d3f0000,0x000000003d4f0000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x0000000000000000

Registers:
RAX=0x0000000000000000, RBX=0x000000003d4eeda0, RCX=0x000000000000006c, RDX=0x0000000000000002
RSP=0x000000003d4edc80, RBP=0x000000006b8739d8, RSI=0x0000000002d39758, RDI=0x000000003d4ee3b0
R8 =0x0000000000000002, R9 =0x0000000000008000, R10=0x0000000000af92c0, R11=0x0000000000af98c0
R12=0x000000006b8e4140, R13=0x0000000000000108, R14=0x000000003d4ef230, R15=0x00000000ffffffff
RIP=0x00007ffca7034404, EFLAGS=0x0000000000010202

Top of Stack: (sp=0x000000003d4edc80)
0x000000003d4edc80:   000000003d4eddb0 0000000000000002
0x000000003d4edc90:   0000000000000002 0000000300000001
0x000000003d4edca0:   0000007c00000000 0000000000000000
0x000000003d4edcb0:   000000003d4eddb0 00000000a703ba37
0x000000003d4edcc0:   0000000000ba9190 0000000000af98f0
0x000000003d4edcd0:   00000000124fdd10 00007ffc00000001
0x000000003d4edce0:   0000000000000000 00000000124fdd10
0x000000003d4edcf0:   0000000011fe13b0 0000000000b06ad0
0x000000003d4edd00:   0000000000000050 00007ffca7035472
0x000000003d4edd10:   000000003d4eddc0 000000003d4ee740
0x000000003d4edd20:   0000000000000000 0000000000000000
0x000000003d4edd30:   0000000000000000 000000003d4ee740
0x000000003d4edd40:   0000000000000001 000000003d4ee8a0
0x000000003d4edd50:   0000000000c40cc0 00000000124c0180
0x000000003d4edd60:   000000003d4edfd8 0000000000000004
0x000000003d4edd70:   0000001000000000 0000000000000001 

Instructions: (pc=0x00007ffca7034404)
0x00007ffca70343e4:   c7 44 24 28 00 00 00 00 48 83 7c 24 50 00 0f 84
0x00007ffca70343f4:   f3 00 00 00 48 8b 44 24 50 48 8b 80 00 08 00 00
0x00007ffca7034404:   48 8b 00 48 89 44 24 58 48 83 7c 24 58 00 0f 84
0x00007ffca7034414:   d1 00 00 00 48 8b 44 24 58 48 8b 40 10 48 89 44 


Register to memory mapping:

RAX=0x0000000000000000 is an unknown value
RBX=0x000000003d4eeda0 is pointing into the stack for thread: 0x000000003acbd000
RCX=0x000000000000006c is an unknown value
RDX=0x0000000000000002 is an unknown value
RSP=0x000000003d4edc80 is pointing into the stack for thread: 0x000000003acbd000
RBP=0x000000006b8739d8 is an unknown value
RSI=0x0000000002d39758 is an unknown value
RDI=0x000000003d4ee3b0 is pointing into the stack for thread: 0x000000003acbd000
R8 =0x0000000000000002 is an unknown value
R9 =0x0000000000008000 is an unknown value
R10=0x0000000000af92c0 is an unknown value
R11=0x0000000000af98c0 is an unknown value
R12=0x000000006b8e4140 is an unknown value
R13=0x0000000000000108 is an unknown value
R14=0x000000003d4ef230 is pointing into the stack for thread: 0x000000003acbd000
R15=0x00000000ffffffff is an unknown value


Stack: [0x000000003d3f0000,0x000000003d4f0000],  sp=0x000000003d4edc80,  free space=1015k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [hsdis-amd64.dll+0x4404]
C  [hsdis-amd64.dll+0x5472]
C  [hsdis-amd64.dll+0x5aa0]
C  [hsdis-amd64.dll+0x1a6be]
V  [jvm.dll+0xa8f88]
V  [jvm.dll+0xa90c8]
V  [jvm.dll+0x8c19d]
V  [jvm.dll+0x8cc76]
V  [jvm.dll+0x440d9b]
V  [jvm.dll+0x40669b]
V  [jvm.dll+0x406814]
V  [jvm.dll+0xa1c40]
V  [jvm.dll+0xa4aa9]
V  [jvm.dll+0x241984]
V  [jvm.dll+0x29853a]
C  [msvcr100.dll+0x21d9f]
C  [msvcr100.dll+0x21e3b]
C  [KERNEL32.DLL+0x13d2]
C  [ntdll.dll+0x15444]


---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
=>0x000000003acbd000 JavaThread "C1 CompilerThread11" daemon [_thread_in_vm, id=8588, stack(0x000000003d3f0000,0x000000003d4f0000)]
  0x000000003acb9800 JavaThread "C1 CompilerThread10" daemon [_thread_blocked, id=5488, stack(0x000000003d2f0000,0x000000003d3f0000)]
  0x000000003acba000 JavaThread "C1 CompilerThread9" daemon [_thread_blocked, id=8048, stack(0x000000003d1f0000,0x000000003d2f0000)]
  0x000000003acbc800 JavaThread "C1 CompilerThread8" daemon [_thread_blocked, id=4192, stack(0x000000003cff0000,0x000000003d0f0000)]
  0x000000003acbb800 JavaThread "C2 CompilerThread7" daemon [_thread_blocked, id=6052, stack(0x000000003cef0000,0x000000003cff0000)]
  0x000000003acb8800 JavaThread "C2 CompilerThread6" daemon [_thread_blocked, id=8520, stack(0x000000003cdf0000,0x000000003cef0000)]
  0x000000003acbb000 JavaThread "C2 CompilerThread5" daemon [_thread_blocked, id=8848, stack(0x000000003ccf0000,0x000000003cdf0000)]
  0x000000003acb7000 JavaThread "C2 CompilerThread4" daemon [_thread_blocked, id=8092, stack(0x000000003cbf0000,0x000000003ccf0000)]
  0x000000003acb8000 JavaThread "C2 CompilerThread3" daemon [_thread_blocked, id=1840, stack(0x000000003caf0000,0x000000003cbf0000)]
  0x000000003acb4000 JavaThread "C2 CompilerThread2" daemon [_thread_blocked, id=4872, stack(0x000000003c9f0000,0x000000003caf0000)]
  0x000000003acb0000 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=8288, stack(0x000000003c8f0000,0x000000003c9f0000)]
  0x000000003acae800 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=7788, stack(0x000000003c7f0000,0x000000003c8f0000)]
  0x000000003acad800 JavaThread "Attach Listener" daemon [_thread_blocked, id=8776, stack(0x000000003c6f0000,0x000000003c7f0000)]
  0x000000003aca8800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=7072, stack(0x000000003c5f0000,0x000000003c6f0000)]
  0x000000003920e800 JavaThread "Finalizer" daemon [_thread_blocked, id=8740, stack(0x000000003c4f0000,0x000000003c5f0000)]
  0x000000003920c800 JavaThread "Reference Handler" daemon [_thread_blocked, id=5800, stack(0x000000003c3f0000,0x000000003c4f0000)]
  0x0000000002993800 JavaThread "main" [_thread_in_vm, id=8528, stack(0x0000000002890000,0x0000000002990000)]

Other Threads:
  0x0000000039205800 VMThread [stack: 0x000000003c2f0000,0x000000003c3f0000] [id=8464]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread:  ([mutex/lock_event])
[0x0000000002748ca0] tty_lock - owner thread: 0x000000003acbd000

Heap:
 PSYoungGen      total 305664K, used 10485K [0x000000066b300000, 0x0000000680800000, 0x00000007c0000000)
  eden space 262144K, 4% used [0x000000066b300000,0x000000066bd3d7c0,0x000000067b300000)
  from space 43520K, 0% used [0x000000067dd80000,0x000000067dd80000,0x0000000680800000)
  to   space 43520K, 0% used [0x000000067b300000,0x000000067b300000,0x000000067dd80000)
 ParOldGen       total 699392K, used 0K [0x00000003c1800000, 0x00000003ec300000, 0x000000066b300000)
  object space 699392K, 0% used [0x00000003c1800000,0x00000003c1800000,0x00000003ec300000)
 Metaspace       used 2126K, capacity 4480K, committed 4480K, reserved 1056768K
  class space    used 233K, capacity 384K, committed 384K, reserved 1048576K

Card table byte_map: [0x0000000012550000,0x0000000014550000] byte_map_base: 0x0000000010744000

Marking Bits: (ParMarkBitMap*) 0x000000006ba94040
 Begin Bits: [0x00000000167a0000, 0x0000000026740000)
 End Bits:   [0x0000000026740000, 0x00000000366e0000)

Polling page: 0x0000000002730000

CodeCache: size=245760Kb used=2746Kb max_used=2747Kb free=243013Kb
 bounds [0x0000000002a90000, 0x0000000002d40000, 0x0000000011a90000]
 total_blobs=222 nmethods=0 adapters=135
 compilation: enabled

Compilation events (0 events):
No events

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (2 events):
Event: 3.531 Thread 0x0000000002993800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000066b30d930) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u45\3627\hotspot\sr€2~Eû?@
Event: 3.531 Thread 0x0000000002993800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000066b30dbc0) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u45\3627\hotspot\src\share\vm\prims\j

Events (10 events):
Event: 3.806 loading class java/lang/ClassValue$Identity
Event: 3.806 loading class java/lang/ClassValue$Identity done
Event: 3.806 loading class java/lang/ClassValue$Version
Event: 3.806 loading class java/lang/ClassValue$Version done
Event: 3.806 loading class java/lang/invoke/MemberName$Factory
Event: 3.806 loading class java/lang/invoke/MemberName$Factory done
Event: 3.806 loading class java/lang/invoke/MethodHandleStatics
Event: 3.806 loading class java/lang/invoke/MethodHandleStatics done
Event: 3.806 loading class java/lang/invoke/MethodHandleStatics$1
Event: 3.806 loading class java/lang/invoke/MethodHandleStatics$1 done


Dynamic libraries:
...

VM Arguments:
jvm_args: -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation -XX:LogFile=C:\Users\Alex\AppData\Local\Temp\jmh4739120209792329061hslog -XX:+PrintAssembly -XX:+PrintInterpreter -XX:+PrintNMethods -XX:+PrintNativeNMethods -XX:+PrintSignatureHandlers -XX:+PrintAdapterHandlers -XX:+PrintStubCode -XX:CompileCommandFile=C:\Users\Alex\AppData\Local\Temp\jmh167406964947341016compilecommand 
java_command: org.openjdk.jmh.runner.ForkedMain 127.0.0.1 58543


...



---------------  S Y S T E M  ---------------

OS: Windows 8.1 , 64 bit Build 9600 (6.3.9600.17415)

CPU:total 16 (1 cores per cpu, 16 threads per core) family 6 model 63 stepping 2, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, aes, clmul, lzcnt, ht, tsc, tscinvbit

Memory: 4k page, physical 67008012k(56324616k free), swap 100562444k(84020988k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.45-b02) for windows-amd64 JRE (1.8.0_45-b15), built on Apr 30 2015 12:40:44 by "java_re" with MS VC++ 10.0 (VS2010)

time: Sun May 24 17:01:33 2015
elapsed time: 4 seconds (0d 0h 0m 4s)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.