@@ -397,16 +397,21 @@ static umf_result_t file_alloc(void *provider, size_t size, size_t alignment,
397
397
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
398
398
}
399
399
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 );
406
409
return UMF_RESULT_ERROR_INVALID_ALIGNMENT ;
407
410
}
408
411
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
+ }
410
415
411
416
void * addr = NULL ;
412
417
size_t alloc_offset_fd ; // needed for critnib_insert()
@@ -578,6 +583,8 @@ typedef struct file_ipc_data_t {
578
583
char path [PATH_MAX ];
579
584
size_t offset_fd ;
580
585
size_t size ;
586
+ unsigned protection ; // combination of OS-specific protection flags
587
+ unsigned visibility ; // memory visibility mode
581
588
} file_ipc_data_t ;
582
589
583
590
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,
623
630
file_ipc_data -> size = size ;
624
631
strncpy (file_ipc_data -> path , file_provider -> path , PATH_MAX - 1 );
625
632
file_ipc_data -> path [PATH_MAX - 1 ] = '\0' ;
633
+ file_ipc_data -> protection = file_provider -> protection ;
634
+ file_ipc_data -> visibility = file_provider -> visibility ;
626
635
627
636
return UMF_RESULT_SUCCESS ;
628
637
}
@@ -672,16 +681,30 @@ static umf_result_t file_open_ipc_handle(void *provider, void *providerIpcData,
672
681
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
673
682
}
674
683
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 );
678
691
(void )utils_close_fd (fd );
679
- if (* ptr == NULL ) {
692
+ if (addr == NULL ) {
680
693
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 ;
683
699
}
684
700
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
+
685
708
return ret ;
686
709
}
687
710
@@ -698,6 +721,9 @@ static umf_result_t file_close_ipc_handle(void *provider, void *ptr,
698
721
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
699
722
}
700
723
724
+ // ptr and size passed to munmap() have to be page-aligned
725
+ size = ALIGN_UP (size , file_provider -> page_size );
726
+
701
727
errno = 0 ;
702
728
int ret = utils_munmap (ptr , size );
703
729
// ignore error when size == 0
0 commit comments