Skip to content

Commit fb74236

Browse files
committed
Add checksum
1 parent 88a13aa commit fb74236

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/provider/provider_level_zero.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <assert.h>
99
#include <stdbool.h>
1010
#include <stddef.h>
11+
#include <stdint.h>
1112
#include <string.h>
1213

1314
#include <umf.h>
@@ -753,9 +754,22 @@ static umf_result_t ze_memory_provider_allocation_split(void *provider,
753754

754755
typedef struct ze_ipc_data_t {
755756
int pid;
757+
uint64_t id; // Unique identifier for the IPC handle
758+
uint64_t checksum;
756759
ze_ipc_mem_handle_t ze_handle;
757760
} ze_ipc_data_t;
758761

762+
// Compute a simple checksum of the ze_handle field in ze_ipc_data_t
763+
static uint64_t ze_ipc_handle_checksum(const ze_ipc_data_t *ipc_data) {
764+
// Interpret the ze_handle as a byte array
765+
const uint8_t *bytes = (const uint8_t *)&ipc_data->ze_handle;
766+
uint64_t checksum = 0;
767+
for (size_t i = 0; i < sizeof(ipc_data->ze_handle); ++i) {
768+
checksum += bytes[i];
769+
}
770+
return checksum;
771+
}
772+
759773
static umf_result_t ze_memory_provider_get_ipc_handle_size(void *provider,
760774
size_t *size) {
761775
(void)provider;
@@ -770,6 +784,8 @@ static umf_result_t ze_memory_provider_get_ipc_handle(void *provider,
770784
void *providerIpcData) {
771785
(void)size;
772786

787+
static uint64_t id = 0;
788+
773789
ze_result_t ze_result;
774790
ze_ipc_data_t *ze_ipc_data = (ze_ipc_data_t *)providerIpcData;
775791
struct ze_memory_provider_t *ze_provider =
@@ -783,6 +799,11 @@ static umf_result_t ze_memory_provider_get_ipc_handle(void *provider,
783799
}
784800

785801
ze_ipc_data->pid = utils_getpid();
802+
ze_ipc_data->checksum = ze_ipc_handle_checksum(ze_ipc_data);
803+
ze_ipc_data->id = id++;
804+
805+
LOG_DEBUG("GET handle(): pid = %d, id = %lu, checksum = %lu", ze_ipc_data->pid,
806+
ze_ipc_data->id, ze_ipc_data->checksum);
786807

787808
return UMF_RESULT_SUCCESS;
788809
}
@@ -801,6 +822,16 @@ static umf_result_t ze_memory_provider_put_ipc_handle(void *provider,
801822
return UMF_RESULT_SUCCESS;
802823
}
803824

825+
if (ze_ipc_data->checksum != ze_ipc_handle_checksum(ze_ipc_data)) {
826+
LOG_FATAL(
827+
"Checksum mismatch for IPC handle data: pid = %d, checksum = %lu",
828+
ze_ipc_data->pid, ze_ipc_data->checksum);
829+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
830+
}
831+
832+
LOG_DEBUG("PUT handle(): pid = %d, id = %lu, checksum = %lu", ze_ipc_data->pid,
833+
ze_ipc_data->id, ze_ipc_data->checksum);
834+
804835
ze_result = g_ze_ops.zeMemPutIpcHandle(ze_provider->context,
805836
ze_ipc_data->ze_handle);
806837
if (ze_result != ZE_RESULT_SUCCESS) {

0 commit comments

Comments
 (0)