|
| 1 | +/* |
| 2 | + * Twin - A Tiny Window System |
| 3 | + * Copyright (c) 2025 National Cheng Kung University, Taiwan |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +/* Closure lifetime management implementation |
| 8 | + * |
| 9 | + * The Mado window system uses an asynchronous event model where widgets |
| 10 | + * schedule work items and timeouts with themselves as closure pointers. When a |
| 11 | + * widget is destroyed, these queued items may still reference the freed memory, |
| 12 | + * leading to use-after-free vulnerabilities and crashes. |
| 13 | + * |
| 14 | + * This implements a reference-counting closure tracking system that: |
| 15 | + * 1. Maintains a registry of all active closure pointers |
| 16 | + * 2. Tracks reference counts for each closure |
| 17 | + * 3. Provides validation before closure use |
| 18 | + * 4. Supports marking closures as "being freed" to prevent new references |
| 19 | + * |
| 20 | + * Flow Control |
| 21 | + * ------------ |
| 22 | + * 1. Registration: When a widget/object is created, it registers itself as a |
| 23 | + * closure |
| 24 | + * 2. Reference: When scheduling work/timeout, the system adds a reference |
| 25 | + * 3. Validation: Before executing callbacks, the system validates the closure |
| 26 | + * 4. Cleanup: When work completes or widget is destroyed, references are |
| 27 | + * removed |
| 28 | + * |
| 29 | + * Important: Closures must be registered before they can be used in |
| 30 | + * work/timeout systems. The typical pattern is: |
| 31 | + * 1. Object creation: _twin_closure_register(obj) |
| 32 | + * 2. Schedule work: twin_set_work() calls _twin_closure_ref() |
| 33 | + * 3. Object destruction: _twin_closure_mark_for_free() then |
| 34 | + * _twin_closure_unregister() |
| 35 | + * 4. Work execution: _twin_closure_is_valid() check prevents use-after-free |
| 36 | + */ |
| 37 | + |
| 38 | +#include <string.h> |
| 39 | + |
| 40 | +#include "twin_private.h" |
| 41 | + |
| 42 | +/* Global closure tracker instance */ |
| 43 | +twin_closure_tracker_t _twin_closure_tracker = {0}; |
| 44 | + |
| 45 | +/* |
| 46 | + * Initialize the closure tracking system. |
| 47 | + * Called once during screen initialization to set up the tracking table. |
| 48 | + */ |
| 49 | +void _twin_closure_tracker_init(void) |
| 50 | +{ |
| 51 | + memset(&_twin_closure_tracker, 0, sizeof(_twin_closure_tracker)); |
| 52 | + _twin_closure_tracker.initialized = true; |
| 53 | + /* TODO: Initialize mutex when threading support is added */ |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | + * Find an entry for a given closure pointer. |
| 58 | + * Uses linear search which is efficient for small closure counts. |
| 59 | + * |
| 60 | + * Returns entry pointer if found, NULL otherwise |
| 61 | + */ |
| 62 | +static twin_closure_entry_t *_twin_closure_find_entry(void *closure) |
| 63 | +{ |
| 64 | + if (!closure) |
| 65 | + return NULL; |
| 66 | + |
| 67 | + /* Quick rejection of obviously invalid pointers */ |
| 68 | + if (!twin_pointer_valid(closure)) |
| 69 | + return NULL; |
| 70 | + |
| 71 | + for (int i = 0; i < _twin_closure_tracker.count; i++) { |
| 72 | + if (_twin_closure_tracker.entries[i].closure == closure) |
| 73 | + return &_twin_closure_tracker.entries[i]; |
| 74 | + } |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | +/* |
| 79 | + * Register a closure pointer with the tracking system. |
| 80 | + * If already registered, increments the reference count. |
| 81 | + * |
| 82 | + * This is typically called when a widget/object is created and may be used |
| 83 | + * as a closure in work items or timeouts. |
| 84 | + * @closure : The pointer to track (usually a widget or toplevel) |
| 85 | + * |
| 86 | + * Returns true if successfully registered, false on failure |
| 87 | + */ |
| 88 | +bool _twin_closure_register(void *closure) |
| 89 | +{ |
| 90 | + if (!closure) |
| 91 | + return false; |
| 92 | + |
| 93 | + /* Quick rejection of obviously invalid pointers */ |
| 94 | + if (!twin_pointer_valid(closure)) |
| 95 | + return false; |
| 96 | + |
| 97 | + /* If tracker not initialized, skip registration */ |
| 98 | + if (!_twin_closure_tracker.initialized) |
| 99 | + return true; /* Pretend success if tracking not yet enabled */ |
| 100 | + |
| 101 | + /* Check if already registered */ |
| 102 | + twin_closure_entry_t *entry = _twin_closure_find_entry(closure); |
| 103 | + if (entry) { |
| 104 | + /* Already registered, just increment ref count */ |
| 105 | + entry->ref_count++; |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + /* Check capacity */ |
| 110 | + if (_twin_closure_tracker.count >= TWIN_MAX_CLOSURES) |
| 111 | + return false; |
| 112 | + |
| 113 | + /* Add new entry at the end */ |
| 114 | + entry = &_twin_closure_tracker.entries[_twin_closure_tracker.count++]; |
| 115 | + entry->closure = closure; |
| 116 | + entry->ref_count = 1; |
| 117 | + entry->marked_for_free = false; |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +/* |
| 123 | + * Remove a closure from the tracking system. |
| 124 | + * Called during object destruction to clean up tracking entries. |
| 125 | + * |
| 126 | + * Uses swap-with-last removal for O(1) deletion without gaps. |
| 127 | + */ |
| 128 | +void _twin_closure_unregister(void *closure) |
| 129 | +{ |
| 130 | + if (!closure) |
| 131 | + return; |
| 132 | + |
| 133 | + for (int i = 0; i < _twin_closure_tracker.count; i++) { |
| 134 | + if (_twin_closure_tracker.entries[i].closure == closure) { |
| 135 | + /* Swap with last entry and decrement count */ |
| 136 | + if (i < _twin_closure_tracker.count - 1) { |
| 137 | + _twin_closure_tracker.entries[i] = |
| 138 | + _twin_closure_tracker |
| 139 | + .entries[_twin_closure_tracker.count - 1]; |
| 140 | + } |
| 141 | + _twin_closure_tracker.count--; |
| 142 | + return; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/* |
| 148 | + * Increment the reference count for a closure. |
| 149 | + * Called when scheduling new work items or timeouts. |
| 150 | + * |
| 151 | + * Fails if closure is not registered or marked for deletion. |
| 152 | + * This prevents new references to objects being destroyed. |
| 153 | + */ |
| 154 | +bool _twin_closure_ref(void *closure) |
| 155 | +{ |
| 156 | + /* Skip if tracker not initialized */ |
| 157 | + if (!_twin_closure_tracker.initialized) |
| 158 | + return true; |
| 159 | + |
| 160 | + twin_closure_entry_t *entry = _twin_closure_find_entry(closure); |
| 161 | + if (!entry || entry->marked_for_free) |
| 162 | + return false; |
| 163 | + |
| 164 | + entry->ref_count++; |
| 165 | + return true; |
| 166 | +} |
| 167 | + |
| 168 | +/* |
| 169 | + * Decrement the reference count for a closure. |
| 170 | + * Called when work items complete or timeouts are cleared. |
| 171 | + * |
| 172 | + * Note: We don't auto-unregister at zero refs to maintain explicit |
| 173 | + * ownership semantics. The owner must call unregister during destruction. |
| 174 | + */ |
| 175 | +bool _twin_closure_unref(void *closure) |
| 176 | +{ |
| 177 | + /* Skip if tracker not initialized */ |
| 178 | + if (!_twin_closure_tracker.initialized) |
| 179 | + return true; |
| 180 | + |
| 181 | + twin_closure_entry_t *entry = _twin_closure_find_entry(closure); |
| 182 | + if (!entry) |
| 183 | + return false; |
| 184 | + |
| 185 | + if (entry->ref_count > 0) |
| 186 | + entry->ref_count--; |
| 187 | + |
| 188 | + return true; |
| 189 | +} |
| 190 | + |
| 191 | +/* |
| 192 | + * Validate a closure pointer before use. |
| 193 | + * This is the critical safety check called before executing callbacks. |
| 194 | + * |
| 195 | + * Validation steps: |
| 196 | + * 1. NULL check |
| 197 | + * 2. Platform-specific pointer validity (checks for obviously bad addresses) |
| 198 | + * 3. Presence in tracking table (untracked = invalid) |
| 199 | + * 4. Not marked for deletion |
| 200 | + * 5. Has active references |
| 201 | + * |
| 202 | + * Returns: true if safe to use, false if potentially freed |
| 203 | + */ |
| 204 | +bool _twin_closure_is_valid(void *closure) |
| 205 | +{ |
| 206 | + if (!closure) |
| 207 | + return false; |
| 208 | + |
| 209 | + /* First-line defense: basic pointer sanity */ |
| 210 | + if (!twin_pointer_valid(closure)) |
| 211 | + return false; |
| 212 | + |
| 213 | + /* If tracker not initialized, fall back to basic pointer validation */ |
| 214 | + if (!_twin_closure_tracker.initialized) |
| 215 | + return true; /* Assume valid if tracking not yet enabled */ |
| 216 | + |
| 217 | + /* Must be tracked to be valid */ |
| 218 | + twin_closure_entry_t *entry = _twin_closure_find_entry(closure); |
| 219 | + if (!entry) |
| 220 | + return false; |
| 221 | + |
| 222 | + /* Marked closures are in process of being freed */ |
| 223 | + if (entry->marked_for_free) |
| 224 | + return false; |
| 225 | + |
| 226 | + /* Must have active references */ |
| 227 | + return entry->ref_count > 0; |
| 228 | +} |
| 229 | + |
| 230 | +/* |
| 231 | + * Mark a closure as being freed. |
| 232 | + * Called at the start of object destruction to prevent races. |
| 233 | + * |
| 234 | + * Once marked: |
| 235 | + * - No new references can be added (ref() will fail) |
| 236 | + * - Existing references remain valid until cleared |
| 237 | + * - is_valid() returns false to prevent new callback execution |
| 238 | + */ |
| 239 | +void _twin_closure_mark_for_free(void *closure) |
| 240 | +{ |
| 241 | + twin_closure_entry_t *entry = _twin_closure_find_entry(closure); |
| 242 | + if (entry) |
| 243 | + entry->marked_for_free = true; |
| 244 | +} |
0 commit comments