PolarSSL's asm code execute different result in iPad4 and simulator

159 views Asked by At

I am developing an encrypt and decrypt app.I use the bignum algorithm.That is polarSSL. In Xcode simulator ,it works well and could decrypt file successfully.But when I run the same project on the real device(iPad 4),it crashed.

So I debug it and I found the reason. In simulator,it execute the following code:

 #if defined(__i386__)
#define MULADDC_INIT                            \
    asm( "movl   %0, %%esi      " :: "m" (s));  \
    asm( "movl   %0, %%edi      " :: "m" (d));  \
    asm( "movl   %0, %%ecx      " :: "m" (c));  \
    asm( "movl   %0, %%ebx      " :: "m" (b));

#define MULADDC_CORE                            \
    asm( "lodsl                 " );            \
    asm( "mull   %ebx           " );            \
    asm( "addl   %ecx,   %eax   " );            \
    asm( "adcl   $0,     %edx   " );            \
    asm( "addl   (%edi), %eax   " );            \
    asm( "adcl   $0,     %edx   " );            \
    asm( "movl   %edx,   %ecx   " );            \
    asm( "stosl                 " );

#define MULADDC_STOP                            \
    asm( "movl   %%ecx, %0      " :: "m" (c));  \
    asm( "movl   %%edi, %0      " :: "m" (d));  \
    asm( "movl   %%esi, %0      " :: "m" (s) :  \
    "eax", "ecx", "edx", "ebx", "esi", "edi" );

while in iPad4, it execute the following code:
#if defined(__arm__)

#define MULADDC_INIT                            \
    asm( "ldr    r0, %0         " :: "m" (s));  \
    asm( "ldr    r1, %0         " :: "m" (d));  \
    asm( "ldr    r2, %0         " :: "m" (c));  \
    asm( "ldr    r3, %0         " :: "m" (b));

#define MULADDC_CORE                            \
    asm( "ldr    r4, [r0], #4   " );            \
    asm( "mov    r5, #0         " );            \
    asm( "ldr    r6, [r1]       " );            \
    asm( "umlal  r2, r5, r3, r4 " );            \
    asm( "adds   r7, r6, r2     " );            \
    asm( "adc    r2, r5, #0     " );            \
    asm( "str    r7, [r1], #4   " );

#define MULADDC_STOP                            \
    asm( "str    r2, %0         " :: "m" (c));  \
    asm( "str    r1, %0         " :: "m" (d));  \
    asm( "str    r0, %0         " :: "m" (s) :  \
    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" );

The reason is clear,device's cpu is ARM architecture while simulator is i386.But i386 code could execute successfully while arm failed confused me.These asm code comes from polarSSL,and is written in standard arm instruction. So I think whether Apple Inc change the CPU architecture,as we know,iPad 4's cpu is A6X,which is designed by Apple. So I don't know how to solve this problem.Hope someone could help me.Thanks a lot

1

There are 1 answers

2
Paul On BEST ANSWER

Based on the code you present, you are using an old version of the library!

If you check the 'current' bn_mul.h, you will see that the statements have changed to a 'single' asm statement (which is required for compiler optimizations)..

Please try the new one and report back :)