armhf sig bus error

(gdb) run
Starting program: /htslib-1.2.1/test/sam test/ce.fa
[Thread debugging using libthread_db enabled]
Using host libthread_db library 
"/lib/arm-linux-gnueabihf/libthread_db.so.1".

Program received signal SIGBUS, Bus error.
0x00025640 in bam_aux2f (
    s=0xa967e "\333\017I@XddiW\024\213\n\277\005@XZZHello, world!",
    s@entry=0xa967d "f\333\017I@XddiW\024\213\n\277\005@XZZHello, world!")
    at sam.c:1181
1181        else if (type == 'f') return *(float*)s;
(gdb)

This is an alignment issue. VFP requires floating point loads and stores 
to be naturally aligned but s is only aligned to 2 bytes whiile a float 
is 4 bytes. Looking further at the code reveals the reason for the 
unalignment. It seems that the code is reading from a data format where 
values are prefixed with single bytes practically gauranteeing that many 
of the accesses will be unaligned.

In portable code unaligned accesses should be avoided. Sometimes they 
will give the right result, sometimes they will give bus errors, 
sometimes they will silently give wrong answers. To a large extent the 
behaviour is driven by architecture (and sometimes version of the 
architecture) but it can also be driven by what instructions the 
compiler choses to use. Even if it does the right thing it may do so 
very slowly. x86 tends to be the most forgiving architecture when it 
comes to unaligned accesses.

The attatched patch replaces a number of unaligned accesses in sam.c 
with memcpy calls. It resulted in a successful build on raspbian 
stretch. I did not include any conditional logic but it would be easy to 
add conditional logic to only use memcpy on non-x86 targets if that was 
considered desirable (I do not if memcpy is faster or slower than 
letting the x86 cpu fixup the unaligned accesses in hardware and if-so 
whether the difference is likely to be significant).


[htslib.debdiff (text/plain, attachment)]






+@@ -1177,8 +1183,8 @@ double bam_aux2f(const uint8_t *s)
+ {
+     int type;
+     type = *s++;
+-    if (type == 'd') return *(double*)s;
+-    else if (type == 'f') return *(float*)s;
++    if (type == 'd') return READUNALIGNED(s,double);
++    else if (type == 'f') return READUNALIGNED(s,float);
+     else return 0.0;
+ }

++#define READUNALIGNED(ptr,type) ({ \
++    type tmp;\
++    memcpy(&tmp,ptr,sizeof(type));\
++    tmp;\
++})




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值