{"action":"created","issue":{"url":"https://api.github.com/repos/libuv/libuv/issues/287","labels_url":"https://api.github.com/repos/libuv/libuv/issues/287/labels{/name}","comments_url":"https://api.github.com/repos/libuv/libuv/issues/287/comments","events_url":"https://api.github.com/repos/libuv/libuv/issues/287/events","html_url":"https://github.com/libuv/libuv/pull/287","id":63845854,"number":287,"title":"memory: fix uv_replace_allocator","user":{"login":"mattsta","id":15594,"avatar_url":"https://avatars.githubusercontent.com/u/15594?v=3","gravatar_id":"","url":"https://api.github.com/users/mattsta","html_url":"https://github.com/mattsta","followers_url":"https://api.github.com/users/mattsta/followers","following_url":"https://api.github.com/users/mattsta/following{/other_user}","gists_url":"https://api.github.com/users/mattsta/gists{/gist_id}","starred_url":"https://api.github.com/users/mattsta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattsta/subscriptions","organizations_url":"https://api.github.com/users/mattsta/orgs","repos_url":"https://api.github.com/users/mattsta/repos","events_url":"https://api.github.com/users/mattsta/events{/privacy}","received_events_url":"https://api.github.com/users/mattsta/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/libuv/libuv/milestones/1","html_url":"https://github.com/libuv/libuv/milestones/1.5.0","labels_url":"https://api.github.com/repos/libuv/libuv/milestones/1/labels","id":1040118,"number":1,"title":"1.5.0","description":"","creator":{"login":"saghul","id":317464,"avatar_url":"https://avatars.githubusercontent.com/u/317464?v=3","gravatar_id":"","url":"https://api.github.com/users/saghul","html_url":"https://github.com/saghul","followers_url":"https://api.github.com/users/saghul/followers","following_url":"https://api.github.com/users/saghul/following{/other_user}","gists_url":"https://api.github.com/users/saghul/gists{/gist_id}","starred_url":"https://api.github.com/users/saghul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saghul/subscriptions","organizations_url":"https://api.github.com/users/saghul/orgs","repos_url":"https://api.github.com/users/saghul/repos","events_url":"https://api.github.com/users/saghul/events{/privacy}","received_events_url":"https://api.github.com/users/saghul/received_events","type":"User","site_admin":false},"open_issues":3,"closed_issues":4,"state":"open","created_at":"2015-03-26T14:53:13Z","updated_at":"2015-04-10T13:36:57Z","due_on":null,"closed_at":null},"comments":20,"created_at":"2015-03-23T22:09:32Z","updated_at":"2015-04-12T16:47:31Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/libuv/libuv/pulls/287","html_url":"https://github.com/libuv/libuv/pull/287","diff_url":"https://github.com/libuv/libuv/pull/287.diff","patch_url":"https://github.com/libuv/libuv/pull/287.patch"},"body":"Hey libuv,\r\n\r\nThis is a follow up to the four (four?!) other issues of #177, #178, #220, #231.\r\n\r\n## Summary\r\n\r\nThe recent custom allocator assignment is a great addition, but we need more coverage of allocator functions.\r\n\r\nlibc has 4 functions to manipulate memory (malloc, calloc, realloc, free) and 2 functions returning libc allocated memory (strdup, strndup).\r\n\r\nAny allocator replacement needs to override (or `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES`) all 6 functions.  Otherwise, if memory returned by one allocator is manipulated by another allocator, memory corruption and crashing occurs.\r\n\r\nThis commit allows setting all 6 functions with custom overridden versions.  All tests pass using `make check` on my OS X machine and on my Ubuntu 14.04 machine.\r\n\r\n## Review\r\n\r\nI took some guesses about where to place/call things, but give it a double check to see if all these locations make sense:\r\n\r\n  - Initializer in windows code is inside `win/core.c`:`uv_init()`: https://github.com/libuv/libuv/commit/2aad0cf8573e81c12d0e297d6e4ff9d68dc6d376#diff-0886ba7381a955af32f753d649ba2e29R90 (which is guarded by `win/core.c`:`uv__once_init()`)\r\n  - Initializer in unix code is inside `unix/loop.c`:`uv_loop_init()`:https://github.com/libuv/libuv/commit/2aad0cf8573e81c12d0e297d6e4ff9d68dc6d376#diff-c5e4480a66af872d6425f7f6ecd55e02R36 (which runs on every loop create—I didn't see a generic unix \"once init?\")\r\n  - The allocator wrappers are in `uv-common.h`: https://github.com/libuv/libuv/commit/2aad0cf8573e81c12d0e297d6e4ff9d68dc6d376#diff-f6578dfe0725c4ea412b7d8f6f34b783R217\r\n\r\n## Commit message\r\nPreviously, `uv_replace_allocator` was added to replace\r\nmalloc() and free(), but we must replace _all_ allocator\r\nfunctions or else we will crash in spectacular ways.\r\n\r\nFor a complete allocator replacement, we must redefine:\r\n  - malloc\r\n  - calloc\r\n  - realloc\r\n  - free\r\n  - strdup\r\n  - strndup\r\n\r\nNow `uv_replace_allocator` takes a struct `uv_malloc_config` with\r\nsix function pointers to each function operating on memory.\r\n\r\nAlso, we call `uv_replace_allocator` the first time a loop is\r\ncreated (windows) or every time a loop is created (unix) to make\r\nsure we are locked into our current allocator configuration.  Users\r\nmust only call `uv_replace_allocator` *before* they create their\r\nfirst event loop.\r\n\r\nAlso, this removes the previously created memory function wrappers\r\nthat were using conditionals to determine which functions to use.  Now\r\nwe use macros referencing the function pointers inside of one global\r\nstruct `uv__malloc_config` holding all allocator function pointers.\r\n\r\nAlso, this replaces the remaining bare allocator functions\r\n(calloc, realloc, strdup, strndup) with proper uv__* macro variants."},"comment":{"url":"https://api.github.com/repos/libuv/libuv/issues/comments/92086845","html_url":"https://github.com/libuv/libuv/pull/287#issuecomment-92086845","issue_url":"https://api.github.com/repos/libuv/libuv/issues/287","id":92086845,"user":{"login":"saghul","id":317464,"avatar_url":"https://avatars.githubusercontent.com/u/317464?v=3","gravatar_id":"","url":"https://api.github.com/users/saghul","html_url":"https://github.com/saghul","followers_url":"https://api.github.com/users/saghul/followers","following_url":"https://api.github.com/users/saghul/following{/other_user}","gists_url":"https://api.github.com/users/saghul/gists{/gist_id}","starred_url":"https://api.github.com/users/saghul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saghul/subscriptions","organizations_url":"https://api.github.com/users/saghul/orgs","repos_url":"https://api.github.com/users/saghul/repos","events_url":"https://api.github.com/users/saghul/events{/privacy}","received_events_url":"https://api.github.com/users/saghul/received_events","type":"User","site_admin":false},"created_at":"2015-04-12T16:47:31Z","updated_at":"2015-04-12T16:47:31Z","body":"@mattsta ping :-)"}}