| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Smoke tests for OPy.
 | 
| 4 | #
 | 
| 5 | # Usage:
 | 
| 6 | #   ./smoke.sh <function name>
 | 
| 7 | 
 | 
| 8 | set -o nounset
 | 
| 9 | set -o pipefail
 | 
| 10 | set -o errexit
 | 
| 11 | 
 | 
| 12 | source common.sh
 | 
| 13 | source compare.sh  # for DIFF
 | 
| 14 | 
 | 
| 15 | #
 | 
| 16 | # Determinism
 | 
| 17 | #
 | 
| 18 | # There are problems here, but it's because of an underlying Python 2.7 issue.
 | 
| 19 | # For now we will do functional tests.
 | 
| 20 | #
 | 
| 21 | 
 | 
| 22 | # Doesn't suffice for for compiler2 determinism.
 | 
| 23 | #export PYTHONHASHSEED=0
 | 
| 24 | 
 | 
| 25 | # For comparing different bytecode.
 | 
| 26 | compare-bytecode() {
 | 
| 27 |   local left=$1
 | 
| 28 |   local right=$2
 | 
| 29 | 
 | 
| 30 |   md5sum "$@"
 | 
| 31 |   ls -l "$@"
 | 
| 32 | 
 | 
| 33 |   opyc-dis $left > _tmp/pyc-left.txt
 | 
| 34 |   opyc-dis $right > _tmp/pyc-right.txt
 | 
| 35 |   $DIFF _tmp/pyc-{left,right}.txt || true
 | 
| 36 | 
 | 
| 37 |   return
 | 
| 38 |   strings $left > _tmp/str-left.txt
 | 
| 39 |   strings $right > _tmp/str-right.txt
 | 
| 40 | 
 | 
| 41 |   # The ORDER of strings is definitely different.  But the SIZE and CONTENTS
 | 
| 42 |   # are too!
 | 
| 43 |   # Solution: can you walk your own code objects and produce custom .pyc files?
 | 
| 44 | 
 | 
| 45 |   diff -u _tmp/str-{left,right}.txt || true
 | 
| 46 | 
 | 
| 47 |   #xxd $left > _tmp/hexleft.txt
 | 
| 48 |   #xxd $right > _tmp/hexright.txt
 | 
| 49 |   #diff -u _tmp/hex{left,right}.txt || true
 | 
| 50 |   echo done
 | 
| 51 | }
 | 
| 52 | 
 | 
| 53 | # Compile opy_ a 100 times and make sure it's the same.
 | 
| 54 | #
 | 
| 55 | # NOTE: This doesn't surface all the problems.  Remember the fix was in
 | 
| 56 | # compiler2/misc.py:Set.
 | 
| 57 | determinism-loop() {
 | 
| 58 |   local func=$1
 | 
| 59 |   local file=${2:-./opy_main.py}
 | 
| 60 | 
 | 
| 61 |   mkdir -p _tmp/det
 | 
| 62 | 
 | 
| 63 |   local name=$(basename $file)
 | 
| 64 |   for i in $(seq 100); do
 | 
| 65 |     echo "--- $i ---"
 | 
| 66 |     $func $file _tmp/det/$name.1
 | 
| 67 |     $func $file _tmp/det/$name.2
 | 
| 68 | 
 | 
| 69 |     opyc-dis-md5 _tmp/det/$name.{1,2} | tee _tmp/det/md5.txt
 | 
| 70 |     awk '
 | 
| 71 |     NR == 1 { left = $2 } 
 | 
| 72 |     NR == 2 { right = $2 } 
 | 
| 73 |     END     { if (NR != 2) {
 | 
| 74 |                 print "Expected two rows, got " NR
 | 
| 75 |                 exit(1)
 | 
| 76 |               }
 | 
| 77 |               if (left != right) { 
 | 
| 78 |                 print "FATAL: .pyc files are different!"
 | 
| 79 |                 exit(1)
 | 
| 80 |               }
 | 
| 81 |             }
 | 
| 82 |     ' _tmp/det/md5.txt
 | 
| 83 |     local status=$?
 | 
| 84 | 
 | 
| 85 |     if test $status != 0; then
 | 
| 86 |       compare-bytecode _tmp/det/$name.{1,2}
 | 
| 87 |       echo "Found problem after $i iterations"
 | 
| 88 |       break
 | 
| 89 |     fi
 | 
| 90 | 
 | 
| 91 |     #local size1=$(stat --format '%s' _tmp/det/$name.1)
 | 
| 92 |     #local size2=$(stat --format '%s' _tmp/det/$name.2)
 | 
| 93 | 
 | 
| 94 |     #if test $size1 != $size2; then
 | 
| 95 |     #  echo "mismatched sizes: $size1 $size2"
 | 
| 96 |     #  # TODO: Import from run.sh
 | 
| 97 |     #  compare-bytecode _tmp/det/$name.{1,2}
 | 
| 98 |     #  echo "Found problem after $i iterations"
 | 
| 99 |     #  break
 | 
| 100 |     #fi
 | 
| 101 |   done
 | 
| 102 | }
 | 
| 103 | 
 | 
| 104 | opyc-compile() { ../bin/opyc compile "$@"; }
 | 
| 105 | opyc-dis() { ../bin/opyc dis "$@"; }
 | 
| 106 | opyc-dis-md5() { ../bin/opyc dis-md5 "$@"; }
 | 
| 107 | stdlib-compile() { misc/stdlib_compile.py "$@"; }
 | 
| 108 | 
 | 
| 109 | # FAILS
 | 
| 110 | opy-determinism-loop() {
 | 
| 111 |   #local file=../core/lexer.py
 | 
| 112 |   local file=../core/word_compile.py  # FIXED
 | 
| 113 |   #local file=../Python-2.7.13/Lib/genericpath.py
 | 
| 114 |   determinism-loop opyc-compile $file
 | 
| 115 | }
 | 
| 116 | 
 | 
| 117 | # FAILS
 | 
| 118 | stdlib-determinism-loop() {
 | 
| 119 |   #local file=../core/lexer.py
 | 
| 120 |   local file=../core/word_compile.py  # flanders has issue
 | 
| 121 |   determinism-loop stdlib-compile $file
 | 
| 122 | }
 | 
| 123 | 
 | 
| 124 | # BUG: FlowGraph flattening was non-deterministic.  It's a graph that is
 | 
| 125 | # correct in several orders.  See order_blocks() in pyassem.py.
 | 
| 126 | 
 | 
| 127 | # Not able to reproduce the non-determinism with d.keys()?  Why not?
 | 
| 128 | hash-determinism() {
 | 
| 129 |   local in=$1
 | 
| 130 |   local out=$2
 | 
| 131 |   # This changes it.  Python 2.7 doesn't have it on by default I guess.
 | 
| 132 |   # So what is the cause in the compiler package?
 | 
| 133 |   #export PYTHONHASHSEED=random
 | 
| 134 | 
 | 
| 135 |   misc/determinism.py $in > $out
 | 
| 136 | }
 | 
| 137 | 
 | 
| 138 | hash-determinism-loop() {
 | 
| 139 |   determinism-loop hash-determinism
 | 
| 140 | }
 | 
| 141 | 
 | 
| 142 | rebuild-and-md5() {
 | 
| 143 |   cd ..
 | 
| 144 |   make clean-repo
 | 
| 145 |   make _bin/oil.ovm-dbg
 | 
| 146 |   local out=_tmp/pyc-md5.txt
 | 
| 147 |   build/metrics.sh pyc-md5 | sort -n | tee $out
 | 
| 148 | 
 | 
| 149 |   log ""
 | 
| 150 |   log "Wrote $out"
 | 
| 151 | }
 | 
| 152 | 
 | 
| 153 | copy-left-right() {
 | 
| 154 |   local src=flanders.local:~/git/oilshell/oil
 | 
| 155 |   mkdir -p _tmp/flanders _tmp/lisa
 | 
| 156 |   scp $src/_build/oil/bytecode-opy.zip $src/_tmp/pyc-md5.txt _tmp/flanders
 | 
| 157 |   src=..
 | 
| 158 |   cp -v $src/_build/oil/bytecode-opy.zip $src/_tmp/pyc-md5.txt _tmp/lisa
 | 
| 159 | }
 | 
| 160 | 
 | 
| 161 | diff-left-right() {
 | 
| 162 |   if diff _tmp/{lisa,flanders}/pyc-md5.txt; then
 | 
| 163 |     echo SAME
 | 
| 164 |   else
 | 
| 165 |     echo DIFFERENT
 | 
| 166 |   fi
 | 
| 167 | }
 | 
| 168 | 
 | 
| 169 | unzip-left-right() {
 | 
| 170 |   for host in lisa flanders; do
 | 
| 171 |     pushd _tmp/$host
 | 
| 172 |     unzip bytecode-opy.zip core/word_compile.pyc
 | 
| 173 |     popd
 | 
| 174 |   done
 | 
| 175 | }
 | 
| 176 | 
 | 
| 177 | diff-one-left-right() {
 | 
| 178 |   for host in lisa flanders; do
 | 
| 179 |     opyc-dis _tmp/$host/core/word_compile.pyc > _tmp/$host/word_compile.dis
 | 
| 180 |   done
 | 
| 181 |   diff -u _tmp/{lisa,flanders}/word_compile.dis
 | 
| 182 | }
 | 
| 183 | 
 | 
| 184 | if test $(basename $0) = 'smoke.sh'; then
 | 
| 185 |   "$@"
 | 
| 186 | fi
 |