brk01: Rewrite from scratch.
RFC PATCH 1/1 brk01: Rewrite from scratch.
This commit rewrites the brk01 test from scratch since the original code wasn't making much sense.
Now we actually run the brk() in a loop increasing and shrinking the break repeatedly a few times, which the original code was supposed to do but never actually did since we do not pass the -i parameter in the runtest files.
The increment is also choosen to be much much more reasonable instead of using the multiple of the absolute address of the page break we choose much more modest 2 * pagesize - 1. Note that the increment is not page aligned since that way we also check that kernel page-aligns it just fine.
The sequence has been choosen to grow the heap first then shrink it to the original size, which should be safe since the heap never shrinks below its original size.
We also write to the newly allocated heap in a case that that the heap has grown, which should be safe enough since that way we will not rewrite anything that was previously stored there.
--- testcases/kernel/syscalls/brk/brk01.c | 239 +++++++--------------------------- 1 file changed, 50 insertions(+), 189 deletions(-)
testcases/kernel/syscalls/brk/brk01.c +50 -189
@@ -1,214 +1,75 @@/* -* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. -* AUTHOR : William Roske -* CO-PILOT : Dave Fenner +* Copyright (c) 2017 Cyril Hrubis <[email protected]> * -* This program is free software; you can redistribute it and/or modify it -* under the terms of version 2 of the GNU General Public License as -* published by the Free Software Foundation. +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. * -* This program is distributed in the hope that it would be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* -* Further, this software is distributed without any warranty that it is -* free of the rightful claim of any third person regarding infringement -* or the like. Any license provided herein, whether implied or -* otherwise, applies only to this software file. Patent licenses, if -* any, provided herein do not apply to combinations of this program with -* other software, or any other product whatsoever. -* -* You should have received a copy of the GNU General Public License along -* with this program; if not, write the Free Software Foundation, Inc., -* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -* -* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, -* Mountain View, CA 94043, or: -* -* http://www.sgi.com -* -* For further information regarding this notice, see: -* -* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. * +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <unistd.h> -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <sys/param.h> -#include <sys/resource.h> #include <stdint.h> #include <inttypes.h> +#include <errno.h> -#include "test.h" -#include "safe_macros.h" - -#ifndef BSIZE -#define BSIZE BBSIZE -#endif - -void setup (); -void cleanup (); - -#define MAX_SIZE_LC 1000/* loop count test will reach max size */ - -char *TCID ="brk01" ; -int TST_TOTAL =1 ; - -uintptr_t Max_brk_byte_size; -uintptr_t Beg_brk_val; - -#if !defined(UCLINUX ) +#include "tst_test.h" -int main (int ac,char **av) +void verify_brk (void ) { -int lc; -int incr; -uintptr_t nbrkpt;/* new brk point value */ -uintptr_t cur_brk_val;/* current size returned by sbrk */ -uintptr_t aft_brk_val;/* current size returned by sbrk */ - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -/* -* Attempt to control how fast we get to test max size. -* Every MAX_SIZE_LC'th lc will be fastest test will reach max size. -*/ - incr = (Max_brk_byte_size - Beg_brk_val) / (MAX_SIZE_LC /2 ); - -if ((incr *2 ) <4096 )/* make sure that process will grow */ - incr +=4096 /2 ; - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -/* -* Determine new value to give brk -* Every even lc value, grow by 2 incr and -* every odd lc value, strink by one incr. -* If lc is equal to 3, no change, special case. -*/ - cur_brk_val = (uintptr_t )sbrk (0 ); -if (lc ==3 ) { - nbrkpt = cur_brk_val;/* no change, special one time case */ - }else if ((lc %2 ) ==0 ) { -/* -* grow -*/ - nbrkpt = cur_brk_val + (2 * incr); - -if (nbrkpt > Max_brk_byte_size) - nbrkpt = Beg_brk_val;/* start over */ - - }else { -/* -* shrink -*/ - nbrkpt = cur_brk_val - incr; +uintptr_t cur_brk, new_brk; +uintptr_t inc =getpagesize () *2 -1 ; +unsigned int i; + + cur_brk = (uintptr_t )sbrk (0 ); + +for (i =0 ; i <33 ; i++) { +switch (i %3 ) { +case 0 : + new_brk = cur_brk + inc; +break ; +case 1 : + new_brk = cur_brk; +break ; +case 2 : + new_brk = cur_brk - inc; +break ; } -/**** -printf("cur_brk_val = %d, nbrkpt = %d, incr = %d, lc = %d\n", -cur_brk_val, nbrkpt, incr, lc); -****/ +tst_res (TINFO ,"cur_brk=%p new_brk=%p" , + (void *)cur_brk, (void *)new_brk); -TEST (brk ((char *)nbrkpt)); +TEST (brk ((void *)new_brk));if (TEST_RETURN ==-1 ) { +tst_res (TFAIL |TERRNO ,"brk() failed" ); +return ; + } - aft_brk_val = (uintptr_t )sbrk (0 ); -tst_resm (TFAIL |TTERRNO , -"brk(%" PRIuPTR") failed (size before %" PRIuPTR", after %" PRIuPTR")" , - nbrkpt, cur_brk_val, aft_brk_val); - - }else { - aft_brk_val = (uintptr_t )sbrk (0 ); -if (aft_brk_val == nbrkpt) { + cur_brk = (uintptr_t )sbrk (0 ); -tst_resm (TPASS , -"brk(%" PRIuPTR") returned %" PRIuPTR", new size verified by sbrk" , - nbrkpt,TEST_RETURN ); - }else { -tst_resm (TFAIL , -"brk(%" PRIuPTR") returned %" PRIuPTR", sbrk before %" PRIuPTR", after %" PRIuPTR"" , - nbrkpt,TEST_RETURN , - cur_brk_val, aft_brk_val); - } +if (cur_brk != new_brk) { +tst_res (TFAIL , +"brk() failed to set address have %p expected %p" , + (void *)cur_brk, (void *)new_brk); +return ; } +/* Try to write to the newly allocated heap */ +if (i %3 ==0 ) + *((char *)cur_brk) =0 ; } -cleanup (); -tst_exit (); -} - -void setup (void ) -{ -unsigned long max_size; -int ncpus; -unsigned long ulim_sz; -unsigned long usr_mem_sz; -struct rlimit lim; - -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - -/*if ((ulim_sz=ulimit(3,0)) == -1) -tst_brkm(TBROK|TERRNO, cleanup, "ulimit(3,0) failed"); */ - -SAFE_GETRLIMIT (cleanup,RLIMIT_DATA , &lim); - ulim_sz = lim.rlim_cur; - -/* -* On IRIX, which is a demand paged system, memory is managed -* different than on Crays systems. For now, pick some value. -*/ - usr_mem_sz =1024 *1024 *sizeof (long ); - -if ((ncpus =sysconf (_SC_NPROCESSORS_ONLN)) ==-1 ) -tst_brkm (TBROK |TERRNO , cleanup, -"sysconf(_SC_NPROCESSORS_ONLN) failed" ); - -/* -* allow 2*ncpus copies to run. -* never attempt to take more than a * 1/4 of memory (by single test) -*/ - - max_size =MIN (ulim_sz, usr_mem_sz); - - max_size = max_size / (2 * ncpus); - -if (max_size > (usr_mem_sz /4 )) - max_size = usr_mem_sz /4 ;/* only fourth mem by single test */ - - Beg_brk_val = (uintptr_t )sbrk (0 ); - -/* -* allow at least 4 times a big as current. -* This will override above code. -*/ -if (max_size < Beg_brk_val *4 )/* running on small mem and/or high # cpus */ - max_size = Beg_brk_val *4 ; - - Max_brk_byte_size = max_size; - -TEST_PAUSE ; - -} - -void cleanup (void ) -{ -} - -#else - -int main (void ) -{ -tst_brkm (TCONF ,NULL ,"test is not available on uClinux" ); +tst_res (TPASS ,"brk() works fine" ); } -#endif /* if !defined(UCLINUX) */ +static struct tst_test test = { + .test_all = verify_brk, +};
Signature · 1 line
2.13.5
RFC PATCH 1/1 brk01: Rewrite from scratch.
----- Original Message -----
Quoted 2 lines
This commit rewrites the brk01 test from scratch since the original code wasn't making much sense.
git format-patch has parameter "-B", that can make big rewrites more readable.
Quoted 12 lines
Now we actually run the
brk()in a loop increasing and shrinking the break repeatedly a few times, which the original code was supposed to do but never actually did since we do not pass the -i parameter in the runtest files.The increment is also choosen to be much much more reasonable instead of using the multiple of the absolute address of the page break we choose much more modest 2 * pagesize - 1. Note that the increment is not page aligned since that way we also check that kernel page-aligns it just fine.
Quoted 7 lines
The sequence has been choosen to grow the heap first then shrink it to the original size, which should be safe since the heap never shrinks below its original size.
We also write to the newly allocated heap in a case that that the heap has grown, which should be safe enough since that way we will not rewrite anything that was previously stored there.
Shrinking and writing to heap seems little dangerous. It doesn't take much to make it crash if someone else uses heap too:
testcases/kernel/syscalls/brk/brk01.c +4 -0
@@ -22,6 +22,7 @@#include "tst_test.h" +void *ptr;void verify_brk (void ) {uintptr_t cur_brk, new_brk;
@@ -65,6 +66,9 @@ void verify_brk(void)/* Try to write to the newly allocated heap */ if (i %3 ==0 ) *((char *)cur_brk) =0 ; + ptr =malloc (2 *4096 ); +printf ("%p\n" , ptr); + }tst_res (TPASS ,"brk() works fine" );
$ ./brk01 ... tst_test.c:1022: BROK: Test killed by SIGSEGV!
My concern is that glibc allocates something as result of tst_* calls (e.g. buffered I/O write) and then we either write over it or de-allocate it. I think we should drop/postpone first tst_res().
Regards,
Jan
RFC PATCH 1/1 brk01: Rewrite from scratch.
Hi!
Quoted 2 lines
git format-patch has parameter "-B", that can make big rewrites more readable.
Will use it next time, thanks.
Quoted 23 lines
testcases/kernel/syscalls/brk/brk01.c +4
-0@@ -22,6 +22,7 @@#include "tst_test.h" +void *ptr;void verify_brk (void ) {uintptr_t cur_brk, new_brk;@@ -65,6 +66,9 @@ void verify_brk(void)/* Try to write to the newly allocated heap */ if (i %3 ==0 ) *((char *)cur_brk) =0 ; + ptr =malloc (2 *4096 ); +printf ("%p\n" , ptr); + }tst_res (TPASS ,"brk() works fine" );
Quoted 8 lines
$ ./brk01 ... tst_test.c:1022: BROK: Test killed by SIGSEGV!My concern is that glibc allocates something as result of tst_* calls (e.g. buffered I/O write) and then we either write over it or de-allocate it. I think we should drop/postpone first
tst_res().
Sounds reasonably I'm okay with dropping the tst_res() in the middle of the loop. I guess that doing any library calls between the brk() that enlarges and shrinks the heap is dangerous indeed.
And we may also increse the heap size by a few pages before we start the actual test just as a safety measure.
Signature · 2 lines
Cyril Hrubis [email protected]