2016-06-15 20:18:51 +03:00
|
|
|
/*
|
2017-01-21 04:28:22 +03:00
|
|
|
refer to issue #575.
|
|
|
|
to run correctly unicorn needs to be compiled for AArch64.
|
2016-06-15 20:18:51 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicorn_test.h"
|
|
|
|
#include <stdio.h>
|
2017-01-20 16:13:21 +03:00
|
|
|
#include "unicorn/platform.h"
|
2016-06-15 20:18:51 +03:00
|
|
|
|
|
|
|
uint64_t trunc_page(uint64_t addr)
|
|
|
|
{
|
2017-01-21 04:28:22 +03:00
|
|
|
return (addr & ~(4095));
|
2016-06-15 20:18:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-15 21:44:22 +03:00
|
|
|
/* Called before every test to set up a new instance */
|
|
|
|
static int init(void **state)
|
|
|
|
{
|
2017-01-21 04:28:22 +03:00
|
|
|
printf("[+] Initializing Unicorn...\n");
|
|
|
|
uc_engine *uc;
|
2016-06-15 21:44:22 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
if (uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc) != UC_ERR_OK) {
|
|
|
|
printf("Error on open. Be sure that your unicorn library supports AArch64.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-15 21:44:22 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
*state = uc;
|
2016-06-15 21:44:22 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
return 0;
|
2016-06-15 21:44:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called after every test to clean up */
|
|
|
|
static int teardown(void **state)
|
|
|
|
{
|
2017-01-21 04:28:22 +03:00
|
|
|
printf("[+] Exiting...\n");
|
|
|
|
uc_engine *uc = *state;
|
2016-06-15 21:44:22 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
uc_close(uc);
|
2016-06-15 21:44:22 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
*state = NULL;
|
|
|
|
return 0;
|
2016-06-15 21:44:22 +03:00
|
|
|
}
|
|
|
|
|
2016-06-15 21:52:28 +03:00
|
|
|
void test_hang(void **state)
|
2016-06-15 20:18:51 +03:00
|
|
|
{
|
2017-01-21 04:28:22 +03:00
|
|
|
uint32_t code[] = {
|
|
|
|
0xd503201f, /* NOP */
|
|
|
|
0xd503201f, /* NOP */
|
|
|
|
0xd503201f, /* NOP */
|
|
|
|
0xaa0103e0 /* MOV X0, X1 */
|
|
|
|
};
|
|
|
|
|
|
|
|
uc_engine *uc = *state;
|
|
|
|
|
|
|
|
uint64_t x0 = 0;
|
|
|
|
uint64_t x1 = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* emulation will hang if some instruction hits every quarter of a page,
|
|
|
|
* i.e. these offsets:
|
|
|
|
* 0x1400, 0x1800, 0x1c00, 0x2000
|
|
|
|
*
|
|
|
|
* in this test, the code to be emulated is mapped just before the 0x1400
|
|
|
|
* offset, so that the final instruction emulated (MOV X0, X1) hits the offset,
|
|
|
|
* causing the hang.
|
|
|
|
* If you try to write the code just four bytes behind, the hang doesn't occur.
|
|
|
|
*
|
|
|
|
* So far, this strange behaviour has only been observed with AArch64 Unicorn APIs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint64_t addr = 0x13f0; // try to map at (0x13f0 - 0x4) and the hang doesn't occur
|
|
|
|
uint64_t trunc_addr = trunc_page(addr); // round down to nearest page
|
|
|
|
|
|
|
|
uc_mem_map(uc, trunc_addr, 2 * 1024 * 1024, UC_PROT_ALL);
|
|
|
|
|
|
|
|
if (uc_mem_write(uc, addr, &code, sizeof(code))) {
|
|
|
|
printf("error on write\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_reg_write(uc, UC_ARM64_REG_X0, &x0);
|
|
|
|
uc_reg_write(uc, UC_ARM64_REG_X1, &x1);
|
|
|
|
|
|
|
|
if (uc_emu_start(uc, addr, addr + sizeof(code), 0, 0)) {
|
|
|
|
printf("error on start\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_reg_read(uc, UC_ARM64_REG_X0, &x0);
|
|
|
|
uc_reg_read(uc, UC_ARM64_REG_X1, &x1);
|
|
|
|
|
|
|
|
printf("x0: %"PRIx64"\n", x0);
|
|
|
|
printf("x1: %"PRIx64"\n", x1);
|
2016-06-15 21:44:22 +03:00
|
|
|
}
|
2016-06-15 20:18:51 +03:00
|
|
|
|
2016-06-15 21:44:22 +03:00
|
|
|
int main(int argc, const char * argv[]) {
|
2016-06-15 20:18:51 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(test_hang, init, teardown),
|
|
|
|
};
|
2016-06-15 20:18:51 +03:00
|
|
|
|
2017-01-21 04:28:22 +03:00
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);;
|
2016-06-15 20:18:51 +03:00
|
|
|
}
|