[3/4] checkasm: aarch64: Clobber the stack before calling functions

Message ID 1475227335-17098-3-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö Sept. 30, 2016, 9:22 a.m.
---
 tests/checkasm/aarch64/checkasm.S | 14 ++++++++++++++
 tests/checkasm/checkasm.h         |  6 +++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

Comments

Janne Grunau Oct. 14, 2016, 5:10 p.m. | #1
On Friday, 30 September 2016 11:22:14 CEST, Martin Storsjö wrote:
> ---
>  tests/checkasm/aarch64/checkasm.S | 14 ++++++++++++++
>  tests/checkasm/checkasm.h         |  6 +++++-
>  2 files changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/tests/checkasm/aarch64/checkasm.S 
> b/tests/checkasm/aarch64/checkasm.S
> index 39e8337..a123ac1 100644
> --- a/tests/checkasm/aarch64/checkasm.S
> +++ b/tests/checkasm/aarch64/checkasm.S
> @@ -52,6 +52,20 @@ endconst
>  // max number of args used by any asm function.
>  #define MAX_ARGS 15
>  
> +#define CLOBBER_STACK ((8*MAX_ARGS + 15) & ~15)
> +
> +function checkasm_stack_clobber, export=1
> +    mov         x29, sp
> +    mov         x1,  #CLOBBER_STACK - 8
> +    sub         sp,  sp,  #CLOBBER_STACK
> +1:
> +    str         x0,  [sp, x1]
> +    subs        x1,  x1,  #8

this could clobber 16 bytes per loop, not important though. patch ok as is

> +    b.ge        1b
> +    mov         sp,  x29
> +    ret
> +endfunc
> +
>  #define ARG_STACK ((8*(MAX_ARGS - 8) + 15) & ~15)
>  
>  function checkasm_checked_call, export=1
> diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
> index 169aa2a..8d54cc7 100644
> --- a/tests/checkasm/checkasm.h
> +++ b/tests/checkasm/checkasm.h
> @@ -133,9 +133,13 @@ extern void (*checkasm_checked_call)(void 
> *func, int dummy, ...);
>  #define declare_new(ret, ...) ret (*checked_call)(void *, int 
> dummy, __VA_ARGS__) = (void *)checkasm_checked_call;
>  #define call_new(...) checked_call(func_new, 0, __VA_ARGS__)
>  #elif ARCH_AARCH64 && !defined(__APPLE__)
> +void checkasm_stack_clobber(uint64_t clobber, ...);
>  void checkasm_checked_call(void *func, ...);
>  #define declare_new(ret, ...) ret (*checked_call)(void *, 
> __VA_ARGS__) = (void *)checkasm_checked_call;
> -#define call_new(...) checked_call(func_new, __VA_ARGS__)
> +#define CLOB (UINT64_C(0xdeadbeefdeadbeef))
> +#define call_new(...) 
> (checkasm_stack_clobber(CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,\
> +                                              
> CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB),\

Any reason why this uses 21 arguments? I would have expected 23 (8 + 
MAX_ARGS)

Janne
Martin Storsjö Oct. 14, 2016, 6:34 p.m. | #2
On Fri, 14 Oct 2016, Janne Grunau wrote:

> On Friday, 30 September 2016 11:22:14 CEST, Martin Storsjö wrote:
>> ---
>>  tests/checkasm/aarch64/checkasm.S | 14 ++++++++++++++
>>  tests/checkasm/checkasm.h         |  6 +++++-
>>  2 files changed, 19 insertions(+), 1 deletion(-)
>>
>> diff --git a/tests/checkasm/aarch64/checkasm.S 
>> b/tests/checkasm/aarch64/checkasm.S
>> index 39e8337..a123ac1 100644
>> --- a/tests/checkasm/aarch64/checkasm.S
>> +++ b/tests/checkasm/aarch64/checkasm.S
>> @@ -52,6 +52,20 @@ endconst
>>  // max number of args used by any asm function.
>>  #define MAX_ARGS 15
>> 
>> +#define CLOBBER_STACK ((8*MAX_ARGS + 15) & ~15)
>> +
>> +function checkasm_stack_clobber, export=1
>> +    mov         x29, sp
>> +    mov         x1,  #CLOBBER_STACK - 8
>> +    sub         sp,  sp,  #CLOBBER_STACK
>> +1:
>> +    str         x0,  [sp, x1]
>> +    subs        x1,  x1,  #8
>
> this could clobber 16 bytes per loop, not important though. patch ok as is

Will fix

>> +    b.ge        1b
>> +    mov         sp,  x29
>> +    ret
>> +endfunc
>> +
>>  #define ARG_STACK ((8*(MAX_ARGS - 8) + 15) & ~15)
>>
>>  function checkasm_checked_call, export=1
>> diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
>> index 169aa2a..8d54cc7 100644
>> --- a/tests/checkasm/checkasm.h
>> +++ b/tests/checkasm/checkasm.h
>> @@ -133,9 +133,13 @@ extern void (*checkasm_checked_call)(void 
>> *func, int dummy, ...);
>>  #define declare_new(ret, ...) ret (*checked_call)(void *, int 
>> dummy, __VA_ARGS__) = (void *)checkasm_checked_call;
>>  #define call_new(...) checked_call(func_new, 0, __VA_ARGS__)
>>  #elif ARCH_AARCH64 && !defined(__APPLE__)
>> +void checkasm_stack_clobber(uint64_t clobber, ...);
>>  void checkasm_checked_call(void *func, ...);
>>  #define declare_new(ret, ...) ret (*checked_call)(void *, 
>> __VA_ARGS__) = (void *)checkasm_checked_call;
>> -#define call_new(...) checked_call(func_new, __VA_ARGS__)
>> +#define CLOB (UINT64_C(0xdeadbeefdeadbeef))
>> +#define call_new(...) 
>> 
> (checkasm_stack_clobber(CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,\
>> + 
>> CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB),\
>
> Any reason why this uses 21 arguments? I would have expected 23 (8 + 
> MAX_ARGS)

No reason at all, I didn't think too much about the number of arguments 
but just copied it from the x86 version. Will amend with two more 
arguments.

// Martin

Patch

diff --git a/tests/checkasm/aarch64/checkasm.S b/tests/checkasm/aarch64/checkasm.S
index 39e8337..a123ac1 100644
--- a/tests/checkasm/aarch64/checkasm.S
+++ b/tests/checkasm/aarch64/checkasm.S
@@ -52,6 +52,20 @@  endconst
 // max number of args used by any asm function.
 #define MAX_ARGS 15
 
+#define CLOBBER_STACK ((8*MAX_ARGS + 15) & ~15)
+
+function checkasm_stack_clobber, export=1
+    mov         x29, sp
+    mov         x1,  #CLOBBER_STACK - 8
+    sub         sp,  sp,  #CLOBBER_STACK
+1:
+    str         x0,  [sp, x1]
+    subs        x1,  x1,  #8
+    b.ge        1b
+    mov         sp,  x29
+    ret
+endfunc
+
 #define ARG_STACK ((8*(MAX_ARGS - 8) + 15) & ~15)
 
 function checkasm_checked_call, export=1
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index 169aa2a..8d54cc7 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -133,9 +133,13 @@  extern void (*checkasm_checked_call)(void *func, int dummy, ...);
 #define declare_new(ret, ...) ret (*checked_call)(void *, int dummy, __VA_ARGS__) = (void *)checkasm_checked_call;
 #define call_new(...) checked_call(func_new, 0, __VA_ARGS__)
 #elif ARCH_AARCH64 && !defined(__APPLE__)
+void checkasm_stack_clobber(uint64_t clobber, ...);
 void checkasm_checked_call(void *func, ...);
 #define declare_new(ret, ...) ret (*checked_call)(void *, __VA_ARGS__) = (void *)checkasm_checked_call;
-#define call_new(...) checked_call(func_new, __VA_ARGS__)
+#define CLOB (UINT64_C(0xdeadbeefdeadbeef))
+#define call_new(...) (checkasm_stack_clobber(CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,\
+                                              CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB,CLOB),\
+                      checked_call(func_new, __VA_ARGS__))
 #else
 #define declare_new(ret, ...)
 #define declare_new_emms(cpu_flags, ret, ...)