Files
wasm-simulator/test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh

170 lines
7.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
linuxcnc_root=${LINUXCNC_ROOT:-../linuxcnc}
work_dir=$(mktemp -d "${TMPDIR:-/tmp}/cnc_sim_linuxcnc_wasm_rs274ngc_pre_link_blockers.XXXXXX")
manifest=${1:-linuxcnc-rs274-wasm-source-files.txt}
keep_report_file=0
cleanup_rs274ngc_pre_blocker_temps() {
rm -rf "$work_dir"
if [[ "$keep_report_file" -eq 0 && -n "${report_file:-}" ]]; then
rm -f "$report_file"
fi
}
trap cleanup_rs274ngc_pre_blocker_temps EXIT
grep -Fx 'trap cleanup_rs274ngc_pre_blocker_temps EXIT' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh >/dev/null
if awk '/^} > "\$report_file" \|\| true$/{ found = 1 } END { exit !found }' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh >/dev/null; then
echo "rs274ngc_pre blocker scan still hides report pipeline failures with || true" >&2
exit 1
fi
if ! awk '
index($0, "check-linuxcnc-inputs-cached.sh --cache-dir \"$preflight_cache_dir\" \"$manifest\"") { check_line = NR }
index($0, "cache_dir=${CNC_SIM_WASM_RS274NGC_PRE_LINK_BLOCKERS_CACHE_DIR-build/wasm-rs274ngc-pre-link-blockers}") { report_line = NR }
END { exit !(check_line && report_line && check_line < report_line) }
' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh; then
echo "rs274ngc_pre blocker scan prepares its report cache before input validation" >&2
exit 1
fi
if ! awk '
index($0, "if [[ \"$keep_report_file\" -eq 0 && -n \"${report_file:-}\" ]]; then") { cleanup_line = NR }
index($0, "keep_report_file=1") { keep_line = NR }
index($0, "echo \"wrote $report_file\"") { wrote_line = NR }
END { exit !(cleanup_line && keep_line && wrote_line && cleanup_line < keep_line && keep_line < wrote_line) }
' test-linuxcnc-wasm-rs274ngc-pre-link-blockers.sh; then
echo "rs274ngc_pre blocker scan does not clean failed reports before preserving successful ones" >&2
exit 1
fi
preflight_cache_dir=${CNC_SIM_PREFLIGHT_CACHE_DIR:-build/wasm-probe-preflight-cache}
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-inputs-cached.sh --cache-dir "$preflight_cache_dir" "$manifest"
cache_dir=${CNC_SIM_WASM_RS274NGC_PRE_LINK_BLOCKERS_CACHE_DIR-build/wasm-rs274ngc-pre-link-blockers}
if [[ -z "$cache_dir" ]]; then
echo "CNC_SIM_WASM_RS274NGC_PRE_LINK_BLOCKERS_CACHE_DIR must not be empty" >&2
exit 1
fi
if [[ "$cache_dir" =~ [[:space:]] ]]; then
echo "CNC_SIM_WASM_RS274NGC_PRE_LINK_BLOCKERS_CACHE_DIR must not contain whitespace: $cache_dir" >&2
exit 1
fi
mkdir -p "$cache_dir"
cache_dir=$(cd "$cache_dir" && pwd)
if [[ "$cache_dir" == "/" ]]; then
echo "CNC_SIM_WASM_RS274NGC_PRE_LINK_BLOCKERS_CACHE_DIR must not be the filesystem root" >&2
exit 1
fi
exec 8>"$cache_dir/rs274ngc-pre-link-blockers.lock"
flock 8
object_output=$(CNC_SIM_PREFLIGHT_CACHE_DIR="$preflight_cache_dir" LINUXCNC_ROOT="$linuxcnc_root" ./build-linuxcnc-wasm-probe-objects.sh --mode rs274ngc-pre "$manifest")
mapfile -t objects <<<"$object_output"
manifest=$(cd "$(dirname "$manifest")" && pwd)/$(basename "$manifest")
linuxcnc_root=$(cd "$linuxcnc_root" && pwd)
undefined_file="$work_dir/undefined.raw"
defined_file="$work_dir/defined.raw"
signature="$cache_dir/rs274ngc-pre-link-blockers.signature"
next_signature="$cache_dir/rs274ngc-pre-link-blockers.signature.next"
report_file="$cache_dir/rs274ngc-pre-link-blockers.txt"
next_report_file="$cache_dir/rs274ngc-pre-link-blockers.txt.next"
{
printf 'PWD=%s\n' "$PWD"
printf 'LINUXCNC_ROOT=%s\n' "$linuxcnc_root"
printf 'MANIFEST=%s\n' "$manifest"
printf 'FILTER_RULE_VERSION=1\n'
printf 'OBJECTS:\n'
printf '%s\n' "${objects[@]}"
printf 'OBJECT_STATS:\n'
stat -c 'OBJECT=%n:%s:%y' "${objects[@]}"
} > "$next_signature"
if [[ -f "$signature" && -f "$report_file" ]] && cmp -s "$signature" "$next_signature"; then
keep_report_file=1
if [[ -s "$report_file" ]]; then
echo "unexpected cached unresolved project symbols after rs274ngc_pre shim coverage:" >&2
sed -n '1,120p' "$report_file" >&2
exit 1
fi
echo "linuxcnc wasm rs274ngc_pre link blocker scan passed"
echo "wrote $report_file"
rm -f "$next_signature"
exit 0
fi
report_file="$next_report_file"
rm -f "$next_report_file"
nm -u "${objects[@]}" \
| awk '{print $2}' \
| sed '/^$/d' \
| LC_ALL=C sort -u \
> "$undefined_file"
nm --defined-only "${objects[@]}" \
| awk '{print $3}' \
| sed '/^$/d' \
| LC_ALL=C sort -u \
> "$defined_file"
{
comm -23 "$undefined_file" "$defined_file" \
| awk '
$0 ~ /^(_GLOBAL_OFFSET_TABLE_|_Unwind_Resume)$/ { next }
$0 ~ /^(__assert_fail|__cxa_|__divdc3|__dso_handle|__errno_location|__gxx_personality_v0|__isoc23_|__muldc3|__stack_chk_fail)/ { next }
$0 ~ /^(abort|access|acos|asin|atan2|atof|atoi|basename|cabs|calloc|ceil|close|closedir|copysign|cos|cosl|exit|exp|fclose|fdatasync|fdopen|feof|fflush|fgetc|fgets|fileno|floor|fmax|fmod|fopen|fprintf|fputs|free|freelocale|fseek|fstat|ftell|fwrite|getcwd|getenv|getpid|gettimeofday|hypot|isalnum|isalpha|islower|isprint|isspace|isupper|isxdigit|link|localtime|log|lstat|malloc|memchr|memcmp|memcpy|memmove|memset|mkstemp|nearbyint|newlocale|open|opendir|perror|pow|printf|pthread_mutex_lock|pthread_mutex_unlock|putc|puts|read|readdir|realpath|realloc|remove|rename|setlocale|sin|sinl|snprintf|sprintf|sqrt|sscanf|stat|stderr|stdout|strcasecmp|strcat|strchr|strcmp|strcpy|strdup|strerror|strlen|strncasecmp|strncat|strncmp|strncpy|strrchr|strspn|strstr|strtod|strtok_r|strtol|strtoul|tan|tanl|tolower|toupper|towlower|unlink|uselocale|vfprintf|vsnprintf|wordexp|wordfree)$/ { next }
$0 ~ /^(_ZN3fmt|_ZSt|_ZNSt|_ZNKSt|_ZNKRSt|_ZNS|_ZTI|_ZTS|_ZTV|_Zdl|_Znwm|_Znam|_Zda|_ZdaPv|_ZdaPvm|_ZdlPvm|_ZTv|_ZTh)/ { next }
{ print }
'
} > "$report_file"
if grep -F "_ZN6Interp13read_commentEPcPiP12block_structPd" "$report_file" >/dev/null; then
echo "unexpected unresolved read_comment after interp_read.cc" >&2
exit 1
fi
if grep -F "_ZN6Interp15find_tool_indexEP5setupiPi" "$report_file" >/dev/null; then
echo "unexpected unresolved find_tool_index after interp_find.cc" >&2
exit 1
fi
if grep -F "_ZN6Interp16find_named_paramEPKcPiPd" "$report_file" >/dev/null; then
echo "unexpected unresolved find_named_param after interp_namedparams.cc" >&2
exit 1
fi
if grep -Fx "emcStatus" "$report_file" >/dev/null; then
echo "unexpected unresolved emcStatus after shim" >&2
exit 1
fi
if grep -Fx "gettext" "$report_file" >/dev/null; then
echo "unexpected unresolved gettext after shim" >&2
exit 1
fi
if grep -Fx "_task" "$report_file" >/dev/null; then
echo "unexpected unresolved _task after runtime shim" >&2
exit 1
fi
if grep -Fx "builtin_modules" "$report_file" >/dev/null; then
echo "unexpected unresolved builtin_modules after runtime shim" >&2
exit 1
fi
for hal_symbol in \
hal_get_param_value_by_name \
hal_get_pin_value_by_name \
hal_get_signal_value_by_name \
hal_init \
hal_ready; do
if grep -Fx "$hal_symbol" "$report_file" >/dev/null; then
echo "unexpected unresolved $hal_symbol after runtime shim" >&2
exit 1
fi
done
if [[ -s "$report_file" ]]; then
echo "unexpected unresolved project symbols after rs274ngc_pre shim coverage:" >&2
sed -n '1,120p' "$report_file" >&2
exit 1
fi
mv "$next_signature" "$signature"
mv "$next_report_file" "$cache_dir/rs274ngc-pre-link-blockers.txt"
report_file="$cache_dir/rs274ngc-pre-link-blockers.txt"
echo "linuxcnc wasm rs274ngc_pre link blocker scan passed"
keep_report_file=1
echo "wrote $report_file"