Skip to content

Commit f332346

Browse files
committed
Format
1 parent 822bb52 commit f332346

File tree

3 files changed

+57
-51
lines changed

3 files changed

+57
-51
lines changed

samples/sample_arm64.c

+17-16
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,13 @@ static void test_arm64_hook_mrs(void)
293293
uc_close(uc);
294294
}
295295

296-
297-
#define CHECK(x) do { \
298-
if((x) != UC_ERR_OK) { \
299-
fprintf(stderr, "FAIL at %s:%d: %s\n", __FILE__, __LINE__, #x); \
300-
exit(1); \
301-
} \
302-
} while(0)
303-
296+
#define CHECK(x) \
297+
do { \
298+
if ((x) != UC_ERR_OK) { \
299+
fprintf(stderr, "FAIL at %s:%d: %s\n", __FILE__, __LINE__, #x); \
300+
exit(1); \
301+
} \
302+
} while (0)
304303

305304
/* Test PAC support in the emulator. Code adapted from
306305
https://github.com/unicorn-engine/unicorn/issues/1789#issuecomment-1536320351 */
@@ -309,16 +308,17 @@ static void test_arm64_pac(void)
309308
uc_engine *uc;
310309
uint64_t x1 = 0x0000aaaabbbbccccULL;
311310

312-
// paciza x1
313-
#define ARM64_PAC_CODE "\xe1\x23\xc1\xda"
311+
// paciza x1
312+
#define ARM64_PAC_CODE "\xe1\x23\xc1\xda"
314313

315314
printf("Try ARM64 PAC\n");
316315

317316
// Initialize emulator in ARM mode
318317
CHECK(uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc));
319318
CHECK(uc_ctl_set_cpu_model(uc, UC_CPU_ARM64_MAX));
320319
CHECK(uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL));
321-
CHECK(uc_mem_write(uc, ADDRESS, ARM64_PAC_CODE, sizeof(ARM64_PAC_CODE) - 1));
320+
CHECK(
321+
uc_mem_write(uc, ADDRESS, ARM64_PAC_CODE, sizeof(ARM64_PAC_CODE) - 1));
322322
CHECK(uc_reg_write(uc, UC_ARM64_REG_X1, &x1));
323323

324324
/** Initialize PAC support **/
@@ -334,7 +334,7 @@ static void test_arm64_pac(void)
334334
CHECK(uc_reg_read(uc, UC_ARM64_REG_CP_REG, &reg));
335335

336336
// NS && RW && API
337-
reg.val |= (1 | (1<<10) | (1<<17));
337+
reg.val |= (1 | (1 << 10) | (1 << 17));
338338

339339
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
340340

@@ -348,10 +348,10 @@ static void test_arm64_pac(void)
348348
CHECK(uc_reg_read(uc, UC_ARM64_REG_CP_REG, &reg));
349349

350350
// EnIA && EnIB
351-
reg.val |= (1<<31) | (1<<30);
351+
reg.val |= (1 << 31) | (1 << 30);
352352

353353
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
354-
354+
355355
// HCR_EL2
356356
reg.op0 = 0b11;
357357
reg.op1 = 0b100;
@@ -360,12 +360,13 @@ static void test_arm64_pac(void)
360360
reg.op2 = 0b000;
361361

362362
// HCR.API
363-
reg.val |= (1ULL<<41);
363+
reg.val |= (1ULL << 41);
364364

365365
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
366366

367367
/** Check that PAC worked **/
368-
CHECK(uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM64_PAC_CODE) - 1, 0, 0));
368+
CHECK(
369+
uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM64_PAC_CODE) - 1, 0, 0));
369370
CHECK(uc_reg_read(uc, UC_ARM64_REG_X1, &x1));
370371

371372
printf("X1 = 0x%" PRIx64 "\n", x1);

tests/unit/test_mem.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ static void test_snapshot_with_vtlb(void)
333333
uc_hook hook;
334334

335335
// mov eax, [0x2020]; inc eax; mov [0x2020], eax
336-
char code[] = "\xA1\x20\x20\x00\x00\x04\x00\x00\x00\xFF\xC0\xA3\x20\x20\x00\x00\x04\x00\x00\x00";
336+
char code[] = "\xA1\x20\x20\x00\x00\x04\x00\x00\x00\xFF\xC0\xA3\x20\x20\x00"
337+
"\x00\x04\x00\x00\x00";
337338

338339
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
339340

@@ -342,9 +343,9 @@ static void test_snapshot_with_vtlb(void)
342343
OK(uc_context_alloc(uc, &c1));
343344
OK(uc_ctl_context_mode(uc, UC_CTL_CONTEXT_MEMORY));
344345

345-
346346
OK(uc_ctl_tlb_mode(uc, UC_TLB_VIRTUAL));
347-
OK(uc_hook_add(uc, &hook, UC_HOOK_TLB_FILL, test_snapshot_with_vtlb_callback, NULL, 1, 0));
347+
OK(uc_hook_add(uc, &hook, UC_HOOK_TLB_FILL,
348+
test_snapshot_with_vtlb_callback, NULL, 1, 0));
348349

349350
// Map physical memory
350351
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_EXEC | UC_PROT_READ));
@@ -354,11 +355,13 @@ static void test_snapshot_with_vtlb(void)
354355
// Initial context save
355356
OK(uc_context_save(uc, c0));
356357

357-
OK(uc_emu_start(uc, 0x400000000 + 0x1000, 0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
358+
OK(uc_emu_start(uc, 0x400000000 + 0x1000,
359+
0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
358360
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
359361
TEST_CHECK(mem == 1);
360362
OK(uc_context_save(uc, c1));
361-
OK(uc_emu_start(uc, 0x400000000 + 0x1000, 0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
363+
OK(uc_emu_start(uc, 0x400000000 + 0x1000,
364+
0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
362365
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
363366
TEST_CHECK(mem == 2);
364367
OK(uc_context_restore(uc, c1));

tests/unit/test_x86.c

+32-30
Original file line numberDiff line numberDiff line change
@@ -1514,64 +1514,66 @@ static void test_x86_64_not_overwriting_tmp0_for_pc_update()
15141514
}
15151515

15161516
#define MEM_BASE 0x40000000
1517-
#define MEM_SIZE 1024*1024
1517+
#define MEM_SIZE 1024 * 1024
15181518
#define MEM_STACK MEM_BASE + (MEM_SIZE / 2)
15191519
#define MEM_TEXT MEM_STACK + 4096
15201520

1521-
static void test_fxsave_fpip_x86(void) {
1521+
static void test_fxsave_fpip_x86(void)
1522+
{
15221523
// note: fxsave was introduced in Pentium II
15231524
uint8_t code_x86[] = {
15241525
// help testing through NOP offset [disassembly in at&t syntax]
1525-
0x90, 0x90, 0x90, 0x90, // nop nop nop nop
1526-
// run a floating point instruction
1527-
0xdb, 0xc9, // fcmovne %st(1), %st
1528-
// fxsave needs 512 bytes of storage space
1529-
0x81, 0xec, 0x00, 0x02, 0x00, 0x00, // subl $512, %esp
1530-
// fxsave needs a 16-byte aligned address for storage
1531-
0x83, 0xe4, 0xf0, // andl $0xfffffff0, %esp
1532-
// store fxsave data on the stack
1533-
0x0f, 0xae, 0x04, 0x24, // fxsave (%esp)
1534-
// fxsave stores FPIP at an 8-byte offset, move FPIP to eax register
1535-
0x8b, 0x44, 0x24, 0x08 // movl 0x8(%esp), %eax
1526+
0x90, 0x90, 0x90, 0x90, // nop nop nop nop
1527+
// run a floating point instruction
1528+
0xdb, 0xc9, // fcmovne %st(1), %st
1529+
// fxsave needs 512 bytes of storage space
1530+
0x81, 0xec, 0x00, 0x02, 0x00, 0x00, // subl $512, %esp
1531+
// fxsave needs a 16-byte aligned address for storage
1532+
0x83, 0xe4, 0xf0, // andl $0xfffffff0, %esp
1533+
// store fxsave data on the stack
1534+
0x0f, 0xae, 0x04, 0x24, // fxsave (%esp)
1535+
// fxsave stores FPIP at an 8-byte offset, move FPIP to eax register
1536+
0x8b, 0x44, 0x24, 0x08 // movl 0x8(%esp), %eax
15361537
};
15371538
uc_err err;
15381539
uint32_t X86_NOP_OFFSET = 4;
1539-
uint32_t stack_top = (uint32_t) MEM_STACK;
1540+
uint32_t stack_top = (uint32_t)MEM_STACK;
15401541
uint32_t value;
15411542
uc_engine *uc;
15421543

15431544
// initialize emulator in X86-32bit mode
15441545
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
1545-
1546+
15461547
// map 1MB of memory for this emulation
15471548
OK(uc_mem_map(uc, MEM_BASE, MEM_SIZE, UC_PROT_ALL));
15481549
OK(uc_mem_write(uc, MEM_TEXT, code_x86, sizeof(code_x86)));
15491550
OK(uc_reg_write(uc, UC_X86_REG_ESP, &stack_top));
15501551
OK(uc_emu_start(uc, MEM_TEXT, MEM_TEXT + sizeof(code_x86), 0, 0));
15511552
OK(uc_reg_read(uc, UC_X86_REG_EAX, &value));
1552-
TEST_CHECK(value == ((uint32_t) MEM_TEXT + X86_NOP_OFFSET));
1553+
TEST_CHECK(value == ((uint32_t)MEM_TEXT + X86_NOP_OFFSET));
15531554
OK(uc_mem_unmap(uc, MEM_BASE, MEM_SIZE));
15541555
OK(uc_close(uc));
15551556
}
15561557

1557-
static void test_fxsave_fpip_x64(void) {
1558+
static void test_fxsave_fpip_x64(void)
1559+
{
15581560
uint8_t code_x64[] = {
15591561
// help testing through NOP offset [disassembly in at&t]
1560-
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
1561-
// run a floating point instruction
1562-
0xdb, 0xc9, // fcmovne %st(1), %st
1563-
// fxsave64 needs 512 bytes of storage space
1564-
0x48, 0x81, 0xec, 0x00, 0x02, 0x00, 0x00, // subq $512, %rsp
1565-
// fxsave needs a 16-byte aligned address for storage
1566-
0x48, 0x83, 0xe4, 0xf0, // andq 0xfffffffffffffff0, %rsp
1567-
// store fxsave64 data on the stack
1568-
0x48, 0x0f, 0xae, 0x04, 0x24, // fxsave64 (%rsp)
1569-
// fxsave64 stores FPIP at an 8-byte offset, move FPIP to rax register
1570-
0x48, 0x8b, 0x44, 0x24, 0x08, // movq 0x8(%rsp), %rax
1562+
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
1563+
// run a floating point instruction
1564+
0xdb, 0xc9, // fcmovne %st(1), %st
1565+
// fxsave64 needs 512 bytes of storage space
1566+
0x48, 0x81, 0xec, 0x00, 0x02, 0x00, 0x00, // subq $512, %rsp
1567+
// fxsave needs a 16-byte aligned address for storage
1568+
0x48, 0x83, 0xe4, 0xf0, // andq 0xfffffffffffffff0, %rsp
1569+
// store fxsave64 data on the stack
1570+
0x48, 0x0f, 0xae, 0x04, 0x24, // fxsave64 (%rsp)
1571+
// fxsave64 stores FPIP at an 8-byte offset, move FPIP to rax register
1572+
0x48, 0x8b, 0x44, 0x24, 0x08, // movq 0x8(%rsp), %rax
15711573
};
15721574

15731575
uc_err err;
1574-
uint64_t stack_top = (uint64_t) MEM_STACK;
1576+
uint64_t stack_top = (uint64_t)MEM_STACK;
15751577
uint64_t X64_NOP_OFFSET = 8;
15761578
uint64_t value;
15771579
uc_engine *uc;
@@ -1585,7 +1587,7 @@ static void test_fxsave_fpip_x64(void) {
15851587
OK(uc_reg_write(uc, UC_X86_REG_RSP, &stack_top));
15861588
OK(uc_emu_start(uc, MEM_TEXT, MEM_TEXT + sizeof(code_x64), 0, 0));
15871589
OK(uc_reg_read(uc, UC_X86_REG_RAX, &value));
1588-
TEST_CHECK(value == ((uint64_t) MEM_TEXT + X64_NOP_OFFSET));
1590+
TEST_CHECK(value == ((uint64_t)MEM_TEXT + X64_NOP_OFFSET));
15891591
OK(uc_mem_unmap(uc, MEM_BASE, MEM_SIZE));
15901592
OK(uc_close(uc));
15911593
}

0 commit comments

Comments
 (0)