mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
tools: fix bounded UUID parsing in stock_uvm_register_probe and harden P2P/fault harnesses
This commit is contained in:
@ -124,7 +124,12 @@ int main(void) {
|
||||
|
||||
uint32_t *h_buf = (uint32_t *)malloc(BYTES);
|
||||
uint32_t *h_out = (uint32_t *)malloc(BYTES);
|
||||
if (!h_buf || !h_out) return 2;
|
||||
if (!h_buf || !h_out) {
|
||||
fprintf(stderr, "malloc(%zu) failed\n", (size_t)BYTES);
|
||||
free(h_buf);
|
||||
free(h_out);
|
||||
return 2;
|
||||
}
|
||||
|
||||
for (int g = 0; g < ngpus && rc == 0; g++) {
|
||||
rc = p_cuDeviceGet(&devs[g], g);
|
||||
|
||||
@ -120,8 +120,48 @@ static int rm_alloc(int ctl, uint32_t root, uint32_t parent, uint32_t obj,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hex_nibble(unsigned char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9') return (int)(c - '0');
|
||||
if (c >= 'a' && c <= 'f') return (int)(c - 'a' + 10);
|
||||
if (c >= 'A' && c <= 'F') return (int)(c - 'A' + 10);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int parse_gid_uuid(const uint8_t *data, size_t data_len, uint8_t out_uuid[16])
|
||||
{
|
||||
memset(out_uuid, 0, 16);
|
||||
if (data_len >= 4 && memcmp(data, "GPU-", 4) == 0) {
|
||||
size_t pos = 4;
|
||||
int out_i = 0;
|
||||
while (pos < data_len && data[pos] != '\0' && out_i < 16) {
|
||||
if (data[pos] == '-') {
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
if (pos + 1 >= data_len || data[pos + 1] == '\0')
|
||||
return -1;
|
||||
int hi = hex_nibble(data[pos]);
|
||||
int lo = hex_nibble(data[pos + 1]);
|
||||
if (hi < 0 || lo < 0)
|
||||
return -1;
|
||||
out_uuid[out_i++] = (uint8_t)((hi << 4) | lo);
|
||||
pos += 2;
|
||||
}
|
||||
return (out_i == 16) ? 0 : -1;
|
||||
}
|
||||
if (data_len < 16)
|
||||
return -1;
|
||||
memcpy(out_uuid, data, 16);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
|
||||
fprintf(stderr, "usage: %s [/dev/stock-nvidia-uvm]\n", argv[0]);
|
||||
return 2;
|
||||
}
|
||||
const char *uvm_path = (argc > 1) ? argv[1] : "/dev/stock-nvidia-uvm";
|
||||
int ctl = open("/dev/nvidiactl", O_RDWR);
|
||||
if (ctl < 0) {
|
||||
@ -175,32 +215,22 @@ int main(int argc, char **argv)
|
||||
ctrl.cmd = 0x2080014aU; /* NV2080_CTRL_CMD_GPU_GET_GID_INFO */
|
||||
ctrl.params = (uint64_t)(uintptr_t)&gid;
|
||||
ctrl.paramsSize = sizeof(gid);
|
||||
if (ioctl(ctl, _IOWR(NV_IOCTL_MAGIC, NV_ESC_RM_CONTROL, nv_rm_control_t), &ctrl) < 0 ||
|
||||
ctrl.status != 0) {
|
||||
fprintf(stderr, "GET_GID_INFO failed: errno=%d status=0x%x\n", errno, ctrl.status);
|
||||
int gid_io_rc = ioctl(ctl, _IOWR(NV_IOCTL_MAGIC, NV_ESC_RM_CONTROL, nv_rm_control_t), &ctrl);
|
||||
if (gid_io_rc < 0 || ctrl.status != 0) {
|
||||
fprintf(stderr, "GET_GID_INFO failed: errno=%d status=0x%x\n",
|
||||
(gid_io_rc < 0 ? errno : 0), ctrl.status);
|
||||
close(gpu_fd);
|
||||
close(ctl);
|
||||
return 5;
|
||||
}
|
||||
|
||||
uint8_t uuid_bin[16];
|
||||
memset(uuid_bin, 0, sizeof(uuid_bin));
|
||||
if (memcmp(gid.data, "GPU-", 4) == 0) {
|
||||
const char *p = (const char *)gid.data + 4;
|
||||
int out_i = 0;
|
||||
while (*p && out_i < 16) {
|
||||
if (*p == '-') {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
unsigned int byte = 0;
|
||||
if (sscanf(p, "%2x", &byte) != 1)
|
||||
break;
|
||||
uuid_bin[out_i++] = (uint8_t)byte;
|
||||
p += 2;
|
||||
}
|
||||
} else {
|
||||
memcpy(uuid_bin, gid.data, 16);
|
||||
size_t gid_len = gid.length < sizeof(gid.data) ? (size_t)gid.length : sizeof(gid.data);
|
||||
if (parse_gid_uuid(gid.data, gid_len, uuid_bin) != 0) {
|
||||
fprintf(stderr, "GET_GID_INFO returned malformed/truncated UUID (length=%u)\n", gid.length);
|
||||
close(gpu_fd);
|
||||
close(ctl);
|
||||
return 5;
|
||||
}
|
||||
|
||||
printf("GPU0 UUID (bin): ");
|
||||
@ -244,8 +274,11 @@ int main(int argc, char **argv)
|
||||
memset(&unreg_p, 0, sizeof(unreg_p));
|
||||
memcpy(unreg_p.gpu_uuid, uuid_bin, 16);
|
||||
int urc = ioctl(uvm, UVM_UNREGISTER_GPU, &unreg_p);
|
||||
printf("UVM_UNREGISTER_GPU: rc=%d rmStatus=0x%x\n", urc, unreg_p.rmStatus);
|
||||
printf("UVM_UNREGISTER_GPU: rc=%d errno=%d rmStatus=0x%x\n",
|
||||
urc, (urc < 0 ? errno : 0), unreg_p.rmStatus);
|
||||
fflush(stdout);
|
||||
if (urc != 0 || unreg_p.rmStatus != 0)
|
||||
ret = 9;
|
||||
}
|
||||
|
||||
close(uvm);
|
||||
|
||||
@ -116,6 +116,10 @@ static void *must_sym(void *h, const char *name) {
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
|
||||
fprintf(stderr, "usage: %s [pci-bdf]\n", argv[0]);
|
||||
return 2;
|
||||
}
|
||||
const char *bdf = (argc > 1) ? argv[1] : "";
|
||||
void *lib = dlopen("libcuda.so.1", RTLD_NOW | RTLD_GLOBAL);
|
||||
if (!lib) {
|
||||
|
||||
Reference in New Issue
Block a user