tools:mm:Check mmap based on return values
Subsystems (3)
PATCH 1/1 tools:mm:Check mmap based on return values
To correctly detect whether mmap is successful, must use if (map_ptr == MAP_FAILED)to avoid incorrectly handling a valid mapping.
--- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/madv_populate.c | 2 +- tools/testing/selftests/mm/soft-dirty.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/ksm_tests.c +1 -1
@@ -201,7 +201,7 @@ static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_ {void *map_ptr =mmap (ptr, map_size,PROT_WRITE , mapping,-1 ,0 ); -if (!map_ptr) { +if (map_ptr ==MAP_FAILED ) {perror ("mmap" );return NULL ; }
tools/testing/selftests/mm/madv_populate.c +1 -1
@@ -34,7 +34,7 @@ static void sense_support(void) addr =mmap (0 , pagesize,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); -if (!addr) +if (addr ==MAP_FAILED )ksft_exit_fail_msg ("mmap failed\n" ); ret =madvise (addr, pagesize,MADV_POPULATE_READ );
tools/testing/selftests/mm/soft-dirty.c +1 -1
@@ -134,7 +134,7 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)if (anon) { map =mmap (NULL , pagesize,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); -if (!map) +if (map ==MAP_FAILED )ksft_exit_fail_msg ("anon mmap failed\n" ); }else { test_fd =open (fname,O_RDWR |O_CREAT ,0664 );
Signature · 1 line
2.34.1
Quoted 2 lines · Yang Ruibin
To correctly detect whether mmap is successful, must use if (
map_ptr== MAP_FAILED)to avoid incorrectly handling a valid mapping.
Add "Fix mmap() error paths to check for MAP_FAILED" at the end of the changelog
Change short log to say "selftests:mm: Fix mmap() error paths to check for MAP_FAILED" selftests is the right subsystem tag for this change.
"Fix" in the short log will make it easier for this change to be picked up by stables.
Quoted 13 lines
Signed-off-by Yang Ruibin <[email protected]>--- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/madv_populate.c | 2 +- tools/testing/selftests/mm/soft-dirty.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)tools/testing/selftests/mm/ksm_tests.c +0
-0
tools/testing/selftests/mm/ksm_tests.c static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_ · line 205
Quoted 6 lines
tools/testing/selftests/mm/ksm_tests.c +1
-1@@ -201,7 +201,7 @@ static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_ {void *map_ptr =mmap (ptr, map_size,PROT_WRITE , mapping,-1 ,0 ); -if (!map_ptr) { +if (map_ptr ==MAP_FAILED ) {perror ("mmap" );
Use ksft_exit_fail_msg("mmap failed\n") to be consistent with the rest.
Quoted 20 lines
tools/testing/selftests/mm/ksm_tests.c +0
-0return NULL ; }tools/testing/selftests/mm/madv_populate.c +1
-1@@ -34,7 +34,7 @@ static void sense_support(void) addr =mmap (0 , pagesize,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,0 ,0 ); -if (!addr) +if (addr ==MAP_FAILED )ksft_exit_fail_msg ("mmap failed\n" ); ret =madvise (addr, pagesize,MADV_POPULATE_READ );tools/testing/selftests/mm/soft-dirty.c +0
-0
tools/testing/selftests/mm/soft-dirty.c static void test_mprotect(int pagemap_fd, int pagesize, bool anon) · line 140
Quoted 8 lines
tools/testing/selftests/mm/soft-dirty.c +1
-1@@ -134,7 +134,7 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)if (anon) { map =mmap (NULL , pagesize,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); -if (!map) +if (map ==MAP_FAILED )ksft_exit_fail_msg ("anon mmap failed\n" ); }else { test_fd =open (fname,O_RDWR |O_CREAT ,0664 );
With the above fixed:
thanks,
-- Shuah