Skip to content

Commit de5544d

Browse files
committed
Implement IPC hooks in OS memory provider
Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent 56fb99b commit de5544d

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

src/provider/provider_os_memory.c

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,105 @@ static umf_result_t os_allocation_merge(void *provider, void *lowPtr,
657657
return UMF_RESULT_SUCCESS;
658658
}
659659

660+
typedef struct os_ipc_data_t {
661+
int pid;
662+
int fd;
663+
size_t fd_offset;
664+
size_t size;
665+
} os_ipc_data_t;
666+
667+
static umf_result_t os_get_ipc_handle_size(void *provider, size_t *size) {
668+
if (provider == NULL || size == NULL) {
669+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
670+
}
671+
672+
*size = sizeof(os_ipc_data_t);
673+
return UMF_RESULT_SUCCESS;
674+
}
675+
676+
static umf_result_t os_get_ipc_handle(void *provider, const void *ptr,
677+
size_t size, void *providerIpcData) {
678+
if (provider == NULL || providerIpcData == NULL) {
679+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
680+
}
681+
682+
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
683+
684+
void *value = critnib_get(os_provider->ptr_off, (uintptr_t)ptr);
685+
if (value == NULL) {
686+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
687+
}
688+
689+
os_ipc_data_t *os_ipc_data = (os_ipc_data_t *)providerIpcData;
690+
os_ipc_data->pid = os_getpid();
691+
os_ipc_data->fd = os_provider->fd;
692+
os_ipc_data->fd_offset = (size_t)value;
693+
os_ipc_data->size = size;
694+
695+
return UMF_RESULT_SUCCESS;
696+
}
697+
698+
static umf_result_t os_put_ipc_handle(void *provider, void *providerIpcData) {
699+
if (provider == NULL || providerIpcData == NULL) {
700+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
701+
}
702+
703+
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
704+
os_ipc_data_t *os_ipc_data = (os_ipc_data_t *)providerIpcData;
705+
706+
if (os_ipc_data->fd != os_provider->fd || os_ipc_data->pid != os_getpid()) {
707+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
708+
}
709+
710+
return UMF_RESULT_SUCCESS;
711+
}
712+
713+
static umf_result_t os_open_ipc_handle(void *provider, void *providerIpcData,
714+
void **ptr) {
715+
if (provider == NULL || providerIpcData == NULL || ptr == NULL) {
716+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
717+
}
718+
719+
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
720+
os_ipc_data_t *os_ipc_data = (os_ipc_data_t *)providerIpcData;
721+
722+
int fd = os_duplicate_fd(os_ipc_data->pid, os_ipc_data->fd);
723+
if (fd == -1) {
724+
LOG_PERR("duplicating file descriptor failed");
725+
return UMF_RESULT_ERROR_UNKNOWN;
726+
}
727+
728+
*ptr = os_mmap(NULL, os_ipc_data->size, os_provider->protection,
729+
os_provider->flag, fd, os_ipc_data->fd_offset);
730+
if (*ptr == NULL) {
731+
os_store_last_native_error(UMF_OS_RESULT_ERROR_ALLOC_FAILED, errno);
732+
LOG_PERR("memory mapping failed");
733+
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
734+
}
735+
736+
return UMF_RESULT_SUCCESS;
737+
}
738+
739+
static umf_result_t os_close_ipc_handle(void *provider, void *ptr) {
740+
if (provider == NULL || ptr == NULL) {
741+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
742+
}
743+
744+
size_t size = 0; // TODO: it should be an argument of this function
745+
746+
errno = 0;
747+
int ret = os_munmap(ptr, size);
748+
// ignore error when size == 0
749+
if (ret && (size > 0)) {
750+
os_store_last_native_error(UMF_OS_RESULT_ERROR_FREE_FAILED, errno);
751+
LOG_PERR("memory unmapping failed");
752+
753+
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
754+
}
755+
756+
return UMF_RESULT_SUCCESS;
757+
}
758+
660759
static umf_memory_provider_ops_t UMF_OS_MEMORY_PROVIDER_OPS = {
661760
.version = UMF_VERSION_CURRENT,
662761
.initialize = os_initialize,
@@ -671,11 +770,11 @@ static umf_memory_provider_ops_t UMF_OS_MEMORY_PROVIDER_OPS = {
671770
.ext.purge_force = os_purge_force,
672771
.ext.allocation_merge = os_allocation_merge,
673772
.ext.allocation_split = os_allocation_split,
674-
.ipc.get_ipc_handle_size = NULL,
675-
.ipc.get_ipc_handle = NULL,
676-
.ipc.put_ipc_handle = NULL,
677-
.ipc.open_ipc_handle = NULL,
678-
.ipc.close_ipc_handle = NULL};
773+
.ipc.get_ipc_handle_size = os_get_ipc_handle_size,
774+
.ipc.get_ipc_handle = os_get_ipc_handle,
775+
.ipc.put_ipc_handle = os_put_ipc_handle,
776+
.ipc.open_ipc_handle = os_open_ipc_handle,
777+
.ipc.close_ipc_handle = os_close_ipc_handle};
679778

680779
umf_memory_provider_ops_t *umfOsMemoryProviderOps(void) {
681780
return &UMF_OS_MEMORY_PROVIDER_OPS;

src/provider/provider_os_memory_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ size_t os_get_page_size(void);
4545

4646
void os_strerror(int errnum, char *buf, size_t buflen);
4747

48+
int os_getpid(void);
49+
50+
int os_duplicate_fd(int pid, int fd);
51+
4852
#ifdef __cplusplus
4953
}
5054
#endif

src/provider/provider_os_memory_linux.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,16 @@ int os_purge(void *addr, size_t length, int advice) {
123123
void os_strerror(int errnum, char *buf, size_t buflen) {
124124
strerror_r(errnum, buf, buflen);
125125
}
126+
127+
int os_getpid(void) { return getpid(); }
128+
129+
int os_duplicate_fd(int pid, int fd) {
130+
int pid_fd = syscall(SYS_pidfd_open, pid, 0);
131+
if (pid_fd == -1) {
132+
return -1;
133+
}
134+
135+
int fd_dup = syscall(SYS_pidfd_getfd, pid_fd, fd, 0);
136+
close(pid_fd);
137+
return fd_dup;
138+
}

src/provider/provider_os_memory_windows.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ size_t os_get_page_size(void) {
112112
void os_strerror(int errnum, char *buf, size_t buflen) {
113113
strerror_s(buf, buflen, errnum);
114114
}
115+
116+
int os_getpid(void) { return GetCurrentProcessId(); }
117+
118+
int os_duplicate_fd(int pid, int fd) {
119+
return -1; // unsupported
120+
}

0 commit comments

Comments
 (0)