Skip to content

Commit 8b554a0

Browse files
authored
Merge pull request #865 from ldorau/Fix_file_alloc_and_file_open_close_ipc_handle
Fix `file_alloc()` and `file_open/close_ipc_handle()`
2 parents cdc0310 + aada873 commit 8b554a0

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/provider/provider_file_memory.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,21 @@ static umf_result_t file_alloc(void *provider, size_t size, size_t alignment,
397397
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
398398
}
399399

400-
// alignment must be a power of two and a multiple of sizeof(void *)
401-
if (alignment &&
402-
((alignment & (alignment - 1)) || (alignment % sizeof(void *)))) {
403-
LOG_ERR("wrong alignment: %zu (not a power of 2 or a multiple of "
404-
"sizeof(void *))",
405-
alignment);
400+
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
401+
402+
// alignment must be a power of two and a multiple or a divider of the page size
403+
if (alignment && ((alignment & (alignment - 1)) ||
404+
((alignment % file_provider->page_size) &&
405+
(file_provider->page_size % alignment)))) {
406+
LOG_ERR("wrong alignment: %zu (not a power of 2 or a multiple or a "
407+
"divider of the page size (%zu))",
408+
alignment, file_provider->page_size);
406409
return UMF_RESULT_ERROR_INVALID_ALIGNMENT;
407410
}
408411

409-
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
412+
if (IS_NOT_ALIGNED(alignment, file_provider->page_size)) {
413+
alignment = ALIGN_UP(alignment, file_provider->page_size);
414+
}
410415

411416
void *addr = NULL;
412417
size_t alloc_offset_fd; // needed for critnib_insert()
@@ -578,6 +583,8 @@ typedef struct file_ipc_data_t {
578583
char path[PATH_MAX];
579584
size_t offset_fd;
580585
size_t size;
586+
unsigned protection; // combination of OS-specific protection flags
587+
unsigned visibility; // memory visibility mode
581588
} file_ipc_data_t;
582589

583590
static umf_result_t file_get_ipc_handle_size(void *provider, size_t *size) {
@@ -623,6 +630,8 @@ static umf_result_t file_get_ipc_handle(void *provider, const void *ptr,
623630
file_ipc_data->size = size;
624631
strncpy(file_ipc_data->path, file_provider->path, PATH_MAX - 1);
625632
file_ipc_data->path[PATH_MAX - 1] = '\0';
633+
file_ipc_data->protection = file_provider->protection;
634+
file_ipc_data->visibility = file_provider->visibility;
626635

627636
return UMF_RESULT_SUCCESS;
628637
}
@@ -672,16 +681,26 @@ static umf_result_t file_open_ipc_handle(void *provider, void *providerIpcData,
672681
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
673682
}
674683

675-
*ptr = utils_mmap_file(NULL, file_ipc_data->size, file_provider->protection,
676-
file_provider->visibility, fd,
677-
file_ipc_data->offset_fd);
684+
char *addr = utils_mmap_file(
685+
NULL, file_ipc_data->size, file_ipc_data->protection,
686+
file_ipc_data->visibility, fd, file_ipc_data->offset_fd);
678687
(void)utils_close_fd(fd);
679-
if (*ptr == NULL) {
688+
if (addr == NULL) {
680689
file_store_last_native_error(UMF_FILE_RESULT_ERROR_ALLOC_FAILED, errno);
681-
LOG_PERR("memory mapping failed");
682-
ret = UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
690+
LOG_PERR("file mapping failed (path: %s, size: %zu, protection: %i, "
691+
"fd: %i, offset: %zu)",
692+
file_ipc_data->path, file_ipc_data->size,
693+
file_ipc_data->protection, fd, file_ipc_data->offset_fd);
694+
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
683695
}
684696

697+
LOG_DEBUG("file mapped (path: %s, size: %zu, protection: %i, fd: %i, "
698+
"offset: %zu) at address %p",
699+
file_ipc_data->path, file_ipc_data->size,
700+
file_ipc_data->protection, fd, file_ipc_data->offset_fd, addr);
701+
702+
*ptr = addr;
703+
685704
return ret;
686705
}
687706

0 commit comments

Comments
 (0)