Skip to content

Instantly share code, notes, and snippets.

@imaami
Created March 20, 2026 21:23
Show Gist options
  • Select an option

  • Save imaami/a7fb6acdec2f37cc5aaa3a5c37c5bfdc to your computer and use it in GitHub Desktop.

Select an option

Save imaami/a7fb6acdec2f37cc5aaa3a5c37c5bfdc to your computer and use it in GitHub Desktop.
Cursed allocator
#define _GNU_SOURCE 1
#include <dlfcn.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "dlalloc.h"
#define force_inline __attribute__((always_inline)) static inline
#define dla_sym_buf_size() (sizeof("__d") + (2u * sizeof(long)))
#define dla_src_buf_size() \
(sizeof("extern char[];\nchar[];\n") + (2u * (dla_sym_buf_size() + \
_Generic(&(char[sizeof(size_t)]){0}, char(*)[4]: 10u, default: 20u))))
struct dla_buf
{
char sym[dla_sym_buf_size()];
char src[dla_src_buf_size()];
size_t len;
};
force_inline unsigned long
dla_id (void)
{
static _Atomic unsigned long dla_ctr__;
return atomic_fetch_add_explicit(&dla_ctr__, 1u, memory_order_relaxed);
}
force_inline char const *
dla_sym_fmt (void)
{
return _Generic(&(char[sizeof(long)]){0},
char(*)[4]: "__d%08lx", default: "__d%016lx");
}
force_inline bool
dla_codegen (struct dla_buf *buf,
size_t size)
{
int r = snprintf(buf->sym, sizeof(buf->sym), dla_sym_fmt(), dla_id());
if (r < 1 || r >= (int)sizeof(buf->sym))
return false;
r = snprintf(buf->src, sizeof(buf->src),
"extern char %s[%zu];\nchar %s[%zu];\n",
buf->sym, size, buf->sym, size);
if (r < 1 || r >= (int)sizeof(buf->src))
return false;
buf->len = (size_t)r;
return true;
}
void *
dlalloc (size_t size)
{
struct dla_buf buf;
if (!dla_codegen(&buf, size))
return NULL;
int pfd[2];
if (pipe(pfd) == -1)
return NULL;
FILE *fp = tmpfile();
if (!fp)
return NULL;
int fd = fileno(fp);
char obj_path[256];
snprintf(obj_path, sizeof(obj_path), "/proc/self/fd/%d", fd);
pid_t pid = fork();
if (!pid) {
close(pfd[1]);
dup2(pfd[0], STDIN_FILENO);
close(pfd[0]);
execlp("cc", "cc", "-x", "c", "-", "-shared", "-fPIC", "-o",
obj_path, (char *)NULL);
exit(EXIT_FAILURE);
}
void *sym = NULL;
if (pid > 0) {
close(pfd[0]);
write(pfd[1], buf.src, buf.len);
close(pfd[1]);
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && !WEXITSTATUS(status)) {
void *obj = dlopen(obj_path, RTLD_NOW | RTLD_LOCAL);
if (obj) {
sym = dlsym(obj, buf.sym);
if (!sym)
dlclose(obj);
}
}
}
fclose(fp);
return sym;
}
void
dlfree (void *ptr)
{
if (ptr) {
Dl_info i = { .dli_fname = NULL };
dladdr(ptr, &i);
if (i.dli_fname) {
void *obj = dlopen(i.dli_fname, RTLD_NOW | RTLD_NOLOAD);
if (obj)
dlclose(obj);
}
}
}
#ifndef DLALLOC_H_
#define DLALLOC_H_
#include <stddef.h>
extern void *dlalloc (size_t size);
extern void dlfree (void *ptr);
#endif /* DLALLOC_H_ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dlalloc.h"
int
main (int argc,
char **argv)
{
if (argc < 2 || !*argv[1])
return EXIT_FAILURE;
size_t len = strlen(argv[1]);
char *ptr = dlalloc(len + 1);
if (!ptr)
return EXIT_FAILURE;
memcpy(ptr, argv[1], len);
ptr[len] = '\0';
puts(ptr);
dlfree(ptr);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment