@@ -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
583590static 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,30 @@ 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+ // length and offset passed to mmap() have to be page-aligned
685+ size_t size_aligned =
686+ ALIGN_UP (file_ipc_data -> size , file_provider -> page_size );
687+
688+ char * addr = utils_mmap_file (NULL , size_aligned , file_ipc_data -> protection ,
689+ file_ipc_data -> visibility , fd ,
690+ file_ipc_data -> offset_fd );
678691 (void )utils_close_fd (fd );
679- if (* ptr == NULL ) {
692+ if (addr == NULL ) {
680693 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 ;
694+ LOG_PERR ("file mapping failed (path: %s, size: %zu, protection: %i, "
695+ "fd: %i, offset: %zu)" ,
696+ file_ipc_data -> path , size_aligned , file_ipc_data -> protection ,
697+ fd , file_ipc_data -> offset_fd );
698+ return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
683699 }
684700
701+ * ptr = addr + (file_ipc_data -> offset_fd - file_ipc_data -> offset_fd );
702+
703+ LOG_DEBUG ("file mapped (path: %s, size: %zu, protection: %i, fd: %i, "
704+ "offset: %zu) at address %p" ,
705+ file_ipc_data -> path , size_aligned , file_ipc_data -> protection , fd ,
706+ file_ipc_data -> offset_fd , * ptr );
707+
685708 return ret ;
686709}
687710
@@ -698,6 +721,9 @@ static umf_result_t file_close_ipc_handle(void *provider, void *ptr,
698721 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
699722 }
700723
724+ // ptr and size passed to munmap() have to be page-aligned
725+ size = ALIGN_UP (size , file_provider -> page_size );
726+
701727 errno = 0 ;
702728 int ret = utils_munmap (ptr , size );
703729 // ignore error when size == 0
0 commit comments