src/runtest

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    
    #!/bin/sh
    
    FILE="$1"
    test="${FILE%.*}"
    ext="${FILE##*.}"
    
    TIMEOUT="timeout --preserve-status 3h"
    
    if test -n "$HTTP_LOGFILE"; then
        exec 2>> "$HTTP_LOGFILE"
    fi
    
    # for debugging
    # BASH_XTRACEFD=2
    # set -x
    
    if test -f $test.c; then
        src=$test.c
    elif test -f $test.m; then
        src=$test.m
    else
        echo "No source file found for $test" >&2
        exit 1
    fi
    
    case $ext in
        ctst) log=clog; out=cout; ref=cref; ;;
        *)    log=log;  out=out; ref=ref; ;;
    esac
    
    case $CC in
        *-D_MPI=*) test -z "$EXEC" && EXEC="mpirun -np "`echo $CC | sed 's/.*-D_MPI=\([0-9]*\).*/\1/'`;
    esac
    
    for opt in $CFLAGS; do
        case $opt in
    	-catch) test -z "$EXEC" && EXEC=" "; # turn off gdb
        esac
    done
    
    case $OSTYPE in
        *darwin*) ;; # gdb is (often) broken on Apple OSX
        *)
    	if test -z "$GDB"; then
    	    GDB=`which gdb`
    	fi
    	;;
    esac
    
    script()
    {
        rm -f $src display.html
        if test -f fail; then
    	return 1
        fi
        test -s warn || rm -f warn
    
        pass=true
        rm -f log-* stencil stencil-* fine fine-* coarse coarse-*
        tail -f --pid=$$ --retry display.html 2> /dev/null | grep -o 'http://.*:[0-9]*' &
        if test -n "$EXEC" -o -z "$GDB"; then
    	if ! $EXEC ./$test 2> $log > $out; then
    	    pass=false
    	    if test "$EXEC" = " " -a -f core -a -n "$GDB"; then
    		cat $log
    		xterm -e "gnuplot plot -" &
    		$GDB -q ./$test core
    	    fi
    	fi
        else
    	if ! $GDB -batch -return-child-result -ex "run 2> $log > $out" ./$test > gdb.log 2> gdb.err; then
    	    pass=false
    	    $AWK '
         {
           if (NF > 0)
             a[nb++] = $0;
         }
         / at .*:[0-9]*$/ {
           for (i = 0; i < nb - 1; i++)
    	 print $(NF) ":error: " a[i];
         }
         ' < gdb.log >> $log
    	fi
    	rm -f gdb.log
        fi
    
        if test -f log-1; then
    	mv -f $log log-0
    	cat log-* > $log
        fi
    
        if $pass; then
    	if test -f $test.$ref; then
    	    ref=$ref
    	else
    	    ref=ref
    	fi
    	if test -f $test.$ref; then
                echo diff $log $test.$ref > fail
                diff $log $test.$ref >> fail && rm -f fail
                rm -f $test.$ref
    	fi
        else
    	cp -f $log fail
        fi
        if test -f fail; then
    	return 1
        fi
        touch pass
    }
    
    # run locally (C code)
    locally()
    {
        echo \[$test.$ext\]
        $BASILISK/qcc -autolink -disable-dimensions $CFLAGS -o $test/$test $src $LIBS -lm > $test/log 2>&1 \
    	|| mv -f $test/log $test/fail
        cd $test
        script
        cd ..
    }
    
    # run locally (Octave code)
    locally_octave()
    {
        echo \[$test.$ext\]
        cd $test
        if octave-cli --path .. -W -H -q $src 2> $log > $out && \
    	    ! grep -q '^error:' $log; then
    	touch pass
        else
    	cp -f $log fail
        fi
        cd ..
    }
    
    # run untrusted code using the 'basilisk-untrusted' user if it exists
    run_untrusted()
    {
        status=0
        if test -n "$chksum"; then
    	if grep -q basilisk-untrusted /etc/passwd; then
    	    if ! mkdir -m 777 ../$chksum; then
    		return 1
    	    fi
    	    if ! sudo -n -u basilisk-untrusted -- \
    		 bash -c "cp -arf * ../$chksum && cd ../$chksum && $1"; then
    		status=1
    	    fi
    	    if ! cp -arf ../$chksum/* .; then
    		status=1
    	    fi
    	    sudo -n -u basilisk-untrusted -- rm -rf ../$chksum/*
    	    rm -r -f ../$chksum
    	else
    	    echo "Could not find the 'basilisk-untrusted' user" >2
    	    status=1
    	fi
        fi
        return $status
    }
    
    doplots()
    {
        if test -f "$1".plot && ! run_untrusted 'gnuplot -e "batch=1; PNG=\"'$PNG'\"; set term '$PNG' enhanced font \",10\"; set output \"plot.png\"; set macros;" '$1'.plot > tmp 2>&1;'; then
            cat tmp >> warn
        fi
        if ! run_untrusted "PNG=$PNG sh $BASILISK/gnuplot.sh $1 > tmp 2>&1"; then
    	cat tmp >> warn
        fi
        if ! run_untrusted "MPLBACKEND=Agg python3 plots.py > tmp 2>&1"; then
    	cat tmp >> warn
        fi
        rm -f tmp
    }
    
    # compile/run on a remote "sandbox"
    # NOTE: update remotely_octave() when changing this
    remotely()
    {
        rhost=$1
        autolink=`$BASILISK/qcc -autolink -progress -source -disable-dimensions $CFLAGS $src`
        chksum=`(cat $test.s && pwd && echo $test) | $GENSUM | cut -d' ' -f1`.$ext
        mkdir $test/$chksum
        cd $test
        run=`echo $test | sed 's/^~//'`
        if test "$run" != "$test"; then
    	cp -f $run.* $chksum 2> /dev/null || true
        fi
        cp -f $test.* $chksum 2> /dev/null || true
        rm -f $chksum/$src.html
        if test `echo "$test" | sed 's/^~//'` != "$test"; then
    	cd $chksum
    	for f in "$test".*; do
    	    mv -f "$f" `echo "$f" | sed 's/^~//'`
    	done
    	cd ..
        fi
        mv -f ../_$src $chksum/$src
        PCC="\$CC99"
        case "$CFLAGS" in
    	*-cadna*) PCC="\$CADNACC" ;;
        esac
        PCFLAGS=`echo $CFLAGS | sed -e 's/-grid=[^-]*//g' -e 's/-cadna//g' -e 's/-progress//g' -e 's/-Wdimensions//g'`
        NCORES=`echo $EXEC | awk 'BEGIN{ np = 1 }{
          if ($1 == "mpirun") np = $3; }END{ print np;}'`
        case "$CFLAGS" in
    	*-fopenmp*)
    	    NCORES=8
    	    TIMEOUT="OMP_NUM_THREADS=8 $TIMEOUT"
    	    ;;
        esac
        cat <<EOF >$chksum/$chksum.sh
    #!/bin/sh
    
    run_untrusted()
    {
      status=0
      notrust=/tmp/$chksum
      if ! sudo -n -u basilisk-untrusted -- bash -c "source /home/basilisk/.bashrc && mkdir \$notrust && cp -arf * \$notrust && cd \$notrust && rm -f completing && \$1 && touch completing"; then
         status=1
      fi
      if ! cp -arf \$notrust/* .; then
         status=1
      fi
      sudo -n -u basilisk-untrusted -- rm -rf \$notrust/
      return \$status
    }
    
    $PCC $PCFLAGS -I\$HOME/include -o $chksum $src -L\$HOME/lib $LIBS $autolink -lm > log 2>&1 || mv -f log fail
    rm -f $src
    if ! test -f fail; then
      pass=true
      if test -n "$EXEC" -o -z "`which gdb`"; then
        if ! run_untrusted "$TIMEOUT $EXEC ./$chksum 2> $log > $out"; then
           pass=false
        fi
      else
        if ! run_untrusted "$TIMEOUT gdb -batch -return-child-result -ex \"run 2> $log > $out\" $chksum > gdb.log 2> gdb.err" > gdb.log 2> gdb.err; then
           pass=false
           cat gdb.err >> $log
           awk '
           {
             if (NF > 0)
               a[nb++] = \$0;
           }
           / at .*:[0-9]*\$/ {
             for (i = 0; i < nb - 1; i++)
       	   print \$(NF) ":error: " a[i];
           }
           ' < gdb.log >> $log
        fi
        rm -f gdb.log
      fi    
    
      if test -f log-1; then
          mv -f $log log-0
          cat log-* > $log
      fi
    
      if \$pass; then
        if test -f $test.$ref; then
          ref=$ref
        else
          ref=ref
        fi
        if test -f $test.\$ref; then
            echo diff $log $test.\$ref > fail
            diff $log $test.\$ref >> fail && rm -f fail
            rm -f $test.\$ref
        fi
      else
        if ! test -s $log; then
           echo "The code timed out after one hour." > $log
        fi
        sed "s/^$chksum: //g" $log > fail
      fi
    fi
    rm -f $chksum $src $chksum.sh $test.*ref
    tar czf \$HOME/$chksum.tgz *
    rm -r -f \$HOME/$chksum
    EOF
        tar czf $chksum.tgz $chksum
        rm -r -f $chksum/
    
        if ! ( scp $chksum.tgz $rhost: && rm -f $chksum.tgz && \
    	       ssh $rhost bash -c "\"tar xmzf $chksum.tgz && cd $chksum && tsp -N \$(test \$(tsp -S) -le $NCORES && echo \$(tsp -S) || echo $NCORES) -n nice -19 bash $chksum.sh\" && sleep 0 && echo $chksum && tsp -w" > tspid.$ext && \
    	       scp -q $rhost:$chksum.tgz $chksum.tgz && \
    	       ssh $rhost rm -r -f $chksum.tgz && \
    	       tar xmzf $chksum.tgz && \
    	       rm -f $chksum.tgz $src $test.*ref progress) > /dev/null 2>> ssh.log;
        then
    	cat ssh.log >> fail
        fi
        rm -f ssh.log
    
        # generate graphics
        doplots "$test"
    
        if test -f fail; then
    	if test -f ../$test.$ext; then
    	    mv -f ../$test.$ext fail.$ext
    	else
    	    short=`echo $test | sed 's/^~//'`
    	    test -f ../$short.$ext && mv -f ../$short.$ext fail.$ext
    	fi
    	echo
    	echo \[$test.$ext\]
    	cat fail
        else
    	touch pass
        fi
        # Complete
        rm -f *pid.$ext completing
    }
    
    octave_deps()
    {
        if test -z "$DOCUMENT_ROOT"; then
    	d=`pwd`
    	while ! test -d _darcs; do
    	    cd ..
    	done
    	DOCUMENT_ROOT=`pwd`
    	cd "$d"	
        fi
        path=`dirname $1`" "`grep --only-matching 'addpath[ \t]*( *"[^"]*" *)' $1 | sed -e 's/addpath[ \t]*( *"\([^"]*\)" *)/\1/g' -e "s|^~|$DOCUMENT_ROOT|g"`
        for i in `grep -E --only-matching -h '[a-zA-Z_0-9]+[ \t]*\(' $1 | sort | uniq | sed 's/(//g'`; do
    	for p in $path; do
    	    test -f "$p/$i.m" -a "$p/$i.m" != "$1" && echo "$p/$i.m" && octave_deps "$p/$i.m" && break;
    	done
        done | sort | uniq
    }
    
    # compile/run on a remote "sandbox" (OCTAVE)
    remotely_octave()
    {
        rhost=$1
        chksum=`(cat $test.s && pwd && echo $test) | $GENSUM | cut -d' ' -f1`.$ext
        mkdir $test/$chksum
        mkdir $test/$chksum/lib/
        cp -f $test.m `octave_deps ./$test.m` $test/$chksum/lib/
        cd $test
        run=`echo $test | sed 's/^~//'`
        if test "$run" != "$test"; then
    	cp -f $run.* $chksum 2> /dev/null || true
        fi
        cp -f $test.* $chksum 2> /dev/null || true
        rm -f $chksum/$src.html
        if test `echo "$test" | sed 's/^~//'` != "$test"; then
    	cd $chksum
    	for f in "$test".*; do
    	    mv -f "$f" `echo "$f" | sed 's/^~//'`
    	done
    	cd ..
        fi
        cp -f ../$src $chksum/$src
        cat <<EOF >$chksum/$chksum.sh
    #!/bin/bash
    
    run_untrusted()
    {
      status=0
      notrust=/tmp/$chksum
      if ! sudo -n -u basilisk-untrusted -- bash -c "source /home/basilisk/.bashrc && mkdir \$notrust && cp -arf * \$notrust && cd \$notrust && rm -f completing && \$1 && touch completing"; then
         status=1
      fi
      if ! cp -arf \$notrust/* .; then
         status=1
      fi
      sudo -n -u basilisk-untrusted -- rm -rf \$notrust/
      return \$status
    }
    
    # workaround for octave bug with ~ in file names
    src1="$src"
    if [[ "$src" = ~* ]]; then  
      src1=_\${src1##\~};
      ln -s -f "$src" "\$src1"
    fi
    
    pass=true
    if ! run_untrusted "$TIMEOUT octave-cli --path lib -W -H -q \$src1 2> $log > $out" || grep -q '^error:' $log; then
        pass=false
    fi
    
    if [[ "\$src1" != "$src" ]]; then
       rm -f "\$src1"
    fi
    
    if \$pass; then
        if test -f $test.$ref; then
          ref=$ref
        else
          ref=ref
        fi
        if test -f $test.\$ref; then
            echo diff $log $test.\$ref > fail
            diff $log $test.\$ref >> fail && rm -f fail
            rm -f $test.\$ref
        fi
    else
        if ! test -s $log; then
           echo "The code timed out after one hour." > $log
        fi
        cp -f $log fail
    fi
    if ! test -f fail; then
        touch pass
    fi
    rm -r -f $chksum $src $chksum.sh $test.*ref lib/
    tar czf \$HOME/$chksum.tgz *
    rm -r -f \$HOME/$chksum
    EOF
        tar czf $chksum.tgz $chksum
        rm -r -f $chksum/
    
        if ! ( scp $chksum.tgz $rhost: && rm -f $chksum.tgz && \
    	       ssh $rhost bash -c "\"tar xmzf $chksum.tgz && cd $chksum && tsp -n nice -19 bash $chksum.sh\" && sleep 0 && echo $chksum && tsp -w" > tspid.$ext && \
    	       scp -q $rhost:$chksum.tgz $chksum.tgz && \
    	       ssh $rhost rm -r -f $chksum.tgz && \
    	       tar xmzf $chksum.tgz && \
    	       rm -f $chksum.tgz $src $test.*ref progress) > /dev/null 2>> ssh.log;
        then
    	cat ssh.log >> fail
        fi
        rm -f ssh.log
    
        if test -f fail; then
    	if test -f ../$test.$ext; then
    	    mv -f ../$test.$ext fail.$ext
    	else
    	    short=`echo $test | sed 's/^~//'`
    	    test -f ../$short.$ext && mv -f ../$short.$ext fail.$ext
    	fi
    	echo
    	echo \[$test.$ext\]
    	cat fail
        else
    	touch pass
        fi
        # Complete
        rm -f *pid.$ext completing
    }
    
    checksum()
    {
        if grep $1 $2 | $CHECKSUM; then 
    	return 0;
        else
    	return 1;
        fi
    }
    
    # check sum for $src
    source_modified=false
    if ! test -f $test.$ext || ! checksum $src $test.$ext; then
        source_modified=true
    fi
    
    killchildren()
    {
        p=$1
        c=`ps -o pid= --ppid $p`
        kill $p
        while test -n "$c"; do
    	p=$c
    	c=`ps -o pid= --ppid $p`
    	kill $p
        done
    }
    
    # check sum for test.s
    if test -f $test.$ext && checksum $test.s $test.$ext; then
        touch $test.$ext
        echo "make: '$test.$ext' is up to date."
        if $source_modified; then
    	if test -n "$SANDBOX"; then
    	    rm -f $test/warn
    	    $AWK -f $BASILISK/gnuplot.awk < $src > $test/plots
    	    $AWK -f $BASILISK/python.awk < $src > $test/plots.py
    	    chksum=`(cat $test.s && pwd && echo $test) | $GENSUM | cut -d' ' -f1`.$ext
    	    cd $test
    	    doplots "$test"
    	    cd ..
    	fi
    	$GENSUM $src $test.s > $test.$ext
        fi
    # check sum for test.s in fail.tst
    elif ! test -f $test/fail.$ext || ! test -f $test/fail || ! checksum $test.s $test/fail.$ext; then
        if test -n "$SANDBOX" -a -f $test/pid.$ext; then
    	killchildren `cat $test/pid.$ext`
    	if test -f $test/tspid.$ext; then
    	    ssh $SANDBOX killtest `cat $test/tspid.$ext`
    	fi
    	rm -f $test/*pid.$ext
        fi
    
        rm -f $test.$ext $test/pass \
    	$test/fail $test/fail.* $test/plot.png $test/plots $test/core
        $GENSUM $src $test.s > $test.$ext
    
        mkdir -p $test && cp -f $src $test
        $AWK -f $BASILISK/gnuplot.awk < $src > $test/plots
        $AWK -f $BASILISK/python.awk < $src > $test/plots.py
        cp -f $test.* $test 2> /dev/null || true
        run=`echo $test | sed 's/^~//'`
        if test "$run" != "$test"; then
    	cp -f $run.* $test 2> /dev/null || true
        fi
        rm -f $test/*.[sd] $test/*.*tst $test/$src.html 2> /dev/null || true
    
        if test -n "$SANDBOX"; then
    	case "$src" in
    	    *.c) ( remotely $SANDBOX ) & ;;
    	    *.m) ( remotely_octave $SANDBOX ) & ;;
    	esac
    	echo $! > $test/pid.$ext
    	sleep 1
        else
    	case "$src" in
    	    *.c) locally ;;
    	    *.m) locally_octave ;;
    	esac
    	rm -f $test/plots $test/plots.py
        fi
    fi
    
    if test -f $test/fail; then
        cat $test/fail
        if test -f $test.$ext; then
    	if test -n "$SANDBOX"; then
    	    mv -f $test.$ext $test/fail.tst
    	else
    	    rm -f $test.$ext $test/fail.tst
    	fi
        fi
        exit 1
    elif test -f $test/pid.$ext; then
        echo \[$test.$ext on $SANDBOX \(`cat $test/pid.$ext`\)\]
        echo "  running..."
    fi