关键词:VEC_ACCESS、coredump、LR、PC等。
CK中存在一种VEC_ACCESS异常,可能原因是用户空间访问了内核空间,还有一种是内核访问不存在的总线地址。
下面简单构造VEC_ACCESS异常,包括变量指针异常和函数指针异常并分析。
1. 变量指针异常
#include <stdio.h> void main(void)
{
char *p = (char *)0xfffffffe; *p = ;
}
执行后异常输出如下,并生成了coredump文件。
[ 1601.325926] User mode Bus Error
[ 1601.329105]
[ 1601.329105] CURRENT PROCESS:
[ 1601.329105]
[ 1601.334955] COMM=buserror PID=
[ 1601.338280] TEXT=-000085c0 DATA=00009f1c-0000a018 BSS=0000a018-0000b000
[ 1601.345596] USER-STACK=7fb0de10 KERNEL-STACK=be829180
[ 1601.345596]
[ 1601.352229] PC: 0x000084ec (0x84ec)----------------------异常点代码位置。
[ 1601.355724] LR: 0x2aae22aa (0x2aae22aa)
[ 1601.359564] SP: 0xbf0b1f68
[ 1601.362362] orig_a0: 0x00000001
[ 1601.365506] PSR: 0x00020340
[ 1601.368308] a0: 0x00000001 a1: 0x7fb0de14 a2: 0x00000001 a3: 0xfffffffe
[ 1601.375539] r4: 0x00008500 r5: 0x00000000 r6: 0x00000000 r7: 0x00000000
[ 1601.382770] r8: 0x7fb0dd3c r9: 0x000c873c r10: 0x00008380 r11: 0x00000000
[ 1601.390000] r12: 0x001030dc r13: 0x001030ac r15: 0x2aae22aa
[ 1601.395753] r16: 0xffffffff r17: 0x00000000 r18: 0x2abfa20c r19: 0x00000000
[ 1601.402984] r20: 0x00000000 r21: 0x00000000 r22: 0x7fb0da10 r23: 0x2aaa8736
[ 1601.410215] r24: 0x2aacdb9c r25: 0x00000000 r26: 0x00000000 r27: 0x7fb0dd50
[ 1601.417445] r28: 0x2abf7000 r29: 0x00000000 r30: 0x00000000 tls: 0x2aac8a18
[ 1601.424673] hi: 0x00000000 lo: 0x00000000
[ 1601.429034]
CODE:
000084ac: e9031066 7bcdfffa 000007f7
000084bc: 000014d0 00009f2c
000084cc: dd0e1421 6e3b2000 e4481421
000084dc: b2602b01 1003e468
000084ec: db63a340 6c000000 d90e6fa3
000084fc: 0000783c 0116ebe0 0012ea8d 0002cf8c
0000850c: 003cc5bc d07c1070 6dc30823 6e4b6e07
0000851c: 7bcd10ae 614e106e c78355a2 e9050026
0000852c: 3400000c 0883d086 6c636ca7 24006c1f
0000853c: 65167bcd ebc00bf8 0000783c
0000854c: 00001af4 0000000c ffffff1c ffffff1c
0000855c: db6e14d2 654e2d00
0000856c: 2c030c06 94607bcd 0bfc654e
0000857c: 000014d0 00009f1c
0000858c: ddee1422 db6e2000
0000859c: ff4ce3ff 4820c400
[ 1601.498888]
KERNEL STACK:
bf0b1f28: bea0e9b4 bf0b1f58 bf0b1f5c 806a1426
bf0b1f38: bf0b1f68 7fb0de14
bf0b1f48: bf0b1f5c 8004afec bf0b1f68
bf0b1f58: 7fb0dd3c 8004a418
bf0b1f68: 2aac8a18 2aae22aa 000084ec
bf0b1f78: 7fb0dd38 7fb0de14
bf0b1f88: fffffffe
bf0b1f98: 7fb0dd3c 000c873c
bf0b1fa8: 001030dc 001030ac
bf0b1fb8: ffffffff 2abfa20c
bf0b1fc8: 7fb0da10 2aaa8736
bf0b1fd8: 2aacdb9c 7fb0dd50
[ 1601.552499]
Call Trace:
Segmentation fault (core dumped)
分析生成的core文件,可以发下问题点在*p = 1;:
# 0x000084ec in main () at buserror.c:
p = 0xfffffffe <error: Cannot access memory at address 0xfffffffe>
再结合objdump和CODE段验证对比,同样结果。
000084d0 <main>:
#include <stdio.h> void main(void)
{
84d0: subi sp, sp,
84d2: dd0e2000 st.w r8, (sp, )
84d6: 6e3b mov r8, sp
84d8: subi sp, sp,
char *p = (char *)0xfffffffe;
84da: e4481003 subi r2, r8,
84de: movi r3,
84e0: 2b01 subi r3,
84e2: b260 st.w r3, (r2, ) *p = ;
84e4: e4681003 subi r3, r8,
84e8: ld.w r3, (r3, )
84ea: movi r2,
84ec: a340 st.b r2, (r3, 0)
84ee: db630000 ld.b r27, (r3, )
}
2. 函数指针异常
构造函数指针异常,即函数指针指向了内核一段空间。
#include <stdio.h> void main(void)
{
void *(*pf)(void);
pf = 0xfffffffe;
pf();
printf("EOP");
}
执行后异常输出如下:
[ 3818.626608] User mode Bus Error
[ 3818.629786]
[ 3818.629786] CURRENT PROCESS:
[ 3818.629786]
[ 3818.635635] COMM=buserror PID=
[ 3818.638960] TEXT=- DATA=00009f1c-0000a018 BSS=0000a018-0000b000
[ 3818.646276] USER-STACK=7fcf3e10 KERNEL-STACK=be829700
[ 3818.646276]
[ 3818.652907] PC: 0xfffffffe (0xfffffffe)-------------------------------这里的PC指针并没有指向代码段,也即函数跳转到内核空间了。
[ 3818.656750] LR: 0x00008524 (0x8524)-----------------------------------重点需要查看LR指针指向的位置。
[ 3818.660241] SP: 0xbf197f68
[ 3818.663038] orig_a0: 0x00000001
[ 3818.666183] PSR: 0x00020340
[ 3818.668986] a0: 0x00000001 a1: 0x7fcf3e14 a2: 0x7fcf3d34 a3: 0xfffffffe
[ 3818.676218] r4: 0x00008548 r5: 0x00000000 r6: 0x00000000 r7: 0x00000000
[ 3818.683448] r8: 0x7fcf3d38 r9: 0x000c873c r10: 0x000083b0 r11: 0x00000000
[ 3818.690677] r12: 0x001030dc r13: 0x001030ac r15: 0x00008524
[ 3818.696430] r16: 0xffffffff r17: 0x00000000 r18: 0x2abfa20c r19: 0x00000000
[ 3818.703662] r20: 0x00000000 r21: 0x00000000 r22: 0x7fcf3a10 r23: 0x2aaa8736
[ 3818.710892] r24: 0x2aacdb9c r25: 0x00000000 r26: 0x00000000 r27: 0xfffffffe
[ 3818.718122] r28: 0x2abf7000 r29: 0x00000000 r30: 0x00000000 tls: 0x2aac8a18
[ 3818.725350] hi: 0x00000000 lo: 0x00000000
[ 3818.729709]
CODE:
ffffffc0: [ 3818.732827] Unable to handle kernel paging request at virtual address 0xfffff000, pc: 0x8004bdc4
[ 3818.741647] Oops:
[ 3818.744373]
[ 3818.744373] CURRENT PROCESS:
[ 3818.744373]
[ 3818.750233] COMM=buserror PID=
[ 3818.753568] TEXT=- DATA=00009f1c-0000a018 BSS=0000a018-0000b000
[ 3818.760899] USER-STACK=7fcf3e10 KERNEL-STACK=be829700
[ 3818.760899]
[ 3818.767549] PC: 0x8004bdc4 (show_regs+0x188/0x340)
[ 3818.772374] LR: 0x8004beb8 (show_regs+0x27c/0x340)
[ 3818.777183] SP: 0xbf197e0c
[ 3818.779993] orig_a0: 0x0000000b
[ 3818.783137] PSR: 0x80140300
[ 3818.785951] a0: 0x0000000b a1: 0x00000000 a2: 0x808fec04 a3: 0x00000000
[ 3818.793196] r4: 0x00000000 r5: 0xffffffc0 r6: 0xbf197f68 r7: 0x808494c4
[ 3818.800441] r8: 0xbf197eb0 r9: 0x808494b8 r10: 0x000083b0 r11: 0x00000000
[ 3818.807684] r12: 0x00000004 r13: 0x8007f6c8 r15: 0x8004beb8
[ 3818.813457] r16: 0xffffffff r17: 0x00000000 r18: 0xbf1add8c r19: 0x0000027f
[ 3818.820702] r20: 0x00000004 r21: 0xffffffe4 r22: 0x00000000 r23: 0x0000001b
[ 3818.827945] r24: 0x00000000 r25: 0x80718358 r26: 0x8004f940 r27: 0x00000020
[ 3818.835190] r28: 0x2abf7000 r29: 0x00000000 r30: 0x00000000 tls: 0x00000000
[ 3818.842434] hi: 0x000000f8 lo: 0xd57a2000
[ 3818.846807]
CODE:
8004bd84: d8464820 d8262024 051ae005
8004bd94: 4820c400 e0051200 c4000515 96a24820
8004bda4: e4652d3f 3b402003 0503c400 614c4361
8004bdb4: ea893400 11fa003a 200fe464 0078e903
8004bdc4: 6c1f9520 e0052403 c40004fd eb444820
8004bdd4: 11130bf2 04f4e005 4820c400
8004bde4: e0051111 c40004ef e4a64820 3400103f
8004bdf4: 002aea89 e46411ea e903200f
8004be04: 24036c1f 04dee005 4820c400 00c0eb44
8004be14: 0bf22503 e0051103 c40004d5 962e4820
8004be24: e0003000 c40000f9 6fa34820 2006d92e
8004be34: 98c498e5 988298a3 2001d9ee 2000d90e
8004be44: 783c1407 8004f950 800607b0
8004be54: 808491d4 800ec7c4 808491ec
8004be64: 808492ac
8004be74: 808492bc 808492f8
[ 3818.916915]
KERNEL STACK:
bf197dcc: bf1add8c 0000027f ffffffe4
bf197ddc: 0000001b bf197eb0
bf197dec: 80049f68 ffffffc0 bf197f68
bf197dfc: 808494c4 808494b8 000083b0
bf197e0c: 8004beb8 8004bdc4
bf197e1c: 7fcf3d34 0000000b 0000000b
bf197e2c: 808fec04 ffffffc0
bf197e3c: bf197f68 808494c4 bf197eb0 808494b8
bf197e4c: 000083b0 8007f6c8
bf197e5c: ffffffff bf1add8c 0000027f
bf197e6c: ffffffe4 0000001b
bf197e7c: 8004f940
[ 3818.970702]
Call Trace:
[<8004af18>] buserr+0x54/0x80
[<8004afec>] trap_c+0xa8/0xe4
[<8004a418>] csky_trap+0x68/0x70
Segmentation fault
从错误log看出LR指向0x00008524 ,也即printf(""EOP);。所以问题出在pf();。
void main(void)
{
: subi sp, sp,
: dd0e2000 st.w r8, (sp, )
: ddee2001 st.w r15, (sp, 0x4)
850a: 6e3b mov r8, sp
850c: subi sp, sp,
void *(*pf)(void);
pf = 0xfffffffe;
850e: e4481003 subi r2, r8,
: movi r3,
: 2b01 subi r3,
: b260 st.w r3, (r2, )
: db622000 ld.w r27, (r2, )
pf();
851c: e4681003 subi r3, r8,
: ld.w r3, (r3, )
: 7bcd jsr r3
printf("EOP");
: lrw r0, 0x85fc // 8540 <main+0x40>
: eae00008 jsri 0x0 // from address pool at 0x8544
852a: c4004820 lsli r0, r0,
}
3.小结
综上所述遇到“User mode Bus Error”类型的错误,可以通过查看PC指针。
如果PC明显不是指向代码段,查看LR地址。这样就可以大概找出问题点。
期间需要借助objdump、hexdump等工具。