2020#include < ucontext.h>
2121
2222extern " C" int safefetch32_cont (int * adr, int errValue);
23+ extern " C" int64_t safefetch64_cont (int64_t * adr, int64_t errValue);
2324
2425#ifdef __APPLE__
2526 #if defined(__x86_64__)
@@ -37,12 +38,12 @@ extern "C" int safefetch32_cont(int* adr, int errValue);
3738#endif
3839
3940/* *
40- Loading a 32-bit value from specific address, the errValue will be returned
41+ Loading a 32-bit/64-bit value from specific address, the errValue will be returned
4142 if the address is invalid.
4243 The load is protected by the 'handle_safefetch` signal handler, who sets next `pc`
43- to `safefetch32_cont`, upon returning from signal handler, `safefetch32_cont` returns `errValue`
44+ to `safefetch32_cont/safefetch64_cont`, upon returning from signal handler,
45+ `safefetch32_cont/safefetch64_cont` returns `errValue`
4446 **/
45-
4647#if defined(__x86_64__)
4748 #ifdef __APPLE__
4849 asm (R"(
@@ -56,6 +57,16 @@ extern "C" int safefetch32_cont(int* adr, int errValue);
5657 _safefetch32_cont:
5758 movl %esi, %eax
5859 ret
60+ .globl _safefetch64_impl
61+ .private_extern _safefetch64_impl
62+ _safefetch64_impl:
63+ movq (%rdi), %rax
64+ ret
65+ .globl _safefetch64_cont
66+ .private_extern _safefetch64_cont
67+ _safefetch64_cont:
68+ movq %rsi, %rax
69+ ret
5970 )" );
6071 #else
6172 asm (R"(
@@ -72,6 +83,18 @@ extern "C" int safefetch32_cont(int* adr, int errValue);
7283 safefetch32_cont:
7384 movl %esi, %eax
7485 ret
86+ .globl safefetch64_impl
87+ .hidden safefetch64_impl
88+ .type safefetch64_imp, %function
89+ safefetch64_impl:
90+ movq (%rdi), %rax
91+ ret
92+ .globl safefetch64_cont
93+ .hidden safefetch64_cont
94+ .type safefetch64_cont, %function
95+ safefetch64_cont:
96+ movq %rsi, %rax
97+ ret
7598 )" );
7699 #endif // __APPLE__
77100#elif defined(__aarch64__)
@@ -85,6 +108,16 @@ extern "C" int safefetch32_cont(int* adr, int errValue);
85108 .globl _safefetch32_cont
86109 .private_extern _safefetch32_cont
87110 _safefetch32_cont:
111+ mov w0, w1
112+ ret
113+ .globl _safefetch64_impl
114+ .private_extern _safefetch64_impl
115+ _safefetch64_impl:
116+ ldr x0, [x0]
117+ ret
118+ .globl _safefetch64_cont
119+ .private_extern _safefetch64_cont
120+ _safefetch64_cont:
88121 mov x0, x1
89122 ret
90123 )" );
@@ -101,6 +134,18 @@ extern "C" int safefetch32_cont(int* adr, int errValue);
101134 .hidden safefetch32_cont
102135 .type safefetch32_cont, %function
103136 safefetch32_cont:
137+ mov w0, w1
138+ ret
139+ .globl safefetch64_impl
140+ .hidden safefetch64_impl
141+ .type safefetch64_impl, %function
142+ safefetch64_impl:
143+ ldr x0, [x0]
144+ ret
145+ .globl safefetch64_cont
146+ .hidden safefetch64_cont
147+ .type safefetch64_cont, %function
148+ safefetch64_cont:
104149 mov x0, x1
105150 ret
106151 )" );
@@ -114,6 +159,9 @@ bool SafeAccess::handle_safefetch(int sig, void* context) {
114159 if (pc == (uintptr_t )safefetch32_impl) {
115160 uc->current_pc = (uintptr_t )safefetch32_cont;
116161 return true ;
162+ } else if (pc == (uintptr_t )safefetch64_impl) {
163+ uc->current_pc = (uintptr_t )safefetch64_cont;
164+ return true ;
117165 }
118166 }
119167 return false ;
0 commit comments