Ticket #5052 (closed bug: fixed)

Opened 2 years ago

Last modified 2 years ago

stdcall on OS X is broken

Reported by: igloo Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.0.2
Keywords: Cc:
Operating System: MacOS X Architecture: x86
Type of failure: None/Unknown Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

1288 is SIGBUSing on OS X 32. The symptom is that task in resumeThread is 3 (the argument we pass). Here's what I think's going on:

On Linux re generate:

0x0804a746 <s1bE_info+30>:      call   0x80839a0 <suspendThread>
0x0804a74b <s1bE_info+35>:      add    $0x4,%esp
0x0804a74e <s1bE_info+38>:      mov    0x40(%esp),%ecx
0x0804a752 <s1bE_info+42>:      push   %ecx
0x0804a753 <s1bE_info+43>:      mov    %eax,0x50(%esp)
0x0804a757 <s1bE_info+47>:      call   0x804a8d0 <test>
0x0804a75c <s1bE_info+52>:      mov    0x4c(%esp),%eax
0x0804a760 <s1bE_info+56>:      push   %eax
0x0804a761 <s1bE_info+57>:      call   0x8083aa4 <resumeThread>

We save %eax to 0x50(%esp), do the call (during which %esp is increased by 4), and then read %eax back from 0x4c(%esp).

And on the Mac:

0x00002282 <s1bi_info+38>:      call   0x240cc4 <suspendThread>
0x00002287 <s1bi_info+43>:      add    $0x10,%esp
0x0000228a <s1bi_info+46>:      sub    $0xc,%esp
0x0000228d <s1bi_info+49>:      mov    0x4c(%esp),%ecx
0x00002291 <s1bi_info+53>:      push   %ecx
0x00002292 <s1bi_info+54>:      mov    %eax,0x5c(%esp)
0x00002296 <s1bi_info+58>:      call   0x242a <test>
0x0000229b <s1bi_info+63>:      sub    $0xc,%esp
0x0000229e <s1bi_info+66>:      mov    0x58(%esp),%eax
0x000022a2 <s1bi_info+70>:      push   %eax
0x000022a3 <s1bi_info+71>:      call   0x240db1 <resumeThread>

We try to do the same thing, but this instruction:

0x0000229b <s1bi_info+63>:      sub    $0xc,%esp

alters %esp in-between us saving and restoring %eax.

Attachments

5052.dpatch Download (1.6 KB) - added by batterseapower 2 years ago.

Change History

Changed 2 years ago by michalt

  • os changed from Unknown/Multiple to MacOS X
  • architecture changed from Unknown/Multiple to x86

Changed 2 years ago by igloo

Also affects tests 2276 and 2276_ghci.

Changed 2 years ago by batterseapower

Changed 2 years ago by batterseapower

  • status changed from new to patch

I've attached a ticket that seems to fix this bug for me. The problem is that the codegen for foreign calls makes sure the stack is 16-byte aligned on OS X by pushing some padding. In the case where the foreign call is cdecl, that padding gets popped after the call, but if the convention is stdcall then it doesn't generate any popping code at all. However, this is incorrect because the stdcall only promises to pop the arguments, not the padding.

The fix is to generate code to pop the padding (if any) on OS X.

Changed 2 years ago by simonmar

Looks good to me; go ahead and commit.

Changed 2 years ago by batterseapower

  • status changed from patch to closed
  • resolution set to fixed

Pushed. Fixed by:

commit 7a29845e5ed105fcbd6ccadb8061bae2c124f817
Author: Max Bolingbroke <batterseapower@hotmail.com>
Date:   Sat Apr 2 00:06:16 2011 +0100

    Pop OS X stack padding even if the foreign call is stdcall (#5052)
Note: See TracTickets for help on using tickets.