{"action":"created","issue":{"url":"https://api.github.com/repos/atgreen/libffi/issues/215","labels_url":"https://api.github.com/repos/atgreen/libffi/issues/215/labels{/name}","comments_url":"https://api.github.com/repos/atgreen/libffi/issues/215/comments","events_url":"https://api.github.com/repos/atgreen/libffi/issues/215/events","html_url":"https://github.com/atgreen/libffi/issues/215","id":119111130,"number":215,"title":"win32 x86 stdcall closure: incorrectly restored stack after closure call","user":{"login":"amitin","id":1537180,"avatar_url":"https://avatars.githubusercontent.com/u/1537180?v=3","gravatar_id":"","url":"https://api.github.com/users/amitin","html_url":"https://github.com/amitin","followers_url":"https://api.github.com/users/amitin/followers","following_url":"https://api.github.com/users/amitin/following{/other_user}","gists_url":"https://api.github.com/users/amitin/gists{/gist_id}","starred_url":"https://api.github.com/users/amitin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amitin/subscriptions","organizations_url":"https://api.github.com/users/amitin/orgs","repos_url":"https://api.github.com/users/amitin/repos","events_url":"https://api.github.com/users/amitin/events{/privacy}","received_events_url":"https://api.github.com/users/amitin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-11-26T23:42:32Z","updated_at":"2015-11-30T19:42:03Z","closed_at":null,"body":"... which causes a broken stack if a caller function is naked.\r\nconsider the following C code:\r\n```C\r\ntypedef int (stdcall * closure_func)(int);\r\nint stdcall call_closure(closure_func cb, int c)\r\n{\r\n  return cb(c);\r\n}\r\n```\r\nwhere 'cb' is a pointer to ffi_closure.\r\n\r\nmingw-gcc 5.1 and 4.9.2 produce the following code for 'call_closure()' (Intel syntax):\r\n```asm\r\nsub    esp,0x1c\r\nmov    eax,DWORD PTR [esp+0x24]\r\nmov    DWORD PTR [esp],eax\r\ncall   DWORD PTR [esp+0x20]\r\nsub    esp,0x4\r\nadd    esp,0x1c\r\nret    0x8\r\n```\r\nAs you can see, there is no prologue allocating a stack frame there. If it was, a stack pointer will be restored correctly with a frame pointer and there is no crash, which hides the problem.\r\n\r\nI found that a possible cause for that is that arguments aligned by 16 in 'ffi_prep_cif_machdep()', see [src/x86/ffi.c:178](https://github.com/atgreen/libffi/blob/master/src/x86/ffi.c#L178). Removing alignment there fixes the issue for me.\r\n"},"comment":{"url":"https://api.github.com/repos/atgreen/libffi/issues/comments/160737033","html_url":"https://github.com/atgreen/libffi/issues/215#issuecomment-160737033","issue_url":"https://api.github.com/repos/atgreen/libffi/issues/215","id":160737033,"user":{"login":"amitin","id":1537180,"avatar_url":"https://avatars.githubusercontent.com/u/1537180?v=3","gravatar_id":"","url":"https://api.github.com/users/amitin","html_url":"https://github.com/amitin","followers_url":"https://api.github.com/users/amitin/followers","following_url":"https://api.github.com/users/amitin/following{/other_user}","gists_url":"https://api.github.com/users/amitin/gists{/gist_id}","starred_url":"https://api.github.com/users/amitin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amitin/subscriptions","organizations_url":"https://api.github.com/users/amitin/orgs","repos_url":"https://api.github.com/users/amitin/repos","events_url":"https://api.github.com/users/amitin/events{/privacy}","received_events_url":"https://api.github.com/users/amitin/received_events","type":"User","site_admin":false},"created_at":"2015-11-30T19:42:03Z","updated_at":"2015-11-30T19:42:03Z","body":"Sure I am. :-) My point is that ffi_closure doesn't work if invoked by such a function generated by the compiler."}}