{"id":15706916,"url":"https://github.com/jhass/crystal-malloc_pthread_shim","last_synced_at":"2025-08-17T20:10:40.362Z","repository":{"id":138715365,"uuid":"263919458","full_name":"jhass/crystal-malloc_pthread_shim","owner":"jhass","description":"Redirect malloc and pthread functions  to bdwgc","archived":false,"fork":false,"pushed_at":"2020-05-22T09:35:19.000Z","size":6,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-29T08:41:47.339Z","etag":null,"topics":["binding","crystal","malloc","pthread","shard","threads"],"latest_commit_sha":null,"homepage":"","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jhass.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-14T13:14:37.000Z","updated_at":"2023-03-27T10:45:53.000Z","dependencies_parsed_at":"2023-07-04T09:31:54.885Z","dependency_job_id":null,"html_url":"https://github.com/jhass/crystal-malloc_pthread_shim","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/jhass/crystal-malloc_pthread_shim","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhass%2Fcrystal-malloc_pthread_shim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhass%2Fcrystal-malloc_pthread_shim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhass%2Fcrystal-malloc_pthread_shim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhass%2Fcrystal-malloc_pthread_shim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhass","download_url":"https://codeload.github.com/jhass/crystal-malloc_pthread_shim/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhass%2Fcrystal-malloc_pthread_shim/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270899582,"owners_count":24664720,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binding","crystal","malloc","pthread","shard","threads"],"created_at":"2024-10-03T20:31:03.547Z","updated_at":"2025-08-17T20:10:40.336Z","avatar_url":"https://github.com/jhass.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# malloc and pthread shim for bdwgc and Crystal\n\nOn Linux this shims the `malloc` family of functions as well as most of the `pthread_` functions for actual thread handling and redirects them to their equivalents from [bdwgc](https://github.com/ivmai/bdwgc).\n\nOn other platforms this does nothing.\n\n## Why would you do this?\n\nLooking at bdwgc's `README.linux`, we can find the following sentence:\n\n\u003e Every file that makes thread calls should define GC_THREADS, and then\n   include gc.h.  The latter redefines some of the pthread primitives as\n   macros which also provide the collector with information it requires.\n\nOn Darwin and Windows bdwgc can and does utilize OS APIs to the necessary tracking, but Linux does not provide these.\n\nBut what do you do if a shared library spawns threads? The Crystal compiler does not make any effort to redirect those calls to bdwgc. This can lead bdwgc to clobbering the memory allocated there, since it allocates memory using `brk(2)`/`sbrk(2)` rather than the malloc interface and therefore needs to made aware of memory allocated otherwise. `README.linux` continues with:\n\n\u003e A new alternative to (3a) is to build the collector and compile GC clients\n   with -DGC_USE_LD_WRAP, and to link the final program with [...]\n\nWhich isn't very practical, especially in the Crystal ecosystem. This shard presents an alternative.\n\n## Usage\n\nMake sure you're using Crystal 0.35 or later. Then:\n\n1. Add the dependency to your `shard.yml`:\n\n   ```yaml\n   dependencies:\n     malloc_pthread_shim:\n       github: jhass/crystal-malloc_pthread_shim\n       version: 0.1.0\n   ```\n\n2. Run `shards install`.\n3. Add `require \"malloc_pthread_shim\"` to your project.\n4. Ensure to link your program against glibc.\n\n   If you're unsure on how to do that, you're most likely already doing it. In doubt, just try to ignore this step.\n\nThat's it!\n\n## So, how does it work?\n\nLuckily for us the linker looks up symbols starting in the main binary, so any function we define there wins over the one with the same name in any shared libraries. This allows us to override these functions and redirect them to bdwgc. When bdwgc then continues to call the originals, we use `dlopen(3)` to call the unwrapped functions. However for `malloc` and a few related functions we cannot do this, as `dlopen` itself calls them and ends up at our shim function again, resulting in infinite recursion. Here the dependency on glibc comes into play, it provides alternative symbols for those essential functions that we can use to call the originals.\n\n## Contributing\n\nCurrently this only handles glibc on Linux. Darwin and Windows seem to be immune to this issue. If you encounter this issue on any other platform or libc, collecting solutions here is welcome!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhass%2Fcrystal-malloc_pthread_shim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhass%2Fcrystal-malloc_pthread_shim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhass%2Fcrystal-malloc_pthread_shim/lists"}