Skip to content

Commit 0168e98

Browse files
committed
Add checksum
1 parent d6e8425 commit 0168e98

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/provider/provider_level_zero.c

Lines changed: 27 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,21 @@ 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 checksum;
756758
ze_ipc_mem_handle_t ze_handle;
757759
} ze_ipc_data_t;
758760

761+
// Compute a simple checksum of the ze_handle field in ze_ipc_data_t
762+
static uint64_t ze_ipc_handle_checksum(const ze_ipc_data_t *ipc_data) {
763+
// Interpret the ze_handle as a byte array
764+
const uint8_t *bytes = (const uint8_t *)&ipc_data->ze_handle;
765+
uint64_t checksum = 0;
766+
for (size_t i = 0; i < sizeof(ipc_data->ze_handle); ++i) {
767+
checksum += bytes[i];
768+
}
769+
return checksum;
770+
}
771+
759772
static umf_result_t ze_memory_provider_get_ipc_handle_size(void *provider,
760773
size_t *size) {
761774
(void)provider;
@@ -783,6 +796,10 @@ static umf_result_t ze_memory_provider_get_ipc_handle(void *provider,
783796
}
784797

785798
ze_ipc_data->pid = utils_getpid();
799+
ze_ipc_data->checksum = ze_ipc_handle_checksum(ze_ipc_data);
800+
801+
LOG_DEBUG("GET handle(): pid = %d, checksum = %lu", ze_ipc_data->pid,
802+
ze_ipc_data->checksum);
786803

787804
return UMF_RESULT_SUCCESS;
788805
}
@@ -794,6 +811,16 @@ static umf_result_t ze_memory_provider_put_ipc_handle(void *provider,
794811
(struct ze_memory_provider_t *)provider;
795812
ze_ipc_data_t *ze_ipc_data = (ze_ipc_data_t *)providerIpcData;
796813

814+
if (ze_ipc_data->checksum != ze_ipc_handle_checksum(ze_ipc_data)) {
815+
LOG_FATAL(
816+
"Checksum mismatch for IPC handle data: pid = %d, checksum = %lu",
817+
ze_ipc_data->pid, ze_ipc_data->checksum);
818+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
819+
}
820+
821+
LOG_DEBUG("PUT handle(): pid = %d, checksum = %lu", ze_ipc_data->pid,
822+
ze_ipc_data->checksum);
823+
797824
if (g_ze_ops.zeMemPutIpcHandle == NULL) {
798825
// g_ze_ops.zeMemPutIpcHandle can be NULL because it was introduced
799826
// starting from Level Zero 1.6. Before Level Zero 1.6 IPC handle

0 commit comments

Comments
 (0)