<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55286>55286</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Stack pointer corruption when profiling mips
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
vit9696
</td>
</tr>
</table>
<pre>
```C
int func2(long int q);
void func(int *a, int b, int c) {
_Alignas(16) long int q;
if (func2(q))
*a = 1;
}
```
Compiled as `clang -target mipsel-gnu-linux -pg t.c -S -o t.s` produces the following code:
```
func: # @func
addiu $sp, $sp, -64
sw $ra, 60($sp) # 4-byte Folded Spill
sw $fp, 56($sp) # 4-byte Folded Spill
move $fp, $sp
addiu $1, $zero, -16
and $sp, $sp, $1
sw $6, 28($sp) # 4-byte Folded Spill
sw $5, 24($sp) # 4-byte Folded Spill
sw $4, 20($sp) # 4-byte Folded Spill
move $1, $ra
addiu $sp, $sp, -8
jal _mcount
nop
lw $4, 20($sp) # 4-byte Folded Reload
lw $5, 24($sp) # 4-byte Folded Reload
lw $6, 28($sp) # 4-byte Folded Reload
sw $4, 52($sp)
sw $5, 48($sp)
sw $6, 44($sp)
lw $4, 32($sp)
jal func2
nop
beqz $2, $BB0_3
nop
j $BB0_2
nop
$BB0_2:
lw $2, 52($sp)
addiu $1, $zero, 1
sw $1, 0($2)
j $BB0_3
nop
$BB0_3:
move $sp, $fp
lw $fp, 56($sp) # 4-byte Folded Reload
lw $ra, 60($sp) # 4-byte Folded Reload
addiu $sp, $sp, 64
jr $ra
nop
```
The important subsequence is:
```
sw $4, 20($sp)
addiu $sp, $sp, -8
lw $4, 20($sp)
sw $4, 52($sp)
lw $2, 52($sp)
sw $1, 0($2)
```
Here
- the first argument in $4 is stored at sp+20
- sp registered is subtracted 8
- the first argument is read off by 8 bytes (wrong) place
- eventually garbage pointer is dereferenced instead of the first argument.
This looks like a regression introduced in llvm 12.0.0. No issue when compiling with llvm 11.0.1. CC @tstellar
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVk1zozgQ_TX4osIF4iNw4BA7ldrTHjZzTwloYyUyIpKwN_PrpyWMY3uMnaRCiC2s169ftxqpS1m_F14aDPfSCx684J63hqz6tqIezYRsG2J_ePNo7kWLATF8biWvHRBxFuLRe-bRpYOX4wAnc-Ld7Q0JXs_3gjct02gVpnb2yEd0hOMrZMxGIc4_3odpvKxD4kUPJPxQdvewH4xBHSteyk3HBdSEaYJTlWDo2TdMNWDIhncahN-0vS942_9P_K4hZl4R_4n4EkcaTUinZN1XoIlZA1lJIeSOI0kla_Ci-2NnZwpcoqJR-rXLoxHx4sAZHIdLWF3z3gFi3dkEHwZ-Gp9C9W7kipVbFJRBsz0-n_Ia--W7AfIoRY1JesJciUnalXOcpD9Bu5FbOKEdGKeCD_eQ36Ckix7r6BTb1qPMs0Q566mQUouw4VyN6MuZShxt_NO0saO9taxfXoAxuVg2ny2-7BT5woT7ft5Usm_N6WQrz9ZV_EBE_4GQrJ7k_fYC3OD9dr1c4j1f2YQe8V6vrTj7DNSpjeNJ6PlCRNMCxhUeNufr61vC2-89Ld2XzWIRPEc3zF4OyXPwKS-H6XHvvRANvZHOKzvL9F7hkPtSpX8n6FT9VLCH6XP1R-_j4WVbTb84396LbxT4t4-OS7zTG8j54fWijhVMpO7S-f4Lz2S-6aQyDNsJ3Zca3npoK_xVf5zOp5ZXd9QBcnvru7qJXfZzoSA_UbM3K_BiXv4BBcPIH_oWrrQh2Pb0G8BE8dapwiQRbaSy_RFmD50uaDCa6Y4oaLg2YOctsi-NYpXBp-wat0Y7VhO5WpHynWTElom2nd1OYd9nK6oTrDrIgy1a9UyId9IwVbIGSCexOQRlqWr0vsJ_XFIU0aIaR33B8fy0LtBWSPmKn_wVCLOxKNCay9Z2nkNPZxmJENsNCek8wD_yr0SnugeyW0OLPZ7tHm23t-NmvUeGiAvnZLm0LZtBQUIwRWZ1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