Add CFI_RESTORE_STATE_AND_DEF_CFA macro on arm

We always want to do restore_state and def_cfa together.

Test: diff generated cfa
Change-Id: I75804c9933fcb3f5db4b728fc9342f42fe07f9d0
diff --git a/runtime/arch/arm/asm_support_arm.S b/runtime/arch/arm/asm_support_arm.S
index 000a2d1..144f761 100644
--- a/runtime/arch/arm/asm_support_arm.S
+++ b/runtime/arch/arm/asm_support_arm.S
@@ -65,6 +65,14 @@
     .endif
 .endm
 
+// The spec is not clear whether the CFA is part of the saved state and tools
+// differ in the behaviour, so explicitly set the CFA to avoid any ambiguity.
+// The restored CFA state should match the CFA state during CFI_REMEMBER_STATE.
+.macro CFI_RESTORE_STATE_AND_DEF_CFA reg, offset
+    .cfi_restore_state
+    .cfi_def_cfa \reg, \offset
+.endm
+
 // Macro to generate the value of Runtime::Current into rDest. As it uses labels
 // then the labels need to be unique. We bind these to the function name in the ENTRY macros.
 .macro RUNTIME_CURRENT name, num, rDest
diff --git a/runtime/arch/arm/jni_entrypoints_arm.S b/runtime/arch/arm/jni_entrypoints_arm.S
index b3d89f9..03b857f 100644
--- a/runtime/arch/arm/jni_entrypoints_arm.S
+++ b/runtime/arch/arm/jni_entrypoints_arm.S
@@ -273,8 +273,7 @@
 
     // Do the tail call.
     bx     ip
-    .cfi_restore_state
-    .cfi_def_cfa sp, FRAME_SIZE_SAVE_REFS_AND_ARGS
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, FRAME_SIZE_SAVE_REFS_AND_ARGS
 
 .Lcritical_deliver_exception:
     // The exception delivery checks that rSELF was saved but the SaveRefsAndArgs
diff --git a/runtime/arch/arm/quick_entrypoints_arm.S b/runtime/arch/arm/quick_entrypoints_arm.S
index a3db9f6..7add6bb 100644
--- a/runtime/arch/arm/quick_entrypoints_arm.S
+++ b/runtime/arch/arm/quick_entrypoints_arm.S
@@ -457,8 +457,7 @@
     .cfi_adjust_cfa_offset -64
     pop    {r4, r5, r6, r7, r8, r9, r10, r11, pc}
 .Losr_entry:
-    .cfi_restore_state
-    .cfi_def_cfa r11, SAVE_SIZE            @ CFA = r11 + SAVE_SIZE
+    CFI_RESTORE_STATE_AND_DEF_CFA r11, SAVE_SIZE  @ CFA = r11 + SAVE_SIZE
     sub sp, sp, r10                        @ Reserve space for callee stack
     sub r10, r10, #4
     str lr, [sp, r10]                      @ Store link register per the compiler ABI
@@ -1452,8 +1451,7 @@
     bx lr      // ret
 
     // Undo the unwinding information from above since it doesn't apply below.
-    .cfi_restore_state
-    .cfi_def_cfa r10, FRAME_SIZE_SAVE_REFS_AND_ARGS
+    CFI_RESTORE_STATE_AND_DEF_CFA r10, FRAME_SIZE_SAVE_REFS_AND_ARGS
 .Lexception_in_native:
     ldr ip, [rSELF, #THREAD_TOP_QUICK_FRAME_OFFSET]
     add ip, ip, #-1  // Remove the GenericJNI tag. ADD/SUB writing directly to SP is UNPREDICTABLE.
@@ -2494,7 +2492,6 @@
     REFRESH_MARKING_REGISTER
     blx lr
 .Ldo_deliver_instrumentation_exception_exit:
-    .cfi_restore_state
-    .cfi_def_cfa sp, FRAME_SIZE_SAVE_EVERYTHING
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, FRAME_SIZE_SAVE_EVERYTHING
     DELIVER_PENDING_EXCEPTION_FRAME_READY
 END art_quick_method_exit_hook
diff --git a/runtime/arch/arm64/asm_support_arm64.S b/runtime/arch/arm64/asm_support_arm64.S
index 48a1f8d..ca6b6fd 100644
--- a/runtime/arch/arm64/asm_support_arm64.S
+++ b/runtime/arch/arm64/asm_support_arm64.S
@@ -70,6 +70,14 @@
     .endif
 .endm
 
+// The spec is not clear whether the CFA is part of the saved state and tools
+// differ in the behaviour, so explicitly set the CFA to avoid any ambiguity.
+// The restored CFA state should match the CFA state during CFI_REMEMBER_STATE.
+.macro CFI_RESTORE_STATE_AND_DEF_CFA reg, offset
+    .cfi_restore_state
+    .cfi_def_cfa \reg, \offset
+.endm
+
 .macro ENTRY_ALIGNED name, alignment
     .type \name, #function
     .hidden \name  // Hide this as a global symbol, so we do not incur plt calls.
diff --git a/runtime/arch/arm64/jni_entrypoints_arm64.S b/runtime/arch/arm64/jni_entrypoints_arm64.S
index 8872362..463767c 100644
--- a/runtime/arch/arm64/jni_entrypoints_arm64.S
+++ b/runtime/arch/arm64/jni_entrypoints_arm64.S
@@ -296,8 +296,7 @@
 
     // Do the tail call.
     br    x13
-    .cfi_restore_state
-    .cfi_def_cfa sp, FRAME_SIZE_SAVE_REFS_AND_ARGS
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, FRAME_SIZE_SAVE_REFS_AND_ARGS
 
 .Lcritical_deliver_exception:
     // The exception delivery checks that xSELF was saved but the SaveRefsAndArgs
diff --git a/runtime/arch/arm64/quick_entrypoints_arm64.S b/runtime/arch/arm64/quick_entrypoints_arm64.S
index 7fb6ff0..a20d558 100644
--- a/runtime/arch/arm64/quick_entrypoints_arm64.S
+++ b/runtime/arch/arm64/quick_entrypoints_arm64.S
@@ -795,8 +795,7 @@
     ret
 
 .Losr_entry:
-    .cfi_restore_state                     // Reset unwind info so following code unwinds.
-    .cfi_def_cfa_offset (SAVE_SIZE+16)     // workaround for clang bug: 31975598
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, (SAVE_SIZE+16)
 
     mov x9, sp                             // Save stack pointer.
     .cfi_def_cfa_register x9
@@ -963,8 +962,7 @@
     .cfi_remember_state
     RESTORE_TWO_REGS_DECREASE_FRAME x0, x1, 32
     ret
-    .cfi_restore_state                // Reset unwind info so following code unwinds.
-    .cfi_def_cfa_offset 32            // workaround for clang bug: 31975598
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, 32
 
 .Lthrow_class_cast_exception:
     // Restore
@@ -1104,8 +1102,7 @@
     lsr x0, x0, #CARD_TABLE_CARD_SHIFT
     strb w3, [x3, x0]
     ret
-    .cfi_restore_state            // Reset unwind info so following code unwinds.
-    .cfi_def_cfa_offset 32        // workaround for clang bug: 31975598
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, 32
 .Lthrow_array_store_exception:
     RESTORE_TWO_REGS x2, xLR, 16
     RESTORE_TWO_REGS_DECREASE_FRAME x0, x1, 32
@@ -1221,8 +1218,7 @@
     RESTORE_SAVE_EVERYTHING_FRAME_KEEP_X0
     REFRESH_MARKING_REGISTER
     ret                        // return
-    .cfi_restore_state
-    .cfi_def_cfa_offset FRAME_SIZE_SAVE_EVERYTHING  // workaround for clang bug: 31975598
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, FRAME_SIZE_SAVE_EVERYTHING
 1:
     DELIVER_PENDING_EXCEPTION_FRAME_READY
 END \name
@@ -1846,8 +1842,7 @@
     ret
 
     // Undo the unwinding information from above since it doesn't apply below.
-    .cfi_restore_state
-    .cfi_def_cfa x28, FRAME_SIZE_SAVE_REFS_AND_ARGS
+    CFI_RESTORE_STATE_AND_DEF_CFA x28, FRAME_SIZE_SAVE_REFS_AND_ARGS
 .Lexception_in_native:
     // Move to x1 then sp to please assembler.
     ldr x1, [xSELF, # THREAD_TOP_QUICK_FRAME_OFFSET]
@@ -2622,8 +2617,7 @@
     REFRESH_MARKING_REGISTER
     ret
 .Ldo_deliver_instrumentation_exception_exit:
-    .cfi_restore_state
-    .cfi_def_cfa sp, FRAME_SIZE_SAVE_EVERYTHING
+    CFI_RESTORE_STATE_AND_DEF_CFA sp, FRAME_SIZE_SAVE_EVERYTHING
     DELIVER_PENDING_EXCEPTION_FRAME_READY
 END art_quick_method_exit_hook