hazard-analysis.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash -ex
  2. [ -n "$WORKSPACE" ]
  3. [ -n "$MOZ_OBJDIR" ]
  4. [ -n "$GECKO_DIR" ]
  5. HAZARD_SHELL_OBJDIR=$WORKSPACE/obj-haz-shell
  6. JS_SRCDIR=$GECKO_DIR/js/src
  7. ANALYSIS_SRCDIR=$JS_SRCDIR/devtools/rootAnalysis
  8. export CC="$TOOLTOOL_DIR/gcc/bin/gcc"
  9. export CXX="$TOOLTOOL_DIR/gcc/bin/g++"
  10. PYTHON=python2.7
  11. if ! which $PYTHON; then
  12. PYTHON=python
  13. fi
  14. function check_commit_msg () {
  15. ( set +e;
  16. if [[ -n "$AUTOMATION" ]]; then
  17. hg --cwd "$GECKO_DIR" log -r. --template '{desc}\n' | grep -F -q -- "$1"
  18. else
  19. echo -- "$SCRIPT_FLAGS" | grep -F -q -- "$1"
  20. fi
  21. )
  22. }
  23. if check_commit_msg "--dep"; then
  24. HAZ_DEP=1
  25. fi
  26. function build_js_shell () {
  27. # Must unset MOZ_OBJDIR and MOZCONFIG here to prevent the build system from
  28. # inferring that the analysis output directory is the current objdir. We
  29. # need a separate objdir here to build the opt JS shell to use to run the
  30. # analysis.
  31. (
  32. unset MOZ_OBJDIR
  33. unset MOZCONFIG
  34. ( cd $JS_SRCDIR; autoconf-2.13 )
  35. if [[ -z "$HAZ_DEP" ]]; then
  36. [ -d $HAZARD_SHELL_OBJDIR ] && rm -rf $HAZARD_SHELL_OBJDIR
  37. fi
  38. mkdir -p $HAZARD_SHELL_OBJDIR || true
  39. cd $HAZARD_SHELL_OBJDIR
  40. $JS_SRCDIR/configure --enable-optimize --disable-debug --enable-ctypes --enable-nspr-build --without-intl-api --with-ccache
  41. make -j4
  42. ) # Restore MOZ_OBJDIR and MOZCONFIG
  43. }
  44. function configure_analysis () {
  45. local analysis_dir
  46. analysis_dir="$1"
  47. if [[ -z "$HAZ_DEP" ]]; then
  48. [ -d "$analysis_dir" ] && rm -rf "$analysis_dir"
  49. fi
  50. mkdir -p "$analysis_dir" || true
  51. (
  52. cd "$analysis_dir"
  53. cat > defaults.py <<EOF
  54. js = "$HAZARD_SHELL_OBJDIR/dist/bin/js"
  55. analysis_scriptdir = "$ANALYSIS_SRCDIR"
  56. objdir = "$MOZ_OBJDIR"
  57. source = "$GECKO_DIR"
  58. sixgill = "$TOOLTOOL_DIR/sixgill/usr/libexec/sixgill"
  59. sixgill_bin = "$TOOLTOOL_DIR/sixgill/usr/bin"
  60. EOF
  61. cat > run-analysis.sh <<EOF
  62. #!/bin/sh
  63. if [ \$# -eq 0 ]; then
  64. set gcTypes
  65. fi
  66. export ANALYSIS_SCRIPTDIR="$ANALYSIS_SRCDIR"
  67. exec "$ANALYSIS_SRCDIR/analyze.py" "\$@"
  68. EOF
  69. chmod +x run-analysis.sh
  70. )
  71. }
  72. function run_analysis () {
  73. local analysis_dir
  74. analysis_dir="$1"
  75. local build_type
  76. build_type="$2"
  77. if [[ -z "$HAZ_DEP" ]]; then
  78. [ -d $MOZ_OBJDIR ] && rm -rf $MOZ_OBJDIR
  79. fi
  80. (
  81. cd "$analysis_dir"
  82. $PYTHON "$ANALYSIS_SRCDIR/analyze.py" --buildcommand="$GECKO_DIR/testing/mozharness/scripts/spidermonkey/build.${build_type}"
  83. )
  84. }
  85. function grab_artifacts () {
  86. local analysis_dir
  87. analysis_dir="$1"
  88. local artifacts
  89. artifacts="$2"
  90. (
  91. cd "$analysis_dir"
  92. ls -lah
  93. # Do not error out if no files found
  94. shopt -s nullglob
  95. set +e
  96. for f in *.txt *.lst; do
  97. gzip -9 -c "$f" > "${artifacts}/$f.gz"
  98. done
  99. # Check whether the user requested .xdb file upload in the top commit comment
  100. if check_commit_msg "--upload-xdbs"; then
  101. HAZ_UPLOAD_XDBS=1
  102. fi
  103. if [ -n "$HAZ_UPLOAD_XDBS" ]; then
  104. for f in *.xdb; do
  105. bzip2 -c "$f" > "${artifacts}/$f.bz2"
  106. done
  107. fi
  108. )
  109. }
  110. function check_hazards () {
  111. (
  112. set +e
  113. NUM_HAZARDS=$(grep -c 'Function.*has unrooted.*live across GC call' "$1"/rootingHazards.txt)
  114. NUM_UNSAFE=$(grep -c '^Function.*takes unsafe address of unrooted' "$1"/refs.txt)
  115. NUM_UNNECESSARY=$(grep -c '^Function.* has unnecessary root' "$1"/unnecessary.txt)
  116. set +x
  117. echo "TinderboxPrint: rooting hazards<br/>$NUM_HAZARDS"
  118. echo "TinderboxPrint: unsafe references to unrooted GC pointers<br/>$NUM_UNSAFE"
  119. echo "TinderboxPrint: unnecessary roots<br/>$NUM_UNNECESSARY"
  120. if [ $NUM_HAZARDS -gt 0 ]; then
  121. echo "TEST-UNEXPECTED-FAIL $NUM_HAZARDS hazards detected" >&2
  122. echo "TinderboxPrint: documentation<br/><a href='https://wiki.mozilla.org/Javascript:Hazard_Builds'>static rooting hazard analysis failures</a>, visit \"Inspect Task\" link for hazard details"
  123. exit 1
  124. fi
  125. )
  126. }