Skip to content

The UC instance must be created twice to reproduce the problem #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions samples/sample_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,14 @@ static void test_thumb(void)
uc_close(uc);
}

static void test_thumb_ite() {
static void test_thumb_ite_internal(bool step, uint32_t *r2_out, uint32_t *r3_out)
{
uc_engine *uc;
uc_err err;

uint32_t sp = 0x1234;
uint32_t r2 = 0, r3 = 1;
uint32_t step_r2, step_r3;

int i, addr=ADDRESS;

printf("Emulate a THUMB ITE block as a whole or per instruction.\n");
err = uc_open(UC_ARCH_ARM, UC_MODE_THUMB, &uc);
if (err) {
printf("Failed on uc_open() with error returned: %u (%s)\n",
Expand All @@ -159,42 +156,55 @@ static void test_thumb_ite() {
uc_reg_write(uc, UC_ARM_REG_R2, &r2);
uc_reg_write(uc, UC_ARM_REG_R3, &r3);

// Run once.
printf("Running the entire binary.\n");
err = uc_emu_start(uc, ADDRESS | 1, ADDRESS + sizeof(ARM_THUM_COND_CODE) - 1, 0, 0);
if (err) {
printf("Failed on uc_emu_start() with error returned: %u\n", err);
if (!step) {
err = uc_emu_start(uc, ADDRESS | 1, ADDRESS + sizeof(ARM_THUM_COND_CODE) - 1, 0, 0);
if (err) {
printf("Failed on uc_emu_start() with error returned: %u\n", err);
}
} else {
int i, addr = ADDRESS;
for (i = 0; i < sizeof(ARM_THUM_COND_CODE) / 2; i++) {
err = uc_emu_start(uc, addr | 1, ADDRESS + sizeof(ARM_THUM_COND_CODE) - 1, 0, 1);
if (err) {
printf("Failed on uc_emu_start() with error returned: %u\n", err);
}
uc_reg_read(uc, UC_ARM_REG_PC, &addr);
}
}

uc_reg_read(uc, UC_ARM_REG_R2, &r2);
uc_reg_read(uc, UC_ARM_REG_R3, &r3);

uc_close(uc);

*r2_out = r2;
*r3_out = r3;
}

static void test_thumb_ite()
{
uint32_t r2, r3;
uint32_t step_r2, step_r3;

printf("Emulate a THUMB ITE block as a whole or per instruction.\n");

// Run once.
printf("Running the entire binary.\n");
test_thumb_ite_internal(false, &r2, &r3);
printf(">>> R2: %d\n", r2);
printf(">>> R3: %d\n\n", r3);

// Step each instruction.
printf("Running the binary one instruction at a time.\n");
for (i = 0; i < sizeof(ARM_THUM_COND_CODE) / 2; i++) {
err = uc_emu_start(uc, addr | 1, ADDRESS + sizeof(ARM_THUM_COND_CODE) - 1, 0, 1);
if (err) {
printf("Failed on uc_emu_start() with error returned: %u\n", err);
}
uc_reg_read(uc, UC_ARM_REG_PC, &addr);
}

uc_reg_read(uc, UC_ARM_REG_R2, &step_r2);
uc_reg_read(uc, UC_ARM_REG_R3, &step_r3);

test_thumb_ite_internal(true, &step_r2, &step_r3);
printf(">>> R2: %d\n", step_r2);
printf(">>> R3: %d\n\n", step_r3);

if (step_r2 != r2 || step_r3 != r3) {
printf("Failed with ARM ITE blocks stepping!\n");
}

uc_close(uc);
}


int main(int argc, char **argv, char **envp)
{
// dynamically load shared library
Expand Down