i386 get_cpuid() function looks wrong

Jussi Kivilinna jussi.kivilinna at iki.fi
Wed Apr 28 18:41:12 CEST 2021


Hello,

On 28.4.2021 4.27, Iru Cai via Gcrypt-devel wrote:
> Hello,
> 
> I built libgcrypt for 32-bit x86, and it reports wrong CPU hardware features. After some debugging, I found the get_cpuid() function go wrong. This function use some inline assembly code:
> 
>    asm volatile
>      ("movl %%ebx, %%edi\n\t"     /* Save GOT register.  */
>       "xorl %%ebx, %%ebx\n\t"
>       "cpuid\n\t"
>       "movl %%ebx, %1\n\t"
>       "movl %%edi, %%ebx\n\t"     /* Restore GOT register. */
>       : "=a" (regs[0]), "=g" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
>       : "0" (in), "2" (0), "3" (0)
>       : "cc", "edi"
>       );
> 
> When I build libgcrypt with CFLAGS '-m32 -march=i686 -Os', '%1' in the above assembly code can be assigned to ebx, thus the code go wrong. After checking the GCC manual, "=g" means any register, so the compiler can allocate ebx to it.
> 

Thanks for reporting this.

You're right, that "=g" is wrong there. I've attached patch that fixes issue by using %edi register operand instead and swapping %ebx contents with %edi before and after cpuid.

-Jussi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-hwf-x86-fix-use-of-wrong-type-operand.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <https://lists.gnupg.org/pipermail/gcrypt-devel/attachments/20210428/a1223427/attachment.bin>


More information about the Gcrypt-devel mailing list