|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# Tests that build_support/asan_symbolize.py, which every test binary pipes its |
| 19 | +# output through, passes bytes that are not valid UTF-8 straight through. A test |
| 20 | +# that fails while printing such a byte used to kill the symbolizer, and with it |
| 21 | +# the rest of the test log. |
| 22 | + |
| 23 | +set -euo pipefail |
| 24 | + |
| 25 | +source_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) |
| 26 | +symbolizer="${source_dir}/build_support/asan_symbolize.py" |
| 27 | +python=${PYTHON:-python3} |
| 28 | +# The vendored script emits SyntaxWarnings for its own regexes; they are unrelated noise here. |
| 29 | +export PYTHONWARNINGS=ignore |
| 30 | + |
| 31 | +status=0 |
| 32 | + |
| 33 | +# Returns non-zero on failure as well as recording it, so that a caller running it in a |
| 34 | +# subshell, where the recorded status would not propagate, can still see the result. |
| 35 | +check() { |
| 36 | + local name=$1 input=$2 expected=$3 actual_hex expected_hex result=0 |
| 37 | + echo "=== ${name} ===" |
| 38 | + # Compare hex dumps so that a mismatch is readable and the shell does not |
| 39 | + # mangle the bytes on the way. A failing symbolizer is a failed check, not a |
| 40 | + # reason to abort the script, so the remaining checks still run. |
| 41 | + if ! actual_hex=$(printf '%b' "${input}" | "${python}" "${symbolizer}" | od -An -tx1 | |
| 42 | + tr -d ' \n'); then |
| 43 | + echo "symbolizer exited non-zero" |
| 44 | + status=1 |
| 45 | + result=1 |
| 46 | + fi |
| 47 | + expected_hex=$(printf '%b' "${expected}" | od -An -tx1 | tr -d ' \n') |
| 48 | + if [[ "${actual_hex}" != "${expected_hex}" ]]; then |
| 49 | + echo "expected: ${expected_hex}" |
| 50 | + echo "actual: ${actual_hex}" |
| 51 | + status=1 |
| 52 | + result=1 |
| 53 | + fi |
| 54 | + return "${result}" |
| 55 | +} |
| 56 | + |
| 57 | +# A lone 0xff is what a failing TINYINT literal assertion prints. Every byte, |
| 58 | +# valid UTF-8 or not, has to come out unchanged. |
| 59 | +check "invalid utf-8 round trips" 'ok\n\xff\xfe binary\nplain\n' 'ok\n\xff\xfe binary\nplain\n' |
| 60 | + |
| 61 | +# Valid multi byte UTF-8 must not be damaged either: test names contain it. |
| 62 | +check "utf-8 round trips" '\xe4\xb8\xad\xe6\x96\x87\n' '\xe4\xb8\xad\xe6\x96\x87\n' |
| 63 | + |
| 64 | +# The bytes have to survive whatever the environment asks Python to use: an encoding taken |
| 65 | +# from PYTHONIOENCODING on one stream and from the locale on the other would re-encode them. |
| 66 | +( |
| 67 | + export PYTHONIOENCODING=latin-1 |
| 68 | + check "invalid utf-8 round trips under PYTHONIOENCODING" 'ok\n\xff\xfe\n' 'ok\n\xff\xfe\n' |
| 69 | + # status is set in this subshell and does not reach the caller, so report it as the exit |
| 70 | + # code, which stays correct if another check is added here. |
| 71 | + exit "${status}" |
| 72 | +) || status=1 |
| 73 | + |
| 74 | +# An invalid byte must not stop the lines that follow it from being processed: |
| 75 | +# the stack frame below is still rewritten by the symbolizer. Its stderr is dropped |
| 76 | +# because addr2line reports the fake binary path there even when the check passes. |
| 77 | +echo "=== keeps processing after an invalid byte ===" |
| 78 | +if ! output=$(printf '%b' '\xff\n #0 0x7f6e35cf2e45 (/blah/foo.so+0x11fe45)\ntail\n' | |
| 79 | + "${python}" "${symbolizer}" 2>/dev/null); then |
| 80 | + echo "symbolizer exited non-zero" |
| 81 | + status=1 |
| 82 | +fi |
| 83 | +# "#0" and "tail" alone would also match unprocessed input, so assert that the frame was |
| 84 | +# actually rewritten: the symbolized form gains " in" and loses the raw binary path. |
| 85 | +if [[ "${output}" != *"#0"* || "${output}" != *"tail"* || "${output}" != *" in"* || |
| 86 | + "${output}" == *"/blah/foo.so"* ]]; then |
| 87 | + echo "frame was not rewritten after the invalid byte, got:" |
| 88 | + echo "${output}" |
| 89 | + status=1 |
| 90 | +fi |
| 91 | + |
| 92 | +exit "${status}" |
0 commit comments