| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Test suite initialization. */ |
| 21 |
|
| 22 |
#include "assembler_t.hpp" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
|
| 26 |
#include <fcml_common.hpp> |
| 27 |
#include <fcml_assembler.hpp> |
| 28 |
#include <fcml_registers.hpp> |
| 29 |
#include <fcml_lag_assembler.hpp> |
| 30 |
#include <fcml_stateful_assembler.hpp> |
| 31 |
#include <fcml_intel_dialect.hpp> |
| 32 |
#include <fcml_intel_mnemonics.hpp> |
| 33 |
|
| 34 |
using namespace fcml; |
| 35 |
using namespace fcml::intel; |
| 36 |
|
| 37 |
fcml_bool fcml_tf_cpp_assembler_suite_init(void) { |
| 38 |
return FCML_TRUE; |
| 39 |
} |
| 40 |
|
| 41 |
fcml_bool fcml_tf_cpp_assembler_suite_cleanup(void) { |
| 42 |
return FCML_TRUE; |
| 43 |
} |
| 44 |
|
| 45 |
void fcml_tf_cpp_assemble_instruction_long_form(void) { |
| 46 |
|
| 47 |
Instruction instruction; |
| 48 |
|
| 49 |
instruction.setMnemonic(fcml::intel::M_MOV); |
| 50 |
instruction.setHints(FCML_HINT_FAR_POINTER | FCML_HINT_LONG_FORM_POINTER); |
| 51 |
instruction.getCondition().setConditionType(Condition::CONDITION_BE); |
| 52 |
instruction.getCondition().setNegation(FCML_TRUE); |
| 53 |
instruction.setOperandsCount(2); |
| 54 |
instruction.setPrefixes(FCML_PREFIX_LOCK | FCML_PREFIX_REPE); |
| 55 |
|
| 56 |
instruction[0].setOperandType(Operand::OT_REGISTER); |
| 57 |
instruction[0].getRegister().setReg(FCML_REG_EAX); |
| 58 |
instruction[0].getRegister().setSize(FCML_DS_32); |
| 59 |
instruction[0].getRegister().setType(Register::REG_GPR); |
| 60 |
instruction[1].setOperandType(Operand::OT_ADDRESS); |
| 61 |
instruction[1].getAddress().setAddressForm(Address::AF_OFFSET); |
| 62 |
instruction[1].getAddress().getOffset().setSigned(FCML_TRUE); |
| 63 |
instruction[1].getAddress().getOffset().setInt32(0x401000); |
| 64 |
instruction[1].getAddress().getOffset().setSize(FCML_DS_32); |
| 65 |
instruction[1].getAddress().setSizeOperator(FCML_DS_32); |
| 66 |
|
| 67 |
fcml_st_instruction instStr; |
| 68 |
|
| 69 |
TypeConverter::convert( instruction, instStr ); |
| 70 |
|
| 71 |
STF_ASSERT_EQUAL( instStr.mnemonic, M_MOV ); |
| 72 |
STF_ASSERT_EQUAL( instStr.hints, FCML_HINT_FAR_POINTER | FCML_HINT_LONG_FORM_POINTER ); |
| 73 |
STF_ASSERT_EQUAL( instStr.operands_count, 2 ); |
| 74 |
STF_ASSERT_EQUAL( instStr.prefixes, FCML_PREFIX_LOCK | FCML_PREFIX_REPE ); |
| 75 |
STF_ASSERT_EQUAL( instStr.condition.condition_type, static_cast<fcml_en_condition_type>( instruction.getCondition().getConditionType() ) ); |
| 76 |
|
| 77 |
STF_ASSERT_EQUAL( FCML_OT_REGISTER, instStr.operands[0].type ); |
| 78 |
STF_ASSERT_EQUAL( FCML_REG_EAX, instStr.operands[0].reg.reg ); |
| 79 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[0].reg.size ); |
| 80 |
STF_ASSERT_EQUAL( FCML_REG_GPR, instStr.operands[0].reg.type ); |
| 81 |
STF_ASSERT_EQUAL( FCML_OT_ADDRESS, instStr.operands[1].type ); |
| 82 |
STF_ASSERT_EQUAL( FCML_AF_OFFSET, instStr.operands[1].address.address_form ); |
| 83 |
STF_ASSERT_EQUAL( FCML_TRUE, instStr.operands[1].address.offset.is_signed ); |
| 84 |
STF_ASSERT_EQUAL( 0x401000, instStr.operands[1].address.offset.off32 ); |
| 85 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].address.offset.size ); |
| 86 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].address.size_operator ); |
| 87 |
|
| 88 |
TypeConverter::free( instStr ); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
void fcml_tf_cpp_assemble_instruction_short_form(void) { |
| 93 |
|
| 94 |
Instruction instruction; |
| 95 |
|
| 96 |
instruction.setMnemonic(FCML_TEXT("mov")); |
| 97 |
instruction.setHints(FCML_HINT_FAR_POINTER | FCML_HINT_LONG_FORM_POINTER); |
| 98 |
instruction.getCondition().setConditionType(Condition::CONDITION_BE); |
| 99 |
instruction.getCondition().setNegation(FCML_TRUE); |
| 100 |
instruction.setOperandsCount(2); |
| 101 |
instruction.setPrefixes(FCML_PREFIX_LOCK | FCML_PREFIX_REPE); |
| 102 |
|
| 103 |
instruction[0].reg( Register::EAX() ); |
| 104 |
instruction[1].off( (fcml_int32_t)0x401000, FCML_DS_32 ); |
| 105 |
|
| 106 |
fcml_st_instruction instStr; |
| 107 |
|
| 108 |
TypeConverter::convert( instruction, instStr ); |
| 109 |
|
| 110 |
STF_ASSERT_STRING_EQUAL( instStr.mnemonic, FCML_TEXT("mov") ); |
| 111 |
STF_ASSERT_EQUAL( instStr.hints, FCML_HINT_FAR_POINTER | FCML_HINT_LONG_FORM_POINTER ); |
| 112 |
STF_ASSERT_EQUAL( instStr.operands_count, 2 ); |
| 113 |
STF_ASSERT_EQUAL( instStr.prefixes, FCML_PREFIX_LOCK | FCML_PREFIX_REPE ); |
| 114 |
STF_ASSERT_EQUAL( instStr.condition.condition_type, static_cast<fcml_en_condition_type>( instruction.getCondition().getConditionType() ) ); |
| 115 |
|
| 116 |
STF_ASSERT_EQUAL( FCML_OT_REGISTER, instStr.operands[0].type ); |
| 117 |
STF_ASSERT_EQUAL( FCML_REG_EAX, instStr.operands[0].reg.reg ); |
| 118 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[0].reg.size ); |
| 119 |
STF_ASSERT_EQUAL( FCML_REG_GPR, instStr.operands[0].reg.type ); |
| 120 |
STF_ASSERT_EQUAL( FCML_OT_ADDRESS, instStr.operands[1].type ); |
| 121 |
STF_ASSERT_EQUAL( FCML_AF_OFFSET, instStr.operands[1].address.address_form ); |
| 122 |
STF_ASSERT_EQUAL( FCML_TRUE, instStr.operands[1].address.offset.is_signed ); |
| 123 |
STF_ASSERT_EQUAL( 0x401000, instStr.operands[1].address.offset.off32 ); |
| 124 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].address.offset.size ); |
| 125 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].address.size_operator ); |
| 126 |
|
| 127 |
TypeConverter::free( instStr ); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
void fcml_tf_cpp_assemble_instruction_builder_short(void) { |
| 132 |
|
| 133 |
Instruction instruction; |
| 134 |
|
| 135 |
// Long form, don't be afraid short alternatives are presented below. |
| 136 |
instruction = IB::inst(_FT("mov")).farPtr().longFormPtr().lock().repe().reg( Register::EAX() ).off( (fcml_int32_t)0x401000, FCML_DS_32 ).build(); |
| 137 |
|
| 138 |
fcml_st_instruction instStr; |
| 139 |
|
| 140 |
TypeConverter::convert( instruction, instStr ); |
| 141 |
|
| 142 |
STF_ASSERT_STRING_EQUAL( instStr.mnemonic, FCML_TEXT("mov") ); |
| 143 |
STF_ASSERT_EQUAL( instStr.hints, FCML_HINT_FAR_POINTER | FCML_HINT_LONG_FORM_POINTER ); |
| 144 |
STF_ASSERT_EQUAL( instStr.operands_count, 2 ); |
| 145 |
STF_ASSERT_EQUAL( instStr.prefixes, FCML_PREFIX_LOCK | FCML_PREFIX_REPE ); |
| 146 |
STF_ASSERT_EQUAL( instStr.condition.condition_type, static_cast<fcml_en_condition_type>( instruction.getCondition().getConditionType() ) ); |
| 147 |
|
| 148 |
STF_ASSERT_EQUAL( FCML_OT_REGISTER, instStr.operands[0].type ); |
| 149 |
STF_ASSERT_EQUAL( FCML_REG_EAX, instStr.operands[0].reg.reg ); |
| 150 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[0].reg.size ); |
| 151 |
STF_ASSERT_EQUAL( FCML_REG_GPR, instStr.operands[0].reg.type ); |
| 152 |
STF_ASSERT_EQUAL( FCML_OT_ADDRESS, instStr.operands[1].type ); |
| 153 |
STF_ASSERT_EQUAL( FCML_AF_OFFSET, instStr.operands[1].address.address_form ); |
| 154 |
STF_ASSERT_EQUAL( FCML_TRUE, instStr.operands[1].address.offset.is_signed ); |
| 155 |
STF_ASSERT_EQUAL( 0x401000, instStr.operands[1].address.offset.off32 ); |
| 156 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].address.offset.size ); |
| 157 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].address.size_operator ); |
| 158 |
|
| 159 |
TypeConverter::free( instStr ); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
void fcml_tf_util_check_instruction( Instruction &instruction ) { |
| 164 |
|
| 165 |
fcml_st_instruction instStr; |
| 166 |
|
| 167 |
TypeConverter::convert( instruction, instStr ); |
| 168 |
|
| 169 |
STF_ASSERT_STRING_EQUAL( instStr.mnemonic, FCML_TEXT("mov") ); |
| 170 |
STF_ASSERT_EQUAL( instStr.hints, 0 ); |
| 171 |
STF_ASSERT_EQUAL( instStr.operands_count, 2 ); |
| 172 |
STF_ASSERT_EQUAL( instStr.prefixes, 0 ); |
| 173 |
STF_ASSERT_EQUAL( instStr.condition.condition_type, static_cast<fcml_en_condition_type>( instruction.getCondition().getConditionType() ) ); |
| 174 |
|
| 175 |
STF_ASSERT_EQUAL( FCML_OT_ADDRESS, instStr.operands[0].type ); |
| 176 |
STF_ASSERT_EQUAL( FCML_AF_COMBINED, instStr.operands[0].address.address_form ); |
| 177 |
STF_ASSERT_EQUAL( FCML_REG_EAX, instStr.operands[0].address.effective_address.base.reg ); |
| 178 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[0].address.effective_address.base.size ); |
| 179 |
STF_ASSERT_EQUAL( FCML_REG_GPR, instStr.operands[0].address.effective_address.base.type ); |
| 180 |
STF_ASSERT_EQUAL( FCML_FALSE, instStr.operands[0].address.effective_address.base.x64_exp ); |
| 181 |
STF_ASSERT_EQUAL( FCML_DS_8, instStr.operands[0].address.size_operator ); |
| 182 |
|
| 183 |
STF_ASSERT_EQUAL( FCML_OT_REGISTER, instStr.operands[1].type ); |
| 184 |
STF_ASSERT_EQUAL( FCML_REG_ECX, instStr.operands[1].reg.reg ); |
| 185 |
STF_ASSERT_EQUAL( FCML_DS_32, instStr.operands[1].reg.size ); |
| 186 |
STF_ASSERT_EQUAL( FCML_REG_GPR, instStr.operands[1].reg.type ); |
| 187 |
|
| 188 |
TypeConverter::free( instStr ); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
void fcml_tf_cpp_assemble_instruction_builder_shortest(void) { |
| 193 |
|
| 194 |
Instruction instruction; |
| 195 |
|
| 196 |
instruction = IB(M_MOV).eff( EAX, 8 ).reg( ECX ).build(); |
| 197 |
|
| 198 |
fcml_tf_util_check_instruction( instruction ); |
| 199 |
|
| 200 |
instruction = IB::inst(M_MOV).eff( EAX, 8 ).reg( ECX ).build(); |
| 201 |
|
| 202 |
fcml_tf_util_check_instruction( instruction ); |
| 203 |
|
| 204 |
instruction = IB::inst(M_MOV).eff( EAX, 8 ).reg( ECX ); |
| 205 |
|
| 206 |
fcml_tf_util_check_instruction( instruction ); |
| 207 |
|
| 208 |
instruction = IB(M_MOV).eff( EAX, 8 ).reg( ECX ); |
| 209 |
|
| 210 |
instruction = IB(M_MOV) << OB::eff( EAX, 8 ) << OB::reg( ECX ); |
| 211 |
|
| 212 |
fcml_tf_util_check_instruction( instruction ); |
| 213 |
|
| 214 |
instruction = IB(M_MOV) << OB::effb( EAX ) << OB::reg( ECX ); |
| 215 |
|
| 216 |
fcml_tf_util_check_instruction( instruction ); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
void fcml_tf_cpp_assemble_instruction_builder_hints_prefixes(void) { |
| 221 |
|
| 222 |
Instruction instruction; |
| 223 |
|
| 224 |
instruction = IB(M_MOV) << IB::LOCK() << IB::REP() << IB::INDIRECT_PTR() << IB::NEAR_PTR() << OB::effb( EAX ) << OB::reg( ECX ); |
| 225 |
|
| 226 |
STF_ASSERT( instruction.getMnemonic() == fcml_cstring(FCML_TEXT("mov")) ); |
| 227 |
STF_ASSERT( instruction.getMnemonic() == M_MOV ); |
| 228 |
STF_ASSERT_EQUAL( instruction.getPrefixes(), FCML_PREFIX_LOCK | FCML_PREFIX_REP ); |
| 229 |
STF_ASSERT( instruction.isLock() ); |
| 230 |
STF_ASSERT( instruction.isRep() ); |
| 231 |
STF_ASSERT_EQUAL( instruction.getHints(), FCML_HINT_INDIRECT_POINTER | FCML_HINT_NEAR_POINTER ); |
| 232 |
STF_ASSERT( instruction.isIndirectPointer() ); |
| 233 |
STF_ASSERT( instruction.isNearPointer() ); |
| 234 |
} |
| 235 |
|
| 236 |
fcml_bool fcml_tf_cmp_instruction( const AssembledInstruction *assembledInstruction, fcml_uint8_t *instruction, fcml_usize len ) { |
| 237 |
if( !assembledInstruction ) { |
| 238 |
return FCML_FALSE; |
| 239 |
} |
| 240 |
if( assembledInstruction->getCodeLength() == len ) { |
| 241 |
for( fcml_usize i = 0; i < len; i++ ) { |
| 242 |
if( assembledInstruction->getCode()[i] != instruction[i] ) { |
| 243 |
return FCML_FALSE; |
| 244 |
} |
| 245 |
} |
| 246 |
return FCML_TRUE; |
| 247 |
} |
| 248 |
return FCML_FALSE; |
| 249 |
} |
| 250 |
|
| 251 |
const fcml_string fcml_iarr_lag_assembler_code_src[] = { |
| 252 |
"start: mov ebx, 1", |
| 253 |
"loop_big: inc ebx", |
| 254 |
" cmp ebx, 10", |
| 255 |
" je finish", |
| 256 |
"loop_small: mov eax, 1", |
| 257 |
"increment: inc eax", |
| 258 |
" cmp eax, 10", |
| 259 |
" je finish_small", |
| 260 |
" jmp increment", |
| 261 |
"finish_small:", |
| 262 |
" jmp loop_big", |
| 263 |
"finish: ret", |
| 264 |
NULL |
| 265 |
}; |
| 266 |
|
| 267 |
const fcml_uint8_t fcml_iarr_lag_assembler_code_bin[] = { |
| 268 |
0xBB, 0x01, 0x00, 0x00, 0x00, |
| 269 |
0x43, |
| 270 |
0x83, 0xFB, 0x0A, |
| 271 |
0x74, 0x0F, |
| 272 |
0xB8, 0x01, 0x00, 0x00, 0x00, |
| 273 |
0x40, |
| 274 |
0x83, 0xF8, 0x0A, |
| 275 |
0x74, 0x02, |
| 276 |
0xEB, 0xF8, |
| 277 |
0xEB, 0xEB, |
| 278 |
0xC3 |
| 279 |
}; |
| 280 |
|
| 281 |
const fcml_string fcml_iarr_lag_assembler_code_src_error[] = { |
| 282 |
"start: mov ebx, 1", |
| 283 |
"loop_big: inc ebx", |
| 284 |
" cmp ebx, 10", |
| 285 |
" je finish_xxxxxx", // Unknown symbol. |
| 286 |
"loop_small: mov eax, 1", |
| 287 |
"increment: inc eax", |
| 288 |
" cmp eax, 10", |
| 289 |
" je finish_small", |
| 290 |
" jmp increment", |
| 291 |
"finish_small:", |
| 292 |
" jmp loop_big", |
| 293 |
"finish: ret", |
| 294 |
NULL |
| 295 |
}; |
| 296 |
|
| 297 |
const fcml_string fcml_iarr_lag_assembler_code_src_error_1[] = { |
| 298 |
"start: mov ebx, 1", |
| 299 |
"loop_big: inc eb, p][]s ddx", // Syntax error. |
| 300 |
" cmp ebx, 10", |
| 301 |
" je finish", // Unknown symbol. |
| 302 |
"loop_small: mov eax, 1", |
| 303 |
"increment: inc eax", |
| 304 |
" cmp eax, 10", |
| 305 |
" je finish_small", |
| 306 |
" jmp increment", |
| 307 |
"finish_small:", |
| 308 |
" jmp loop_big", |
| 309 |
"finish: ret", |
| 310 |
NULL |
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
void fcml_tf_cpp_multi_pass_assemble(void) { |
| 315 |
|
| 316 |
fcml_ceh_error error; |
| 317 |
|
| 318 |
try { |
| 319 |
|
| 320 |
IntelDialect dialect; |
| 321 |
|
| 322 |
MultiPassAssemblerContext ctx( EntryPoint::OM_32_BIT, 0x401000 ); |
| 323 |
|
| 324 |
MultiPassAssemblerResult result; |
| 325 |
|
| 326 |
MultiPassAssembler assembler(dialect); |
| 327 |
|
| 328 |
error = assembler.assemble( ctx, fcml_iarr_lag_assembler_code_src, result ); |
| 329 |
|
| 330 |
STF_ASSERT_EQUAL( error, FCML_CEH_GEC_NO_ERROR ); |
| 331 |
|
| 332 |
if( !error ) { |
| 333 |
|
| 334 |
fcml_int i = 0; |
| 335 |
CodeIterator it = result.getCodeIterator(); |
| 336 |
while( it.hasNext() ) { |
| 337 |
fcml_uint8_t codeByte = it.next(); |
| 338 |
if( codeByte != fcml_iarr_lag_assembler_code_bin[i++] ) { |
| 339 |
STF_FAIL( "Assembled code is not the same." ); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if( i != sizeof( fcml_iarr_lag_assembler_code_bin ) ) { |
| 344 |
STF_FAIL( "Assembled code is not the same." ); |
| 345 |
} |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
} catch( BaseException &exc ) { |
| 350 |
STF_FAIL("Exception while assembling the code."); |
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
void fcml_tf_cpp_multi_pass_assemble_error(void) { |
| 356 |
|
| 357 |
try { |
| 358 |
|
| 359 |
IntelDialect dialect; |
| 360 |
|
| 361 |
MultiPassAssemblerContext ctx( EntryPoint::OM_32_BIT, 0x401000 ); |
| 362 |
|
| 363 |
MultiPassAssemblerResult result; |
| 364 |
|
| 365 |
MultiPassAssembler assembler(dialect); |
| 366 |
|
| 367 |
try { |
| 368 |
assembler.assemble( ctx, fcml_iarr_lag_assembler_code_src_error, result ); |
| 369 |
STF_FAIL( "Should fail!" ); |
| 370 |
} catch( AssemblingFailedException &exc ) { |
| 371 |
// Undefined symbol, see the assembled source code. |
| 372 |
STF_ASSERT_EQUAL( exc.getMsg(), fcml_cstring( FCML_TEXT( "Assembling failed: Undefined symbol: finish_xxxxxx." ) ) ); |
| 373 |
STF_ASSERT_EQUAL( exc.getError(), FCML_CEH_GEC_UNDEFINED_SYMBOL ); |
| 374 |
STF_ASSERT( true ); // OK. |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
} catch( BaseException &exc ) { |
| 379 |
STF_FAIL("Exception while assembling the code."); |
| 380 |
} |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
void fcml_tf_cpp_multi_pass_assemble_error_1(void) { |
| 385 |
|
| 386 |
try { |
| 387 |
|
| 388 |
IntelDialect dialect; |
| 389 |
|
| 390 |
MultiPassAssemblerContext ctx( EntryPoint::OM_32_BIT, 0x401000 ); |
| 391 |
|
| 392 |
MultiPassAssemblerResult result; |
| 393 |
|
| 394 |
MultiPassAssembler assembler(dialect); |
| 395 |
|
| 396 |
try { |
| 397 |
assembler.assemble( ctx, fcml_iarr_lag_assembler_code_src_error_1, result ); |
| 398 |
STF_FAIL( "Should fail!" ); |
| 399 |
} catch( AssemblingFailedException &exc ) { |
| 400 |
// Undefined symbol, see the assembled source code. |
| 401 |
STF_ASSERT_EQUAL( exc.getMsg(), fcml_cstring( FCML_TEXT( "Assembling failed: syntax error" ) ) ); |
| 402 |
STF_ASSERT_EQUAL( exc.getError(), FCML_CEH_GEC_INVALID_INPUT ); |
| 403 |
STF_ASSERT( true ); // OK. |
| 404 |
} |
| 405 |
|
| 406 |
} catch( BaseException &exc ) { |
| 407 |
STF_FAIL("Exception while assembling the code."); |
| 408 |
} |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
void fcml_tf_cpp_assemble(void) { |
| 413 |
|
| 414 |
fcml_ceh_error error; |
| 415 |
|
| 416 |
try { |
| 417 |
|
| 418 |
IntelDialect dialect; |
| 419 |
|
| 420 |
AssemblerContext ctx( EntryPoint::OM_32_BIT, 0x401000 ); |
| 421 |
|
| 422 |
AssemblerResult result; |
| 423 |
|
| 424 |
Assembler assembler(dialect); |
| 425 |
|
| 426 |
Instruction instruction = IB(M_MOV) << OB::reg(EAX) << OB::offd( 0x40302010 ); |
| 427 |
|
| 428 |
error = assembler.assemble( ctx, instruction, result ); |
| 429 |
STF_ASSERT_EQUAL( error, FCML_CEH_GEC_NO_ERROR ); |
| 430 |
|
| 431 |
if( !error ) { |
| 432 |
fcml_uint8_t inst[] = {0xA1, 0x10, 0x20, 0x30, 0x40}; |
| 433 |
STF_ASSERT( fcml_tf_cmp_instruction( result.getChosenInstruction(), inst, 5 ) ); |
| 434 |
|
| 435 |
// Checks access to the constant assembled instruction. |
| 436 |
const AssembledInstruction chosenInstruction = *result.getChosenInstruction(); |
| 437 |
const fcml_uint8_t *buffer = chosenInstruction.getCode(); |
| 438 |
STF_ASSERT_PTR_NOT_NULL( buffer ); |
| 439 |
fcml_usize len = chosenInstruction.getCodeLength(); |
| 440 |
STF_ASSERT_EQUAL( len, 5 ); |
| 441 |
const ErrorContainer &errors = chosenInstruction.getWarningContainer(); |
| 442 |
STF_ASSERT_EQUAL( errors.getSize(), 0 ); |
| 443 |
try { |
| 444 |
errors.getFirstError(); |
| 445 |
STF_FAIL("Container is empty."); |
| 446 |
} catch( IllegalStateException &exc ) { |
| 447 |
STF_ASSERT( true ); |
| 448 |
} |
| 449 |
STF_ASSERT("" == errors.getFirstErrorMessage() ); |
| 450 |
} |
| 451 |
|
| 452 |
} catch( BaseException &exc ) { |
| 453 |
STF_FAIL("Exception while assembling the code."); |
| 454 |
} |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
/* A 64-bit GDB dump used as an example. |
| 459 |
Dump of assembler code for function main(int, char**): |
| 460 |
0x0000000000404ddc <+0>: 55 push %rbp |
| 461 |
0x0000000000404ddd <+1>: 48 89 e5 mov %rsp,%rbp |
| 462 |
0x0000000000404de0 <+4>: 48 83 ec 20 sub $0x20,%rsp |
| 463 |
0x0000000000404de4 <+8>: 89 7d ec mov %edi,-0x14(%rbp) |
| 464 |
0x0000000000404de7 <+11>: 48 89 75 e0 mov %rsi,-0x20(%rbp) |
| 465 |
0x0000000000404deb <+15>: be 60 d2 62 00 mov $0x62d260,%esi |
| 466 |
0x0000000000404df0 <+20>: bf e8 db 41 00 mov $0x41dbe8,%edi |
| 467 |
0x0000000000404df5 <+25>: e8 1f 89 01 00 callq 0x41d719 |
| 468 |
*/ |
| 469 |
|
| 470 |
fcml_uint8_t fcml_iarr_stateful_assembler_result_expected[] = { |
| 471 |
0x55, |
| 472 |
0x48, 0x8b, 0xec, // (It's just a different way to encode "mov %rsp,%rbp", everything is OK.) |
| 473 |
0x48, 0x83, 0xec, 0x20, |
| 474 |
0x89, 0x7d, 0xec, |
| 475 |
0x48, 0x89, 0x75, 0xe0, |
| 476 |
0xbe, 0x60, 0xd2, 0x62, 0x00, |
| 477 |
0xbf, 0xe8, 0xdb, 0x41, 0x00, |
| 478 |
0xe8, 0x1f, 0x89, 0x01, 0x00 |
| 479 |
}; |
| 480 |
|
| 481 |
/* Tests CodeBuffer helper class. */ |
| 482 |
void fcml_tf_cpp_assemble_statefull_assembler(void) { |
| 483 |
|
| 484 |
try { |
| 485 |
|
| 486 |
// Only because Intel mnemonics had been already included. |
| 487 |
IntelDialect dialect; |
| 488 |
|
| 489 |
AssemblerContext ctx( EntryPoint::OM_64_BIT, 0x404ddc ); |
| 490 |
|
| 491 |
Assembler assembler(dialect); |
| 492 |
|
| 493 |
StatefulAssembler sasm(assembler, ctx); |
| 494 |
|
| 495 |
sasm << M_PUSH << OB::reg(RBP); |
| 496 |
sasm << M_MOV << OB::reg(RBP) << OB::reg(RSP); |
| 497 |
// Check direct use of the immediate and register operands. |
| 498 |
sasm << M_SUB << RSP << 0x20; |
| 499 |
sasm << M_MOV << OB::effd(RBP,-0x14) << OB::reg(EDI); |
| 500 |
sasm << M_MOV << OB::effq(RBP,-0x20) << OB::reg(RSI); |
| 501 |
sasm << M_MOV << OB::reg(ESI) << OB::imm(0x62d260); |
| 502 |
// And using plain methods. |
| 503 |
sasm.inst(M_MOV).op(EDI).op(0x41dbe8); |
| 504 |
// Flush instruction builder immediately. |
| 505 |
sasm << M_CALL << InstructionHint::DIRECT_POINTER() << OB::offq(0x41d719) << StatefulAssembler::FLUSH(); |
| 506 |
|
| 507 |
fcml_uint8_t *result_bytes = fcml_iarr_stateful_assembler_result_expected; |
| 508 |
|
| 509 |
bool failed = false; |
| 510 |
|
| 511 |
CodeIterator it = sasm.getCodeIterator(); |
| 512 |
while( it.hasNext() ) { |
| 513 |
fcml_uint8_t b = it.next(); |
| 514 |
if( *result_bytes++ != b ) { |
| 515 |
failed = true; |
| 516 |
break; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if( failed ) { |
| 521 |
STF_FAIL( "Assembled code and result differs." ); |
| 522 |
} else { |
| 523 |
STF_ASSERT( true ); |
| 524 |
} |
| 525 |
|
| 526 |
} catch( BaseException &exc ) { |
| 527 |
STF_FAIL("Exception while assembling the code."); |
| 528 |
} |
| 529 |
|
| 530 |
} |
| 531 |
|
| 532 |
/* Tests CodeBuffer helper class. */ |
| 533 |
void fcml_tf_cpp_assemble_stateful_assembler_with_parser(void) { |
| 534 |
|
| 535 |
try { |
| 536 |
|
| 537 |
// Only because Intel mnemonics had been already included. |
| 538 |
IntelDialect dialect; |
| 539 |
|
| 540 |
AssemblerContext ctx( EntryPoint::OM_64_BIT, 0x404ddc ); |
| 541 |
|
| 542 |
Assembler assembler(dialect); |
| 543 |
|
| 544 |
StatefulAssembler sasm(assembler, ctx, true); |
| 545 |
|
| 546 |
// Enable symbols support. |
| 547 |
sasm.getParserConfig().setDisableSymbolsDeclaration(false); |
| 548 |
|
| 549 |
// By the way, check if symbols works as expected. |
| 550 |
SymbolTable st; |
| 551 |
st.add( _FT("_start"), 0x41d719 ); |
| 552 |
sasm.setSymbolTable( &st ); |
| 553 |
|
| 554 |
sasm << _FT( "push rbp" ); |
| 555 |
sasm << _FT( "mov rbp, rsp" ); |
| 556 |
sasm << _FT( "sub rsp, 0x20" ); |
| 557 |
sasm << _FT( "_label: mov dword [rbp-0x14], edi" ); |
| 558 |
sasm << _FT( "mov qword [rbp-0x20], rsi" ); |
| 559 |
sasm << _FT( "mov esi, 0x62d260" ); |
| 560 |
sasm << _FT( "mov edi, 0x41dbe8" ); |
| 561 |
sasm << _FT( "call _start" ); |
| 562 |
|
| 563 |
Symbol symbol = st.get( _FT("_label") ); |
| 564 |
const Symbol symbolConst = symbol; |
| 565 |
STF_ASSERT( !symbolConst.isEmpty() ); |
| 566 |
STF_ASSERT_EQUAL( symbolConst.getValue(), 0x404de4 ); |
| 567 |
|
| 568 |
const SymbolTable &stConst = st; |
| 569 |
|
| 570 |
fcml_int64_t label = stConst.get( _FT("_label") ); |
| 571 |
STF_ASSERT_EQUAL( label, 0x404de4 ); |
| 572 |
|
| 573 |
// Check if generated code is the same as expected one. |
| 574 |
|
| 575 |
fcml_uint8_t *result_bytes = fcml_iarr_stateful_assembler_result_expected; |
| 576 |
|
| 577 |
bool failed = false; |
| 578 |
|
| 579 |
CodeIterator it = sasm.getCodeIterator(); |
| 580 |
while( it.hasNext() ) { |
| 581 |
fcml_uint8_t b = it.next(); |
| 582 |
if( *result_bytes++ != b ) { |
| 583 |
failed = true; |
| 584 |
break; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if( failed ) { |
| 589 |
STF_FAIL( "Assembled code and result differs." ); |
| 590 |
} else { |
| 591 |
STF_ASSERT( true ); |
| 592 |
} |
| 593 |
|
| 594 |
std::vector<AssembledInstruction>& instructions = sasm.getAssembledInstructions(); |
| 595 |
STF_ASSERT_EQUAL( instructions.size(), 8 ); |
| 596 |
|
| 597 |
} catch( BaseException &exc ) { |
| 598 |
STF_FAIL("Exception while assembling the code."); |
| 599 |
} |
| 600 |
|
| 601 |
} |
| 602 |
|
| 603 |
fcml_stf_test_case fcml_ti_cpp_assembler[] = { |
| 604 |
{ "fcml_tf_cpp_assemble_instruction_long_form", fcml_tf_cpp_assemble_instruction_long_form }, |
| 605 |
{ "fcml_tf_cpp_assemble_instruction_short_form", fcml_tf_cpp_assemble_instruction_short_form }, |
| 606 |
{ "fcml_tf_cpp_assemble_instruction_builder_short", fcml_tf_cpp_assemble_instruction_builder_short }, |
| 607 |
{ "fcml_tf_cpp_assemble_instruction_builder_shortest", fcml_tf_cpp_assemble_instruction_builder_shortest }, |
| 608 |
{ "fcml_tf_cpp_assemble_instruction_builder_hints_prefixes", fcml_tf_cpp_assemble_instruction_builder_hints_prefixes }, |
| 609 |
{ "fcml_tf_cpp_multi_pass_assemble", fcml_tf_cpp_multi_pass_assemble }, |
| 610 |
{ "fcml_tf_cpp_multi_pass_assemble_error", fcml_tf_cpp_multi_pass_assemble_error }, |
| 611 |
{ "fcml_tf_cpp_multi_pass_assemble_error_1", fcml_tf_cpp_multi_pass_assemble_error_1 }, |
| 612 |
{ "fcml_tf_cpp_assemble", fcml_tf_cpp_assemble }, |
| 613 |
{ "fcml_tf_cpp_assemble_statefull_assembler", fcml_tf_cpp_assemble_statefull_assembler }, |
| 614 |
{ "fcml_tf_cpp_assemble_stateful_assembler_with_parser", fcml_tf_cpp_assemble_stateful_assembler_with_parser }, |
| 615 |
FCML_STF_NULL_TEST |
| 616 |
}; |
| 617 |
|
| 618 |
fcml_stf_test_suite fcml_si_cpp_assembler = { |
| 619 |
"suite-fcml-cpp-assembler", fcml_tf_cpp_assembler_suite_init, fcml_tf_cpp_assembler_suite_cleanup, fcml_ti_cpp_assembler |
| 620 |
}; |