Skip to content

Commit 42564f9

Browse files
committed
[fuzzer][fuchsia] Support RISC-V
Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D153082
1 parent 0c03f48 commit 42564f9

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ elseif (OS_NAME MATCHES "Windows")
4545
set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64})
4646
elseif(OS_NAME MATCHES "Android")
4747
set(ALL_FUZZER_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64})
48+
elseif(OS_NAME MATCHES "Fuchsia")
49+
set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64})
4850
else()
4951
set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64})
5052
endif()

compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ void AlarmHandler(int Seconds) {
8787
// Alternatively, Fuchsia may in future actually implement basic signal
8888
// handling for the machine trap signals.
8989
#if defined(__x86_64__)
90+
9091
#define FOREACH_REGISTER(OP_REG, OP_NUM) \
9192
OP_REG(rax) \
9293
OP_REG(rbx) \
@@ -107,6 +108,7 @@ void AlarmHandler(int Seconds) {
107108
OP_REG(rip)
108109

109110
#elif defined(__aarch64__)
111+
110112
#define FOREACH_REGISTER(OP_REG, OP_NUM) \
111113
OP_NUM(0) \
112114
OP_NUM(1) \
@@ -140,6 +142,41 @@ void AlarmHandler(int Seconds) {
140142
OP_NUM(29) \
141143
OP_REG(sp)
142144

145+
#elif defined(__riscv)
146+
147+
#define FOREACH_REGISTER(OP_REG, OP_NUM) \
148+
OP_REG(ra) \
149+
OP_REG(sp) \
150+
OP_REG(gp) \
151+
OP_REG(tp) \
152+
OP_REG(t0) \
153+
OP_REG(t1) \
154+
OP_REG(t2) \
155+
OP_REG(s0) \
156+
OP_REG(s1) \
157+
OP_REG(a0) \
158+
OP_REG(a1) \
159+
OP_REG(a2) \
160+
OP_REG(a3) \
161+
OP_REG(a4) \
162+
OP_REG(a5) \
163+
OP_REG(a6) \
164+
OP_REG(a7) \
165+
OP_REG(s2) \
166+
OP_REG(s3) \
167+
OP_REG(s4) \
168+
OP_REG(s5) \
169+
OP_REG(s6) \
170+
OP_REG(s7) \
171+
OP_REG(s8) \
172+
OP_REG(s9) \
173+
OP_REG(s10) \
174+
OP_REG(s11) \
175+
OP_REG(t3) \
176+
OP_REG(t4) \
177+
OP_REG(t5) \
178+
OP_REG(t6) \
179+
143180
#else
144181
#error "Unsupported architecture for fuzzing on Fuchsia"
145182
#endif
@@ -200,6 +237,13 @@ void MakeTrampoline() {
200237
".cfi_offset 30, %c[lr]\n"
201238
"bl %c[StaticCrashHandler]\n"
202239
"brk 1\n"
240+
#elif defined(__riscv)
241+
".cfi_return_column 64\n"
242+
".cfi_def_cfa sp, 0\n"
243+
".cfi_offset 64, %[pc]\n"
244+
FOREACH_REGISTER(CFI_OFFSET_REG, CFI_OFFSET_NUM)
245+
"call %c[StaticCrashHandler]\n"
246+
"unimp\n"
203247
#else
204248
#error "Unsupported architecture for fuzzing on Fuchsia"
205249
#endif
@@ -209,8 +253,11 @@ void MakeTrampoline() {
209253
".cfi_startproc\n"
210254
: // No outputs
211255
: FOREACH_REGISTER(ASM_OPERAND_REG, ASM_OPERAND_NUM)
256+
#if defined(__aarch64__) || defined(__riscv)
257+
ASM_OPERAND_REG(pc)
258+
#endif
212259
#if defined(__aarch64__)
213-
ASM_OPERAND_REG(pc) ASM_OPERAND_REG(lr)
260+
ASM_OPERAND_REG(lr)
214261
#endif
215262
[StaticCrashHandler] "i"(StaticCrashHandler));
216263
}
@@ -294,6 +341,7 @@ void CrashHandler() {
294341
// onto the stack and jump into a trampoline with CFI instructions on how
295342
// to restore it.
296343
#if defined(__x86_64__)
344+
297345
uintptr_t StackPtr =
298346
(GeneralRegisters.rsp - (128 + sizeof(GeneralRegisters))) &
299347
-(uintptr_t)16;
@@ -302,7 +350,8 @@ void CrashHandler() {
302350
GeneralRegisters.rsp = StackPtr;
303351
GeneralRegisters.rip = reinterpret_cast<zx_vaddr_t>(CrashTrampolineAsm);
304352

305-
#elif defined(__aarch64__)
353+
#elif defined(__aarch64__) || defined(__riscv)
354+
306355
uintptr_t StackPtr =
307356
(GeneralRegisters.sp - sizeof(GeneralRegisters)) & -(uintptr_t)16;
308357
__unsanitized_memcpy(reinterpret_cast<void *>(StackPtr), &GeneralRegisters,

0 commit comments

Comments
 (0)