Msg/ClobberedRA: Difference between revisions

From CPUlator Wiki

< Msg
No edit summary
No edit summary
 
Line 6: Line 6:
==== ARMv7 ====
==== ARMv7 ====


<syntaxhighlight lang="Asm" line highlight="11">
<syntaxhighlight lang="Asm" line highlight="10">
.global _start
.global _start
_start:
_start:
    mov sp, #0x1000 // Initialize SP to something sane
     bl MyFunction
     bl MyFunction
     nop // Should return here
     nop // Should return here
Line 20: Line 19:
</syntaxhighlight>
</syntaxhighlight>
==== Nios II ====
==== Nios II ====
<syntaxhighlight lang="Asm" line highlight="10">
<syntaxhighlight lang="Asm" line highlight="9">
.global _start
.global _start
_start:
_start:
    movi sp, 0x1000 # Initialize SP
     call Function
     call Function
     nop # Should return here
     nop # Should return here
Line 32: Line 30:
     ret # return to a different location
     ret # return to a different location
</syntaxhighlight>
</syntaxhighlight>
==== MIPS ====
<syntaxhighlight lang="Asm" line highlight="9">
.global _start
_start:
    jal Function
    nop # Should return here
    nop # Actually returns here
Function:
    addiu $ra, $ra, 4 # Modify ra
    jr $ra # return to a different location
</syntaxhighlight>


=== Debugging ===
=== Debugging ===

Latest revision as of 03:50, 17 March 2019

A function should normally return to the instruction after the call instruction in the caller that called this function. This message tells you that this didn't happen: the function return is returning somewhere other than the instruction following the matching call.

If you also clobbered sp, see also Msg/ClobberedSP

Examples

ARMv7

.global _start
_start:
    bl MyFunction
    nop					// Should return here
    nop					// Actually returns here
    # ...

MyFunction:
    add lr, #4				// Change LR
    bx lr					// return to a different location

Nios II

.global _start
_start:
    call Function
    nop					# Should return here
    nop					# Actually returns here
    
Function:
    addi ra, ra, 4			# Modify ra
    ret					# return to a different location

MIPS

.global _start
_start:
    jal Function
    nop					# Should return here
    nop					# Actually returns here

Function:
    addiu $ra, $ra, 4	# Modify ra
    jr $ra				# return to a different location


Debugging

  • This message is complaining that the return address (or link address for ARM) differs between the start of the function and at the function return. Use breakpoints and make a note of the value of the return address (ra or lr) at both the function entry and return. Are they the same?
  • Typically, the return address register is not used in a function body except for saving and restoring it to the stack when there is a nested function call. A common cause of the return address changing is a problem during save and restore, e.g., popping from a different location than the corresponding push, or modifying the value that was on the stack. Watch the return address's save and restore and ensure that both the location on the stack and values are the same.

Implementation

The simulator identifies idiomatic call and return instructions executed at runtime. It records the values of registers when executing call instructions, and verifies at return instructions that the return address matches the location of the matching call. This warning is generated at the return instruction.

Disabling this message

This debugging check can be disabled in the Debugging Checks section of the Settings box: Function clobbered ra or sp.

ARMv7: Function clobbered sp, or bad return