Skip to content

Run frontier MoE models on hardware you already own β€” pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(windows): enforce exact HIP runtime binding

Kenneth-Javier Ortigozaa5ef3d7a63fc
diff --git a/c/backend_loader.c b/c/backend_loader.c
index 9c4adf1f..c386e2f9 100644
--- a/c/backend_loader.c
+++ b/c/backend_loader.c
@@ -42,6 +42,7 @@
* and the diagnostic label differ. */
#ifdef COLI_HIP_DLL
#define COLI_BACKEND_DLL "coli_hip.dll"
+#define COLI_BACKEND_DLL_W L"coli_hip.dll" /* the HIP host loads it wide */
#define COLI_VENDOR_TAG "[HIP]"
#else
#define COLI_BACKEND_DLL "coli_cuda.dll"
@@ -123,6 +124,14 @@ static struct {
int loaded; /* 1 = load attempted (success or fail), 0 = not yet */
int available; /* 1 = DLL loaded and all symbols resolved */
HMODULE dll;
+#ifdef COLI_HIP_DLL
+ /* The one explicit reference this loader holds on the configured
+ * amdhip64_7.dll. Non-NULL IS the ownership flag β€” it is set only after
+ * everything below it succeeded, and released only in shutdown. The
+ * backend's own import table also references the runtime, but that
+ * reference belongs to the backend, not to us. */
+ HMODULE hip_runtime;
+#endif
fn_init init;
fn_shutdown shutdown;
fn_device_count device_count;
@@ -288,81 +297,54 @@ static int coli_hip_env_dup(wchar_t **out, DWORD *err){
return -1;
}
-/* 1 when the configured runtime directory is usable, 0 (with a diagnostic
- * naming the specific problem) otherwise. Validation only β€” see the block
- * comment above. */
-static int coli_hip_runtime_dir_ok(void){
- wchar_t *dir = NULL, *file = NULL;
+/* The validated directory, owned by the caller, or NULL after a diagnostic
+ * naming the specific problem. Only the DIRECTORY is settled here; the runtime
+ * file beneath it is joined, classified and identified in one step further
+ * down, once the identity helpers are in scope. */
+static wchar_t *coli_hip_runtime_dir_value(void){
+ wchar_t *dir = NULL;
DWORD err = 0, attrs;
size_t len;
- int rc, ok = 0;
+ int rc;
rc = coli_hip_env_dup(&dir, &err);
if(rc == 0){
fprintf(stderr, COLI_VENDOR_TAG " COLI_HIP_RUNTIME_DIR is not set; "
"set it to the directory containing amdhip64_7.dll. "
"GPU tier disabled (CPU path remains active).\n");
- return 0;
+ return NULL;
}
if(rc < 0){
fprintf(stderr, COLI_VENDOR_TAG " could not read COLI_HIP_RUNTIME_DIR "
"(error %lu); GPU tier disabled "
"(CPU path remains active).\n", (unsigned long)err);
- return 0;
+ return NULL;
}
len = wcslen(dir);
if(len == 0){
coli_hip_reject("COLI_HIP_RUNTIME_DIR is empty", NULL);
- goto done;
+ goto fail;
}
if(!coli_hip_path_is_absolute(dir)){
coli_hip_reject("COLI_HIP_RUNTIME_DIR must be an absolute path", dir);
- goto done;
+ goto fail;
}
attrs = GetFileAttributesW(dir);
if(attrs == INVALID_FILE_ATTRIBUTES){
coli_hip_reject("COLI_HIP_RUNTIME_DIR does not exist", dir);
- goto done;
+ goto fail;
}
if(!(attrs & FILE_ATTRIBUTE_DIRECTORY)){
coli_hip_reject("COLI_HIP_RUNTIME_DIR is not a directory", dir);
- goto done;
- }
-
- /* Append the runtime filename under one separator. A value the user ended
- * with a slash must not become a doubled separator: that reads as a UNC
- * root after a drive letter and would fail for a reason unrelated to the
- * real configuration. */
- file = (wchar_t *)malloc((len + 1 + wcslen(COLI_HIP_RUNTIME_FILE) + 1) *
- sizeof(wchar_t));
- if(!file){
- fprintf(stderr, COLI_VENDOR_TAG " out of memory validating "
- "COLI_HIP_RUNTIME_DIR; GPU tier disabled "
- "(CPU path remains active).\n");
- goto done;
- }
- wcscpy(file, dir);
- if(!coli_hip_is_sep(dir[len - 1])) wcscat(file, L"\\");
- wcscat(file, COLI_HIP_RUNTIME_FILE);
-
- attrs = GetFileAttributesW(file);
- if(attrs == INVALID_FILE_ATTRIBUTES){
- coli_hip_reject("amdhip64_7.dll not found in COLI_HIP_RUNTIME_DIR", file);
- goto done;
+ goto fail;
}
- if(attrs & FILE_ATTRIBUTE_DIRECTORY){
- coli_hip_reject("amdhip64_7.dll is a directory, not a file", file);
- goto done;
- }
-
- ok = 1;
+ return dir;
-done:
- free(file);
+fail:
free(dir);
- return ok;
+ return NULL;
}
#endif /* COLI_HIP_DLL */
@@ -421,18 +403,18 @@ static void coli_win_error_phrase(DWORD code, char *buf, size_t n){
* physical file identity rather than on path text. Paths are kept only to tell
* a human which file we meant.
*
- * These functions have external linkage but appear in no header, are never
- * exported, and live in backend_loader.o, which links into the host executable
- * only β€” coli_hip.dll and coli_cuda.dll are built from backend_cuda.cu and
- * never see this file. External linkage rather than `static` is deliberate:
- * unused static functions would warn, and the warning would be noise rather
- * than a finding while the wiring is still a slice away. */
+ * These functions are file-static and appear in no header: they live in
+ * backend_loader.o, which links into the host executable only β€” coli_hip.dll
+ * and coli_cuda.dll are built from backend_cuda.cu and never see this file.
+ * The whole block is compiled only where something consumes it, so a CUDA host
+ * carries none of it. */
+#if defined(COLI_HIP_DLL) || defined(COLI_LOADER_TEST_API)
#define COLI_W32_RUNTIME_BASENAME L"amdhip64_7.dll"
#define COLI_W32_PATH_START 512u /* wchar_t; grows, never a correctness bound */
#define COLI_W32_PATH_ROUNDS 8 /* 512 -> 65536 wchar_t before giving up */
-int coli_win32_loader_is_sep(wchar_t c){ return c == L'\\' || c == L'/'; }
+static int coli_win32_loader_is_sep(wchar_t c){ return c == L'\\' || c == L'/'; }
/* --- physical file identity ------------------------------------------
*
@@ -452,7 +434,7 @@ typedef struct {
DWORD bh_index_low;
} ColiW32FileId;
-int coli_win32_loader_identity_valid(const ColiW32FileId *id){
+static int coli_win32_loader_identity_valid(const ColiW32FileId *id){
return id && (id->have_id_info || id->have_by_handle);
}
@@ -464,7 +446,7 @@ int coli_win32_loader_identity_valid(const ColiW32FileId *id){
* would be indistinguishable from a real one. When no shared format exists the
* answer is "unknown" β€” never a text comparison, which would reject hard links
* to the very file that was configured. */
-int coli_win32_loader_identity_equal(const ColiW32FileId *a, const ColiW32FileId *b){
+static int coli_win32_loader_identity_equal(const ColiW32FileId *a, const ColiW32FileId *b){
if(!a || !b) return -1;
if(a->have_id_info && b->have_id_info)
return (a->idi_volume == b->idi_volume &&
@@ -489,7 +471,7 @@ static int coli_w32_grow_ok(size_t cap){
* GetModuleFileNameW signals truncation by returning the buffer size AND
* setting ERROR_INSUFFICIENT_BUFFER; the length alone cannot be trusted,
* which is why the error code is cleared first and then consulted. */
-wchar_t *coli_win32_loader_module_path(HMODULE mod, DWORD *err){
+static wchar_t *coli_win32_loader_module_path(HMODULE mod, DWORD *err){
size_t cap = COLI_W32_PATH_START;
int round;
if(err) *err = 0;
@@ -518,7 +500,7 @@ wchar_t *coli_win32_loader_module_path(HMODULE mod, DWORD *err){
return NULL;
}
-wchar_t *coli_win32_loader_exe_path(DWORD *err){
+static wchar_t *coli_win32_loader_exe_path(DWORD *err){
return coli_win32_loader_module_path(NULL, err);
}
@@ -528,7 +510,7 @@ wchar_t *coli_win32_loader_exe_path(DWORD *err){
* and this string is only ever shown to a human. The VOLUME_NAME_DOS form is
* returned as Windows gives it, including any \\?\ prefix: stripping that
* prefix can change how a path is interpreted, so it is left intact. */
-wchar_t *coli_win32_loader_final_path(HANDLE handle, DWORD *err){
+static wchar_t *coli_win32_loader_final_path(HANDLE handle, DWORD *err){
size_t cap = COLI_W32_PATH_START;
int round;
if(err) *err = 0;
@@ -559,7 +541,7 @@ wchar_t *coli_win32_loader_final_path(HANDLE handle, DWORD *err){
/* <dir> + one separator + <child>. Purely textual: nothing is resolved,
* expanded or searched for. A value already ending in a separator does not
* gain a second one, because "C:\x\\y" reads as a UNC root. */
-wchar_t *coli_win32_loader_join(const wchar_t *dir, const wchar_t *child, DWORD *err){
+static wchar_t *coli_win32_loader_join(const wchar_t *dir, const wchar_t *child, DWORD *err){
size_t dl, cl, need;
int sep;
wchar_t *out;
@@ -583,7 +565,7 @@ wchar_t *coli_win32_loader_join(const wchar_t *dir, const wchar_t *child, DWORD
* needs no read permission and cannot disturb a mapped module. Full sharing
* means the probe never blocks anyone else, and FILE_FLAG_BACKUP_SEMANTICS
* lets the same call work if the target turns out to be a directory. */
-int coli_win32_loader_probe(const wchar_t *path, ColiW32FileId *id,
+static int coli_win32_loader_probe(const wchar_t *path, ColiW32FileId *id,
wchar_t **final_path, DWORD *err){
HANDLE h;
FILE_ID_INFO fii;
@@ -650,7 +632,7 @@ typedef struct {
ColiW32Module *items;
} ColiW32Inventory;
-void coli_win32_loader_inventory_free(ColiW32Inventory *inv){
+static void coli_win32_loader_inventory_free(ColiW32Inventory *inv){
size_t i;
if(!inv) return;
for(i = 0; i < inv->count; i++){
@@ -677,7 +659,7 @@ static wchar_t *coli_w32_wdup(const wchar_t *s){
* it must never be mistaken for "nothing is loaded", because the two lead to
* opposite decisions. Reference counts are untouched β€” the HMODULEs recorded
* here are observations, not owned references. */
-int coli_win32_loader_inventory(const wchar_t *basename, ColiW32Inventory *out){
+static int coli_win32_loader_inventory(const wchar_t *basename, ColiW32Inventory *out){
int attempt;
if(!out) return COLI_W32_INV_ALLOC_FAILED;
memset(out, 0, sizeof *out);
@@ -777,7 +759,7 @@ enum {
COLI_BIND_REJECT_UNVERIFIABLE
};
-int coli_win32_loader_decide(int configured_valid, int inventory_status,
+static int coli_win32_loader_decide(int configured_valid, int inventory_status,
size_t count, const int *match_equality){
if(!configured_valid) return COLI_BIND_REJECT_UNVERIFIABLE;
if(inventory_status != COLI_W32_INV_OK) return COLI_BIND_REJECT_UNVERIFIABLE;
@@ -808,7 +790,7 @@ typedef struct {
} ColiW32ConfiguredRuntime;
/* Idempotent, and safe on a zero-initialised object. */
-void coli_win32_loader_configured_free(ColiW32ConfiguredRuntime *cr){
+static void coli_win32_loader_configured_free(ColiW32ConfiguredRuntime *cr){
if(!cr) return;
free(cr->path);
free(cr->final_path);
@@ -817,7 +799,7 @@ void coli_win32_loader_configured_free(ColiW32ConfiguredRuntime *cr){
cr->valid = 0;
}
-int coli_win32_loader_configured(const wchar_t *dir, ColiW32ConfiguredRuntime *out){
+static int coli_win32_loader_configured(const wchar_t *dir, ColiW32ConfiguredRuntime *out){
DWORD err = 0;
if(!out) return 0;
memset(out, 0, sizeof *out);
@@ -958,22 +940,325 @@ int coli_loader_test_inventory(const wchar_t *configured_path,
return inv.status;
}
#endif /* COLI_LOADER_TEST_API */
+#endif /* COLI_HIP_DLL || COLI_LOADER_TEST_API */
+
+#ifdef COLI_HIP_DLL
+/* ============ Fail-closed HIP runtime binding =============================
+ *
+ * A HIP host may only reach GPU work if it can show that the amdhip64_7.dll
+ * bound to coli_hip.dll is exactly the one COLI_HIP_RUNTIME_DIR names. Anything
+ * it cannot show β€” a different runtime already loaded, two of them, an
+ * enumeration that did not complete β€” disables the GPU tier instead of
+ * guessing. The CPU path stays available throughout.
+ *
+ * Honest limit: the runtime is verified AFTER it is mapped. LoadLibraryExW
+ * takes a path, not an open handle, so a rename between identifying the file
+ * and loading it cannot be prevented β€” only detected, which the post-load
+ * inventory does. Nothing here is atomic and nothing claims to be. */
+
+/* Everything the loader learned about the configured runtime, valid for the
+ * length of one initialisation attempt. */
+static int coli_hip_configure(ColiW32ConfiguredRuntime *out){
+ wchar_t *dir;
+ DWORD attrs;
+ int ok;
+
+ memset(out, 0, sizeof *out);
+ dir = coli_hip_runtime_dir_value(); /* diagnoses its own failures */
+ if(!dir) return 0;
+
+ /* One join, then classify what it landed on. The path survives a failed
+ * probe precisely so these messages can name it. */
+ ok = coli_win32_loader_configured(dir, out);
+ free(dir);
+ if(!out->path){
+ fprintf(stderr, COLI_VENDOR_TAG " could not build the amdhip64_7.dll "
+ "path under COLI_HIP_RUNTIME_DIR (error %lu); GPU tier "
+ "disabled (CPU path remains active).\n",
+ (unsigned long)out->error);
+ return 0;
+ }
+ /* Classified independently of the probe: identity inspection opens with
+ * backup semantics and therefore SUCCEEDS on a directory, so "is it a
+ * file" has to be asked separately or a directory named amdhip64_7.dll
+ * would sail through and fail much later as an opaque access error. */
+ attrs = GetFileAttributesW(out->path);
+ if(attrs == INVALID_FILE_ATTRIBUTES){
+ coli_hip_reject("amdhip64_7.dll not found in COLI_HIP_RUNTIME_DIR", out->path);
+ } else if(attrs & FILE_ATTRIBUTE_DIRECTORY){
+ coli_hip_reject("amdhip64_7.dll is a directory, not a file", out->path);
+ } else if(!ok){
+ /* A real file whose identity could not be read: nothing downstream
+ * could be proven, so this is a refusal too. */
+ coli_hip_reject("the identity of amdhip64_7.dll could not be established",
+ out->path);
+ } else {
+ return 1;
+ }
+ coli_win32_loader_configured_free(out);
+ return 0;
+}
+
+/* Name a runtime in a message: the canonical path when we have one, otherwise
+ * whatever the module list gave us. */
+static void coli_hip_report_module(const char *label, const ColiW32Module *m){
+ const wchar_t *shown = m->final_path ? m->final_path :
+ (m->path ? m->path : m->entry_path);
+ char *utf8 = shown ? coli_hip_utf8(shown) : NULL;
+ fprintf(stderr, COLI_VENDOR_TAG " %s: %s\n", label,
+ utf8 ? utf8 : "<path unavailable>");
+ free(utf8);
+}
+
+static void coli_hip_report_configured(const ColiW32ConfiguredRuntime *cfg){
+ const wchar_t *shown = cfg->final_path ? cfg->final_path : cfg->path;
+ char *utf8 = shown ? coli_hip_utf8(shown) : NULL;
+ fprintf(stderr, COLI_VENDOR_TAG " configured: %s\n",
+ utf8 ? utf8 : "<path unavailable>");
+ free(utf8);
+}
+
+/* Exactly one amdhip64_7.dll, it is the configured file, and it is the module
+ * this loader holds a reference to. Anything else fails closed. *stage* names
+ * the moment so a reader can tell a bad starting state from one the backend
+ * created. */
+static int coli_hip_verify_bound(const ColiW32ConfiguredRuntime *cfg,
+ HMODULE expected, const char *stage){
+ ColiW32Inventory inv;
+ int ok = 0;
+
+ coli_win32_loader_inventory(COLI_W32_RUNTIME_BASENAME, &inv);
+ if(inv.status != COLI_W32_INV_OK){
+ fprintf(stderr, COLI_VENDOR_TAG " %s: the loaded-module list could not "
+ "be read (status %d, error %lu); GPU tier disabled "
+ "(CPU path remains active).\n",
+ stage, inv.status, (unsigned long)inv.error);
+ } else if(inv.count != 1){
+ size_t i;
+ fprintf(stderr, COLI_VENDOR_TAG " %s: expected exactly one "
+ "amdhip64_7.dll, found %u; GPU tier disabled "
+ "(CPU path remains active).\n",
+ stage, (unsigned)inv.count);
+ coli_hip_report_configured(cfg);
+ for(i = 0; i < inv.count; i++) coli_hip_report_module("loaded", &inv.items[i]);
+ } else if(!inv.items[0].path){
+ fprintf(stderr, COLI_VENDOR_TAG " %s: the path of the loaded "
+ "amdhip64_7.dll is unavailable; GPU tier disabled "
+ "(CPU path remains active).\n", stage);
+ } else if(inv.items[0].module != expected){
+ fprintf(stderr, COLI_VENDOR_TAG " %s: the loaded amdhip64_7.dll is not "
+ "the module this process referenced; GPU tier disabled "
+ "(CPU path remains active).\n", stage);
+ coli_hip_report_configured(cfg);
+ coli_hip_report_module("loaded", &inv.items[0]);
+ } else if(coli_win32_loader_identity_equal(&cfg->id, &inv.items[0].id) != 1){
+ fprintf(stderr, COLI_VENDOR_TAG " %s: the loaded amdhip64_7.dll is not "
+ "the configured file; GPU tier disabled "
+ "(CPU path remains active).\n", stage);
+ coli_hip_report_configured(cfg);
+ coli_hip_report_module("loaded", &inv.items[0]);
+ } else {
+ ok = 1;
+ }
+ coli_win32_loader_inventory_free(&inv);
+ return ok;
+}
+
+/* Decide against what is already loaded, then take one explicit reference to
+ * the configured file. Returns the owned HMODULE, or NULL after diagnosing.
+ *
+ * Both acceptable decisions end in the same LoadLibraryExW on the same exact
+ * absolute path: when the module is already present that call returns the
+ * existing handle and raises its reference count, so "loaded it" and "adopted
+ * it" produce one uniform ownership rule and one uniform release. */
+static HMODULE coli_hip_acquire_runtime(const ColiW32ConfiguredRuntime *cfg){
+ ColiW32Inventory inv;
+ int *equality = NULL;
+ HMODULE runtime = NULL;
+ int decision;
+ size_t i;
+
+ coli_win32_loader_inventory(COLI_W32_RUNTIME_BASENAME, &inv);
+ if(inv.status == COLI_W32_INV_OK && inv.count > 0){
+ equality = (int *)malloc(inv.count * sizeof *equality);
+ if(!equality){
+ fprintf(stderr, COLI_VENDOR_TAG " out of memory comparing loaded "
+ "runtimes; GPU tier disabled "
+ "(CPU path remains active).\n");
+ coli_win32_loader_inventory_free(&inv);
+ return NULL;
+ }
+ for(i = 0; i < inv.count; i++)
+ equality[i] = coli_win32_loader_identity_equal(&cfg->id, &inv.items[i].id);
+ }
+
+ decision = coli_win32_loader_decide(cfg->valid, inv.status, inv.count, equality);
+ switch(decision){
+ case COLI_BIND_REJECT_WRONG_RUNTIME:
+ fprintf(stderr, COLI_VENDOR_TAG " a different amdhip64_7.dll is already "
+ "loaded in this process; GPU tier disabled "
+ "(CPU path remains active).\n");
+ coli_hip_report_configured(cfg);
+ coli_hip_report_module("already loaded", &inv.items[0]);
+ break;
+ case COLI_BIND_REJECT_MULTIPLE_RUNTIMES:
+ fprintf(stderr, COLI_VENDOR_TAG " %u different amdhip64_7.dll modules "
+ "are already loaded in this process; which one an import "
+ "binds to is not ours to choose. GPU tier disabled "
+ "(CPU path remains active).\n", (unsigned)inv.count);
+ coli_hip_report_configured(cfg);
+ for(i = 0; i < inv.count; i++)
+ coli_hip_report_module("already loaded", &inv.items[i]);
+ break;
+ case COLI_BIND_REJECT_UNVERIFIABLE:
+ fprintf(stderr, COLI_VENDOR_TAG " the loaded amdhip64_7.dll state could "
+ "not be established (status %d, error %lu); GPU tier "
+ "disabled (CPU path remains active).\n",
+ inv.status, (unsigned long)inv.error);
+ coli_hip_report_configured(cfg);
+ break;
+ default: {
+ /* NEED_EXACT_LOAD and REUSE_EXACT_MATCH take the same action. The path
+ * is absolute and exact: no PATH, no current directory, no System32,
+ * and never a bare name. */
+ DWORD err;
+ runtime = LoadLibraryExW(cfg->path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
+ if(!runtime){
+ char phrase[160];
+ err = GetLastError();
+ coli_win_error_phrase(err, phrase, sizeof phrase);
+ fprintf(stderr, COLI_VENDOR_TAG " the configured amdhip64_7.dll "
+ "could not be loaded: %s; GPU tier disabled "
+ "(CPU path remains active).\n", phrase);
+ coli_hip_report_configured(cfg);
+ break;
+ }
+ if(!coli_hip_verify_bound(cfg, runtime, "runtime verification")){
+ FreeLibrary(runtime);
+ runtime = NULL;
+ }
+ break;
+ }
+ }
+ free(equality);
+ coli_win32_loader_inventory_free(&inv);
+ return runtime;
+}
+
+/* <executable directory>\coli_hip.dll, Unicode and dynamically sized, loaded
+ * by absolute path only. There is deliberately no bare-name fallback: a
+ * fallback that can reach System32 would let some other coli_hip.dll satisfy
+ * the load and quietly undo the runtime contract above. */
+static HMODULE coli_hip_load_backend(void){
+ DWORD err = 0, attrs;
+ wchar_t *exe, *candidate = NULL, *slash, *p;
+ HMODULE backend = NULL;
+ char phrase[160], *shown;
+
+ exe = coli_win32_loader_exe_path(&err);
+ if(!exe){
+ coli_win_error_phrase(err, phrase, sizeof phrase);
+ fprintf(stderr, COLI_VENDOR_TAG " the executable path could not be "
+ "determined: %s; GPU tier disabled "
+ "(CPU path remains active).\n", phrase);
+ return NULL;
+ }
+ /* Trim to the directory, keeping the separator so a drive root stays
+ * "C:\" rather than becoming the drive-relative "C:". */
+ slash = NULL;
+ for(p = exe; *p; p++) if(coli_win32_loader_is_sep(*p)) slash = p;
+ if(!slash){
+ fprintf(stderr, COLI_VENDOR_TAG " the executable path has no directory "
+ "component; GPU tier disabled "
+ "(CPU path remains active).\n");
+ free(exe);
+ return NULL;
+ }
+ slash[1] = L'\0';
+
+ candidate = coli_win32_loader_join(exe, COLI_BACKEND_DLL_W, &err);
+ if(!candidate){
+ coli_win_error_phrase(err, phrase, sizeof phrase);
+ fprintf(stderr, COLI_VENDOR_TAG " " COLI_BACKEND_DLL " path could not be "
+ "built: %s; GPU tier disabled "
+ "(CPU path remains active).\n", phrase);
+ free(exe);
+ return NULL;
+ }
+
+ backend = LoadLibraryExW(candidate, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
+ if(!backend){
+ err = GetLastError(); /* before anything else runs */
+ coli_win_error_phrase(err, phrase, sizeof phrase);
+ attrs = GetFileAttributesW(candidate);
+ shown = coli_hip_utf8(candidate);
+ fprintf(stderr, COLI_VENDOR_TAG " " COLI_BACKEND_DLL " could not be "
+ "loaded; GPU tier disabled (CPU path remains active).\n");
+ fprintf(stderr, COLI_VENDOR_TAG " candidate %s: %s; load failed with %s\n",
+ shown ? shown : "<unprintable path>",
+ attrs == INVALID_FILE_ATTRIBUTES ? "does not exist" :
+ (attrs & FILE_ATTRIBUTE_DIRECTORY) ? "exists but is a directory" :
+ "exists as a file",
+ phrase);
+ free(shown);
+ }
+ free(candidate);
+ free(exe);
+ return backend;
+}
+#endif /* COLI_HIP_DLL */
/* Resolve the DLL and all 11 symbols. Returns 1 on success, 0 otherwise.
* Idempotent: the first call (success or fail) sticks; later calls are no-ops
* that return the cached result. The engine treats a 0 return as "CUDA
* unavailable" and falls back to the CPU path without aborting. */
static int coli_cuda_load(void){
+#ifdef COLI_HIP_DLL
+ ColiW32ConfiguredRuntime cfg;
+ HMODULE runtime = NULL; /* owned here until the final transfer */
+#endif
if(g_cuda.loaded) return g_cuda.available;
+ /* Set before any work, and never cleared: one attempt per process.
+ *
+ * COLI_HIP_RUNTIME_DIR is start-up configuration, and a wrong or ambiguous
+ * set of loaded runtimes cannot be repaired from inside a running process
+ * β€” a retry would re-run a deterministic failure against the same module
+ * state. It is also what keeps this function safe without a lock: see the
+ * single-entry note below. */
g_cuda.loaded = 1;
#ifdef COLI_HIP_DLL
- /* Configuration gate: refuse before touching the filesystem search order,
- * so a misconfigured host fails with a message about its configuration
- * rather than about a DLL it was never going to find. */
- if(!coli_hip_runtime_dir_ok()) return 0;
-#endif
-
+ /* No synchronisation here, and that is a traced conclusion rather than an
+ * assumption: coli_cuda_load has exactly two callers, coli_cuda_init and
+ * coli_cuda_attention_project_ragged. colibri.c calls init on the main
+ * thread before model initialisation and therefore before any worker
+ * thread exists, and the ragged path is reachable only after that init
+ * succeeded. If a third caller ever appears, or the guard in colibri.c
+ * moves, this needs an explicit lock instead. */
+ if(!coli_hip_configure(&cfg)) return 0;
+ runtime = coli_hip_acquire_runtime(&cfg);
+ if(!runtime){
+ coli_win32_loader_configured_free(&cfg);
+ return 0;
+ }
+ g_cuda.dll = coli_hip_load_backend();
+ if(!g_cuda.dll){
+ FreeLibrary(runtime);
+ coli_win32_loader_configured_free(&cfg);
+ return 0;
+ }
+ /* The backend is mapped, which pulled in its own dependencies. Ask again:
+ * if loading it introduced a second amdhip64_7.dll, or displaced the one
+ * we referenced, nothing below this point can be trusted. */
+ if(!coli_hip_verify_bound(&cfg, runtime, "post-backend verification")){
+ FreeLibrary(g_cuda.dll);
+ g_cuda.dll = NULL;
+ FreeLibrary(runtime);
+ coli_win32_loader_configured_free(&cfg);
+ return 0;
+ }
+ coli_win32_loader_configured_free(&cfg);
+#else
/* Load the backend DLL from the engine's OWN directory, by absolute path β€”
* never a bare name. LoadLibraryA(COLI_BACKEND_DLL) searches the current
* working directory (and, without SafeDllSearchMode, other writable dirs):
@@ -1039,7 +1324,8 @@ static int coli_cuda_load(void){
} else {
/* The MAX_PATH-bounded construction above is the only way to get
* here; it is a real limitation, so say so instead of implying the
- * file was looked for and missed. Retiring it is W1-B2d's job. */
+ * file was looked for and missed. The HIP host no longer has it β€”
+ * its path is built dynamically β€” but CUDA's flow is unchanged. */
fprintf(stderr, COLI_VENDOR_TAG " candidate path could not be constructed "
"(executable directory unavailable or longer than MAX_PATH); "
"no primary load was attempted\n");
@@ -1048,6 +1334,16 @@ static int coli_cuda_load(void){
fallback_msg);
return 0;
}
+#endif /* COLI_HIP_DLL */
+
+ /* A mandatory symbol is missing: the backend goes, and so does the runtime
+ * reference the HIP path acquired. The CUDA build expands the release to
+ * nothing, so its behaviour is byte-for-byte what it was. */
+#ifdef COLI_HIP_DLL
+ #define COLI_RELEASE_RUNTIME_ON_FAIL FreeLibrary(runtime);
+#else
+ #define COLI_RELEASE_RUNTIME_ON_FAIL
+#endif
#define RESOLVE(name, type) \
/* GetProcAddress returns FARPROC (void(*)(void)); casting it to a \
@@ -1061,7 +1357,9 @@ static int coli_cuda_load(void){
if(!g_cuda.name){ \
fprintf(stderr, COLI_VENDOR_TAG " " COLI_BACKEND_DLL \
" missing symbol coli_cuda_" #name "\n"); \
- FreeLibrary(g_cuda.dll); g_cuda.dll=NULL; return 0; }
+ FreeLibrary(g_cuda.dll); g_cuda.dll=NULL; \
+ COLI_RELEASE_RUNTIME_ON_FAIL \
+ return 0; }
/* Optional symbol: a DLL predating it leaves the pointer NULL and only that
* feature degrades (fmt=6 tensors stay CPU-side), instead of taking the whole
@@ -1125,7 +1423,15 @@ static int coli_cuda_load(void){
RESOLVE(shared_mlp_w4a16, fn_shared_mlp_w4a16)
RESOLVE(tensor_update, fn_tensor_update)
#undef RESOLVE
+ #undef COLI_RELEASE_RUNTIME_ON_FAIL
+#ifdef COLI_HIP_DLL
+ /* Everything above proved: the configured file is the only amdhip64_7.dll
+ * in the process, it is the module we referenced, it survived the backend
+ * load, and every mandatory symbol resolved. Only now does the reference
+ * stop being local, so no earlier failure path can leak or double-free it. */
+ g_cuda.hip_runtime = runtime;
+#endif
g_cuda.available = 1;
return 1;
}
@@ -1142,6 +1448,20 @@ int coli_cuda_init(const int *devices, int count){
void coli_cuda_shutdown(void){
if(g_cuda.available && g_cuda.shutdown) g_cuda.shutdown();
+#ifdef COLI_HIP_DLL
+ /* Backend first, then the runtime it imports: releasing the runtime while
+ * the backend is still mapped would leave the backend holding the only
+ * reference to a module we no longer track. Safe after a failed
+ * initialisation too, because both handles are NULL then.
+ *
+ * Note for the record, not a defect introduced here: colibri.c only calls
+ * this on the ANS-pack early exit, so in ordinary runs the practical
+ * cleanup path is process teardown. Adding the missing normal-exit call is
+ * a separate lifecycle question. */
+ if(g_cuda.dll){ FreeLibrary(g_cuda.dll); g_cuda.dll = NULL; }
+ if(g_cuda.hip_runtime){ FreeLibrary(g_cuda.hip_runtime); g_cuda.hip_runtime = NULL; }
+ g_cuda.available = 0;
+#endif
}
int coli_cuda_device_count(void){
diff --git a/c/tests/test_backend_loader.py b/c/tests/test_backend_loader.py
index dc8bb62f..9e7939d6 100644
--- a/c/tests/test_backend_loader.py
+++ b/c/tests/test_backend_loader.py
@@ -281,34 +281,40 @@ static int runtime_inventory(const char *prefix)
int main(int argc, char **argv)
{
- const char *preload = (argc > 1) ? argv[1] : "NONE";
- int requested = (strcmp(preload, "NONE") != 0);
int devices[1];
- int rc;
-
+ int rc, i, requested = 0;
+
+ /* Every argument after argv[0] is an absolute runtime to preload, except
+ * the literal NONE. The handles are deliberately NEVER released: holding
+ * them through the post-shutdown inventory is what makes production's own
+ * reference ownership measurable. */
+ for (i = 1; i < argc; i++)
+ if (strcmp(argv[i], "NONE") != 0) requested++;
printf("preload_requested=%d\n", requested);
runtime_inventory("runtime_before");
- if (requested) {
+ for (i = 1; i < argc; i++) {
wchar_t wide[WBUF], got[WBUF];
HMODULE runtime;
int (*marker)(void);
- MultiByteToWideChar(CP_UTF8, 0, preload, -1, wide, WBUF);
+ char key[64];
+ if (strcmp(argv[i], "NONE") == 0) continue;
+ MultiByteToWideChar(CP_UTF8, 0, argv[i], -1, wide, WBUF);
/* Absolute path, Unicode API. No PATH edit, no SetDllDirectory, and
- * the runtime is never copied beside the harness. The handle is held
- * until the process exits so the module stays resident. */
+ * the runtime is never copied beside the harness. */
runtime = LoadLibraryExW(wide, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (!runtime) {
- printf("preload_ok=0\n");
- printf("preload_error=%lu\n", GetLastError());
+ printf("preload_%d_ok=0\n", i - 1);
+ printf("preload_%d_error=%lu\n", i - 1, GetLastError());
return 4;
}
- printf("preload_ok=1\n");
- if (GetModuleFileNameW(runtime, got, WBUF)) emit_path("preload_path", got);
+ printf("preload_%d_ok=1\n", i - 1);
+ sprintf(key, "preload_%d_path", i - 1);
+ if (GetModuleFileNameW(runtime, got, WBUF)) emit_path(key, got);
marker = (int (*)(void))(void *)GetProcAddress(runtime,
"coli_test_runtime_marker");
- if (!marker) { printf("preload_marker_missing=1\n"); return 5; }
- printf("preload_marker=%d\n", marker());
+ if (!marker) { printf("preload_%d_marker_missing=1\n", i - 1); return 5; }
+ printf("preload_%d_marker=%d\n", i - 1, marker());
}
runtime_inventory("runtime_after_preload");
@@ -339,6 +345,13 @@ int main(int argc, char **argv)
}
}
runtime_inventory("runtime_after_backend");
+
+ /* Real shutdown, then look again. Production must release exactly its own
+ * reference: a runtime only it loaded disappears, while one this harness
+ * also holds stays mapped. */
+ coli_cuda_shutdown();
+ printf("shutdown_called=1\n");
+ runtime_inventory("runtime_after_shutdown");
return 0;
}
'''
@@ -787,6 +800,7 @@ int main(int argc, char **argv)
backend = self.backend if vendor == "hip" else self.cuda_backend
exe = case_dir / source_exe.name
shutil.copy2(source_exe, exe)
+ preloads = [preload] if isinstance(preload, (str, Path)) else list(preload)
if with_backend:
shutil.copy2(backend_override or backend, case_dir / backend.name)
for extra in extra_files:
@@ -794,9 +808,13 @@ int main(int argc, char **argv)
child = {k: v for k, v in os.environ.items()
if k != _RUNTIME_DIR_VAR}
child.update(env or {})
+ # encoding="utf-8": the harness converts every path with
+ # WideCharToMultiByte(CP_UTF8, ...), so a Unicode sandbox path survives
+ # only if it is decoded as UTF-8 rather than the locale codepage.
proc = subprocess.run(
- [str(exe), preload], cwd=str(case_dir), text=True, env=child,
- errors="replace", capture_output=True, timeout=120)
+ [str(exe)] + [str(p) for p in preloads],
+ cwd=str(case_dir), text=True, env=child,
+ encoding="utf-8", errors="replace", capture_output=True, timeout=120)
fields = {}
for line in (proc.stdout or "").splitlines():
if "=" in line:
@@ -1024,9 +1042,9 @@ class LoaderRuntimeBindingTest(unittest.TestCase):
self.assertEqual(proc.returncode, 0, "harness failed" + detail)
self.assertEqual(out.get("preload_requested"), "1", detail)
- self.assertEqual(out.get("preload_ok"), "1", detail)
- self.assertEqual(out.get("preload_marker"), str(marker), detail)
- self.assertTrue(self._same_path(out["preload_path"], str(runtime)), detail)
+ self.assertEqual(out.get("preload_0_ok"), "1", detail)
+ self.assertEqual(out.get("preload_0_marker"), str(marker), detail)
+ self.assertTrue(self._same_path(out["preload_0_path"], str(runtime)), detail)
self.assertEqual(out.get("loader_init"), "1", "loader init failed" + detail)
self.assertTrue(self._same_path(out["backend_path"],
@@ -1063,48 +1081,39 @@ class LoaderRuntimeBindingTest(unittest.TestCase):
_RUNTIME_MARKER_B, f.runtime_a)
self.assertEqual(out.get("runtime_before_count"), "0")
- def test_ambient_no_preload_is_classified_not_assumed(self):
- """With no preload the outcome is environment-dependent; classify it.
+ def test_no_preload_is_now_deterministic_because_production_loads_it(self):
+ """Nothing preloaded: production loads the configured runtime itself.
+
+ Before enforcement this case was environment-dependent and had to be
+ classified rather than asserted. It no longer is: the loader preloads
+ exactly the configured file by absolute path, so the outcome is fixed
+ on any machine β€” including one with a system-wide amdhip64_7.dll,
+ which is never consulted.
- This deliberately asserts no particular ambient winner: whether a
- machine-wide amdhip64_7.dll exists, and whether the loader reaches it,
- varies by host. The contract here is that the harness stays honest β€”
- it runs, its inventory parses, and neither controlled runtime was
- preloaded.
+ The post-shutdown inventory is the ownership proof: the harness holds
+ no reference here, so releasing production's must drop the count to 0.
"""
f = self.fixture
- case = Path(self.cases.name) / "case_ambient"
- # A valid runtime directory is configured so the loader is actually
- # entered; nothing is preloaded from it, which is the point.
+ case = Path(self.cases.name) / "case_no_preload"
proc, out = f.run_harness(case, "NONE",
env={_RUNTIME_DIR_VAR: str(f.runtime_a_dir)})
detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, proc.stderr)
self.assertEqual(proc.returncode, 0, "harness crashed" + detail)
self.assertEqual(out.get("preload_requested"), "0", detail)
- self.assertNotIn("preload_marker", out, detail)
- for key in ("runtime_before_count", "runtime_after_preload_count",
- "loader_init", "runtime_after_backend_count"):
- self.assertIn(key, out, "unparseable harness output" + detail)
- if key.endswith("_count"):
- self.assertGreaterEqual(int(out[key]), 0, detail)
-
- init = out["loader_init"]
- self.assertIn(init, ("0", "1"), detail)
- if init == "1":
- # Succeeded: something satisfied the import, and it must not be a
- # controlled runtime, because none was preloaded.
- self.assertIn("bound_marker", out, detail)
- for key in out:
- if key.startswith("runtime_after_backend_") and key[-1].isdigit():
- self.assertFalse(self._same_path(out[key], str(f.runtime_a)), detail)
- self.assertFalse(self._same_path(out[key], str(f.runtime_b)), detail)
- else:
- # Failed: backend_loader.c's own diagnostic is preserved, and no
- # claim is made about which transient module was rejected β€” it is
- # no longer loaded, so its path cannot be proven.
- self.assertIn("coli_hip.dll", proc.stderr or "", detail)
- self.assertNotIn("backend_path", out, detail)
+ self.assertEqual(out.get("runtime_before_count"), "0", detail)
+ self.assertEqual(out.get("runtime_after_preload_count"), "0", detail)
+
+ self.assertEqual(out.get("loader_init"), "1", detail)
+ self.assertEqual(out.get("bound_marker"), str(_RUNTIME_MARKER_A), detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "1", detail)
+ self.assertTrue(self._same_path(out["runtime_after_backend_0"],
+ str(f.runtime_a)), detail)
+ self.assertFalse(self._same_path(out["runtime_after_backend_0"],
+ str(f.runtime_b)), detail)
+ self.assertEqual(out.get("shutdown_called"), "1", detail)
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "0",
+ "production did not release its own reference" + detail)
def test_ambient_environment_is_recorded_without_inference(self):
"""Record whether a machine-wide runtime exists; prove nothing from it."""
@@ -1225,11 +1234,12 @@ class LoaderRuntimeDirContractTest(unittest.TestCase):
self.assertEqual(proc.returncode, 0, "harness crashed" + detail)
self.assertNotIn(_RUNTIME_DIR_VAR, err,
"a valid directory was rejected" + detail)
- self.assertIn("loader_init", out, detail)
- # Reaching the backend phase shows up one of exactly two ways.
- reached = (out.get("backend_loaded") == "1"
- or "coli_hip.dll could not be loaded" in err)
- self.assertTrue(reached, "never reached the backend load" + detail)
+ # Since enforcement landed this is no longer "we got as far as the
+ # backend": a valid directory means the runtime is loaded, verified and
+ # bound, so the whole path is deterministic.
+ self.assertEqual(out.get("loader_init"), "1", detail)
+ self.assertEqual(out.get("backend_loaded"), "1", detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "1", detail)
return proc, out
# --- rejected values ------------------------------------------------
@@ -1312,8 +1322,19 @@ class LoaderRuntimeDirContractTest(unittest.TestCase):
# --- configured vs. actually bound ----------------------------------
+ @staticmethod
+ def _same_path(a, b):
+ return os.path.normcase(os.path.normpath(a)) == \
+ os.path.normcase(os.path.normpath(b))
+
def test_configured_and_preloaded_runtime_agree(self):
- """Configured A, preloaded A: validation passes and A is bound."""
+ """Configured A, A already loaded: reused, and only one stays mapped.
+
+ Production takes its own counted reference to the very same module
+ rather than mapping a second copy β€” so the inventory shows one module
+ before and after, and after shutdown the harness's own reference is
+ what keeps it alive.
+ """
f = self.fixture
proc, out = f.run_harness(
Path(self.cases.name) / "agree", str(f.runtime_a),
@@ -1322,38 +1343,175 @@ class LoaderRuntimeDirContractTest(unittest.TestCase):
self.assertNotIn(_RUNTIME_DIR_VAR, proc.stderr or "", detail)
self.assertEqual(out.get("loader_init"), "1", detail)
self.assertEqual(out.get("bound_marker"), str(_RUNTIME_MARKER_A), detail)
+ # Reused, not duplicated.
+ self.assertEqual(out.get("runtime_after_preload_count"), "1", detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "1", detail)
+ # Production released only its own reference; the harness still holds one.
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "1",
+ "production released a reference it did not own" + detail)
+ self.assertTrue(self._same_path(out["runtime_after_shutdown_0"],
+ str(f.runtime_a)), detail)
+
+ def test_configured_a_with_b_preloaded_fails_closed(self):
+ """A wrong runtime is already loaded, so the GPU tier must not start.
+
+ Windows binds an import by base name against whatever is already
+ mapped, so with runtime B resident the backend would silently get B no
+ matter which file the configuration names. There is no safe way to
+ pick a winner here β€” the only honest outcome is to refuse before
+ anything GPU-related is loaded, and leave B exactly as it was found.
+ """
+ f = self.fixture
+ proc, out = f.run_harness(
+ Path(self.cases.name) / "wrong_runtime", str(f.runtime_b),
+ env={_RUNTIME_DIR_VAR: str(f.runtime_a_dir)})
+ err = proc.stderr or ""
+ detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, err)
+
+ self.assertEqual(proc.returncode, 0, "harness crashed" + detail)
+ self.assertEqual(out.get("preload_0_marker"), str(_RUNTIME_MARKER_B), detail)
+
+ # Fails closed, before the backend.
+ self.assertEqual(out.get("loader_init"), "0", detail)
+ self.assertEqual(out.get("backend_loaded"), "0", detail)
+ self.assertNotIn("backend_path", out, detail)
+ self.assertNotIn("bound_marker", out, detail)
- def test_configured_a_but_preloaded_b_still_binds_b(self):
- """TEMPORARY REGRESSION WITNESS for W1-B2d β€” not accepted behaviour.
+ # B is untouched and A was never loaded.
+ self.assertEqual(out.get("runtime_after_backend_count"), "1", detail)
+ self.assertTrue(self._same_path(out["runtime_after_backend_0"],
+ str(f.runtime_b)), detail)
+ self.assertFalse(self._same_path(out["runtime_after_backend_0"],
+ str(f.runtime_a)), detail)
+ # Nothing was released either: production never took a reference.
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "1", detail)
+
+ # The diagnostic names the mismatch and both sides of it.
+ self.assertIn("[HIP] a different amdhip64_7.dll is already loaded", err, detail)
+ self.assertIn("[HIP] configured:", err, detail)
+ self.assertIn("[HIP] already loaded:", err, detail)
+ self.assertIn("CPU path remains active", err, detail)
+ # It never reached the backend, so it must not talk about one.
+ self.assertNotIn("could not be loaded", err, detail)
- COLI_HIP_RUNTIME_DIR names runtime A and the directory validates, yet
- runtime B was already loaded under the same basename and B is what the
- backend ends up bound to. Validating the configuration proves the
- configuration is well-formed; it proves nothing about which runtime the
- process is actually using.
+ def test_two_preloaded_runtimes_fail_closed(self):
+ """Two same-basename runtimes resident: ambiguous, so refuse.
- This test exists to pin that gap in place so it cannot be mistaken for
- enforcement. W1-B2d must invert the assertion: once the loader checks
- the already-loaded module and verifies the bound path, this case has to
- fail closed with a diagnostic instead of silently binding B.
+ Even though one of them IS the configured file, which module an import
+ binds to is decided by load order rather than by us. Neither is
+ unloaded β€” they were not ours to unload.
"""
f = self.fixture
proc, out = f.run_harness(
- Path(self.cases.name) / "mismatch", str(f.runtime_b),
+ Path(self.cases.name) / "two_runtimes",
+ [str(f.runtime_a), str(f.runtime_b)],
env={_RUNTIME_DIR_VAR: str(f.runtime_a_dir)})
- detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, proc.stderr)
+ err = proc.stderr or ""
+ detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, err)
- # Configuration accepted: A really is a valid runtime directory.
- self.assertNotIn(_RUNTIME_DIR_VAR, proc.stderr or "", detail)
- self.assertEqual(out.get("preload_marker"), str(_RUNTIME_MARKER_B), detail)
- # ...and the mismatch goes through unreported.
+ self.assertEqual(proc.returncode, 0, "harness crashed" + detail)
+ self.assertEqual(out.get("preload_0_marker"), str(_RUNTIME_MARKER_A), detail)
+ self.assertEqual(out.get("preload_1_marker"), str(_RUNTIME_MARKER_B), detail)
+ self.assertEqual(out.get("runtime_after_preload_count"), "2", detail)
+
+ self.assertEqual(out.get("loader_init"), "0", detail)
+ self.assertEqual(out.get("backend_loaded"), "0", detail)
+ self.assertNotIn("bound_marker", out, detail)
+
+ # Both survive, untouched, and both remain distinct modules.
+ self.assertEqual(out.get("runtime_after_backend_count"), "2", detail)
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "2", detail)
+ paths = {out["runtime_after_backend_0"].lower(),
+ out["runtime_after_backend_1"].lower()}
+ self.assertEqual(len(paths), 2, detail)
+
+ self.assertIn("[HIP] 2 different amdhip64_7.dll modules", err, detail)
+ self.assertIn("[HIP] configured:", err, detail)
+ self.assertEqual(err.count("[HIP] already loaded:"), 2, detail)
+
+ def test_configured_through_a_hardlink_alias_is_accepted(self):
+ """Same physical file reached by another name: accept, do not compare text.
+
+ The configuration points at a hard link whose path text differs from
+ the module Windows reports, so a textual check would refuse a perfectly
+ correct setup. Physical identity says they are one file, and that is
+ what decides.
+ """
+ f = self.fixture
+ alias_dir = Path(self.cases.name) / "alias runtime dir"
+ alias_dir.mkdir(parents=True, exist_ok=True)
+ alias = alias_dir / _RUNTIME_BASENAME
+ try:
+ if not alias.exists():
+ os.link(f.runtime_a, alias)
+ except (OSError, NotImplementedError, AttributeError) as exc:
+ self.skipTest("hard links unavailable here: %s" % exc)
+
+ # Preload through the canonical name, configure through the alias.
+ proc, out = f.run_harness(
+ Path(self.cases.name) / "hardlink", str(f.runtime_a),
+ env={_RUNTIME_DIR_VAR: str(alias_dir)})
+ err = proc.stderr or ""
+ detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, err)
+
+ self.assertEqual(out.get("loader_init"), "1",
+ "a hard link to the configured file was refused" + detail)
+ self.assertEqual(out.get("bound_marker"), str(_RUNTIME_MARKER_A), detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "1", detail)
+ self.assertNotIn("is not the configured file", err, detail)
+
+ def test_unicode_executable_directory_loads_the_backend(self):
+ """The HIP backend path is built wide, so a non-ASCII install works.
+
+ The old ANSI construction went through the active code page and would
+ mangle exactly this directory name. The runtime stays in its own plain
+ directory, so what is under test is the backend path alone.
+ """
+ f = self.fixture
+ case = Path(self.cases.name) / "kΓΆrtid Ο€ sandbox"
+ proc, out = f.run_harness(case, "NONE",
+ env={_RUNTIME_DIR_VAR: str(f.runtime_a_dir)})
+ err = proc.stderr or ""
+ detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, err)
+
+ self.assertFalse(str(case).isascii())
self.assertEqual(out.get("loader_init"), "1", detail)
- self.assertEqual(out.get("bound_marker"), str(_RUNTIME_MARKER_B),
- "pre-B2d behaviour changed" + detail)
+ self.assertEqual(out.get("bound_marker"), str(_RUNTIME_MARKER_A), detail)
+ # The exact Unicode candidate, round-tripped intact through UTF-8.
+ self.assertTrue(self._same_path(out["backend_path"],
+ str(case / f.backend.name)), detail)
+ self.assertIn("kΓΆrtid Ο€", out["backend_path"], detail)
+ self.assertNotIn("?", out["backend_path"], detail)
self.assertEqual(out.get("runtime_after_backend_count"), "1", detail)
- self.assertTrue(
- os.path.normcase(os.path.normpath(out["runtime_after_backend_0"])) ==
- os.path.normcase(os.path.normpath(str(f.runtime_b))), detail)
+ self.assertNotIn("fallback", err, detail)
+
+ def test_configured_runtime_that_is_not_a_valid_pe_fails_before_the_backend(self):
+ """Structurally valid configuration, unloadable runtime: 193, no backend.
+
+ The file exists and is a file, so W1-B2c1 is satisfied; it is only when
+ the loader tries to map it that the truth appears. Nothing GPU-related
+ is attempted afterwards and nothing stays loaded.
+ """
+ f = self.fixture
+ bad_dir = Path(self.cases.name) / "bad runtime dir"
+ bad_dir.mkdir(parents=True, exist_ok=True)
+ shutil.copy2(f.bad_pe, bad_dir / _RUNTIME_BASENAME)
+
+ proc, out = f.run_harness(
+ Path(self.cases.name) / "bad_runtime", "NONE",
+ env={_RUNTIME_DIR_VAR: str(bad_dir)})
+ err = proc.stderr or ""
+ detail = "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, err)
+
+ self.assertEqual(out.get("loader_init"), "0", detail)
+ self.assertEqual(out.get("backend_loaded"), "0", detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "0", detail)
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "0", detail)
+ self.assertIn("[HIP] the configured amdhip64_7.dll could not be loaded", err, detail)
+ self.assertIn("Windows error 193", err, detail)
+ self.assertIn("invalid executable format or architecture", err, detail)
+ # The runtime never loaded, so the backend must not be mentioned.
+ self.assertNotIn("coli_hip.dll could not be loaded", err, detail)
class _HelperTestBase(unittest.TestCase):
@@ -1754,11 +1912,12 @@ class LoaderFailureDiagnosticTest(unittest.TestCase):
# Vendor and container are named, and the CPU fallback is promised.
self.assertIn("[HIP] coli_hip.dll could not be loaded", err, detail)
self.assertIn("CPU path remains active", err, detail)
- self.assertIn("[HIP] fallback by name coli_hip.dll:", err, detail)
self.assertNotIn("coli_cuda.dll", err, detail)
- # Both attempts happened, so both codes must be present.
- self.assertEqual(len(self._errors(err)), 2,
- "expected a primary and a fallback code" + detail)
+ # HIP has exactly one backend attempt now: the absolute
+ # executable-directory candidate. No fallback line, and no second code.
+ self.assertNotIn("fallback by name", err, detail)
+ self.assertEqual(len(self._errors(err)), 1,
+ "HIP should report exactly one backend error" + detail)
# Claims the loader cannot support.
self.assertNotIn("System32", err, detail)
self.assertNotIn("coli_cuda_", err, detail)
@@ -1779,14 +1938,18 @@ class LoaderFailureDiagnosticTest(unittest.TestCase):
# --- the four classes ---------------------------------------------
def test_absent_backend_is_reported_as_absent(self):
- """Nothing at the candidate path: say so, and give the codes."""
+ """Nothing at the candidate path: say so, and give the code.
+
+ Production loaded the runtime itself here, so the post-shutdown
+ inventory also proves that a failed backend load releases it again.
+ """
proc, out, err, detail = self._fail("absent", with_backend=False)
self.assertRegex(err, r"candidate .*coli_hip\.dll: does not exist", detail)
self.assertNotIn("exists as a file", err, detail)
- primary, fallback = self._errors(err)
- self.assertEqual(primary, 126, detail) # ERROR_MOD_NOT_FOUND
- self.assertEqual(fallback, 126, detail)
+ self.assertEqual(self._errors(err)[0], 126, detail) # ERROR_MOD_NOT_FOUND
self.assertIn("module or one of its dependencies was not found", err, detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "0",
+ "the runtime reference leaked past a backend failure" + detail)
self._assert_only_preloaded_runtime(out, detail, None)
def test_invalid_pe_is_not_reported_as_missing(self):
@@ -1796,9 +1959,9 @@ class LoaderFailureDiagnosticTest(unittest.TestCase):
"badpe", backend_override=f.bad_pe)
self.assertRegex(err, r"candidate .*coli_hip\.dll: exists as a file", detail)
self.assertNotIn("does not exist", err, detail)
- primary, _ = self._errors(err)
- self.assertEqual(primary, 193, detail) # ERROR_BAD_EXE_FORMAT
+ self.assertEqual(self._errors(err)[0], 193, detail) # ERROR_BAD_EXE_FORMAT
self.assertIn("invalid executable format or architecture", err, detail)
+ self.assertEqual(out.get("runtime_after_backend_count"), "0", detail)
def test_missing_dependency_is_not_reported_as_missing_backend(self):
"""The backend is right there; its dependency is not. That is 126.
@@ -1813,13 +1976,15 @@ class LoaderFailureDiagnosticTest(unittest.TestCase):
backend_override=f.backend_dep)
self.assertRegex(err, r"candidate .*coli_hip\.dll: exists as a file", detail)
self.assertNotIn("does not exist", err, detail)
- primary, _ = self._errors(err)
- self.assertEqual(primary, 126, detail) # ERROR_MOD_NOT_FOUND
+ self.assertEqual(self._errors(err)[0], 126, detail) # ERROR_MOD_NOT_FOUND
self.assertIn("module or one of its dependencies was not found", err, detail)
# The message must not name a culprit Windows never identified.
self.assertNotIn(_DEP_BASENAME, err, detail)
self.assertNotIn(_DEP_EXTRA, err, detail)
- self.assertEqual(out.get("preload_marker"), str(_RUNTIME_MARKER_A), detail)
+ self.assertEqual(out.get("preload_0_marker"), str(_RUNTIME_MARKER_A), detail)
+ # Production reused the harness's runtime and released only its own
+ # reference, so the harness-owned module survives.
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "1", detail)
self._assert_only_preloaded_runtime(out, detail, f.runtime_a)
def test_missing_procedure_is_reported_as_a_procedure_failure(self):
@@ -1837,13 +2002,13 @@ class LoaderFailureDiagnosticTest(unittest.TestCase):
"missing_proc", preload=str(f.runtime_a),
backend_override=f.backend_dep, extra_files=(f.dep_lean,))
self.assertRegex(err, r"candidate .*coli_hip\.dll: exists as a file", detail)
- primary, _ = self._errors(err)
- self.assertEqual(primary, 127, detail) # ERROR_PROC_NOT_FOUND
+ self.assertEqual(self._errors(err)[0], 127, detail) # ERROR_PROC_NOT_FOUND
self.assertIn("a required procedure was not found", err, detail)
self.assertNotIn("module or one of its dependencies was not found",
err, detail)
self.assertNotIn(_DEP_EXTRA, err, detail)
- self.assertEqual(out.get("preload_marker"), str(_RUNTIME_MARKER_A), detail)
+ self.assertEqual(out.get("preload_0_marker"), str(_RUNTIME_MARKER_A), detail)
+ self.assertEqual(out.get("runtime_after_shutdown_count"), "1", detail)
self._assert_only_preloaded_runtime(out, detail, f.runtime_a)
def test_the_four_classes_are_distinguishable_from_each_other(self):
@@ -1924,7 +2089,7 @@ class LoaderCudaModeIgnoresRuntimeDirTest(unittest.TestCase):
self.assertNotIn("[HIP]", proc.stderr or "", detail)
for key in ("loader_init", "backend_loaded", "bound_marker",
- "preload_marker", "runtime_after_backend_count"):
+ "preload_0_marker", "runtime_after_backend_count"):
self.assertEqual(baseline.get(key), poisoned.get(key),
"%s diverged" % key + detail)
@@ -1950,6 +2115,96 @@ class LoaderCudaModeIgnoresRuntimeDirTest(unittest.TestCase):
self.assertNotIn(_RUNTIME_DIR_VAR, err, detail)
+class LoaderProductionStructureTest(unittest.TestCase):
+ """Source-order facts that a behavioural test cannot pin down.
+
+ Reads c/backend_loader.c only β€” no build, no subprocess, and it runs on any
+ platform. These guard the ordering guarantees the runtime tests depend on
+ but cannot themselves observe from outside the process.
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ cls.src = (HERE / "backend_loader.c").read_text(encoding="utf-8",
+ errors="replace")
+
+ def _index(self, needle):
+ at = self.src.find(needle)
+ self.assertNotEqual(at, -1, "not found in backend_loader.c: %s" % needle)
+ return at
+
+ def test_post_backend_verification_precedes_symbol_resolution(self):
+ """Verify the runtime again after the backend maps, before GetProcAddress.
+
+ Loading the backend pulls in its dependencies and is the one moment a
+ second amdhip64_7.dll could appear. Checking afterwards is what makes
+ the binding claim true; checking before symbol resolution is what stops
+ an unverifiable backend from being used at all.
+ """
+ load = self._index("g_cuda.dll = coli_hip_load_backend();")
+ verify = self.src.find('coli_hip_verify_bound(&cfg, runtime, "post-backend', load)
+ self.assertNotEqual(verify, -1, "no post-backend verification after the load")
+ resolve = self.src.find("RESOLVE(init,", verify)
+ self.assertNotEqual(resolve, -1, "no symbol resolution after verification")
+ self.assertLess(load, verify)
+ self.assertLess(verify, resolve)
+
+ def test_runtime_is_verified_before_the_backend_is_loaded(self):
+ """The pre-backend check happens inside acquisition, not after it."""
+ acquire = self._index("runtime = coli_hip_acquire_runtime(&cfg);")
+ load = self._index("g_cuda.dll = coli_hip_load_backend();")
+ self.assertLess(acquire, load)
+ self.assertIn('coli_hip_verify_bound(cfg, runtime, "runtime verification")',
+ self.src)
+
+ def test_hip_loads_only_one_backend_candidate(self):
+ """No bare-name fallback anywhere in the HIP path."""
+ hip = self.src[self._index("static HMODULE coli_hip_load_backend(void)"):]
+ hip = hip[:hip.index("\n#endif /* COLI_HIP_DLL */")]
+ self.assertEqual(hip.count("LoadLibraryExW("), 1, hip)
+ self.assertIn("LOAD_WITH_ALTERED_SEARCH_PATH", hip)
+ self.assertNotIn("LOAD_LIBRARY_SEARCH_SYSTEM32", hip)
+ self.assertNotIn("LOAD_LIBRARY_SEARCH_APPLICATION_DIR", hip)
+ self.assertNotIn("fallback", hip)
+ # Unicode throughout: no ANSI loader call and no fixed path buffer.
+ self.assertNotIn("LoadLibraryExA(", hip)
+ self.assertNotIn("MAX_PATH", hip)
+
+ def test_cuda_keeps_exactly_its_two_historical_attempts(self):
+ """CUDA is the control and must be untouched by HIP hardening."""
+ self.assertEqual(self.src.count("LoadLibraryExA("), 2)
+ primary = self._index(
+ "g_cuda.dll = LoadLibraryExA(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);")
+ fallback = self._index("g_cuda.dll = LoadLibraryExA(COLI_BACKEND_DLL, NULL,")
+ self.assertLess(primary, fallback, "the two attempts changed order")
+ tail = self.src[fallback:fallback + 200]
+ self.assertIn("LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32",
+ tail)
+ # Both errors still captured separately, immediately after each call.
+ self.assertIn("if(!g_cuda.dll) primary_err = GetLastError();", self.src)
+ self.assertIn("if(!g_cuda.dll) fallback_err = GetLastError();", self.src)
+
+ def test_runtime_reference_is_transferred_only_after_everything_passed(self):
+ """g_cuda.hip_runtime is assigned once, past the last failure gate."""
+ self.assertEqual(self.src.count("g_cuda.hip_runtime = runtime;"), 1)
+ transfer = self._index("g_cuda.hip_runtime = runtime;")
+ self.assertLess(self._index("RESOLVE(tensor_update, fn_tensor_update)"), transfer)
+ self.assertLess(transfer, self._index("g_cuda.available = 1;"))
+
+ def test_shutdown_releases_backend_before_runtime(self):
+ """Order matters: the backend imports the runtime, so it goes first."""
+ body = self.src[self._index("void coli_cuda_shutdown(void){"):]
+ body = body[:body.index("\n}")]
+ self.assertLess(body.index("FreeLibrary(g_cuda.dll)"),
+ body.index("FreeLibrary(g_cuda.hip_runtime)"))
+
+ def test_no_helper_is_publicly_exported(self):
+ """Internal by construction: static, no header, no dllexport."""
+ self.assertNotIn("__declspec(dllexport)", self.src)
+ self.assertEqual(len(re.findall(r"^(?:int|void|wchar_t)\s*\*?coli_win32_loader_",
+ self.src, re.M)), 0)
+
+
class LoaderBackendSelectionTest(unittest.TestCase):
"""Which backend DLL does a Windows host look for, and how does it label its
own loader messages?
@@ -1993,6 +2248,26 @@ class LoaderBackendSelectionTest(unittest.TestCase):
)
cls._hosts[key] = built
+ # Since enforcement landed, a HIP host loads the configured runtime
+ # before it ever looks for the backend, so the placeholder file this
+ # class used to drop in the sandbox is no longer enough β€” it has to be
+ # a real, loadable DLL. It is a six-line stub with no GPU content, no
+ # DllMain and no vendor header, built with the gcc this class already
+ # requires, and it is never loaded by Python.
+ cls._runtime_tmp = tempfile.TemporaryDirectory(prefix="coli host runtime ")
+ src = Path(cls._runtime_tmp.name) / "stub.c"
+ src.write_text(
+ "/* generated stub runtime for the host matrix: no GPU work. */\n"
+ "__declspec(dllexport) int coli_test_runtime_marker(void)\n"
+ "{ return 0; }\n", encoding="ascii")
+ cls._stub_runtime = Path(cls._runtime_tmp.name) / _RUNTIME_BASENAME
+ proc = subprocess.run(
+ ["gcc", "-O0", "-shared", str(src), "-o", str(cls._stub_runtime)],
+ text=True, errors="replace", capture_output=True, timeout=300)
+ if proc.returncode != 0 or not cls._stub_runtime.is_file():
+ raise unittest.SkipTest(
+ "could not build the host-matrix stub runtime: %s" % proc.stderr[-400:])
+
@classmethod
def tearDownClass(cls):
for path in cls._hosts.values():
@@ -2001,6 +2276,9 @@ class LoaderBackendSelectionTest(unittest.TestCase):
except OSError:
pass
cls._hosts.clear()
+ if getattr(cls, "_runtime_tmp", None) is not None:
+ cls._runtime_tmp.cleanup()
+ cls._runtime_tmp = None
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
@@ -2019,13 +2297,12 @@ class LoaderBackendSelectionTest(unittest.TestCase):
``COLI_CUDA=1`` request set below and tests the not-requested contract
instead. The default removes nothing, so existing callers are unchanged.
- Since W1-B2c1 a HIP host refuses to reach the backend at all without a
- valid COLI_HIP_RUNTIME_DIR, so the sandbox gets one: a throwaway
- directory holding an empty placeholder file of the right name. The
- loader only asks whether that file exists β€” it never opens, maps or
- loads it β€” so this stays a CPU-only test with no HIP SDK and no GPU.
- The ambient value is dropped either way, so a developer's real ROCm
- install cannot influence the result.
+ A HIP host refuses to reach the backend without a valid
+ COLI_HIP_RUNTIME_DIR, and since W1-B2d2 it also loads and verifies what
+ that directory names β€” so the sandbox gets a throwaway directory
+ holding the generated stub runtime built in setUpClass. No HIP SDK, no
+ ROCm and no GPU is involved; the ambient variable is dropped either
+ way, so a developer's real install cannot influence the result.
"""
# One sandbox per host, so a test may exercise both in a single method.
sandbox = Path(self.tmp.name) / ("bin_" + key)
@@ -2038,7 +2315,7 @@ class LoaderBackendSelectionTest(unittest.TestCase):
if key == "hip":
runtime_dir = sandbox / "runtime"
runtime_dir.mkdir(exist_ok=True)
- (runtime_dir / _RUNTIME_BASENAME).write_bytes(b"")
+ shutil.copy2(self._stub_runtime, runtime_dir / _RUNTIME_BASENAME)
merged[_RUNTIME_DIR_VAR] = str(runtime_dir)
for name in unset:
merged.pop(name, None)
@@ -2066,9 +2343,12 @@ class LoaderBackendSelectionTest(unittest.TestCase):
"""
err = self._run_isolated("hip").stderr or ""
self.assertIn("[HIP] coli_hip.dll could not be loaded", err)
- self.assertIn("[HIP] fallback by name coli_hip.dll:", err)
self.assertIn("does not exist", err)
self.assertNotIn("coli_cuda.dll", err)
+ # HIP loads only the absolute executable-directory candidate, so there
+ # is no by-name fallback line to print and no System32 involvement.
+ self.assertNotIn("fallback by name", err)
+ self.assertNotIn("System32", err)
def test_cuda_dll_host_with_coli_cuda_unset_stays_on_cpu(self):
"""CUDA_DLL host, COLI_CUDA absent: no request, no loader, no message.
diff --git a/docs/windows.md b/docs/windows.md
index 454c84d8..c46c803b 100644
--- a/docs/windows.md
+++ b/docs/windows.md
@@ -181,7 +181,7 @@ available.
β†’ CUDA attention+dense 0.72 β†’ **1.07 tok/s** with the GPU-resident pipeline at
decode ([#273](https://github.com/JustVugg/colibri/issues/273), merged in #274).
-## AMD GPU (experimental, build only)
+## AMD GPU (experimental)
The AMD sibling of the CUDA split above, and the same reasoning: MinGW gcc
cannot compile `.cu`, and Windows hipcc targets the MSVC ABI, so the backend is
@@ -189,10 +189,13 @@ built into a standalone `coli_hip.dll` rather than linked into the host. The
same `c/backend_cuda.cu` and the same `coli_cuda_*` ABI the Linux HIP path
already reuses are used unchanged β€” see [GPU_BACKENDS.md](../GPU_BACKENDS.md).
-> **This is build support, not a working GPU tier yet.** The host does not load
-> `coli_hip.dll` at runtime, so there is no end-to-end AMD GPU inference on
-> Windows today. Building the DLL verifies that it compiles, exports and links;
-> wiring it into the engine is separate follow-up work.
+> **Still experimental: no validated end-to-end AMD GPU inference on Windows.**
+> The host now has a complete runtime-loading path β€” it resolves and loads
+> `coli_hip.dll`, and enforces which HIP runtime that DLL binds to β€” but none
+> of it has been exercised against real hardware yet. Nothing here claims
+> working inference, verified token generation, or upstream acceptance. Real
+> `gfx1151` validation is the next step, and until it happens treat this as a
+> developer-facing path only. The CPU path is unaffected and always available.
### What you need
@@ -237,6 +240,57 @@ git-ignored and removed by `make -C c clean`.
> coverage** for this path, because no hosted runner ships a Windows HIP
> toolchain.
+### Runtime setup (experimental, developer-facing)
+
+Running a HIP host needs two things in place. Both are checked before any GPU
+work starts, and anything that cannot be proven disables the GPU tier rather
+than guessing β€” the engine continues on the CPU path.
+
+**1. `coli_hip.dll` next to `colibri.exe`.** The HIP host loads *only* that
+exact absolute path. There is deliberately no bare-name fallback and no
+System32 search: a fallback could let some other `coli_hip.dll` satisfy the
+load and quietly undo the runtime check below.
+
+**2. `COLI_HIP_RUNTIME_DIR` naming the HIP runtime.** An absolute path to the
+directory containing `amdhip64_7.dll`:
+
+```powershell
+$env:COLI_HIP_RUNTIME_DIR = "C:\path\to\hip\bin"
+$env:COLI_CUDA = "1"
+.\colibri.exe
+```
+
+Relative, drive-relative (`C:runtime`) and rooted-without-drive (`\runtime`)
+values are rejected, because resolving them against the current directory is
+exactly the ambiguity the setting exists to remove. Spaces, non-ASCII
+characters and a trailing separator are all fine.
+
+Why it is mandatory: Windows resolves a DLL import by **base name** against
+whatever is already loaded, and a machine can easily carry more than one
+`amdhip64_7.dll` (a system-wide ROCm install plus whatever you unpacked). Left
+to chance, the backend binds to whichever one happened to load first. So the
+loader refuses to start the GPU tier when it finds:
+
+- a **different** `amdhip64_7.dll` already loaded than the one configured
+- **more than one** `amdhip64_7.dll` loaded, even if one of them is the right
+ file β€” which module an import binds to is decided by load order
+- a loaded-module list it could not read, or a file whose identity it could
+ not establish
+
+Files are compared by **physical identity**, not by path text, so reaching the
+configured runtime through a hard link or a differently-spelled path is
+accepted.
+
+One limitation stated plainly: the runtime is verified *after* it is mapped.
+`LoadLibraryExW` takes a path rather than an open handle, so a file swapped
+between validation and loading is **detected, not prevented**. Long paths
+beyond `MAX_PATH` no longer hit a fixed buffer in the loader, but whether they
+work still depends on your process manifest and Windows policy, and that has
+not been validated on hardware here.
+
+**CUDA is unaffected.** A `CUDA_DLL=1` host ignores `COLI_HIP_RUNTIME_DIR`
+entirely and keeps its existing `coli_cuda.dll` search behaviour unchanged.
+
### AMD failure index
| Symptom | Cause | Fix |