mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
tools: guard unchecked host allocations, NUL-terminate NVRTC PTX, and replace sprintf in C parity suites
- tools/cuda_parity_suite.c: add must_malloc/must_calloc helpers guarding all 11 previously unchecked host allocations (memset, cuBLAS SGEMM/TensorCore/batched, cuFFT 1D/2D, cuRAND, cuBLASLt, NVRTC PTX), NUL-terminate NVRTC PTX buffer, and replace sprintf with bounded snprintf for UUID hex formatting. - tools/cuda_ddp_training_parity.c & tools/cuda_ml_training_parity.c: add must_malloc/must_calloc guarding NVRTC compile log, PTX buffer (allocated as ptx_sz + 1 and explicitly NUL-terminated), and host weight buffers; destroy nvrtcProgram on compile failure before returning. - tools/cuda_multigpu_parity.c: add must_malloc/must_calloc guarding h_tmp, h_expected_sum, h_in[g], and h_out[g].
This commit is contained in:
@ -57,6 +57,24 @@ static void *must_sym(void *lib, const char *name) {
|
||||
return sym;
|
||||
}
|
||||
|
||||
static void *must_malloc(size_t bytes) {
|
||||
void *p = malloc(bytes ? bytes : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "malloc(%zu) failed\n", bytes);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *must_calloc(size_t nmemb, size_t size) {
|
||||
void *p = calloc(nmemb ? nmemb : 1, size ? size : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "calloc(%zu, %zu) failed\n", nmemb, size);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
#define CHECK_CU(expr) \
|
||||
do { \
|
||||
CUresult _r = (expr); \
|
||||
@ -784,16 +802,18 @@ int main(void) {
|
||||
if (compile_rc != 0) {
|
||||
size_t log_sz = 0;
|
||||
nvrtcGetProgramLogSize(prog, &log_sz);
|
||||
char *log = calloc(log_sz + 1, 1);
|
||||
char *log = must_calloc(log_sz + 1, 1);
|
||||
nvrtcGetProgramLog(prog, log);
|
||||
fprintf(stderr, "NVRTC compile failed (%d):\n%s\n", compile_rc, log);
|
||||
free(log);
|
||||
nvrtcDestroyProgram(&prog);
|
||||
return 1;
|
||||
}
|
||||
size_t ptx_sz = 0;
|
||||
CHECK_LIB(nvrtcGetPTXSize(prog, &ptx_sz));
|
||||
char *ptx = malloc(ptx_sz);
|
||||
char *ptx = must_malloc(ptx_sz + 1);
|
||||
CHECK_LIB(nvrtcGetPTX(prog, ptx));
|
||||
ptx[ptx_sz] = '\0';
|
||||
CHECK_LIB(nvrtcDestroyProgram(&prog));
|
||||
|
||||
GpuWorker workers[MAX_GPUS];
|
||||
@ -987,8 +1007,8 @@ int main(void) {
|
||||
int finite_ok = 1;
|
||||
int weight_mismatches = 0;
|
||||
size_t max_elems = D * F;
|
||||
float *hbuf0 = malloc(max_elems * sizeof(float));
|
||||
float *hbufg = malloc(max_elems * sizeof(float));
|
||||
float *hbuf0 = must_malloc(max_elems * sizeof(float));
|
||||
float *hbufg = must_malloc(max_elems * sizeof(float));
|
||||
|
||||
for (int p = 0; p < NUM_PARAMS; p++) {
|
||||
size_t cnt = PARAM_COUNTS[p];
|
||||
|
||||
@ -55,6 +55,24 @@ static void *must_sym(void *lib, const char *name) {
|
||||
return sym;
|
||||
}
|
||||
|
||||
static void *must_malloc(size_t bytes) {
|
||||
void *p = malloc(bytes ? bytes : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "malloc(%zu) failed\n", bytes);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *must_calloc(size_t nmemb, size_t size) {
|
||||
void *p = calloc(nmemb ? nmemb : 1, size ? size : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "calloc(%zu, %zu) failed\n", nmemb, size);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
#define CHECK_CU(expr) \
|
||||
do { \
|
||||
CUresult _r = (expr); \
|
||||
@ -655,16 +673,18 @@ int main(void) {
|
||||
if (compile_rc != 0) {
|
||||
size_t log_sz = 0;
|
||||
nvrtcGetProgramLogSize(prog, &log_sz);
|
||||
char *log = calloc(log_sz + 1, 1);
|
||||
char *log = must_calloc(log_sz + 1, 1);
|
||||
nvrtcGetProgramLog(prog, log);
|
||||
fprintf(stderr, "NVRTC compile failed (%d):\n%s\n", compile_rc, log);
|
||||
free(log);
|
||||
nvrtcDestroyProgram(&prog);
|
||||
return 1;
|
||||
}
|
||||
size_t ptx_sz = 0;
|
||||
CHECK_LIB(nvrtcGetPTXSize(prog, &ptx_sz));
|
||||
char *ptx = malloc(ptx_sz);
|
||||
char *ptx = must_malloc(ptx_sz + 1);
|
||||
CHECK_LIB(nvrtcGetPTX(prog, ptx));
|
||||
ptx[ptx_sz] = '\0';
|
||||
CHECK_LIB(nvrtcDestroyProgram(&prog));
|
||||
|
||||
CUmodule mod = NULL;
|
||||
@ -832,7 +852,7 @@ int main(void) {
|
||||
uint32_t weight_digest = 2166136261u;
|
||||
int finite_ok = 1;
|
||||
size_t max_elems = D * F;
|
||||
float *hbuf = malloc(max_elems * sizeof(float));
|
||||
float *hbuf = must_malloc(max_elems * sizeof(float));
|
||||
for (int p = 0; p < 10; p++) {
|
||||
CHECK_CU(cuMemcpyDtoH(hbuf, params[p], param_counts[p] * sizeof(float)));
|
||||
for (size_t i = 0; i < param_counts[p]; i++) {
|
||||
|
||||
@ -172,6 +172,24 @@ static void format_uuid(const CUuuid_local *u, char out[40]) {
|
||||
u->bytes[12], u->bytes[13], u->bytes[14], u->bytes[15]);
|
||||
}
|
||||
|
||||
static void *must_malloc(size_t bytes) {
|
||||
void *p = malloc(bytes ? bytes : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "malloc(%zu) failed\n", bytes);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *must_calloc(size_t nmemb, size_t size) {
|
||||
void *p = calloc(nmemb ? nmemb : 1, size ? size : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "calloc(%zu, %zu) failed\n", nmemb, size);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
#define LOAD_SYM(handle, name, ret, args) \
|
||||
typedef ret (*fn_##name##_t) args; \
|
||||
fn_##name##_t p_##name = (fn_##name##_t)dlsym(handle, #name); \
|
||||
@ -267,8 +285,8 @@ int main(void) {
|
||||
const size_t BYTES = (size_t)N * sizeof(uint32_t);
|
||||
uint32_t *h_in[MAX_GPUS];
|
||||
uint32_t *h_out[MAX_GPUS];
|
||||
uint32_t *h_tmp = (uint32_t *)malloc(BYTES);
|
||||
uint32_t *h_expected_sum = (uint32_t *)calloc(N, sizeof(uint32_t));
|
||||
uint32_t *h_tmp = (uint32_t *)must_malloc(BYTES);
|
||||
uint32_t *h_expected_sum = (uint32_t *)must_calloc(N, sizeof(uint32_t));
|
||||
memset(h_in, 0, sizeof(h_in));
|
||||
memset(h_out, 0, sizeof(h_out));
|
||||
|
||||
@ -286,8 +304,8 @@ int main(void) {
|
||||
};
|
||||
|
||||
for (int g = 0; g < ngpus; g++) {
|
||||
h_in[g] = (uint32_t *)malloc(BYTES);
|
||||
h_out[g] = (uint32_t *)malloc(BYTES);
|
||||
h_in[g] = (uint32_t *)must_malloc(BYTES);
|
||||
h_out[g] = (uint32_t *)must_malloc(BYTES);
|
||||
uint32_t base = ((uint32_t)(g + 1)) << 28;
|
||||
uint32_t mult = MULT_SEEDS[g & 7];
|
||||
for (uint32_t i = 0; i < N; i++) {
|
||||
|
||||
@ -293,6 +293,24 @@ static void *must_sym(void *h, const char *name) {
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *must_malloc(size_t bytes) {
|
||||
void *p = malloc(bytes ? bytes : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "malloc(%zu) failed\n", bytes);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *must_calloc(size_t nmemb, size_t size) {
|
||||
void *p = calloc(nmemb ? nmemb : 1, size ? size : 1);
|
||||
if (!p) {
|
||||
fprintf(stderr, "calloc(%zu, %zu) failed\n", nmemb, size);
|
||||
exit(2);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static uint32_t expected_word(uint32_t seed, uint32_t i) {
|
||||
/* Same formula verified by native_hello.sm70.cubin:
|
||||
* check native-hello output or run with seed and compare against stock nvidia! */
|
||||
@ -636,7 +654,7 @@ int main(int argc, char **argv) {
|
||||
CUdeviceptr d_buf = 0;
|
||||
CHECK(cuMemAlloc(&d_buf, nwords * sizeof(uint32_t)));
|
||||
CHECK(cuMemsetD32(d_buf, 0xA55A1234u, nwords));
|
||||
uint32_t *h_out = calloc(nwords, sizeof(uint32_t));
|
||||
uint32_t *h_out = must_calloc(nwords, sizeof(uint32_t));
|
||||
CHECK(cuMemcpyDtoH(h_out, d_buf, nwords * sizeof(uint32_t)));
|
||||
int memset_ok = 1;
|
||||
for (size_t i = 0; i < nwords; i++) {
|
||||
@ -1051,7 +1069,7 @@ int main(int argc, char **argv) {
|
||||
CHECK(cublasCreate(&bh));
|
||||
const int M = 256;
|
||||
size_t mat_bytes = (size_t)M * M * sizeof(float);
|
||||
float *hA = malloc(mat_bytes), *hB = malloc(mat_bytes), *hC = calloc(M * M, sizeof(float));
|
||||
float *hA = must_malloc(mat_bytes), *hB = must_malloc(mat_bytes), *hC = must_calloc((size_t)M * M, sizeof(float));
|
||||
for (int i = 0; i < M * M; i++) {
|
||||
hA[i] = (float)((i % 17) - 8) * 0.25f;
|
||||
hB[i] = (float)(((i * 3) % 13) - 6) * 0.25f;
|
||||
@ -1089,8 +1107,8 @@ int main(int argc, char **argv) {
|
||||
const int TM = 128;
|
||||
size_t h16_bytes = (size_t)TM * TM * sizeof(uint16_t);
|
||||
size_t f32_bytes = (size_t)TM * TM * sizeof(float);
|
||||
uint16_t *h16 = malloc(h16_bytes);
|
||||
float *hTC = calloc(TM * TM, sizeof(float));
|
||||
uint16_t *h16 = must_malloc(h16_bytes);
|
||||
float *hTC = must_calloc((size_t)TM * TM, sizeof(float));
|
||||
for (int i = 0; i < TM * TM; i++) h16[i] = 0x3c00u; /* IEEE 754 binary16 1.0 */
|
||||
CUdeviceptr dTA = 0, dTB = 0, dTC = 0;
|
||||
CHECK(cuMemAlloc(&dTA, h16_bytes));
|
||||
@ -1124,7 +1142,7 @@ int main(int argc, char **argv) {
|
||||
const int BM = 64, BATCH = 8;
|
||||
size_t b_elems = (size_t)BATCH * BM * BM;
|
||||
size_t b_bytes = b_elems * sizeof(float);
|
||||
float *hbA = malloc(b_bytes), *hbB = malloc(b_bytes), *hbC = calloc(b_elems, sizeof(float));
|
||||
float *hbA = must_malloc(b_bytes), *hbB = must_malloc(b_bytes), *hbC = must_calloc(b_elems, sizeof(float));
|
||||
for (size_t i = 0; i < b_elems; i++) {
|
||||
hbA[i] = 1.0f;
|
||||
hbB[i] = (float)((i / (BM * BM)) + 1);
|
||||
@ -1179,7 +1197,7 @@ int main(int argc, char **argv) {
|
||||
int (*cufftDestroy)(cufftHandle) = must_sym(cufft, "cufftDestroy");
|
||||
const int FN = 1024;
|
||||
size_t fbytes = (size_t)FN * 2 * sizeof(float);
|
||||
float *h_in = malloc(fbytes), *h_fwd = calloc((size_t)FN * 2, sizeof(float)), *h_inv = calloc((size_t)FN * 2, sizeof(float));
|
||||
float *h_in = must_malloc(fbytes), *h_fwd = must_calloc((size_t)FN * 2, sizeof(float)), *h_inv = must_calloc((size_t)FN * 2, sizeof(float));
|
||||
for (int i = 0; i < FN; i++) {
|
||||
h_in[2 * i] = (float)((i % 16) - 8) * 0.25f;
|
||||
h_in[2 * i + 1] = (float)(((i * 5) % 11) - 5) * 0.25f;
|
||||
@ -1215,7 +1233,7 @@ int main(int argc, char **argv) {
|
||||
/* 2D 64x64 C2C FFT forward + inverse round-trip */
|
||||
const int NX = 64, NY = 64, N2 = NX * NY;
|
||||
size_t f2bytes = (size_t)N2 * 2 * sizeof(float);
|
||||
float *h2_in = malloc(f2bytes), *h2_fwd = calloc((size_t)N2 * 2, sizeof(float)), *h2_inv = calloc((size_t)N2 * 2, sizeof(float));
|
||||
float *h2_in = must_malloc(f2bytes), *h2_fwd = must_calloc((size_t)N2 * 2, sizeof(float)), *h2_inv = must_calloc((size_t)N2 * 2, sizeof(float));
|
||||
for (int i = 0; i < N2; i++) {
|
||||
h2_in[2 * i] = (float)((i % 13) - 6) * 0.25f;
|
||||
h2_in[2 * i + 1] = (float)(((i * 7) % 9) - 4) * 0.25f;
|
||||
@ -1261,7 +1279,7 @@ int main(int argc, char **argv) {
|
||||
int (*curandDestroyGenerator)(curandGenerator_t) = must_sym(curand, "curandDestroyGenerator");
|
||||
const size_t RN = 4096;
|
||||
size_t rbytes = RN * sizeof(float);
|
||||
float *h_rng = calloc(RN, sizeof(float));
|
||||
float *h_rng = must_calloc(RN, sizeof(float));
|
||||
CUdeviceptr d_rng = 0;
|
||||
CHECK(cuMemAlloc(&d_rng, rbytes));
|
||||
curandGenerator_t gen = NULL;
|
||||
@ -1312,7 +1330,7 @@ int main(int argc, char **argv) {
|
||||
const void *, void *, size_t, CUstream) = must_sym(cublaslt, "cublasLtMatmul");
|
||||
const int LM = 128;
|
||||
size_t lbytes = (size_t)LM * LM * sizeof(float);
|
||||
float *hlA = malloc(lbytes), *hlB = malloc(lbytes), *hlC = calloc((size_t)LM * LM, sizeof(float));
|
||||
float *hlA = must_malloc(lbytes), *hlB = must_malloc(lbytes), *hlC = must_calloc((size_t)LM * LM, sizeof(float));
|
||||
for (int i = 0; i < LM * LM; i++) {
|
||||
hlA[i] = 1.0f;
|
||||
hlB[i] = 2.0f;
|
||||
@ -1367,7 +1385,8 @@ int main(int argc, char **argv) {
|
||||
memset(uuid_bytes, 0, sizeof(uuid_bytes));
|
||||
CHECK(cuDeviceGetUuid(uuid_bytes, dev));
|
||||
char uuid_hex[33];
|
||||
for (int i = 0; i < 16; i++) sprintf(uuid_hex + i * 2, "%02x", uuid_bytes[i]);
|
||||
for (int i = 0; i < 16; i++)
|
||||
snprintf(uuid_hex + i * 2, sizeof(uuid_hex) - (size_t)(i * 2), "%02x", uuid_bytes[i]);
|
||||
uuid_hex[32] = '\0';
|
||||
|
||||
unsigned int ptr_mem_type = 0;
|
||||
@ -1773,8 +1792,9 @@ int main(int argc, char **argv) {
|
||||
CHECK(nvrtcCompileProgram(prog, 1, opts));
|
||||
size_t ptx_sz = 0;
|
||||
CHECK(nvrtcGetPTXSize(prog, &ptx_sz));
|
||||
char *ptx = malloc(ptx_sz + 1);
|
||||
char *ptx = must_malloc(ptx_sz + 1);
|
||||
CHECK(nvrtcGetPTX(prog, ptx));
|
||||
ptx[ptx_sz] = '\0';
|
||||
CHECK(nvrtcDestroyProgram(&prog));
|
||||
CUmodule nmod = NULL;
|
||||
CUfunction nfn = NULL;
|
||||
|
||||
Reference in New Issue
Block a user