@@ -26,6 +26,7 @@ class amd64jit {
2626 uint8_t * ptr;
2727 size_t size;
2828 std::stack<uint8_t *> stk;
29+
2930public:
3031 amd64jit (const size_t );
3132 ~amd64jit ();
@@ -42,135 +43,137 @@ class amd64jit {
4243};
4344
4445amd64jit::amd64jit (const size_t _size) {
45- size= _size;
46+ size = _size;
4647#ifdef _WIN32
47- mem= (uint8_t *)VirtualAlloc (nullptr ,size,
48- MEM_COMMIT| MEM_RESERVE,
48+ mem = (uint8_t *)VirtualAlloc (nullptr , size,
49+ MEM_COMMIT | MEM_RESERVE,
4950 PAGE_EXECUTE_READWRITE);
5051#else
51- mem= (uint8_t *)mmap (nullptr ,size,
52- PROT_READ| PROT_WRITE,
53- MAP_PRIVATE| MAP_ANONYMOUS| MAP_JIT,
54- -1 ,0 );
52+ mem = (uint8_t *)mmap (nullptr , size,
53+ PROT_READ | PROT_WRITE,
54+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_JIT,
55+ -1 , 0 );
5556#endif
56- if (!mem) {
57- std::cout<< " failed to allocate memory\n " ;
57+ if (!mem) {
58+ std::cout << " failed to allocate memory\n " ;
5859 std::exit (-1 );
5960 }
60- memset (mem,0 , size);
61- ptr= mem;
61+ memset (mem, 0 , size);
62+ ptr = mem;
6263}
6364
6465amd64jit::~amd64jit () {
6566#ifdef _WIN32
66- VirtualFree (mem,size,MEM_RELEASE);
67+ VirtualFree (mem, size, MEM_RELEASE);
6768#else
68- munmap (mem,size);
69+ munmap (mem, size);
6970#endif
70- mem= nullptr ;
71+ mem = nullptr ;
7172}
7273
7374void amd64jit::err () {
74- std::cout<< " data overflow, please try a memory size greater than " << size<< ' \n ' ;
75+ std::cout << " data overflow, please try a memory size greater than " << size << ' \n ' ;
7576 std::exit (-1 );
7677}
7778
7879void amd64jit::exec () {
79- std::cout<< " getchar : 0x" << std::hex<< std::setw (16 )<< std::setfill (' 0' )<< (uint64_t )getchar<< std::dec<< std::endl;
80- std::cout<< " putchar : 0x" << std::hex<< std::setw (16 )<< std::setfill (' 0' )<< (uint64_t )putchar<< std::dec<< std::endl;
81- std::cout<< " memory : 0x" << std::hex<< std::setw (16 )<< std::setfill (' 0' )<< (uint64_t )mem<< std::dec<< std::endl;
80+ std::cout << " getchar : 0x" << std::hex << std::setw (16 ) << std::setfill (' 0' ) << (uint64_t )getchar << std::dec << std::endl;
81+ std::cout << " putchar : 0x" << std::hex << std::setw (16 ) << std::setfill (' 0' ) << (uint64_t )putchar << std::dec << std::endl;
82+ std::cout << " memory : 0x" << std::hex << std::setw (16 ) << std::setfill (' 0' ) << (uint64_t )mem << std::dec << std::endl;
8283#ifndef _WIN32
83- mprotect (mem,size,PROT_READ| PROT_EXEC);
84+ mprotect (mem, size, PROT_READ | PROT_EXEC);
8485#endif
8586 ((func)mem)();
8687}
8788
8889void amd64jit::print () {
89- const char tbl[]= " 0123456789abcdef" ;
90- std::cout<< " size: " << (uint64_t )(ptr- mem)<< std::endl;
91- for (uint8_t * i= mem;i< ptr;++i) {
92- printf (" %c%c%c" ,tbl[((*i)>> 4 )& 0x0f ],tbl[(*i)& 0x0f ]," \n " [!((i- mem+ 1 )& 0xf )]);
90+ const char tbl[] = " 0123456789abcdef" ;
91+ std::cout << " size: " << (uint64_t )(ptr - mem) << std::endl;
92+ for (uint8_t * i = mem; i < ptr; ++i) {
93+ printf (" %c%c%c" , tbl[((*i) >> 4 ) & 0x0f ], tbl[(*i) & 0x0f ], " \n " [!((i - mem + 1 ) & 0xf )]);
9394 }
9495 printf (" \n " );
9596}
9697
9798amd64jit& amd64jit::push (std::initializer_list<uint8_t > codes) {
98- for (auto c: codes) {
99- ptr[0 ]= c;
99+ for (auto c : codes) {
100+ ptr[0 ] = c;
100101 ++ptr;
101- if (ptr>= mem+ size) {
102+ if (ptr >= mem + size) {
102103 err ();
103104 }
104105 }
105106 return *this ;
106107}
107108
108109amd64jit& amd64jit::push8 (uint8_t n) {
109- if (ptr+ 1 >= mem+ size) {
110+ if (ptr + 1 >= mem + size) {
110111 err ();
111112 }
112- ptr[0 ]= n;
113+ ptr[0 ] = n;
113114 ++ptr;
114115 return *this ;
115116}
116117
117118amd64jit& amd64jit::push16 (uint16_t n) {
118- if ( ptr+ 2 >= mem+ size) {
119+ if ( ptr + 2 >= mem + size) {
119120 err ();
120121 }
121- ptr[0 ]=n& 0xff ;
122- ptr[1 ]=(n>> 8 )& 0xff ;
123- ptr+= 2 ;
122+ ptr[0 ] = n & 0xff ;
123+ ptr[1 ] = (n >> 8 ) & 0xff ;
124+ ptr += 2 ;
124125 return *this ;
125126}
126127
127128amd64jit& amd64jit::push32 (uint32_t n) {
128- if (ptr+ 4 >= mem+ size) {
129+ if (ptr + 4 >= mem + size) {
129130 err ();
130131 }
131- ptr[0 ]=n& 0xff ;
132- ptr[1 ]=(n>> 8 )& 0xff ;
133- ptr[2 ]=(n>> 16 )& 0xff ;
134- ptr[3 ]=(n>> 24 )& 0xff ;
135- ptr+= 4 ;
132+ ptr[0 ] = n & 0xff ;
133+ ptr[1 ] = (n >> 8 ) & 0xff ;
134+ ptr[2 ] = (n >> 16 ) & 0xff ;
135+ ptr[3 ] = (n >> 24 ) & 0xff ;
136+ ptr += 4 ;
136137 return *this ;
137138}
138139
139140amd64jit& amd64jit::push64 (uint64_t n) {
140- if (ptr+ 8 >= mem+ size) {
141+ if (ptr + 8 >= mem + size) {
141142 err ();
142143 }
143- ptr[0 ]=n& 0xff ;
144- ptr[1 ]=(n>> 8 )& 0xff ;
145- ptr[2 ]=(n>> 16 )& 0xff ;
146- ptr[3 ]=(n>> 24 )& 0xff ;
147- ptr[4 ]=(n>> 32 )& 0xff ;
148- ptr[5 ]=(n>> 40 )& 0xff ;
149- ptr[6 ]=(n>> 48 )& 0xff ;
150- ptr[7 ]=(n>> 56 )& 0xff ;
151- ptr+= 8 ;
144+ ptr[0 ] = n & 0xff ;
145+ ptr[1 ] = (n >> 8 ) & 0xff ;
146+ ptr[2 ] = (n >> 16 ) & 0xff ;
147+ ptr[3 ] = (n >> 24 ) & 0xff ;
148+ ptr[4 ] = (n >> 32 ) & 0xff ;
149+ ptr[5 ] = (n >> 40 ) & 0xff ;
150+ ptr[6 ] = (n >> 48 ) & 0xff ;
151+ ptr[7 ] = (n >> 56 ) & 0xff ;
152+ ptr += 8 ;
152153 return *this ;
153154}
154155
155156amd64jit& amd64jit::je () {
156- push ({0x0f ,0x84 }).push32 (0x0 );// je
157+ push ({0x0f , 0x84 }).push32 (0x0 ); // je
157158 stk.push (ptr);
158159 return *this ;
159160}
160161
161162amd64jit& amd64jit::jne () {
162- push ({0x0f ,0x85 }).push32 (0x0 );// jne
163- uint8_t * je_next=stk.top ();stk.pop ();
164- uint8_t * jne_next=ptr;
165- uint64_t p0=jne_next-je_next;
166- uint64_t p1=je_next-jne_next;
167- jne_next[-4 ]=(p1&0xff );
168- jne_next[-3 ]=((p1>>8 )&0xff );
169- jne_next[-2 ]=((p1>>16 )&0xff );
170- jne_next[-1 ]=((p1>>24 )&0xff );
171- je_next[-4 ]=(p0&0xff );
172- je_next[-3 ]=((p0>>8 )&0xff );
173- je_next[-2 ]=((p0>>16 )&0xff );
174- je_next[-1 ]=((p0>>24 )&0xff );
163+ push ({0x0f , 0x85 }).push32 (0x0 ); // jne
164+ uint8_t * je_next = stk.top ();
165+ stk.pop ();
166+
167+ uint8_t * jne_next = ptr;
168+ uint64_t p0 = jne_next - je_next;
169+ uint64_t p1 = je_next - jne_next;
170+ jne_next[-4 ] = p1 & 0xff ;
171+ jne_next[-3 ] = (p1 >> 8 ) & 0xff ;
172+ jne_next[-2 ] = (p1 >> 16 ) & 0xff ;
173+ jne_next[-1 ] = (p1 >> 24 ) & 0xff ;
174+ je_next[-4 ] = p0 & 0xff ;
175+ je_next[-3 ] = (p0 >> 8 ) & 0xff ;
176+ je_next[-2 ] = (p0 >> 16 ) & 0xff ;
177+ je_next[-1 ] = (p0 >> 24 ) & 0xff ;
175178 return *this ;
176179}
0 commit comments