{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/238","repository_url":"https://api.github.com/repos/Microsoft/BashOnWindows","labels_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/238/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/238/comments","events_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/238/events","html_url":"https://github.com/Microsoft/BashOnWindows/issues/238","id":149936632,"number":238,"title":"Strange behaviour with writev()","user":{"login":"arachnist","id":117764,"avatar_url":"https://avatars.githubusercontent.com/u/117764?v=3","gravatar_id":"","url":"https://api.github.com/users/arachnist","html_url":"https://github.com/arachnist","followers_url":"https://api.github.com/users/arachnist/followers","following_url":"https://api.github.com/users/arachnist/following{/other_user}","gists_url":"https://api.github.com/users/arachnist/gists{/gist_id}","starred_url":"https://api.github.com/users/arachnist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arachnist/subscriptions","organizations_url":"https://api.github.com/users/arachnist/orgs","repos_url":"https://api.github.com/users/arachnist/repos","events_url":"https://api.github.com/users/arachnist/events{/privacy}","received_events_url":"https://api.github.com/users/arachnist/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/BashOnWindows/labels/fixinbound","name":"fixinbound","color":"0e8a16"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-04-21T01:55:30Z","updated_at":"2016-05-14T19:17:28Z","closed_at":null,"body":"I took a simple bit of code:\r\n```C\r\n#include <stdio.h>\r\n#include <unistd.h>\r\n\r\nint main(int argc, char *argv[]) {\r\n\tprintf(\"this printf\\ndoes not work\\n\");\r\n\tprintf(\"this printf works\\n\"); \r\n\treturn 0;\r\n}\r\n```\r\n\r\nCompiled it statically on a **musl** system, copied it over to the windows box and the results were interesting:\r\n```\r\nroot@localhost:~# ./test.musl\r\nthis printf works\r\nroot@localhost:~#\r\n```\r\n\r\nSo… what's happening here? This is what strace says:\r\n```\r\nroot@localhost:~# strace -o test.musl.strace ./test.musl\r\nstrace: Test for PTRACE_O_TRACESYSGOOD failed, giving up using this feature.\r\nthis printf works\r\n_exit returned!\r\nroot@localhost:~# cat test.musl.strace\r\nexecve(\"./test.musl\", [\"./test.musl\"], [/* 13 vars */]) = 0\r\narch_prctl(ARCH_SET_FS, 0x602570)       = 0\r\nset_tid_address(0x6025a8)               = 2381\r\nioctl(1, TIOCGWINSZ, {ws_row=88, ws_col=237, ws_xpixel=0, ws_ypixel=0}) = 0\r\nwritev(1, [{0x602168, 0}, {\"this printf\\n\", 12}], 2) = -1 EFAULT (Bad address)\r\nwritev(1, [{\"this printf works\", 17}, {\"\\n\", 1}], 2) = 18\r\nexit_group(0)                           = ?\r\n<... exit_group resumed> )              = ?\r\n+++ exited with 0 +++\r\n```\r\n\r\nThe `writev(1, [{0x602168, 0}, {\"this printf\\n\", 12}], 2) = -1 EFAULT (Bad address)` line looks especially interesting.\r\n\r\nFor comparison, this is what strace on a linux box says for the exact same binary:\r\n```\r\nexecve(\"./test.musl\", [\"./test.musl\"], [/* 27 vars */]) = 0\r\narch_prctl(ARCH_SET_FS, 0x602570)       = 0\r\nset_tid_address(0x6025a8)               = 4108\r\nioctl(1, TIOCGWINSZ, {ws_row=58, ws_col=85, ws_xpixel=850, ws_ypixel=1044}) = 0\r\nwritev(1, [{\"\", 0}, {\"this printf\\n\", 12}], 2) = 12\r\nwritev(1, [{\"does not work\", 13}, {\"\\n\", 1}], 2) = 14\r\nwritev(1, [{\"this printf works\", 17}, {\"\\n\", 1}], 2) = 18\r\nexit_group(0)                           = ?\r\n+++ exited with 0 +++\r\n```\r\n\r\nI've attached the static musl binary in question.\r\n[test.musl.zip](https://github.com/Microsoft/BashOnWindows/files/229093/test.musl.zip)\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/comments/219238178","html_url":"https://github.com/Microsoft/BashOnWindows/issues/238#issuecomment-219238178","issue_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/238","id":219238178,"user":{"login":"pbfy0","id":666011,"avatar_url":"https://avatars.githubusercontent.com/u/666011?v=3","gravatar_id":"","url":"https://api.github.com/users/pbfy0","html_url":"https://github.com/pbfy0","followers_url":"https://api.github.com/users/pbfy0/followers","following_url":"https://api.github.com/users/pbfy0/following{/other_user}","gists_url":"https://api.github.com/users/pbfy0/gists{/gist_id}","starred_url":"https://api.github.com/users/pbfy0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pbfy0/subscriptions","organizations_url":"https://api.github.com/users/pbfy0/orgs","repos_url":"https://api.github.com/users/pbfy0/repos","events_url":"https://api.github.com/users/pbfy0/events{/privacy}","received_events_url":"https://api.github.com/users/pbfy0/received_events","type":"User","site_admin":false},"created_at":"2016-05-14T19:17:27Z","updated_at":"2016-05-14T19:17:27Z","body":"I wrote a workaround that emulates the writev syscall using ptrace and ld_preload. It's pretty ugly, and it may not work in all cases, but it generally seems to work. You can find it at https://github.com/pbfy0/writev_fix"}}