interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / docs / osc / audit / baseline / shim.c

106 lines · 4414 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// LD_PRELOAD allocation counting shim. Counts malloc/calloc/realloc/free (+memalign family),
18// tracks live bytes with malloc_usable_size, prints totals + mallinfo2 at exit to stderr.
19#define _GNU_SOURCE
20#include <malloc.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdint.h>
25#include <stdatomic.h>
26
27extern void *__libc_malloc(size_t);
28extern void *__libc_calloc(size_t, size_t);
29extern void *__libc_realloc(void *, size_t);
30extern void __libc_free(void *);
31extern void *__libc_memalign(size_t, size_t);
32
33static atomic_ulong n_malloc, n_calloc, n_realloc, n_free, n_memalign, n_free_null;
34static atomic_ulong req_bytes; // sum of requested bytes (malloc/calloc/realloc new size/memalign)
35static atomic_long live, peak; // live usable bytes
36static atomic_ulong peak_count_live, live_count; // live block count / peak
37static atomic_long live_cnt_l, peak_cnt_l;
38
39static void add_live(void *p) {
40 if (!p) return;
41 long u = (long)malloc_usable_size(p);
42 long l = atomic_fetch_add(&live, u) + u;
43 long pk = atomic_load(&peak);
44 while (l > pk && !atomic_compare_exchange_weak(&peak, &pk, l)) {}
45 long c = atomic_fetch_add(&live_cnt_l, 1) + 1;
46 long pc = atomic_load(&peak_cnt_l);
47 while (c > pc && !atomic_compare_exchange_weak(&peak_cnt_l, &pc, c)) {}
48}
49static void sub_live(void *p) {
50 if (!p) return;
51 atomic_fetch_sub(&live, (long)malloc_usable_size(p));
52 atomic_fetch_sub(&live_cnt_l, 1);
53}
54
55void *malloc(size_t n) {
56 void *p = __libc_malloc(n);
57 atomic_fetch_add(&n_malloc, 1); atomic_fetch_add(&req_bytes, n);
58 add_live(p); return p;
59}
60void *calloc(size_t a, size_t b) {
61 void *p = __libc_calloc(a, b);
62 atomic_fetch_add(&n_calloc, 1); atomic_fetch_add(&req_bytes, a * b);
63 add_live(p); return p;
64}
65void *realloc(void *o, size_t n) {
66 atomic_fetch_add(&n_realloc, 1); atomic_fetch_add(&req_bytes, n);
67 if (o) sub_live(o);
68 void *p = __libc_realloc(o, n);
69 if (p) add_live(p); else if (o && n) add_live(o); /* failed: old block stays live */
70 else if (o && n == 0) { /* realloc(p,0) frees */ }
71 return p;
72}
73void free(void *p) {
74 if (!p) { atomic_fetch_add(&n_free_null, 1); return; }
75 atomic_fetch_add(&n_free, 1);
76 sub_live(p);
77 __libc_free(p);
78}
79void *memalign(size_t al, size_t n) {
80 void *p = __libc_memalign(al, n);
81 atomic_fetch_add(&n_memalign, 1); atomic_fetch_add(&req_bytes, n);
82 add_live(p); return p;
83}
84int posix_memalign(void **out, size_t al, size_t n) {
85 void *p = __libc_memalign(al, n);
86 if (!p) return 12;
87 atomic_fetch_add(&n_memalign, 1); atomic_fetch_add(&req_bytes, n);
88 add_live(p); *out = p; return 0;
89}
90void *aligned_alloc(size_t al, size_t n) { return memalign(al, n); }
91
92__attribute__((destructor)) static void report(void) {
93 struct mallinfo2 m = mallinfo2();
94 unsigned long a = atomic_load(&n_malloc), c = atomic_load(&n_calloc), r = atomic_load(&n_realloc),
95 f = atomic_load(&n_free), ma = atomic_load(&n_memalign);
96 // allocs = malloc+calloc+memalign (new blocks; realloc(NULL,n) counted in realloc); realloc reported separately
97 fprintf(stderr,
98 "SHIM malloc=%lu calloc=%lu realloc=%lu memalign=%lu free=%lu free_null=%lu\n"
99 "SHIM new_block_allocs(malloc+calloc+memalign)=%lu allocs_minus_frees=%ld\n"
100 "SHIM live_blocks_at_exit=%ld live_usable_bytes_at_exit=%ld peak_live_usable_bytes=%ld peak_live_blocks=%ld requested_bytes_total=%lu\n"
101 "SHIM mallinfo2 arena=%zu uordblks=%zu fordblks=%zu hblks=%zu hblkhd=%zu keepcost=%zu\n",
102 a, c, r, ma, f, atomic_load(&n_free_null),
103 a + c + ma, (long)(a + c + ma) - (long)f,
104 atomic_load(&live_cnt_l), atomic_load(&live), atomic_load(&peak), atomic_load(&peak_cnt_l), atomic_load(&req_bytes),
105 m.arena, m.uordblks, m.fordblks, m.hblks, m.hblkhd, m.keepcost);
106}