Skip to content

VecPtrAt

Description

Pointer to the element at idx. Use this rather than indexing data directly so the canonical element stride is used.

Parameters

Name Direction Description
v in Vector to query.
idx in Index in [0, length).

Usage example (Cross-references)

Usage examples (Cross-references)
            if ((ValidateVec(UNPL(pv)), 1) && UNPL(pv)->length > 0)                                                        \
                for (u64 idx = 0, UNPL(d) = 1; UNPL(d); UNPL(d)--)                                                         \
                    for (VEC_DATATYPE(UNPL(pv)) *var = NULL; idx < UNPL(pv)->length && (var = VecPtrAt(UNPL(pv), idx), 1); \
                         idx++)
                for (u64 idx = UNPL(pv)->length; idx-- > 0 && idx < UNPL(pv)->length;)                                     \
                    for (u8 UNPL(run_once) = 1; UNPL(run_once); UNPL(run_once) = 0)                                        \
                        for (VEC_DATATYPE(UNPL(pv)) *var = VecPtrAt(UNPL(pv), idx); UNPL(run_once); UNPL(run_once) = 0)
    
    ///
                     idx >= UNPL(s) && idx < UNPL(e) && idx < UNPL(pv)->length && UNPL(s) <= UNPL(e);                      \
                     ++idx, UNPL(d) = 1)                                                                                   \
                    for (VEC_DATATYPE(UNPL(pv)) *var = VecPtrAt(UNPL(pv), idx); UNPL(d); UNPL(d) = 0)
    
    ///
    /// TAGS: Str, Access, Index, Pointer
    ///
    #define StrCharPtrAt(str, idx) VecPtrAt(str, idx)
    
    ///
                uint16_t idx = extract_u16(data, offset, data_size);
                if (idx < VecLen(vec)) {
                    volatile i32 *ptr = VecPtrAt(vec, idx);
                    volatile i32  val = *ptr;
                    (void)val;
                if (VecLen(vec) > 0 && *offset + 4 <= data_size) {
                    size_t index = extract_u32(data, offset, data_size) % VecLen(vec);
                    char **ptr   = VecPtrAt(vec, index);
                    (void)ptr; // Use the result to avoid warnings
                }
                if (VecLen(vec) > 0 && *offset + 4 <= data_size) {
                    size_t index = extract_u32(data, offset, data_size) % VecLen(vec);
                    Str   *ptr   = VecPtrAt(vec, index);
                    (void)ptr; // Use the result to avoid warnings
                }
        for (u64 i = 0; i < n_specs; ++i) {
            left_col[i] = StrInit(self->alloc);
            left_w[i]   = spec_format_left(VecPtrAt(&self->specs, i), &left_col[i]);
            if (left_w[i] > max_w)
                max_w = left_w[i];
        bool printed_options_header    = false;
        for (u64 i = 0; i < n_specs; ++i) {
            ArgSpec *sp = VecPtrAt(&self->specs, i);
            if (sp->role != ARG_ROLE_POSITIONAL)
                continue;
    
        for (u64 i = 0; i < n_specs; ++i) {
            ArgSpec *sp = VecPtrAt(&self->specs, i);
            if (sp->role == ARG_ROLE_POSITIONAL)
                continue;
    
    static GenericGraphSlot *graph_slot_ptr_raw(GenericGraph *graph, u32 index) {
        return VecPtrAt(&graph->slots, index);
    }
    
    static const GenericGraphSlot *graph_slot_ptr_const_raw(const GenericGraph *graph, u32 index) {
        return VecPtrAt((GraphSlots *)&graph->slots, index);
    }
        for (idx = 0; idx < VecLen(&graph->pending_edge_removals); idx++) {
            const GraphPendingEdgeRemoval *pending =
                VecPtrAt((GraphPendingEdgeRemovals *)&graph->pending_edge_removals, idx);
            if ((pending->from == from) && (pending->to == to)) {
                return idx;
    
        for (slot_index = 0; slot_index < VecLen(&graph->slots); slot_index++) {
            const GenericGraphSlot *slot    = VecPtrAt((GraphSlots *)&graph->slots, slot_index);
            GraphNodeId             self_id = graph_make_node_id((u32)slot_index, slot->generation);
            u64                     neighbor_i;
            }
    
            if (graph_slot_is_occupied(VecPtrAt((GraphSlots *)&graph->slots, index))) {
                LOG_FATAL("Graph free index points to an occupied slot");
            }
        for (free_index_i = 0; free_index_i < VecLen(&graph->pending_edge_removals); free_index_i++) {
            const GraphPendingEdgeRemoval *pending =
                VecPtrAt((GraphPendingEdgeRemovals *)&graph->pending_edge_removals, free_index_i);
            const GraphNeighbors *neighbors;
    
        for (slot_index = 0; slot_index < VecLen(&graph->slots); slot_index++) {
            GenericGraphSlot *slot = VecPtrAt(&graph->slots, slot_index);
            if (graph_slot_is_occupied(slot)) {
                graph_release_slot(graph, slot, item_size);
        explicit_edge_removal_count = VecLen(&graph->pending_edge_removals);
        for (edge_idx = 0; edge_idx < VecLen(&graph->pending_edge_removals); edge_idx++) {
            GraphPendingEdgeRemoval *pending = VecPtrAt(&graph->pending_edge_removals, edge_idx);
            (void)graph_remove_edge_now(graph, pending->from, pending->to);
        }
    
        for (slot_index = 0; slot_index < VecLen(&graph->slots); slot_index++) {
            GenericGraphSlot *slot = VecPtrAt(&graph->slots, slot_index);
            if (graph_slot_is_occupied(slot) && graph_slot_is_marked(slot)) {
                GraphNodeId marked_id = graph_make_node_id((u32)slot_index, slot->generation);
    
        for (slot_index = 0; slot_index < VecLen(&graph->slots); slot_index++) {
            GenericGraphSlot *slot = VecPtrAt(&graph->slots, slot_index);
            if (graph_slot_is_occupied(slot) && !graph_slot_is_marked(slot)) {
                GraphNodeId live_id = graph_make_node_id((u32)slot_index, slot->generation);
    
        for (slot_index = 0; slot_index < VecLen(&graph->slots); slot_index++) {
            GenericGraphSlot *slot = VecPtrAt(&graph->slots, slot_index);
            if (graph_slot_is_occupied(slot) && graph_slot_is_marked(slot)) {
                if (VecLen(&slot->out_neighbors) || VecLen(&slot->in_neighbors)) {
        while (iter->slot_index < VecLen(&iter->graph->slots)) {
            u32               index  = (u32)iter->slot_index;
            GenericGraphSlot *slot   = VecPtrAt(&iter->graph->slots, iter->slot_index);
            iter->slot_index        += 1;
    static const DebugFreedEntry *debug_freed_find(const DebugAllocator *dbg, void *ptr) {
        for (u32 i = 0; i < VecLen(&dbg->freed); i++) {
            if (VecPtrAt(&dbg->freed, i)->ptr == ptr)
                return VecPtrAt(&dbg->freed, i);
        }
        for (u32 i = 0; i < VecLen(&dbg->freed); i++) {
            if (VecPtrAt(&dbg->freed, i)->ptr == ptr)
                return VecPtrAt(&dbg->freed, i);
        }
        return NULL;
        bool trail_sep = (path_len > 0 && path[path_len - 1] == '/');
        for (size i = 0; i < VecLen(&dc); ++i) {
            DirEntry *e        = VecPtrAt(&dc, i);
            Zstr      entry_nm = StrBegin(&e->name);
            if (ZstrCompare(entry_nm, ".") == 0 || ZstrCompare(entry_nm, "..") == 0) {
    static ResolverCacheEntry *resolver_cache_find_or_open(SymbolResolver *self, Zstr path) {
        for (u64 i = 0; i < VecLen(&self->cache); ++i) {
            ResolverCacheEntry *e = VecPtrAt(&self->cache, i);
            if (e->path == path) {
                return e;
            return NULL;
        }
        return VecPtrAt(&self->cache, VecLen(&self->cache) - 1);
    }
            return;
        for (u64 i = 0; i < VecLen(&self->cache); ++i) {
            ResolverCacheEntry *e = VecPtrAt(&self->cache, i);
    #if FEATURE_PARSER_DWARF
            if (e->dwarf_built && e->dwarf_ok) {
        const ElfSymbol *best = NULL;
        for (u64 i = 0; i < VecLen(syms); ++i) {
            const ElfSymbol *s = VecPtrAt(syms, i);
            // Consider only real address symbols. Like llvm-symbolizer, keep
            // NOTYPE/FUNC/OBJECT (NOTYPE covers hand-written asm) and drop
    static PdbCacheEntry *cache_find_or_open(PdbCache *self, Zstr module_path) {
        for (size i = 0; i < VecLen(&self->entries); ++i) {
            PdbCacheEntry *e = VecPtrAt(&self->entries, i);
            if (StrBegin(&e->module_path) && ZstrCompare(StrBegin(&e->module_path), module_path) == 0) {
                return e;
            return NULL;
        }
        return VecPtrAt(&self->entries, VecLen(&self->entries) - 1);
    }
            return;
        for (size i = 0; i < VecLen(&self->entries); ++i) {
            PdbCacheEntry *e = VecPtrAt(&self->entries, i);
            if (e->pdb_open)
                PdbDeinit(&e->pdb);
    static MachoCacheEntry *cache_find_or_create(MachoCache *self, Zstr module_path) {
        for (size i = 0; i < VecLen(&self->entries); ++i) {
            MachoCacheEntry *e = VecPtrAt(&self->entries, i);
            if (StrBegin(&e->module_path) && ZstrCompare(StrBegin(&e->module_path), module_path) == 0) {
                return e;
            return NULL;
        }
        return VecPtrAt(&self->entries, VecLen(&self->entries) - 1);
    }
            return;
        for (size i = 0; i < VecLen(&self->entries); ++i) {
            MachoCacheEntry *e = VecPtrAt(&self->entries, i);
            if (e->fns_built && e->fns_ok)
                DwarfFunctionsDeinit(&e->fns);
            return NULL;
        for (size i = 0; i < VecLen(&self->sections); ++i) {
            const PeSection *s = VecPtrAt(&self->sections, i);
            if (ZstrCompare(s->name, name) == 0) {
                return s;
            return false;
        for (size i = 0; i < VecLen(&self->sections); ++i) {
            const PeSection *s = VecPtrAt(&self->sections, i);
            // Compute the section end in u64; u32 + u32 can wrap.
            u64 vstart = (u64)s->virtual_address;
    static void abbrev_table_deinit(AbbrevTable *t) {
        for (size i = 0; i < VecLen(t); ++i) {
            VecDeinit(&VecPtrAt(t, i)->attrs);
        }
        VecDeinit(t);
    static const AbbrevEntry *abbrev_table_find(const AbbrevTable *t, u64 code) {
        for (size i = 0; i < VecLen(t); ++i) {
            if (VecPtrAt(t, i)->code == code)
                return VecPtrAt(t, i);
        }
        for (size i = 0; i < VecLen(t); ++i) {
            if (VecPtrAt(t, i)->code == code)
                return VecPtrAt(t, i);
        }
        return NULL;
    
            for (size ai = 0; ai < VecLen(&e->attrs); ++ai) {
                const AbbrevAttr *a = VecPtrAt(&e->attrs, ai);
                AttrVal           v;
                if (!read_form(&cu_cur, a->form, addr_size, &v))
            // has stopped growing.
            for (size i = 0; i < VecLen(&pending); ++i) {
                const PendingFn *pf = VecPtrAt(&pending, i);
                DwarfFunction    f  = {
                        .low_pc  = pf->low_pc,
        while (lo < hi) {
            size mid = lo + (hi - lo) / 2;
            if (VecPtrAt(&self->entries, mid)->low_pc <= vaddr)
                lo = mid + 1;
            else
        if (lo == 0)
            return NULL;
        const DwarfFunction *e = VecPtrAt(&self->entries, lo - 1);
        if (vaddr >= e->low_pc && vaddr < e->high_pc)
            return e;
        // sizes by next-rva diff.
        for (size i = 0; i < VecLen(&pending); ++i) {
            const PendingPub *pp = VecPtrAt(&pending, i);
            PdbFunction       f  = {
                       .rva  = pp->rva,
                // order. If next.rva < f.rva (impossible today), leave
                // size = 0 rather than wrap.
                u32 next_rva = VecPtrAt(&pending, i + 1)->rva;
                f.size       = next_rva >= f.rva ? next_rva - f.rva : 0;
            }
        while (lo < hi) {
            size mid = lo + (hi - lo) / 2;
            if (((const PdbFunction *)VecPtrAt(&self->functions, mid))->rva <= rva)
                lo = mid + 1;
            else
        if (lo == 0)
            return NULL;
        const PdbFunction *f = VecPtrAt(&self->functions, lo - 1);
        // size == 0 means "until next entry"; we already accept that case.
        // Widen to u64 to avoid u32 wrap: rva and size are both u32, so a
        // Cache the lowest mapped address so callers don't rescan the vector.
        for (u64 i = 0; i < VecLen(&out->entries); ++i) {
            const ProcMapEntry *e = VecPtrAt(&out->entries, i);
            if (i == 0 || e->start < out->min_addr)
                out->min_addr = e->start;
            return NULL;
        for (u64 i = 0; i < VecLen(&self->entries); ++i) {
            const ProcMapEntry *e = VecPtrAt(&self->entries, i);
            if (addr >= e->start && addr < e->end) {
                return e;
            return NULL;
        for (u64 i = 0; i < VecLen(&self->cies); ++i) {
            if (VecPtrAt(&self->cies, i)->offset == cie_offset) {
                return VecPtrAt(&self->cies, i);
            }
        for (u64 i = 0; i < VecLen(&self->cies); ++i) {
            if (VecPtrAt(&self->cies, i)->offset == cie_offset) {
                return VecPtrAt(&self->cies, i);
            }
        }
        // Linear scan -- fine up to a few thousand FDEs.
        for (u64 i = 0; i < VecLen(&self->fdes); ++i) {
            const DwarfFde *f = VecPtrAt(&self->fdes, i);
            if (vaddr >= f->pc_begin && vaddr < f->pc_begin + f->pc_range) {
                return f;
            return NULL;
        for (size i = 0; i < VecLen(&self->sections); ++i) {
            const MachoSection *s = VecPtrAt(&self->sections, i);
            if (ZstrCompare(s->segment, segment) == 0 && ZstrCompare(s->section, section) == 0) {
                return s;
    
        for (size i = 0; i < VecLen(&self->symbols); ++i) {
            const MachoSymbol *s = VecPtrAt(&self->symbols, i);
            if (s->type & N_STAB_MASK)
                continue; // any high bit set => STAB (debug) entry
            return false;
        }
        const ElfSection *strtab = VecPtrAt(&self->sections, strtab_idx);
        if (!elf_range_ok(self, strtab->offset, strtab->size)) {
            LOG_ERROR("Elf: strtab out of range");
    
        for (u64 i = 0; i < VecLen(&self->sections); ++i) {
            const ElfSection *s = VecPtrAt(&self->sections, i);
            if (s->type == ELF_SECTION_TYPE_SYMTAB) {
                symtab = s;
        const ElfSymbol *best = NULL;
        for (u64 i = 0; i < VecLen(syms); ++i) {
            const ElfSymbol *s = VecPtrAt(syms, i);
            if (s->size == 0) {
                // Some symbols (e.g. labels) have zero size — only match
            return NULL;
        for (u64 i = 0; i < VecLen(&self->sections); ++i) {
            const ElfSection *s = VecPtrAt(&self->sections, i);
            if (s->name && ZstrCompare(s->name, name) == 0) {
                return s;
                u64 fo                           = VecAt(&pending_file_offsets, i);
                u64 dofs                         = VecAt(&pending_dir_offsets, i);
                VecPtrAt(&out->entries, i)->file = fo ? (Zstr)(StrBegin(&out->string_pool) + fo) : NULL;
                VecPtrAt(&out->entries, i)->dir  = dofs ? (Zstr)(StrBegin(&out->string_pool) + dofs) : NULL;
            }
                u64 dofs                         = VecAt(&pending_dir_offsets, i);
                VecPtrAt(&out->entries, i)->file = fo ? (Zstr)(StrBegin(&out->string_pool) + fo) : NULL;
                VecPtrAt(&out->entries, i)->dir  = dofs ? (Zstr)(StrBegin(&out->string_pool) + dofs) : NULL;
            }
        }
        const DwarfLineEntry *seq_open = NULL;
        for (u64 i = 0; i < VecLen(&self->entries); ++i) {
            const DwarfLineEntry *e = VecPtrAt(&self->entries, i);
            if (e->end_sequence) {
                // Sequence ends at this row's address (exclusive upper).
        // `DebugFreedEntry` structs with no public accessor; reach in to
        // confirm each entry carries the ptr + both traces.
        ok = ok && (VecPtrAt(&dbg.freed, 0)->ptr == p1) && (VecPtrAt(&dbg.freed, 0)->requested_size == 16);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->alloc_trace_n > 0) && (VecPtrAt(&dbg.freed, 0)->free_trace_n > 0);
        ok = ok && (VecPtrAt(&dbg.freed, 2)->ptr == p3) && (VecPtrAt(&dbg.freed, 2)->requested_size == 64);
        // confirm each entry carries the ptr + both traces.
        ok = ok && (VecPtrAt(&dbg.freed, 0)->ptr == p1) && (VecPtrAt(&dbg.freed, 0)->requested_size == 16);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->alloc_trace_n > 0) && (VecPtrAt(&dbg.freed, 0)->free_trace_n > 0);
        ok = ok && (VecPtrAt(&dbg.freed, 2)->ptr == p3) && (VecPtrAt(&dbg.freed, 2)->requested_size == 64);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->ptr == p1) && (VecPtrAt(&dbg.freed, 0)->requested_size == 16);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->alloc_trace_n > 0) && (VecPtrAt(&dbg.freed, 0)->free_trace_n > 0);
        ok = ok && (VecPtrAt(&dbg.freed, 2)->ptr == p3) && (VecPtrAt(&dbg.freed, 2)->requested_size == 64);
    
        DebugAllocatorDeinit(&dbg);
        AllocatorFree(adbg, p);
        ok = ok && (DebugAllocatorFreedCount(&dbg) == 1);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->alloc_trace_n == live_n);
    
        DebugAllocatorDeinit(&dbg);
        ok = ok && (DebugAllocatorFreedCount(&dbg) == 1);
    
        const DebugFreedEntry *fe    = VecPtrAt(&dbg.freed, 0);
        bool                   match = ok;
        for (u32 i = 0; ok && i < n; i++) {
    
        ok = ok && (DebugAllocatorFreedCount(&dbg) == 1);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->free_trace_n == 0);
    
        DebugAllocatorDeinit(&dbg);
    
        ok = ok && (DebugAllocatorFreedCount(&dbg) == 1);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->free_trace_n == 2);
    
        DebugAllocatorDeinit(&dbg);
    
        ok = ok && (DebugAllocatorFreedCount(&dbg) == 1);
        ok = ok && (VecPtrAt(&dbg.freed, 0)->free_trace_n <= DEBUG_ALLOCATOR_MAX_TRACE);
    
        DebugAllocatorDeinit(&dbg);
        // overshoots from this deep stack.
        if (ok) {
            u32 n = VecPtrAt(&dbg.freed, 0)->free_trace_n;
            ok    = ok && (n > 0) && (n <= DEBUG_ALLOCATOR_MAX_TRACE);
        }
        // logical lengths to 0 (stale b remains in a.out's buffer at index 0) so
        // the graph looks edge-free while the pending removal still names a->b.
        GenericGraphSlot *slot_a     = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(a));
        GenericGraphSlot *slot_b     = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(b));
        slot_a->out_neighbors.length = 0;
        // the graph looks edge-free while the pending removal still names a->b.
        GenericGraphSlot *slot_a     = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(a));
        GenericGraphSlot *slot_b     = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(b));
        slot_a->out_neighbors.length = 0;
        slot_b->in_neighbors.length  = 0;
            // `graph.slots` is the typed `Vec(GraphSlot(int))`, so iterate via
            // the runtime-shared layout to avoid an anonymous-struct annotation.
            GenericGraphSlot *slot = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, slot_index);
            result                 = result && (slot->data == NULL);
            result                 = result && (slot->visit_count == 0);
        }
    
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(first_id))->generation == (first_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(second_id))->generation == (second_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(third_id))->generation == (third_generation + 1));
    
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(first_id))->generation == (first_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(second_id))->generation == (second_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(third_id))->generation == (third_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(first_id))->generation == (first_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(second_id))->generation == (second_generation + 1));
        result = result && (VecPtrAt(&graph.slots, GraphNodeIdIndex(third_id))->generation == (third_generation + 1));
    
        GraphDeinit(&graph);
    static GenericGraphSlot *mutant_slot(void *graph_handle, GraphNodeId id) {
        GenericGraph *g = GENERIC_GRAPH(graph_handle);
        return (GenericGraphSlot *)VecPtrAt(&g->slots, GraphNodeIdIndex(id));
    }
        GenericGraphSlot *b_slot = mutant_slot(&graph, b);
        // b.in_neighbors is [a, c]; overwrite the 'a' entry with 'c' -> [c, c].
        *VecPtrAt(&b_slot->in_neighbors, 0) = c;
    
        MAGIC_MARK_DIRTY(GENERIC_GRAPH(&graph));
        GenericGraphSlot *c_slot = mutant_slot(&graph, c);
        // c.in_neighbors is [a, b]; overwrite the 'a' entry with 'b' -> [b, b].
        *VecPtrAt(&c_slot->in_neighbors, 0) = b;
    
        MAGIC_MARK_DIRTY(GENERIC_GRAPH(&graph));
        GenericGraphSlot *b_slot = mutant_slot(&graph, b);
        // b.out_neighbors is [a, c]; overwrite the 'a' entry with 'c' -> [c, c].
        *VecPtrAt(&b_slot->out_neighbors, 0) = c;
    
        MAGIC_MARK_DIRTY(GENERIC_GRAPH(&graph));
        GenericGraphSlot *c_slot = mutant_slot(&graph, c);
        // c.out_neighbors is [a, b]; overwrite the 'a' entry with 'b' -> [b, b].
        *VecPtrAt(&c_slot->out_neighbors, 0) = b;
    
        MAGIC_MARK_DIRTY(GENERIC_GRAPH(&graph));
        GenericGraph *g = GENERIC_GRAPH(&graph);
        // free_indices now holds b's old slot index; redirect it to a (occupied).
        *VecPtrAt(&g->free_indices, 0) = GraphNodeIdIndex(a);
    
        MAGIC_MARK_DIRTY(g);
        GenericGraph *g = GENERIC_GRAPH(&graph);
        // Point the free index exactly one past the last slot.
        *VecPtrAt(&g->free_indices, 0) = (u32)VecLen(&g->slots);
    
        MAGIC_MARK_DIRTY(g);
        // free_indices now holds two freed indices; redirect the last to a
        // (occupied) so only a loop that reaches the last entry catches it.
        *VecPtrAt(&g->free_indices, VecLen(&g->free_indices) - 1) = GraphNodeIdIndex(a);
    
        MAGIC_MARK_DIRTY(g);
        GenericGraph *g = GENERIC_GRAPH(&graph);
        // pending_edge_removals = [{a, b}]; redirect 'to' to d (no a->d edge).
        VecPtrAt(&g->pending_edge_removals, 0)->to = d;
    
        MAGIC_MARK_DIRTY(g);
        GenericGraph *g = GENERIC_GRAPH(&graph);
        // pending_edge_removals = [{a, b}, {a, c}]; break the second one.
        VecPtrAt(&g->pending_edge_removals, 1)->to = d;
    
        MAGIC_MARK_DIRTY(g);
        // node but not an out-neighbor of `a`, so the recorded removal now refers
        // to a non-existent edge.
        VecPtrAt(&graph.pending_edge_removals, 0)->to = c;
    
        // Re-arm the memoized deep validation so the next ValidateGraph re-walks
        // neighbor id so its generation is 0 (an always-invalid generation),
        // without bumping the mutation epoch or re-flagging the validated bit.
        GenericGraphSlot *slot_a = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(a));
        GraphNodeId      *entry  = VecPtrAt(&slot_a->out_neighbors, 0);
        *entry                   = (GraphNodeId)GraphNodeIdIndex(b);
        // without bumping the mutation epoch or re-flagging the validated bit.
        GenericGraphSlot *slot_a = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(a));
        GraphNodeId      *entry  = VecPtrAt(&slot_a->out_neighbors, 0);
        *entry                   = (GraphNodeId)GraphNodeIdIndex(b);
        // Intentional bypass: rewrite `b`'s sole in-neighbor (a) so its generation
        // is 0, leaving the epoch and validated bit untouched.
        GenericGraphSlot *slot_b = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(b));
        GraphNodeId      *entry  = VecPtrAt(&slot_b->in_neighbors, 0);
        *entry                   = (GraphNodeId)GraphNodeIdIndex(a);
        // is 0, leaving the epoch and validated bit untouched.
        GenericGraphSlot *slot_b = (GenericGraphSlot *)VecPtrAt(&GENERIC_GRAPH(&graph)->slots, GraphNodeIdIndex(b));
        GraphNodeId      *entry  = VecPtrAt(&slot_b->in_neighbors, 0);
        *entry                   = (GraphNodeId)GraphNodeIdIndex(a);
        // Length is now 3; the raw slot at index 4 was part of the vacated tail
        // and must read back zero.
        bool result = (VecLen(&vec) == 3) && (*VecPtrAt(&vec, 4) == 0);
    
        VecDeinit(&vec);
        VecDeleteRangeFast(&vec, 0, 2);
    
        bool result = (VecLen(&vec) == 3) && (*VecPtrAt(&vec, 4) == 0);
    
        VecDeinit(&vec);
    
        // Dead slot 9 (previously a duplicate of 9) must be zeroed.
        bool result = (VecLen(&vec) == 8 && *VecPtrAt(&vec, 9) == 0);
    
        VecDeinit(&vec);
    
        // Dead slot 9 must be zeroed, not left as a stale duplicate.
        bool result = (VecLen(&vec) == 8 && *VecPtrAt(&vec, 9) == 0);
    
        VecDeinit(&vec);
    // Test VecPtrAt function
    bool test_vec_ptr_at(void) {
        WriteFmt("Testing VecPtrAt\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Get pointers to elements
        int *ptr0 = VecPtrAt(&vec, 0);
        int *ptr1 = VecPtrAt(&vec, 1);
        int *ptr2 = VecPtrAt(&vec, 2);
        // Get pointers to elements
        int *ptr0 = VecPtrAt(&vec, 0);
        int *ptr1 = VecPtrAt(&vec, 1);
        int *ptr2 = VecPtrAt(&vec, 2);
        int *ptr0 = VecPtrAt(&vec, 0);
        int *ptr1 = VecPtrAt(&vec, 1);
        int *ptr2 = VecPtrAt(&vec, 2);
    
        // Check values through pointers
        WriteFmt("Testing BitVecPrefixMatch(NULL, patterns, 1) - should fatal\n");
        BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        BitVecPush(VecPtrAt(&vp, 0), true);
        BitVecPrefixMatch(NULL, &vp);
        VecDeinit(&vp);
        WriteFmt("Testing BitVecSuffixMatch(NULL, patterns, 1) - should fatal\n");
        BitVecs vp = VecInitWithDeepCopy(NULL, BitVecDeinit, ALLOCATOR_OF(&alloc));
        BitVecPush(VecPtrAt(&vp, 0), true);
        BitVecSuffixMatch(NULL, &vp);
        VecDeinit(&vp);
        result            = result && VecLen(&runs) == 5;
        if (result) {
            result = result && VecPtrAt(&runs, 0)->length == 3 && VecPtrAt(&runs, 0)->value == true;
            result = result && VecPtrAt(&runs, 1)->length == 2 && VecPtrAt(&runs, 1)->value == false;
            result = result && VecPtrAt(&runs, 2)->length == 1 && VecPtrAt(&runs, 2)->value == true;
        if (result) {
            result = result && VecPtrAt(&runs, 0)->length == 3 && VecPtrAt(&runs, 0)->value == true;
            result = result && VecPtrAt(&runs, 1)->length == 2 && VecPtrAt(&runs, 1)->value == false;
            result = result && VecPtrAt(&runs, 2)->length == 1 && VecPtrAt(&runs, 2)->value == true;
            result = result && VecPtrAt(&runs, 3)->length == 1 && VecPtrAt(&runs, 3)->value == false;
            result = result && VecPtrAt(&runs, 0)->length == 3 && VecPtrAt(&runs, 0)->value == true;
            result = result && VecPtrAt(&runs, 1)->length == 2 && VecPtrAt(&runs, 1)->value == false;
            result = result && VecPtrAt(&runs, 2)->length == 1 && VecPtrAt(&runs, 2)->value == true;
            result = result && VecPtrAt(&runs, 3)->length == 1 && VecPtrAt(&runs, 3)->value == false;
            result = result && VecPtrAt(&runs, 4)->length == 1 && VecPtrAt(&runs, 4)->value == true;
            result = result && VecPtrAt(&runs, 1)->length == 2 && VecPtrAt(&runs, 1)->value == false;
            result = result && VecPtrAt(&runs, 2)->length == 1 && VecPtrAt(&runs, 2)->value == true;
            result = result && VecPtrAt(&runs, 3)->length == 1 && VecPtrAt(&runs, 3)->value == false;
            result = result && VecPtrAt(&runs, 4)->length == 1 && VecPtrAt(&runs, 4)->value == true;
        }
            result = result && VecPtrAt(&runs, 2)->length == 1 && VecPtrAt(&runs, 2)->value == true;
            result = result && VecPtrAt(&runs, 3)->length == 1 && VecPtrAt(&runs, 3)->value == false;
            result = result && VecPtrAt(&runs, 4)->length == 1 && VecPtrAt(&runs, 4)->value == true;
        }
        BitVecPush(&source, true);
    
        BitVec *p0 = VecPtrAt(&patterns, 0);
        BitVec *p1 = VecPtrAt(&patterns, 1);
        BitVec *p2 = VecPtrAt(&patterns, 2);
    
        BitVec *p0 = VecPtrAt(&patterns, 0);
        BitVec *p1 = VecPtrAt(&patterns, 1);
        BitVec *p2 = VecPtrAt(&patterns, 2);
        BitVec *p0 = VecPtrAt(&patterns, 0);
        BitVec *p1 = VecPtrAt(&patterns, 1);
        BitVec *p2 = VecPtrAt(&patterns, 2);
    
        *p0 = BitVecInit(ALLOCATOR_OF(&alloc));
        BitVecPush(&source, true);
    
        BitVec *p0 = VecPtrAt(&patterns, 0);
        BitVec *p1 = VecPtrAt(&patterns, 1);
        BitVec *p2 = VecPtrAt(&patterns, 2);
    
        BitVec *p0 = VecPtrAt(&patterns, 0);
        BitVec *p1 = VecPtrAt(&patterns, 1);
        BitVec *p2 = VecPtrAt(&patterns, 2);
        BitVec *p0 = VecPtrAt(&patterns, 0);
        BitVec *p1 = VecPtrAt(&patterns, 1);
        BitVec *p2 = VecPtrAt(&patterns, 2);
    
        *p0 = BitVecInit(ALLOCATOR_OF(&alloc));
        bool result = (VecLen(&split) == 3);
        if (VecLen(&split) >= 3) {
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 0)), "Hello") == 0);
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 1)), "World") == 0);
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 2)), "Test") == 0);
        if (VecLen(&split) >= 3) {
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 0)), "Hello") == 0);
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 1)), "World") == 0);
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 2)), "Test") == 0);
        }
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 0)), "Hello") == 0);
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 1)), "World") == 0);
            result = result && (ZstrCompare(StrBegin(VecPtrAt(&split, 2)), "Test") == 0);
        }
            // (the base-pointer of an Iter range is the Iter contract --
            // direct read is the documented usage for view types).
            StrIter *iter1       = VecPtrAt(&iters, 0);
            char     buffer1[10] = {0};
            MemCopy(buffer1, iter1->data, StrIterLength(iter1));
            result = result && (ZstrCompare(buffer1, "Hello") == 0);
    
            StrIter *iter2       = VecPtrAt(&iters, 1);
            char     buffer2[10] = {0};
            MemCopy(buffer2, iter2->data, StrIterLength(iter2));
            result = result && (ZstrCompare(buffer2, "World") == 0);
    
            StrIter *iter3       = VecPtrAt(&iters, 2);
            char     buffer3[10] = {0};
            MemCopy(buffer3, iter3->data, StrIterLength(iter3));
        bool result = (VecLen(&iters) == 3);
        if (VecLen(&iters) == 3) {
            result = result && (StrIterLength(VecPtrAt(&iters, 0)) == 1);
            result = result && (StrIterLength(VecPtrAt(&iters, 1)) == 1);
            result = result && (StrIterLength(VecPtrAt(&iters, 2)) == 0);
        if (VecLen(&iters) == 3) {
            result = result && (StrIterLength(VecPtrAt(&iters, 0)) == 1);
            result = result && (StrIterLength(VecPtrAt(&iters, 1)) == 1);
            result = result && (StrIterLength(VecPtrAt(&iters, 2)) == 0);
        }
            result = result && (StrIterLength(VecPtrAt(&iters, 0)) == 1);
            result = result && (StrIterLength(VecPtrAt(&iters, 1)) == 1);
            result = result && (StrIterLength(VecPtrAt(&iters, 2)) == 0);
        }
        Str  s                                    = StrInitFromZstr("a,b,c", &alloc);
        Strs strs                                 = StrSplit(&s, ",");
        GENERIC_VEC(VecPtrAt(&strs, 0))->__magic ^= 0x1;
        ValidateStrs(&strs);
        ValidateStrs(&strs);
    
        GENERIC_VEC(VecPtrAt(&strs, 0))->__magic ^= 0x1;
        VecDeinit(&strs);
        StrDeinit(&s);
            return false;
        }
        Str  s  = SocketAddrFormat(VecPtrAt(&r->nameservers, i), a);
        bool ok = (StrLen(&s) > 0) && ZstrCompare(StrBegin(&s), expect) == 0;
        StrDeinit(&s);
        bool     got = DnsResolve(&r, "203.0.113.7:9999", SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && VecPtrAt(&out, 0)->family == SOCKET_FAMILY_INET;
        if (ok) {
            Str s = SocketAddrFormat(VecPtrAt(&out, 0), a);
        bool ok = got && VecLen(&out) == 1 && VecPtrAt(&out, 0)->family == SOCKET_FAMILY_INET;
        if (ok) {
            Str s = SocketAddrFormat(VecPtrAt(&out, 0), a);
            ok    = (StrLen(&s) > 0) && ZstrCompare(StrBegin(&s), "203.0.113.7:9999") == 0;
            StrDeinit(&s);
        bool     got = DnsResolve(&r, "[::1]:443", SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && VecPtrAt(&out, 0)->family == SOCKET_FAMILY_INET6;
        if (ok) {
            Str s = SocketAddrFormat(VecPtrAt(&out, 0), a);
        bool ok = got && VecLen(&out) == 1 && VecPtrAt(&out, 0)->family == SOCKET_FAMILY_INET6;
        if (ok) {
            Str s = SocketAddrFormat(VecPtrAt(&out, 0), a);
            ok    = (StrLen(&s) > 0) && ZstrCompare(StrBegin(&s), "[::1]:443") == 0;
            StrDeinit(&s);
        DnsResolverAddResolvPath(&r, &path);
    
        bool ok = VecLen(&r.nameservers) == 1 && VecPtrAt(&r.nameservers, 0)->family == SOCKET_FAMILY_INET6 &&
                  ns_fmt_is(&r, 0, a, "[::1]:53");
        bool     got = dns_resolve_5_zstr(&r, "A@Z", 80, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "10.0.0.1:80");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, "HOST...", 80, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "10.0.0.2:80");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, "A.B.C.", 80, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "10.0.0.3:80");
    
        VecDeinit(&out);
        bool ok = got && VecLen(&out) == 1;
        if (ok) {
            const SocketAddr *ad = VecPtrAt(&out, 0);
            ok                   = ad->family == SOCKET_FAMILY_INET && ad->length == 16u;
            // Network byte order: port bytes are 0x12 then 0x34 at raw[2..4].
        bool ok = got && VecLen(&out) == 1;
        if (ok) {
            const SocketAddr *ad = VecPtrAt(&out, 0);
            ok                   = ad->family == SOCKET_FAMILY_INET6 && ad->length == 28u;
            // sin6_port at raw[2..4], network order.
        bool     got = dns_resolve_5_zstr(&r, "single", 80, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "1.2.3.4:80");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, "multi", 1234, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:1234") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:1234");
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:1234") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:1234");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, "both", 443, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "1.1.1.1:443") &&
                  v6_is(VecPtrAt(&out, 1), a, "[2606::1]:443");
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "1.1.1.1:443") &&
                  v6_is(VecPtrAt(&out, 1), a, "[2606::1]:443");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, "v6name", 53, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v6_is(VecPtrAt(&out, 0), a, "[::1]:53");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, "SINGLE.", 80, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "1.2.3.4:80");
    
        VecDeinit(&out);
        bool     got = dns_resolve_5_zstr(&r, name, 80, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "1.2.3.4:80");
    
        VecDeinit(&out);
        bool     got  = dns_resolve_5_str(&r, &name, 7000, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:7000") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:7000");
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:7000") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:7000");
    
        StrDeinit(&name);
        bool     got = dns_resolve_4_vec_zstr(&r, "single:80", SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "1.2.3.4:80");
    
        VecDeinit(&out);
        bool     got = dns_resolve_4_vec_zstr(&r, "multi:9090", SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:9090") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:9090");
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:9090") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:9090");
    
        VecDeinit(&out);
        bool     got = dns_resolve_4_vec_zstr(&r, "203.0.113.7:9999", SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "203.0.113.7:9999");
    
        VecDeinit(&out);
        bool     got = dns_resolve_4_vec_zstr(&r, "single:65535", SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 1 && v4_is(VecPtrAt(&out, 0), a, "1.2.3.4:65535");
    
        VecDeinit(&out);
        bool     got  = dns_resolve_4_vec_str(&r, &spec, SOCKET_KIND_TCP, &out);
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:8080") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:8080");
    
        bool ok = got && VecLen(&out) == 2 && v4_is(VecPtrAt(&out, 0), a, "5.6.7.8:8080") &&
                  v4_is(VecPtrAt(&out, 1), a, "9.10.11.12:8080");
    
        StrDeinit(&spec);
        if (ProcMapsLoad(&maps, ALLOCATOR_OF(&a))) {
            for (u64 i = 0; i < VecLen(&maps.entries); ++i) {
                const ProcMapEntry *m = VecPtrAt(&maps.entries, i);
                if (m->path && m->path[0] == '/' && (m->perms & PROC_MAP_PERM_EXEC) &&
                    ZstrCompare(m->path, self_path) != 0) {
        if (ProcMapsLoad(&maps, ALLOCATOR_OF(&a))) {
            for (u64 i = 0; i < VecLen(&maps.entries); ++i) {
                const ProcMapEntry *m = VecPtrAt(&maps.entries, i);
                if (m->path && ZstrCompare(m->path, path) == 0 && (m->perms & PROC_MAP_PERM_WRITE) && addr >= m->start &&
                    addr < m->end) {
        ok = pe.machine == PE_MACHINE_X86_64 && pe.is_pe32_plus && pe.image_base == 0x140000000ull;
        ok = ok && VecLen(&pe.sections) == 1;
        ok = ok && ZstrCompare(VecPtrAt(&pe.sections, 0)->name, ".debug") == 0;
        ok = ok && VecPtrAt(&pe.sections, 0)->virtual_address == SECTION_VA;
        ok = ok && pe.codeview.present;
        ok = ok && VecLen(&pe.sections) == 1;
        ok = ok && ZstrCompare(VecPtrAt(&pe.sections, 0)->name, ".debug") == 0;
        ok = ok && VecPtrAt(&pe.sections, 0)->virtual_address == SECTION_VA;
        ok = ok && pe.codeview.present;
        ok = ok && pe.codeview.age == 0x2a;
    
        bool ok = VecLen(&pe.sections) == 2;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 0)->name, ".text") == 0;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 1)->name, ".rdata") == 0;
        ok      = ok && VecPtrAt(&pe.sections, 0)->virtual_address == 0x1000;
        bool ok = VecLen(&pe.sections) == 2;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 0)->name, ".text") == 0;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 1)->name, ".rdata") == 0;
        ok      = ok && VecPtrAt(&pe.sections, 0)->virtual_address == 0x1000;
        ok      = ok && VecPtrAt(&pe.sections, 1)->virtual_address == 0x2000;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 0)->name, ".text") == 0;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 1)->name, ".rdata") == 0;
        ok      = ok && VecPtrAt(&pe.sections, 0)->virtual_address == 0x1000;
        ok      = ok && VecPtrAt(&pe.sections, 1)->virtual_address == 0x2000;
        ok      = ok && ZstrCompare(VecPtrAt(&pe.sections, 1)->name, ".rdata") == 0;
        ok      = ok && VecPtrAt(&pe.sections, 0)->virtual_address == 0x1000;
        ok      = ok && VecPtrAt(&pe.sections, 1)->virtual_address == 0x2000;
    
        PeDeinit(&pe);
            return false;
        }
        const PeSection *s  = VecPtrAt(&pe.sections, 0);
        bool             ok = VecLen(&pe.sections) == 1;
        ok                  = ok && ZstrLen(s->name) == 8;
        if (ok) {
            ok = pe.machine == PE_MACHINE_X86_64 && VecLen(&pe.sections) == 1 &&
                 ZstrCompare(VecPtrAt(&pe.sections, 0)->name, ".text") == 0;
            PeDeinit(&pe);
        }
    
        if (match) {
            DnsRecord *r0 = VecPtrAt(&resp.answers, 0);
            match         = r0->type == DNS_TYPE_A && r0->ttl == 300 && r0->ipv4[0] == 93 && r0->ipv4[1] == 184 &&
                    r0->ipv4[2] == 216 && r0->ipv4[3] == 34 && ZstrCompare(StrBegin(&r0->name), "example.com") == 0;
        }
        if (match) {
            DnsRecord *r1 = VecPtrAt(&resp.answers, 1);
            match = r1->type == DNS_TYPE_AAAA && r1->ipv6[0] == 0x26 && r1->ipv6[1] == 0x06 && r1->ipv6[14] == 0x19 &&
                    r1->ipv6[15] == 0x46;
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_CNAME && ZstrCompare(StrBegin(&r->target), "example.com") == 0;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->ttl == 300 && r->ipv4[0] == 0x0a && r->ipv4[1] == 0x0b &&
                    r->ipv4[2] == 0x0c && r->ipv4[3] == 0x0d && ZstrCompare(StrBegin(&r->name), "www.example.com") == 0;
        bool        match = ok && q2_off == 256 && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && ZstrCompare(StrBegin(&r->name), "zz") == 0;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match =
                r->type == DNS_TYPE_A && r->ttl == 2 && ZstrCompare(StrBegin(&r->name), "abc.de") == 0 && r->ipv4[0] == 9;
        bool        match = ok && VecLen(&resp.answers) == 2;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->rclass == 1 && r->ttl == 0x01020304u && r->ipv4[0] == 8 &&
                    r->ipv4[1] == 8 && r->ipv4[2] == 4 && r->ipv4[3] == 4 && r->ipv6[0] == 0 &&
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->rclass == 0x00FE;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->ttl == 0x80112233u;
        }
        bool        match = ok && VecLen(&resp.answers) == 2;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_AAAA && r->ttl == 10 && VecLen(&r->target) == 0;
            static const u8 ip6[] =
        bool        match = ok && VecLen(&resp.answers) == 2;
        if (match) {
            DnsRecord *r0 = VecPtrAt(&resp.answers, 0);
            DnsRecord *r1 = VecPtrAt(&resp.answers, 1);
            match = ZstrCompare(StrBegin(&r0->name), "x") == 0 && r0->ttl == 1 && r0->ipv4[0] == 1 && r0->ipv4[3] == 4 &&
        if (match) {
            DnsRecord *r0 = VecPtrAt(&resp.answers, 0);
            DnsRecord *r1 = VecPtrAt(&resp.answers, 1);
            match = ZstrCompare(StrBegin(&r0->name), "x") == 0 && r0->ttl == 1 && r0->ipv4[0] == 1 && r0->ipv4[3] == 4 &&
                    ZstrCompare(StrBegin(&r1->name), "y") == 0 && r1->ttl == 2 && r1->ipv4[0] == 5 && r1->ipv4[3] == 8;
        bool match = ok && VecLen(&resp.answers) == 1 && VecLen(&resp.authority) == 1 && VecLen(&resp.additional) == 1;
        if (match) {
            DnsRecord *an = VecPtrAt(&resp.answers, 0);
            DnsRecord *ns = VecPtrAt(&resp.authority, 0);
            DnsRecord *ar = VecPtrAt(&resp.additional, 0);
        if (match) {
            DnsRecord *an = VecPtrAt(&resp.answers, 0);
            DnsRecord *ns = VecPtrAt(&resp.authority, 0);
            DnsRecord *ar = VecPtrAt(&resp.additional, 0);
            match         = ZstrCompare(StrBegin(&an->name), "an") == 0 && an->ipv4[0] == 11 &&
            DnsRecord *an = VecPtrAt(&resp.answers, 0);
            DnsRecord *ns = VecPtrAt(&resp.authority, 0);
            DnsRecord *ar = VecPtrAt(&resp.additional, 0);
            match         = ZstrCompare(StrBegin(&an->name), "an") == 0 && an->ipv4[0] == 11 &&
                    ZstrCompare(StrBegin(&ns->name), "ns") == 0 && ns->ipv4[0] == 22 &&
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = ZstrCompare(StrBegin(&r->name), "q") == 0 && r->ipv4[0] == 9 && r->ttl == 5;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_CNAME && ZstrCompare(StrBegin(&r->target), "to") == 0;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->ipv4[0] == 1 && r->ipv4[3] == 4;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->ttl == 7 && ZstrCompare(StrBegin(&r->name), "zz") == 0;
        }
        bool        ok   = DnsParseResponse(&resp, wire, sizeof(wire), adbg);
        ok               = ok && (VecLen(&resp.answers) == 1);
        DnsRecord *r     = ok ? VecPtrAt(&resp.answers, 0) : NULL;
        ok               = ok && (r->type == DNS_TYPE_CNAME);
        // Heap-backed target ("example.com") -- the Str the dropped StrDeinit leaks.
        ok = VecLen(&pdb.functions) == 1;
        if (ok) {
            const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
            ok                   = ok && f->rva == 0x1100 && f->name && ZstrCompare(f->name, "my_function") == 0;
        }
        // Functions are stored sorted by RVA regardless of input order.
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "fn_alpha") == 0;
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "fn_alpha") == 0;
            ok                    = ok && f1->rva == 0x1200 && ZstrCompare(f1->name, "fn_beta") == 0;
        int n = (int)VecLen(&pdb.functions);
        if (n >= 1 && out_rva0)
            *out_rva0 = ((const PdbFunction *)VecPtrAt(&pdb.functions, 0))->rva;
        PdbDeinit(&pdb);
        return n;
        ok = VecLen(&pdb.functions) == 4;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
            const PdbFunction *f3 = VecPtrAt(&pdb.functions, 3);
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
            const PdbFunction *f3 = VecPtrAt(&pdb.functions, 3);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "fn_one") == 0;
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *f2 = VecPtrAt(&pdb.functions, 2);
            const PdbFunction *f3 = VecPtrAt(&pdb.functions, 3);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "fn_one") == 0;
            ok                    = ok && f1->rva == 0x1200 && ZstrCompare(f1->name, "fn_two") == 0;
        if (ok) {
            for (size i = 0; i < VecLen(&pdb.functions); ++i) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, i);
                if (ZstrCompare(f->name, "data_sym") == 0) {
                    ok = false;
        if (ok) {
            // sorted: 0x1040 (text) then 0x8010 (rdata)
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x1040 && ZstrCompare(f0->name, "in_text") == 0;
            // sorted: 0x1040 (text) then 0x8010 (rdata)
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x1040 && ZstrCompare(f0->name, "in_text") == 0;
            ok                    = ok && f1->rva == 0x8010 && ZstrCompare(f1->name, "in_rdata") == 0;
        ok = VecLen(&pdb.functions) == 2;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "valid_fn") == 0;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "valid_fn") == 0;
            ok                    = ok && f1->rva == 0x1200 && ZstrCompare(f1->name, "valid_fn2") == 0;
        if (ok) {
            for (size i = 0; i < VecLen(&pdb.functions); ++i) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, i);
                if (ZstrCompare(f->name, "bad_seg_fn") == 0) {
                    ok = false;
        ok = VecLen(&pdb.functions) == 3;
        if (ok) {
            const PdbFunction *a = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *b = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *c = VecPtrAt(&pdb.functions, 2);
        if (ok) {
            const PdbFunction *a = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *b = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *c = VecPtrAt(&pdb.functions, 2);
            ok                   = ok && a->rva == 0x1100 && a->size == 0x200; // 0x1300 - 0x1100
            const PdbFunction *a = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *b = VecPtrAt(&pdb.functions, 1);
            const PdbFunction *c = VecPtrAt(&pdb.functions, 2);
            ok                   = ok && a->rva == 0x1100 && a->size == 0x200; // 0x1300 - 0x1100
            ok                   = ok && b->rva == 0x1300 && b->size == 0x80;  // 0x1380 - 0x1300
            u32 prev = 0;
            for (size i = 0; i < VecLen(&pdb.functions); ++i) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, i);
                if (i > 0 && !(f->rva > prev)) {
                    ok = false;
        // would flip these.
        if (ok) {
            const PdbFunction *first = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *last  = VecPtrAt(&pdb.functions, 4);
            ok                       = ok && first->rva == 0x1100 && ZstrCompare(first->name, "a") == 0;
        if (ok) {
            const PdbFunction *first = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *last  = VecPtrAt(&pdb.functions, 4);
            ok                       = ok && first->rva == 0x1100 && ZstrCompare(first->name, "a") == 0;
            ok                       = ok && last->rva == 0x1500 && ZstrCompare(last->name, "e") == 0;
        ok = VecLen(&pdb.functions) == 2;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "fn_keep") == 0;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x1100 && ZstrCompare(f0->name, "fn_keep") == 0;
            ok                    = ok && f1->rva == 0x1300 && ZstrCompare(f1->name, "fn_keep2") == 0;
        if (ok) {
            for (size i = 0; i < VecLen(&pdb.functions); ++i) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, i);
                if (ZstrCompare(f->name, "AAAAAAAA") == 0) {
                    ok = false;
        ok = VecLen(&pdb.functions) == 2;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x2050u && ZstrCompare(f0->name, "fn_low") == 0;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0);
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1);
            ok                    = ok && f0->rva == 0x2050u && ZstrCompare(f0->name, "fn_low") == 0;
            ok                    = ok && f1->rva == 0xFFFFFFFFu && ZstrCompare(f1->name, "fn_max") == 0;
        if (ok) {
            for (size i = 0; i < VecLen(&pdb.functions); ++i) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, i);
                if (ZstrCompare(f->name, "fn_ovf") == 0) {
                    ok = false;
        ok = VecLen(&pdb.functions) == 2;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0); // rva 0x2000
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1); // rva 0x3000
            ok                    = ok && f0->rva == 0x2000 && ZstrCompare(f0->name, "second_block_function") == 0;
        if (ok) {
            const PdbFunction *f0 = VecPtrAt(&pdb.functions, 0); // rva 0x2000
            const PdbFunction *f1 = VecPtrAt(&pdb.functions, 1); // rva 0x3000
            ok                    = ok && f0->rva == 0x2000 && ZstrCompare(f0->name, "second_block_function") == 0;
            ok                    = ok && f1->rva == 0x3000 && ZstrCompare(f1->name, "straddling_boundary_function") == 0;
            ok = VecLen(&pdb.functions) == 1;
            if (ok) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
                ok                   = f->rva == 0x6010 && ZstrCompare(f->name, "sixth") == 0;
            }
            ok = VecLen(&pdb.functions) == 1;
            if (ok) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
                ok                   = f->rva == 0x1040 && ZstrCompare(f->name, "good") == 0;
            }
            ok = VecLen(&pdb.functions) == 3;
            if (ok) {
                const PdbFunction *a = VecPtrAt(&pdb.functions, 0);            // alpha 0x1100
                const PdbFunction *b = VecPtrAt(&pdb.functions, 1);            // beta  0x1200
                const PdbFunction *c = VecPtrAt(&pdb.functions, 2);            // gamma 0x1300
            if (ok) {
                const PdbFunction *a = VecPtrAt(&pdb.functions, 0);            // alpha 0x1100
                const PdbFunction *b = VecPtrAt(&pdb.functions, 1);            // beta  0x1200
                const PdbFunction *c = VecPtrAt(&pdb.functions, 2);            // gamma 0x1300
                ok                   = ok && a->rva == 0x1100 && a->size == 0x100;
                const PdbFunction *a = VecPtrAt(&pdb.functions, 0);            // alpha 0x1100
                const PdbFunction *b = VecPtrAt(&pdb.functions, 1);            // beta  0x1200
                const PdbFunction *c = VecPtrAt(&pdb.functions, 2);            // gamma 0x1300
                ok                   = ok && a->rva == 0x1100 && a->size == 0x100;
                ok                   = ok && b->rva == 0x1200 && b->size == 0x100;
            ok = VecLen(&pdb.functions) == 1;
            if (ok) {
                const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
                ok                   = f->rva == 0x1010 && ZstrCompare(f->name, "named_fn") == 0;
            }
        bool ok = VecLen(&pdb.functions) == 1;
        if (ok) {
            const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
            ok                   = f->rva == 0x1100 && ZstrCompare(f->name, "deepblk") == 0;
        }
        bool ok     = opened && VecLen(&pdb.functions) == 1 && pdb.num_streams == N;
        if (ok) {
            const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
            ok                   = f->rva == 0x1100 && ZstrCompare(f->name, "deepfn") == 0;
        }
        bool ok     = opened && VecLen(&pdb.functions) == 1;
        if (ok) {
            const PdbFunction *f = VecPtrAt(&pdb.functions, 0);
            ok                   = f->rva == 0x1100 && ZstrCompare(f->name, "fn") == 0;
        }
        bool any_exec = false;
        for (u64 i = 0; i < VecLen(&maps.entries); ++i) {
            if (VecPtrAt(&maps.entries, i)->perms & PROC_MAP_PERM_EXEC) {
                any_exec = true;
                break;
            const ProcMapEntry *region = NULL;
            for (u64 i = 0; i < VecLen(&maps.entries); ++i) {
                const ProcMapEntry *cand = VecPtrAt(&maps.entries, i);
                if (cand->end > cand->start) {
                    region = cand;
        if (ok) {
            // The entry with the highest `end` -- `end` itself maps to nothing.
            const ProcMapEntry *top = VecPtrAt(&maps.entries, 0);
            for (u64 i = 1; i < VecLen(&maps.entries); ++i) {
                const ProcMapEntry *cand = VecPtrAt(&maps.entries, i);
            const ProcMapEntry *top = VecPtrAt(&maps.entries, 0);
            for (u64 i = 1; i < VecLen(&maps.entries); ++i) {
                const ProcMapEntry *cand = VecPtrAt(&maps.entries, i);
                if (cand->end > top->end)
                    top = cand;
        bool found_gap = false;
        for (u64 i = 0; i < VecLen(&maps.entries) && !found_gap; ++i) {
            u64 hole = VecPtrAt(&maps.entries, i)->end;
            // Is `hole` inside any region?
            bool inside = false;
            bool inside = false;
            for (u64 j = 0; j < VecLen(&maps.entries); ++j) {
                const ProcMapEntry *e = VecPtrAt(&maps.entries, j);
                if (hole >= e->start && hole < e->end) {
                    inside = true;
        bool ok = VecLen(&maps.entries) > 0;
        if (ok) {
            u64 lowest = VecPtrAt(&maps.entries, 0)->start;
            for (u64 i = 1; i < VecLen(&maps.entries); ++i) {
                u64 start = VecPtrAt(&maps.entries, i)->start;
            u64 lowest = VecPtrAt(&maps.entries, 0)->start;
            for (u64 i = 1; i < VecLen(&maps.entries); ++i) {
                u64 start = VecPtrAt(&maps.entries, i)->start;
                if (start < lowest)
                    lowest = start;
        bool ok = VecLen(&m.entries) == 1;
        if (ok) {
            const ProcMapEntry *e = VecPtrAt(&m.entries, 0);
            ok                    = e->start == 0x1000ULL && e->end == 0x2000ULL && e->file_offset == 0xdeadULL &&
                 e->perms == (u32)(PROC_MAP_PERM_READ | PROC_MAP_PERM_EXEC | PROC_MAP_PERM_PRIVATE) &&
        bool ok = VecLen(&m.entries) == 1;
        if (ok) {
            const ProcMapEntry *e = VecPtrAt(&m.entries, 0);
            ok                    = e->start == 0xABCDEFULL && e->end == 0xFEDCBAULL && e->file_offset == 0xCA00ULL;
        }
        bool ok = VecLen(&m.entries) == 1;
        if (ok) {
            const ProcMapEntry *e = VecPtrAt(&m.entries, 0);
            ok                    = e->start == 0x9ULL && e->end == 0x1990ULL;
        }
        bool ok = VecLen(&m.entries) == 2;
        if (ok) {
            const ProcMapEntry *e0 = VecPtrAt(&m.entries, 0);
            const ProcMapEntry *e1 = VecPtrAt(&m.entries, 1);
            ok                     = e0->start == 0x5000ULL && e1->start == 0x1000ULL && m.min_addr == 0x1000ULL;
        if (ok) {
            const ProcMapEntry *e0 = VecPtrAt(&m.entries, 0);
            const ProcMapEntry *e1 = VecPtrAt(&m.entries, 1);
            ok                     = e0->start == 0x5000ULL && e1->start == 0x1000ULL && m.min_addr == 0x1000ULL;
        }
        bool ok = VecLen(&m.entries) == 1;
        if (ok) {
            const ProcMapEntry *e = VecPtrAt(&m.entries, 0);
            ok                    = e->start == 0x3000ULL && e->end == 0x4000ULL && ZstrCompare(e->path, "/good") == 0;
        }
        ok = ok && m.has_uuid && MemCompare(m.uuid, kUuid, 16) == 0;
        ok = ok && VecLen(&m.segments) == 1;
        ok = ok && ZstrCompare(VecPtrAt(&m.segments, 0)->name, "__TEXT") == 0;
        ok = ok && VecLen(&m.sections) == 1;
        ok = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->section, "__text") == 0;
        ok = ok && ZstrCompare(VecPtrAt(&m.segments, 0)->name, "__TEXT") == 0;
        ok = ok && VecLen(&m.sections) == 1;
        ok = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->section, "__text") == 0;
        ok = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->segment, "__TEXT") == 0;
        ok = ok && VecLen(&m.symbols) == 1;
        ok = ok && VecLen(&m.sections) == 1;
        ok = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->section, "__text") == 0;
        ok = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->segment, "__TEXT") == 0;
        ok = ok && VecLen(&m.symbols) == 1;
        ok = ok && VecPtrAt(&m.symbols, 0)->name && ZstrCompare(VecPtrAt(&m.symbols, 0)->name, "my_function") == 0;
        ok = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->segment, "__TEXT") == 0;
        ok = ok && VecLen(&m.symbols) == 1;
        ok = ok && VecPtrAt(&m.symbols, 0)->name && ZstrCompare(VecPtrAt(&m.symbols, 0)->name, "my_function") == 0;
        ok = ok && VecPtrAt(&m.symbols, 0)->value == 0x100000010ull;
        ok = ok && VecLen(&m.symbols) == 1;
        ok = ok && VecPtrAt(&m.symbols, 0)->name && ZstrCompare(VecPtrAt(&m.symbols, 0)->name, "my_function") == 0;
        ok = ok && VecPtrAt(&m.symbols, 0)->value == 0x100000010ull;
    
        MachoDeinit(&m);
        ok       = ok && VecLen(&m.symbols) == 1;
        if (VecLen(&m.symbols) == 1)
            ok = ok && ZstrCompare(VecPtrAt(&m.symbols, 0)->name, "fn_a") == 0;
        if (ok)
            MachoDeinit(&m);
        ok       = ok && VecLen(&m.symbols) == 2;
        if (VecLen(&m.symbols) == 2) {
            const MachoSymbol *s0 = VecPtrAt(&m.symbols, 0);
            const MachoSymbol *s1 = VecPtrAt(&m.symbols, 1);
            ok = ok && ZstrCompare(s0->name, "alpha") == 0 && s0->value == 0x100000010ull && s0->type == 0x0F &&
        if (VecLen(&m.symbols) == 2) {
            const MachoSymbol *s0 = VecPtrAt(&m.symbols, 0);
            const MachoSymbol *s1 = VecPtrAt(&m.symbols, 1);
            ok = ok && ZstrCompare(s0->name, "alpha") == 0 && s0->value == 0x100000010ull && s0->type == 0x0F &&
                 s0->section_index == 1;
        Macho m;
        bool  ok = open_blob(&m, b, BUF, &alloc);
        ok       = ok && VecLen(&m.symbols) == 1 && VecPtrAt(&m.symbols, 0)->section_index == 5;
        if (ok)
            MachoDeinit(&m);
        Macho m;
        bool  ok = open_blob(&m, b, BUF, &alloc); // real: accepts
        ok       = ok && VecLen(&m.symbols) == 1 && ZstrCompare(VecPtrAt(&m.symbols, 0)->name, "fit") == 0;
        if (ok)
            MachoDeinit(&m);
        Macho m;
        bool  ok = open_blob(&m, b, BUF, &alloc);
        ok       = ok && VecLen(&m.symbols) == 1 && ZstrCompare(VecPtrAt(&m.symbols, 0)->name, "edge") == 0;
        if (ok)
            MachoDeinit(&m);
        bool  ok = MachoOpenFromMemoryCopy(&m, buf, sizeof(buf), base);
        ok       = ok && VecLen(&m.segments) == 1 && VecLen(&m.sections) == 0;
        ok       = ok && ZstrCompare(VecPtrAt(&m.segments, 0)->name, "__TEXT") == 0;
        ok       = ok && VecPtrAt(&m.segments, 0)->vmaddr == 0x1000;
        if (ok)
        ok       = ok && VecLen(&m.segments) == 1 && VecLen(&m.sections) == 0;
        ok       = ok && ZstrCompare(VecPtrAt(&m.segments, 0)->name, "__TEXT") == 0;
        ok       = ok && VecPtrAt(&m.segments, 0)->vmaddr == 0x1000;
        if (ok)
            MachoDeinit(&m);
        bool  ok = MachoOpenFromMemoryCopy(&m, buf, sizeof(buf), base);
        ok       = ok && VecLen(&m.segments) == 1;
        ok       = ok && ZstrCompare(VecPtrAt(&m.segments, 0)->name, "ABCDEFGHIJKLMNOP") == 0;
        if (ok)
            MachoDeinit(&m);
        bool  ok = MachoOpenFromMemoryCopy(&m, buf, len, base);
        ok       = ok && VecLen(&m.segments) == 1 && VecLen(&m.sections) == 2;
        ok       = ok && VecPtrAt(&m.sections, 0)->addr == 0x100000000ull;
        ok       = ok && VecPtrAt(&m.sections, 1)->addr == 0x100001000ull;
        if (ok)
        ok       = ok && VecLen(&m.segments) == 1 && VecLen(&m.sections) == 2;
        ok       = ok && VecPtrAt(&m.sections, 0)->addr == 0x100000000ull;
        ok       = ok && VecPtrAt(&m.sections, 1)->addr == 0x100001000ull;
        if (ok)
            MachoDeinit(&m);
        bool  ok = MachoOpenFromMemoryCopy(&m, buf, sizeof(buf), base);
        ok       = ok && VecLen(&m.sections) == 1;
        ok       = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->section, "abcdefghijklmnop") == 0;
        ok       = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->segment, "QRSTUVWXYZ012345") == 0;
        if (ok)
        ok       = ok && VecLen(&m.sections) == 1;
        ok       = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->section, "abcdefghijklmnop") == 0;
        ok       = ok && ZstrCompare(VecPtrAt(&m.sections, 0)->segment, "QRSTUVWXYZ012345") == 0;
        if (ok)
            MachoDeinit(&m);
        bool ok = false;
        for (u64 i = 0; i < VecLen(&elf.symbols); ++i) {
            const ElfSymbol *s = VecPtrAt(&elf.symbols, i);
            if (s->type == ELF_SYMBOL_TYPE_FUNC && s->size > 0 && s->name && s->name[0] != '\0') {
                ok = true;
        bool found_func = false;
        for (u64 i = 0; i < VecLen(&elf.symbols); ++i) {
            const ElfSymbol *s = VecPtrAt(&elf.symbols, i);
            if (s->name && ZstrCompare(s->name, "my_func") == 0) {
                found_func = s->value == FUNC_VADDR && s->size == FUNC_SIZE && s->type == ELF_SYMBOL_TYPE_FUNC &&
            ok = VecLen(&elf.segments) == 2;
            if (ok) {
                const ElfSegment *s0 = VecPtrAt(&elf.segments, 0);
                const ElfSegment *s1 = VecPtrAt(&elf.segments, 1);
                ok                   = s0->type == ELF_PT_LOAD && s0->flags == SEG0_FLAGS && s0->offset == SEG0_OFF &&
            if (ok) {
                const ElfSegment *s0 = VecPtrAt(&elf.segments, 0);
                const ElfSegment *s1 = VecPtrAt(&elf.segments, 1);
                ok                   = s0->type == ELF_PT_LOAD && s0->flags == SEG0_FLAGS && s0->offset == SEG0_OFF &&
                     s0->vaddr == SEG0_VADDR && s0->filesz == SEG0_FILESZ && s0->memsz == SEG0_MEMSZ &&
    
        // First section is the NULL section (name ""), last is .strtab.
        const ElfSection *first = VecPtrAt(&elf.sections, 0);
        const ElfSection *last  = VecPtrAt(&elf.sections, N_SECTIONS - 1);
        ok                      = ok && first->name && first->name[0] == '\0' && first->type == ELF_SECTION_TYPE_NULL;
        // First section is the NULL section (name ""), last is .strtab.
        const ElfSection *first = VecPtrAt(&elf.sections, 0);
        const ElfSection *last  = VecPtrAt(&elf.sections, N_SECTIONS - 1);
        ok                      = ok && first->name && first->name[0] == '\0' && first->type == ELF_SECTION_TYPE_NULL;
        ok = ok && last->name && ZstrCompare(last->name, ".strtab") == 0 && last->type == ELF_SECTION_TYPE_STRTAB &&
        // Section 1's name idx points at "AB" with no terminator inside the
        // declared strtab size => real decodes "".
        const ElfSection *s1 = VecPtrAt(&elf.sections, 1);
        bool              ok = VecLen(&elf.sections) == 2 && s1->name != NULL && s1->name[0] == '\0';
        }
    
        const ElfSection *s1 = VecPtrAt(&elf.sections, 1);
        bool              ok = VecLen(&elf.sections) == 2 && s1->name != NULL && s1->name[0] == '\0';
        }
    
        const ElfSection *s1 = VecPtrAt(&elf.sections, 1);
        bool              ok = VecLen(&elf.sections) == 2 && s1->name != NULL && ZstrCompare(s1->name, "XYZ") == 0;
        // .strtab is the final section in the table.
        const ElfSection *last     = ElfFindSection(&elf, ".strtab");
        const ElfSection *expected = VecPtrAt(&elf.sections, N_SECTIONS - 1);
        bool              ok       = last != NULL && last == expected;
            return false;
        }
        const ElfSymbol *a = VecPtrAt(&elf.symbols, 1);
        const ElfSymbol *b = VecPtrAt(&elf.symbols, 3);
        const ElfSymbol *l = VecPtrAt(&elf.symbols, 4);
        }
        const ElfSymbol *a = VecPtrAt(&elf.symbols, 1);
        const ElfSymbol *b = VecPtrAt(&elf.symbols, 3);
        const ElfSymbol *l = VecPtrAt(&elf.symbols, 4);
        bool ok = a->name && ZstrCompare(a->name, "alpha") == 0 && b->name && ZstrCompare(b->name, "beta") == 0 &&
        const ElfSymbol *a = VecPtrAt(&elf.symbols, 1);
        const ElfSymbol *b = VecPtrAt(&elf.symbols, 3);
        const ElfSymbol *l = VecPtrAt(&elf.symbols, 4);
        bool ok = a->name && ZstrCompare(a->name, "alpha") == 0 && b->name && ZstrCompare(b->name, "beta") == 0 &&
                  l->name && ZstrCompare(l->name, "local") == 0;
            return false;
        }
        const ElfSymbol *a  = VecPtrAt(&elf.symbols, 1);
        bool             ok = a->value == SF_ALPHA_VADDR && a->size == SF_ALPHA_SIZE && a->type == ELF_SYMBOL_TYPE_FUNC &&
                  a->bind == ELF_SYMBOL_BIND_GLOBAL && a->section_index == SF_SEC_TEXT;
        bool ok = VecLen(&elf.dynamic_symbols) == 2;
        if (ok) {
            const ElfSymbol *d = VecPtrAt(&elf.dynamic_symbols, 1);
            ok                 = d->name && ZstrCompare(d->name, "dyn_sym") == 0 && d->value == SF_DYN_VADDR;
        }
        if (strings_match) {
            for (size i = 0; i < VecLen(&original_strings); i++) {
                if (StrLen(VecPtrAt(&original_strings, i)) != StrLen(VecPtrAt(&parsed_strings, i)) ||
                    (StrLen(VecPtrAt(&original_strings, i)) &&
                     StrCmp(VecPtrAt(&original_strings, i), VecPtrAt(&parsed_strings, i)) != 0)) {
            for (size i = 0; i < VecLen(&original_strings); i++) {
                if (StrLen(VecPtrAt(&original_strings, i)) != StrLen(VecPtrAt(&parsed_strings, i)) ||
                    (StrLen(VecPtrAt(&original_strings, i)) &&
                     StrCmp(VecPtrAt(&original_strings, i), VecPtrAt(&parsed_strings, i)) != 0)) {
                    strings_match = false;
                if (StrLen(VecPtrAt(&original_strings, i)) != StrLen(VecPtrAt(&parsed_strings, i)) ||
                    (StrLen(VecPtrAt(&original_strings, i)) &&
                     StrCmp(VecPtrAt(&original_strings, i), VecPtrAt(&parsed_strings, i)) != 0)) {
                    strings_match = false;
                    break;
Last updated on