Skip to content

Commit b0abbe2

Browse files
WorksButNotTestedYour Name
andauthored
Persistent mode for PPC32 targets (#50)
* Add QASAN support for PPC * Support persistent mode on PPC --------- Co-authored-by: Your Name <[email protected]>
1 parent 0569eff commit b0abbe2

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

qemuafl/api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,11 @@ struct mips_regs {
205205
};
206206
#endif
207207

208+
struct ppc_regs {
209+
target_ulong gpr[32]; /* general purpose registers */
210+
target_ulong lr;
211+
target_ulong ctr;
212+
uint32_t crf[8]; /* condition register */
213+
};
214+
208215
#endif

qemuafl/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
/* MIPS_PATCH */
5858
#elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
5959
#define api_regs mips_regs
60+
#elif defined(TARGET_PPC)
61+
#define api_regs ppc_regs
6062
#else
6163
struct generic_api_regs { int v; };
6264
#define api_regs generic_api_regs
@@ -141,7 +143,7 @@ void afl_float_compcov_log_80(target_ulong cur_loc, floatx80 arg1,
141143
abi_ulong afl_get_brk(void);
142144
abi_ulong afl_set_brk(abi_ulong new_brk);
143145

144-
#if defined(TARGET_X86_64) || defined(TARGET_I386) || defined(TARGET_AARCH64) || defined(TARGET_ARM) || defined(TARGET_MIPS) || defined(TARGET_MIPS64)
146+
#if defined(TARGET_X86_64) || defined(TARGET_I386) || defined(TARGET_AARCH64) || defined(TARGET_ARM) || defined(TARGET_MIPS) || defined(TARGET_MIPS64) || defined(TARGET_PPC)
145147
void afl_save_regs(struct api_regs* regs, CPUArchState* env);
146148
void afl_restore_regs(struct api_regs* regs, CPUArchState* env);
147149
#else

qemuafl/qasan-qemu.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct shadow_stack_block {
4646

4747
int index;
4848
target_ulong buf[SHADOW_BK_SIZE];
49-
49+
5050
struct shadow_stack_block* next;
5151

5252
};
@@ -87,6 +87,17 @@ extern __thread struct shadow_stack qasan_shadow_stack;
8787
#define BP_GET(env) ((env)->active_tc.gpr[29])
8888
#define SP_GET(env) ((env)->active_tc.gpr[30])
8989

90+
#elif defined(TARGET_PPC)
91+
92+
#define PC_GET(env) ((env)->nip)
93+
/*
94+
* PPC doesn't really have a frame pointer since stack frames are built into a
95+
* linked list. The BP is used only for display purposes in any case, so we will
96+
* just use the SP here.
97+
*/
98+
#define BP_GET(env) ((env)->gpr[1])
99+
#define SP_GET(env) ((env)->gpr[1])
100+
90101
#else
91102
//#error "Target not supported by asan-giovese"
92103
#define DO_NOT_USE_QASAN 1

target/ppc/translate.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,47 @@
3737
#include "exec/log.h"
3838
#include "qemu/atomic128.h"
3939

40+
#include "qemuafl/cpu-translate.h"
41+
42+
#define AFL_QEMU_TARGET_PPC_SNIPPET \
43+
if (is_persistent) { \
44+
\
45+
if (ctx->base.pc_next == afl_persistent_addr) { \
46+
\
47+
gen_helper_afl_persistent_routine(cpu_env); \
48+
\
49+
if (afl_persistent_ret_addr == 0 && !persistent_exits) { \
50+
\
51+
tcg_gen_movi_i32(cpu_lr, afl_persistent_addr); \
52+
\
53+
} \
54+
\
55+
if (!persistent_save_gpr) afl_gen_tcg_plain_call(&afl_persistent_loop); \
56+
\
57+
} else if (afl_persistent_ret_addr && \
58+
ctx->base.pc_next == afl_persistent_ret_addr) { \
59+
\
60+
gen_setlr(ctx, afl_persistent_addr); \
61+
gen_bclr(ctx); \
62+
\
63+
} \
64+
\
65+
}
66+
67+
void afl_save_regs(struct api_regs* r, CPUArchState* env) {
68+
memcpy(r->gpr, env->gpr, sizeof(r->gpr));
69+
r->lr = env->lr;
70+
r->ctr = env->ctr;
71+
memcpy(r->crf, env->crf, sizeof(r->crf));
72+
}
73+
74+
void afl_restore_regs(struct api_regs* r, CPUArchState* env) {
75+
memcpy(env->gpr, r->gpr, sizeof(r->gpr));
76+
env->lr = r->lr;
77+
env->ctr = r->ctr;
78+
memcpy(env->crf, r->crf, sizeof(r->crf));
79+
}
80+
4081

4182
#define CPU_SINGLE_STEP 0x1
4283
#define CPU_BRANCH_STEP 0x2
@@ -8002,6 +8043,10 @@ static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
80028043
LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
80038044
ctx->base.pc_next, ctx->mem_idx, (int)msr_ir);
80048045

8046+
#if defined(TARGET_PPC)
8047+
AFL_QEMU_TARGET_PPC_SNIPPET
8048+
#endif
8049+
80058050
ctx->opcode = translator_ldl_swap(env, ctx->base.pc_next,
80068051
need_byteswap(ctx));
80078052

0 commit comments

Comments
 (0)