ARM Hacking: EXC_ARM_DA_ALIGN exception

http://www.galloway.me.uk/2010/10/arm-hacking-exc_arm_da_align-exception/


I came across a problem today that I’d seen before but couldn’t remember when. Then I stumbled across Peter Bakhirev writing up his findings at Byte Club about a problem I’d helped him with. So I thought I’d quickly write up my summary here incase it helps anyone else.

It started back when I was browsing the Apple developer forums once – I came across someone having a problem where the EXC_ARM_DA_ALIGN exception was thrown. It turned out that this was a problem with setting the value of a variable by dereferencing a pointer, like so:

1
2
3
4
char *mem = malloc(16); // alloc 16 bytes of data
double *dbl = mem + 2;
double set = 10.0;
*dbl = set;

The compiler was emitting a STMIA instruction that was using a memory location that isn’t word (32-bit) aligned (because of the +2 into the ‘mem’ memory location – assuming mem is word aligned). This causes the processor to throw the exception that bubbles up as an EXC_ARM_DA_ALIGN.

The solution above would be to memcpy instead of dereferencing like so:

1
2
3
4
char *mem = malloc(16); // alloc 16 bytes of data
double *dbl = mem + 2;
double set = 10.0;
memcpy(dbl, &set, sizeof(set));

This is just a simple example of the problems that can occur when you’re not careful with reading/writing arbitrary memory that you have allocated. Granted, it’s probably a compiler bug in this case, but it illustrates the point.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值