Repository navigation
Expand file tree
/
Copy pathNVMTLForward.m
More file actions
273 lines (249 loc) · 10.9 KB
/
Copy pathNVMTLForward.m
File metadata and controls
273 lines (249 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* NullMoth NVIDIA driver for macOS
* Copyright (c) 2026 NullMoth Systems.
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
*/
#include <unistd.h>
#import <objc/runtime.h>
#import <objc/message.h>
#import <os/lock.h>
static os_unfair_lock gFwdLock = OS_UNFAIR_LOCK_INIT;
static CFMutableDictionaryRef gEncCache;
static CFMutableDictionaryRef gSeen;
static int gIndexBuilt = 0;
typedef NSMethodSignature *(*nvfwd_msfs_fn)(id, SEL, SEL);
static struct { __unsafe_unretained Class cls; nvfwd_msfs_fn orig; } gOrig[32];
static int gNOrig = 0;
static nvfwd_msfs_fn nvfwd_original_for(Class c)
{
for (Class k = c; k; k = class_getSuperclass(k))
for (int i = 0; i < gNOrig; i++)
if (gOrig[i].cls == k) return gOrig[i].orig;
return NULL;
}
static const char *const kNVCounterpart[][2] = {
{"NVMTLDevice", "MTLIOAccelDevice"},
{"NVMTLCommandQueue", "MTLIOAccelCommandQueue"},
{"NVMTLCommandBuffer", "MTLIOAccelCommandBuffer"},
{"NVMTLBuffer", "MTLIOAccelBuffer"},
{"NVMTLTexture", "MTLIOAccelTexture"},
{"NVMTLRenderCommandEncoder", "MTLIOAccelRenderCommandEncoder"},
{"NVMTLBlitCommandEncoder", "MTLIOAccelBlitCommandEncoder"},
{"NVMTLAccelerationStructureCommandEncoder", "_MTLAccelerationStructureCommandEncoder"},
{"NVMTLAccelerationStructure", "MTLIOAccelAccelerationStructure"},
{"NVMTLComputeCommandEncoder", "MTLIOAccelComputeCommandEncoder"},
{"NVMTLHeap", "MTLIOAccelHeap"},
{"NVMTLFence", "MTLIOAccelFence"},
{"NVMTLSamplerState", "MTLIOAccelSamplerState"},
{"NVMTLDepthStencilState", "MTLIOAccelDepthStencilState"},
{"NVMTLLibrary", "MTLLibrary"},
{"NVMTLFunction", "MTLFunction"},
{"NVMTLArgumentEncoder", "MTLArgumentEncoder"},
{"NVMTLRenderPipelineState", "MTLRenderPipelineState"},
{"NVMTLComputePipelineState", "MTLComputePipelineState"},
{"NVMTLIndirectCommandBuffer", "MTLIndirectCommandBuffer"},
{NULL, NULL}
};
static const char *nvfwd_counterpart_encoding(Class c, SEL sel)
{
const char *cn = class_getName(c);
if (!cn) return NULL;
for (int i = 0; kNVCounterpart[i][0]; i++) {
if (strcmp(cn, kNVCounterpart[i][0])) continue;
const char *an = kNVCounterpart[i][1];
Class ac = objc_getClass(an);
if (ac) {
Method m = class_getInstanceMethod(ac, sel);
if (m) return method_getTypeEncoding(m);
}
Protocol *p = objc_getProtocol(an);
if (p) {
for (int req = 0; req < 2; req++)
for (int inst = 0; inst < 1; inst++) {
struct objc_method_description d =
protocol_getMethodDescription(p, sel, req == 0, YES);
if (d.name && d.types) return d.types;
}
}
return NULL;
}
return NULL;
}
static void nvfwd_index_class(Class c)
{
unsigned mc = 0;
Method *ms = class_copyMethodList(c, &mc);
for (unsigned j = 0; j < mc; j++) {
const char *sn = sel_getName(method_getName(ms[j]));
const char *te = method_getTypeEncoding(ms[j]);
if (!sn || !te) continue;
CFStringRef k = CFStringCreateWithCString(NULL, sn, kCFStringEncodingUTF8);
if (k) {
if (!CFDictionaryContainsKey(gEncCache, k)) {
CFStringRef v = CFStringCreateWithCString(NULL, te, kCFStringEncodingUTF8);
if (v) { CFDictionarySetValue(gEncCache, k, v); CFRelease(v); }
}
CFRelease(k);
}
}
free(ms);
}
static void nvfwd_build_index(void)
{
if (gIndexBuilt) return;
gIndexBuilt = 1;
int nclass = 0, nproto = 0;
unsigned n = 0;
Class *all = objc_copyClassList(&n);
for (unsigned i = 0; i < n; i++) {
const char *cn = class_getName(all[i]);
if (!cn) continue;
if (strncmp(cn, "MTL", 3) && strncmp(cn, "AGX", 3) && strncmp(cn, "AMD", 3) &&
strncmp(cn, "IOAccel", 7) && strncmp(cn, "CA", 2) && strncmp(cn, "Apple", 5)) continue;
nvfwd_index_class(all[i]);
nclass++;
}
free(all);
unsigned np = 0;
Protocol *__unsafe_unretained *ps = objc_copyProtocolList(&np);
for (unsigned i = 0; i < np; i++) {
const char *pn = protocol_getName(ps[i]);
if (!pn || strncmp(pn, "MTL", 3)) continue;
nproto++;
for (int req = 0; req < 2; req++) {
unsigned dc = 0;
struct objc_method_description *ds =
protocol_copyMethodDescriptionList(ps[i], req == 0, YES, &dc);
for (unsigned j = 0; j < dc; j++) {
const char *sn = ds[j].name ? sel_getName(ds[j].name) : NULL;
const char *te = ds[j].types;
if (!sn || !te) continue;
CFStringRef k = CFStringCreateWithCString(NULL, sn, kCFStringEncodingUTF8);
if (k) {
if (!CFDictionaryContainsKey(gEncCache, k)) {
CFStringRef v = CFStringCreateWithCString(NULL, te, kCFStringEncodingUTF8);
if (v) { CFDictionarySetValue(gEncCache, k, v); CFRelease(v); }
}
CFRelease(k);
}
}
free(ds);
}
}
free(ps);
nvlog("NVMTLForward: encoding index built from %d classes + %d protocols, %ld selectors",
nclass, nproto, (long)CFDictionaryGetCount(gEncCache));
}
static NSMethodSignature *nvfwd_synthesize(SEL sel)
{
const char *sn = sel_getName(sel);
int colons = 0;
for (const char *p = sn; *p; p++) if (*p == ':') colons++;
char enc[80];
if (colons > 60) return nil;
int o = 0;
enc[o++] = '@'; enc[o++] = '@'; enc[o++] = ':';
for (int i = 0; i < colons; i++) enc[o++] = '@';
enc[o] = 0;
nvlog("NVMTLForward: SYNTHESIZED signature \"%s\" for %s — no class or protocol declares it", enc, sn);
return [NSMethodSignature signatureWithObjCTypes:enc];
}
static NSMethodSignature *nvfwd_msfs(id self, SEL _cmd, SEL sel)
{
nvfwd_msfs_fn orig = nvfwd_original_for(object_getClass(self));
if (orig) {
NSMethodSignature *s = orig(self, _cmd, sel);
if (s) return s;
}
const char *cte = nvfwd_counterpart_encoding(object_getClass(self), sel);
if (cte) return [NSMethodSignature signatureWithObjCTypes:cte];
const char *sn = sel_getName(sel);
CFStringRef key = CFStringCreateWithCString(NULL, sn, kCFStringEncodingUTF8);
if (!key) return nil;
os_unfair_lock_lock(&gFwdLock);
nvfwd_build_index();
CFStringRef enc = (CFStringRef)CFDictionaryGetValue(gEncCache, key);
os_unfair_lock_unlock(&gFwdLock);
CFRelease(key);
if (enc) {
char buf[512];
if (CFStringGetCString(enc, buf, sizeof buf, kCFStringEncodingUTF8))
return [NSMethodSignature signatureWithObjCTypes:buf];
}
return nvfwd_synthesize(sel);
}
static void nvfwd_forward(id self, SEL _cmd, NSInvocation *inv)
{
SEL sel = [inv selector];
const char *cn = class_getName(object_getClass(self));
const char *sn = sel_getName(sel);
char key[320];
snprintf(key, sizeof key, "%s %s", cn, sn);
long count = 0;
CFStringRef k = CFStringCreateWithCString(NULL, key, kCFStringEncodingUTF8);
if (k) {
os_unfair_lock_lock(&gFwdLock);
if (!gSeen) gSeen = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, NULL);
count = (long)(intptr_t)CFDictionaryGetValue(gSeen, k) + 1;
CFDictionarySetValue(gSeen, k, (const void *)(intptr_t)count);
os_unfair_lock_unlock(&gFwdLock);
CFRelease(k);
}
if (count <= 3 || (count % 1000) == 0) {
extern const char *nvmtl_log_path(void);
FILE *lf = fopen(nvmtl_log_path(), "a");
const char *rt_ = [[inv methodSignature] methodReturnType];
static const char *const kApple_[] = { "_MTLFunctionInternal", "_MTLFunction", "_MTLLibrary",
"_MTLDevice", "_MTLCommandQueue", "_MTLCommandBuffer", NULL };
for (int ci_ = 0; kApple_[ci_]; ci_++) {
Class ac_ = objc_getClass(kApple_[ci_]);
Method am_ = ac_ ? class_getInstanceMethod(ac_, [inv selector]) : NULL;
const char *te_ = am_ ? method_getTypeEncoding(am_) : NULL;
if (te_) { rt_ = te_; break; }
}
int deref_ = rt_ && (rt_[0] == '^' || (rt_[0] == 'r' && rt_[1] == '^'));
if (lf) { fprintf(lf, "pid %d NVMTL: SPI-STUB -[%s %s] (call #%ld, zero-returned — NOT IMPLEMENTED)%s\n", getpid(), cn, sn, count,
deref_ ? " POINTER RETURN — a caller that dereferences this NULL will crash" : ""); fclose(lf); }
}
NSUInteger len = [[inv methodSignature] methodReturnLength];
if (len) {
void *z = calloc(1, len);
if (z) { [inv setReturnValue:z]; free(z); }
}
}
static const char *const kNVFwdClasses[] = {
"NVMTLDevice", "NVMTLCommandQueue", "NVMTLCommandBuffer", "NVMTLBuffer", "NVMTLTexture",
"NVMTLLibrary", "NVMTLFunction", "NVMTLArgumentEncoder", "NVMTLRenderPipelineState",
"NVMTLRenderCommandEncoder", "NVMTLBlitCommandEncoder", "NVMTLComputeCommandEncoder",
"NVMTLAccelerationStructureCommandEncoder", "NVMTLAccelerationStructure",
"NVMTLComputePipelineState", "NVMTLDepthStencilState", "NVMTLSamplerState", "NVMTLFence",
"NVMTLHeap", "NVMTLIndirectCommand", "NVMTLIndirectCommandBuffer",
"NVMTLParallelRenderCommandEncoder", "NVMTLRenderPipelineReflection", "NVMTLComputePipelineReflection", "NVMTLArgument",
"NVMTLStructType", "NVMTLStructMember", "NVMTLPointerType", "NVMTLTextureReferenceType", "NVMTLVertexAttribute",
NULL
};
@interface NVMTLForwardInstaller : NSObject @end
@implementation NVMTLForwardInstaller
+ (void)load
{
gEncCache = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
int installed = 0;
for (int i = 0; kNVFwdClasses[i]; i++) {
Class c = objc_getClass(kNVFwdClasses[i]);
if (!c) { nvlog("NVMTLForward: class %s not found — NOT protected", kNVFwdClasses[i]); continue; }
if (gNOrig < (int)(sizeof gOrig / sizeof gOrig[0])) {
nvfwd_msfs_fn prev = (nvfwd_msfs_fn)class_getMethodImplementation(
c, @selector(methodSignatureForSelector:));
if (prev == (nvfwd_msfs_fn)nvfwd_msfs) prev = NULL;
gOrig[gNOrig].cls = c;
gOrig[gNOrig].orig = prev;
gNOrig++;
}
class_replaceMethod(c, @selector(methodSignatureForSelector:), (IMP)nvfwd_msfs, "@@::");
class_replaceMethod(c, @selector(forwardInvocation:), (IMP)nvfwd_forward, "v@:@");
installed++;
}
nvlog("NVMTLForward: forwarding net installed on %d classes (was: log-then-abort on 20)", installed);
}
@end