Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing clobbered register in cpuinfo #159

Closed
SDLBugzilla opened this issue Feb 10, 2021 · 0 comments
Closed

Missing clobbered register in cpuinfo #159

SDLBugzilla opened this issue Feb 10, 2021 · 0 comments

Comments

@SDLBugzilla
Copy link
Collaborator

This bug report was migrated from our old Bugzilla tracker.

These attachments are available in the static archive:

Reported in version: HG 2.0
Reported for operating system, platform: Linux, x86

Comments on the original bug report:

On 2009-04-26 21:34:05 +0000, Nicholas Phillips wrote:

I am using x64 Linux (using Intel Core 2 DUO), and I have noticed that there is an error in SDL_cpuinfo.c, function CPU_getCPUIDFeaturesExt for my platform.

The code in question is:

#elif defined(GNUC) && defined (x86_64)
asm (
" movq %%rbx,%%rdi\n"
" movl $0x80000000,%%eax # Query for extended functions \n"
" cpuid # Get extended function limit \n"
" cmpl $0x80000001,%%eax \n"
" jl 1f # Nope, we dont have function 800000001h\n"
" movl $0x80000001,%%eax # Setup extended function 800000001h\n"
" cpuid # and get the information \n"
" movl %%edx,%0 \n"
"1: \n"
" movq %%rdi,%%rbx\n"
: "=m" (features)
:
: "%rax", "%rcx", "%rdx", "%rdi"
);

Even though %rbx is saved and restored manually, it still needs to appear on the clobbered register list, because it is modified by the cpuid instruction. If GCC chose to use %rbx to store %0 (features) it would cause big problems. For example consider the following code produced by GCC with the -S switch (code in question used out of context):

_ZN5CTest14MemberFunctionEv:
.LFB1029:
pushq %rbx
.LCFI1:
movl $10, %edx
movl $.LC0, %esi
movq %rdi, %rbx
call _ZNSs6assignEPKcm
#APP

225 "test.cpp" 1

      movq    %rbx,%rdi
    movl    $0x80000000,%eax   # Query for extended functions    
    cpuid                       # Get extended function limit     
    cmpl    $0x80000001,%eax                                     
    jl      1f                  # Nope, we dont have function 800000001h
    movl    $0x80000001,%eax   # Setup extended function 800000001h
    cpuid                       # and get the information         
    movl    %edx,48(%rbx)                                              

1:
movq %rdi,%rbx

0 "" 2

#NO_APP

This causes a segmentation fault for obvious reasons.

Adding %rbx to the clobbered register list makes GCC use a different register to store %0, and eliminates the problem.

Cheers.

On 2009-04-26 21:46:26 +0000, Nicholas Phillips wrote:

Created attachment 324
Proposed patch for SDL_cpuinfo.c

I noticed that the same problem could happen in other functions and for other platforms found in SDL_cpuinfo.c. Here is a simple patch.

On 2009-06-25 10:56:27 +0000, Ozkan Sezer wrote:

Is SDL-1.2 not affected?

On 2009-09-26 03:05:47 +0000, Sam Lantinga wrote:

Thank you very much for your patch!

Do you give me permission to release your code with SDL 1.3 and future
versions of SDL under both the LGPL and a closed-source commercial
license?

On 2009-09-26 09:35:19 +0000, Joshua Root wrote:

Building on i386 OS X seem to be broken now:
./src/cpuinfo/SDL_cpuinfo.c: In function 'SDL_GetCPUFeatures':
./src/cpuinfo/SDL_cpuinfo.c:150: error: PIC register '%ebx' clobbered in 'asm'

On 2009-09-26 14:40:57 +0000, Sam Lantinga wrote:

Whoops, fixed, thanks!

On 2009-10-29 21:46:02 +0000, Sam Lantinga wrote:

Okay, I tracked this down and took care of it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant