Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cmake/HostBinaryFormat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,46 @@ function(vitasdk_check_binary_directory directory system_name host_triple)
endif()
endforeach()
endfunction()

# Windows top-level bin files are native host programs except for the few
# compatibility/data files whose names are part of the SDK contract.
function(vitasdk_check_windows_top_level_bin directory)
if(NOT IS_DIRECTORY "${directory}")
return()
endif()

file(GLOB entries LIST_DIRECTORIES TRUE "${directory}/*")
foreach(entry IN LISTS entries)
if(IS_DIRECTORY "${entry}" OR IS_SYMLINK "${entry}")
continue()
endif()

get_filename_component(name "${entry}" NAME)
if(name STREQUAL "makepkg.conf")
continue()
endif()
# vita-makepkg currently requires an external MSYS2 environment.
if(name STREQUAL "vita-makepkg")
continue()
endif()
# vitasdk-update is the legacy compatibility updater; vdpm is the
# current native path.
if(name STREQUAL "vitasdk-update")
continue()
endif()
# This is an upstream GNU GDB POSIX helper retained unchanged for
# compatibility.
if(name STREQUAL "arm-vita-eabi-gdb-add-index")
continue()
endif()

vitasdk_binary_format("${entry}" format machine)
if(format STREQUAL "PE")
continue()
endif()

message(FATAL_ERROR
"${entry} is not a PE host executable or an allowed regular "
"top-level Windows SDK bin file")
endforeach()
endfunction()
10 changes: 6 additions & 4 deletions cmake/ValidateSdk.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ foreach(directory include lib)
endif()
endforeach()

# Everything in a bin directory must be a binary for this host. A staged
# build merges two machines into one tree, so this is where a producer's
# binary that rode along on an import gets caught.
# A staged build merges two machines into one tree, so validate both the
# Windows top-level bin contract and the host format of every native binary.
include("${CMAKE_CURRENT_LIST_DIR}/HostBinaryFormat.cmake")
if(HOST_SYSTEM_NAME STREQUAL "Windows")
vitasdk_check_windows_top_level_bin("${SDK_DIR}/bin")
endif()
if(DEFINED HOST_TRIPLE AND NOT HOST_TRIPLE STREQUAL "")
include("${CMAKE_CURRENT_LIST_DIR}/HostBinaryFormat.cmake")
vitasdk_check_binary_directory("${SDK_DIR}/bin" "${HOST_SYSTEM_NAME}" "${HOST_TRIPLE}")
vitasdk_check_binary_directory("${sysroot}/bin" "${HOST_SYSTEM_NAME}" "${HOST_TRIPLE}")
endif()
Expand Down
6 changes: 6 additions & 0 deletions cmake/ValidateWindowsBin.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if(NOT DEFINED DIRECTORY)
message(FATAL_ERROR "DIRECTORY is required")
endif()

include("${CMAKE_CURRENT_LIST_DIR}/HostBinaryFormat.cmake")
vitasdk_check_windows_top_level_bin("${DIRECTORY}")
2 changes: 2 additions & 0 deletions cmake/recipes/FinalizeSdk.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ add_custom_target(check-sdk-partition
-P ${CMAKE_SOURCE_DIR}/tests/cmake/sysroot-manifest.cmake
COMMAND ${CMAKE_COMMAND}
-P ${CMAKE_SOURCE_DIR}/tests/cmake/host-binary-format.cmake
COMMAND ${CMAKE_COMMAND}
-P ${CMAKE_SOURCE_DIR}/tests/cmake/windows-bin-contract.cmake
VERBATIM)

add_custom_target(check-static-policy
Expand Down
11 changes: 11 additions & 0 deletions scripts/validate-core-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fi

package=$1
require_package_client=${REQUIRE_PACKAGE_CLIENT:-1}
script_directory=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
cmake_command=${CMAKE_COMMAND:-cmake}

[[ -f $package ]] || {
printf 'core package not found: %s\n' "$package" >&2
Expand Down Expand Up @@ -52,6 +54,15 @@ fi
bsdtar -xOf "$package" .MTREE | gzip -t

if [[ $pkgname == vitasdk-core ]]; then
if [[ $architecture == *-w64-mingw32 ]]; then
(
policy_root=$(mktemp -d "${TMPDIR:-/tmp}/vitasdk-core-bin-policy.XXXXXXXX")
trap 'rm -rf -- "$policy_root"' EXIT
bsdtar -xf "$package" -C "$policy_root"
"$cmake_command" "-DDIRECTORY=$policy_root/bin" \
-P "$script_directory/../cmake/ValidateWindowsBin.cmake"
)
fi
grep -Eq '^bin/arm-vita-eabi-gcc(\.exe)?$' <<< "$archive_entries" || {
printf 'core package does not contain the Vita compiler driver\n' >&2
exit 1
Expand Down
10 changes: 7 additions & 3 deletions tests/cmake/pkgconf-sdk-validation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ function(create_sdk_fixture root suffix)
file(REMOVE_RECURSE "${root}")
file(MAKE_DIRECTORY "${root}/bin" "${root}/arm-vita-eabi/include" "${root}/arm-vita-eabi/lib")
foreach(tool IN LISTS required_tools)
file(WRITE "${root}/bin/arm-vita-eabi-${tool}${suffix}" "")
set(contents "")
if(suffix STREQUAL ".exe")
set(contents "MZ${tool}")
endif()
file(WRITE "${root}/bin/arm-vita-eabi-${tool}${suffix}" "${contents}")
endforeach()
endfunction()

Expand Down Expand Up @@ -41,8 +45,8 @@ run_validation("${unix_root}" Linux TRUE)
set(windows_root "${test_root}/windows")
create_sdk_fixture("${windows_root}" ".exe")
run_validation("${windows_root}" Windows FALSE)
file(WRITE "${windows_root}/bin/pkgconf.exe" "")
file(WRITE "${windows_root}/bin/arm-vita-eabi-pkg-config.exe" "")
file(WRITE "${windows_root}/bin/pkgconf.exe" "MZpkgconf")
file(WRITE "${windows_root}/bin/arm-vita-eabi-pkg-config.exe" "MZwrapper")
run_validation("${windows_root}" Windows TRUE)

file(REMOVE_RECURSE "${test_root}")
Expand Down
6 changes: 3 additions & 3 deletions tests/cmake/static-sdk-validation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ file(WRITE "${target_lib}/libSceKernel_stub.a" "")
# the product system must select the executable suffix explicitly.
file(MAKE_DIRECTORY "${fixture}/bin")
foreach(tool gcc g++ ld as ar ranlib strip objcopy objdump readelf nm gdb)
file(WRITE "${fixture}/bin/${target_triple}-${tool}.exe" "")
file(WRITE "${fixture}/bin/${target_triple}-${tool}.exe" "MZ${tool}")
endforeach()
file(WRITE "${fixture}/bin/pkgconf.exe" "")
file(WRITE "${fixture}/bin/${target_triple}-pkg-config.exe" "")
file(WRITE "${fixture}/bin/pkgconf.exe" "MZpkgconf")
file(WRITE "${fixture}/bin/${target_triple}-pkg-config.exe" "MZwrapper")
execute_process(
COMMAND "${CMAKE_COMMAND}"
-DSDK_DIR=${fixture}
Expand Down
136 changes: 136 additions & 0 deletions tests/cmake/windows-bin-contract.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
cmake_minimum_required(VERSION 3.16)

set(repository_root "${CMAKE_CURRENT_LIST_DIR}/../..")
string(RANDOM LENGTH 12 fixture_id)
if(DEFINED ENV{TMPDIR} AND NOT "$ENV{TMPDIR}" STREQUAL "")
set(temp_root "$ENV{TMPDIR}")
elseif(DEFINED ENV{TEMP} AND NOT "$ENV{TEMP}" STREQUAL "")
set(temp_root "$ENV{TEMP}")
else()
set(temp_root "${CMAKE_CURRENT_BINARY_DIR}")
endif()
set(scratch "${temp_root}/vitasdk-windows-bin-contract-${fixture_id}")

function(write_pe path)
file(WRITE "${path}" "MZ01234567890123456789")
endfunction()

function(populate_required_sdk sdk system_name)
file(MAKE_DIRECTORY "${sdk}/bin" "${sdk}/arm-vita-eabi/include"
"${sdk}/arm-vita-eabi/lib")
set(suffix "")
if(system_name STREQUAL "Windows")
set(suffix ".exe")
endif()
foreach(tool gcc g++ ld as ar ranlib strip objcopy objdump readelf nm gdb)
set(path "${sdk}/bin/arm-vita-eabi-${tool}${suffix}")
if(system_name STREQUAL "Windows")
write_pe("${path}")
else()
file(WRITE "${path}" "fixture")
endif()
endforeach()
if(system_name STREQUAL "Windows")
write_pe("${sdk}/bin/pkgconf.exe")
write_pe("${sdk}/bin/arm-vita-eabi-pkg-config.exe")
else()
file(WRITE "${sdk}/bin/pkgconf" "fixture")
file(WRITE "${sdk}/bin/arm-vita-eabi-pkg-config" "fixture")
endif()
endfunction()

function(expect_policy_rejection name contents)
set(directory "${scratch}/reject-${name}/bin")
file(MAKE_DIRECTORY "${directory}")
file(WRITE "${directory}/${name}" "${contents}")
execute_process(
COMMAND ${CMAKE_COMMAND}
-DDIRECTORY=${directory}
-P "${repository_root}/cmake/ValidateWindowsBin.cmake"
RESULT_VARIABLE result
OUTPUT_QUIET
ERROR_VARIABLE error)
if(result EQUAL 0)
message(FATAL_ERROR "the Windows bin contract accepted ${name}")
endif()
if(NOT error MATCHES "${name}")
message(FATAL_ERROR "the rejection did not name ${name}: ${error}")
endif()
endfunction()

include("${repository_root}/cmake/HostBinaryFormat.cmake")

set(policy_bin "${scratch}/policy/bin")
file(MAKE_DIRECTORY "${policy_bin}/libmakepkg")
write_pe("${policy_bin}/native-tool.exe")
file(WRITE "${policy_bin}/makepkg.conf" "CARCH=\"vita\"\n")
file(WRITE "${policy_bin}/vita-makepkg" "#!/usr/bin/env sh\n")
file(WRITE "${policy_bin}/vitasdk-update" "#!/usr/bin/env sh\n")
file(WRITE "${policy_bin}/arm-vita-eabi-gdb-add-index" "#!/usr/bin/env sh\n")
file(WRITE "${policy_bin}/libmakepkg/internal-helper" "not top-level\n")
vitasdk_check_windows_top_level_bin("${policy_bin}")

expect_policy_rejection("unknown-script" "#!/usr/bin/env sh\n")
expect_policy_rejection("unknown-data" "not executable data\n")

set(windows_sdk "${scratch}/windows-sdk")
populate_required_sdk("${windows_sdk}" Windows)
file(WRITE "${windows_sdk}/bin/makepkg.conf" "CARCH=\"vita\"\n")
file(WRITE "${windows_sdk}/bin/vita-makepkg" "#!/usr/bin/env sh\n")
file(WRITE "${windows_sdk}/bin/vitasdk-update" "#!/usr/bin/env sh\n")
file(WRITE "${windows_sdk}/bin/arm-vita-eabi-gdb-add-index" "#!/usr/bin/env sh\n")
file(MAKE_DIRECTORY "${windows_sdk}/bin/libmakepkg")
file(WRITE "${windows_sdk}/bin/libmakepkg/internal-helper" "not top-level\n")

execute_process(
COMMAND ${CMAKE_COMMAND}
-DSDK_DIR=${windows_sdk}
-DTARGET_TRIPLE=arm-vita-eabi
-DHOST_SYSTEM_NAME=Windows
-DHOST_TRIPLE=x86_64-w64-mingw32
-P "${repository_root}/cmake/ValidateSdk.cmake"
RESULT_VARIABLE valid_result
OUTPUT_VARIABLE valid_output
ERROR_VARIABLE valid_error)
if(NOT valid_result EQUAL 0)
message(FATAL_ERROR
"the staged validator rejected the Windows allowlist:\n${valid_output}${valid_error}")
endif()

file(WRITE "${windows_sdk}/bin/staged-unknown-script" "#!/usr/bin/env sh\n")
execute_process(
COMMAND ${CMAKE_COMMAND}
-DSDK_DIR=${windows_sdk}
-DTARGET_TRIPLE=arm-vita-eabi
-DHOST_SYSTEM_NAME=Windows
-DHOST_TRIPLE=x86_64-w64-mingw32
-P "${repository_root}/cmake/ValidateSdk.cmake"
RESULT_VARIABLE invalid_result
OUTPUT_QUIET
ERROR_VARIABLE invalid_error)
if(invalid_result EQUAL 0)
message(FATAL_ERROR "the staged validator accepted an unknown Windows bin script")
endif()
if(NOT invalid_error MATCHES "staged-unknown-script")
message(FATAL_ERROR "the staged rejection omitted its path: ${invalid_error}")
endif()

set(linux_sdk "${scratch}/linux-sdk")
populate_required_sdk("${linux_sdk}" Linux)
file(WRITE "${linux_sdk}/bin/host-helper" "#!/usr/bin/env sh\n")
execute_process(
COMMAND ${CMAKE_COMMAND}
-DSDK_DIR=${linux_sdk}
-DTARGET_TRIPLE=arm-vita-eabi
-DHOST_SYSTEM_NAME=Linux
-P "${repository_root}/cmake/ValidateSdk.cmake"
RESULT_VARIABLE linux_result
OUTPUT_VARIABLE linux_output
ERROR_VARIABLE linux_error)
if(NOT linux_result EQUAL 0)
message(FATAL_ERROR
"the Windows-only contract changed Linux validation:\n${linux_output}${linux_error}")
endif()

file(REMOVE_RECURSE "${scratch}")
message(STATUS "Windows top-level bin contract checks passed")
8 changes: 4 additions & 4 deletions tests/package/test-core-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ windows_root="$temporary_root/windows-sdk"
mkdir -p "$windows_root/bin" "$windows_root/arm-vita-eabi/lib" \
"$windows_root/share/vdpm/licenses" "$windows_root/share/licenses/pkgconf" \
"$windows_root/share/vdpm/msys/usr/bin"
printf 'gcc\n' > "$windows_root/bin/arm-vita-eabi-gcc.exe"
printf 'pkgconf\n' > "$windows_root/bin/pkgconf.exe"
printf 'wrapper\n' > "$windows_root/bin/arm-vita-eabi-pkg-config.exe"
printf 'vdpm\n' > "$windows_root/bin/vdpm.exe"
printf 'MZgcc\n' > "$windows_root/bin/arm-vita-eabi-gcc.exe"
printf 'MZpkgconf\n' > "$windows_root/bin/pkgconf.exe"
printf 'MZwrapper\n' > "$windows_root/bin/arm-vita-eabi-pkg-config.exe"
printf 'MZvdpm\n' > "$windows_root/bin/vdpm.exe"
printf 'archive\n' > "$windows_root/arm-vita-eabi/lib/libfixture.a"
printf 'source=fixture\nworld vita (float-abi=hard)\n' > "$windows_root/version_info.txt"
printf 'CARCH="vita"\n' > "$windows_root/bin/makepkg.conf"
Expand Down
77 changes: 77 additions & 0 deletions tests/package/test-windows-bin-contract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env bash

set -euo pipefail

repository_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)
temporary_root=$(mktemp -d "${TMPDIR:-/tmp}/vitasdk-windows-bin-package.XXXXXXXX")
sdk_root="$temporary_root/sdk"

cleanup() {
rm -rf -- "$temporary_root"
}
trap cleanup EXIT

mkdir -p "$sdk_root/bin/libmakepkg" "$sdk_root/arm-vita-eabi/lib" \
"$sdk_root/share/licenses/pkgconf" "$sdk_root/share/vdpm/licenses" \
"$sdk_root/share/vdpm/msys/usr/bin"

for executable in arm-vita-eabi-gcc.exe pkgconf.exe arm-vita-eabi-pkg-config.exe; do
printf 'MZ%s\n' "$executable" > "$sdk_root/bin/$executable"
done
printf 'MZvdpm\n' > "$sdk_root/bin/vdpm.exe"
printf 'CARCH="vita"\n' > "$sdk_root/bin/makepkg.conf"
printf '#!/usr/bin/env sh\n' > "$sdk_root/bin/vita-makepkg"
printf '#!/usr/bin/env sh\n' > "$sdk_root/bin/vitasdk-update"
printf '#!/usr/bin/env sh\n' > "$sdk_root/bin/arm-vita-eabi-gdb-add-index"
printf 'nested helper\n' > "$sdk_root/bin/libmakepkg/internal-helper"
printf 'archive\n' > "$sdk_root/arm-vita-eabi/lib/libfixture.a"
printf 'source=fixture\nworld vita (float-abi=hard)\n' > "$sdk_root/version_info.txt"
printf 'pkgconf license\n' > "$sdk_root/share/licenses/pkgconf/COPYING"
printf 'notices\n' > "$sdk_root/share/vdpm/THIRD_PARTY_NOTICES.md"
printf 'vdpm license\n' > "$sdk_root/share/vdpm/licenses/vdpm-LGPL-2.1.txt"
printf 'pacman license\n' > "$sdk_root/share/vdpm/licenses/pacman-GPL-2.0.txt"
printf 'refresh\n' > "$sdk_root/share/vdpm/refresh-repositories.ps1"
printf 'version=0.1.0\nhost=x86_64-w64-mingw32\n' > "$sdk_root/share/vdpm/release-info.txt"
for runtime in pacman.exe vdpm-channel.exe msys-2.0.dll; do
printf 'MZ%s\n' "$runtime" > "$sdk_root/share/vdpm/msys/usr/bin/$runtime"
done

SOURCE_DATE_EPOCH=1700000000 \
"$repository_root/scripts/create-core-package.sh" \
"$sdk_root" "$temporary_root/valid" x86_64-w64-mingw32 \
0.1 0123456789abcdef >/dev/null

core_package="$temporary_root/valid/vitasdk-core-0.1-1-x86_64-w64-mingw32.pkg.tar.xz"
core_entries=$(bsdtar -tf "$core_package")
for path in bin/makepkg.conf bin/vita-makepkg bin/vitasdk-update \
bin/arm-vita-eabi-gdb-add-index bin/libmakepkg/internal-helper; do
grep -Fqx "$path" <<< "$core_entries" || {
printf 'the Windows core package lost %s\n' "$path" >&2
exit 1
}
done

expect_rejected() {
local offender=$1 contents=$2
local invalid_sdk="$temporary_root/sdk-$offender"
local output="$temporary_root/output-$offender" log="$temporary_root/$offender.log"
cp -a "$sdk_root" "$invalid_sdk"
printf '%s\n' "$contents" > "$invalid_sdk/bin/$offender"
if SOURCE_DATE_EPOCH=1700000000 \
"$repository_root/scripts/create-core-package.sh" \
"$invalid_sdk" "$output" x86_64-w64-mingw32 \
0.1 0123456789abcdef >"$log" 2>&1; then
printf 'the package validator accepted bin/%s\n' "$offender" >&2
exit 1
fi
grep -Fq "$offender" "$log" || {
printf 'the package rejection did not name bin/%s\n' "$offender" >&2
cat "$log" >&2
exit 1
}
}

expect_rejected unknown-script '#!/usr/bin/env sh'
expect_rejected unknown-data 'not executable data'

printf 'Windows package bin contract checks passed\n'
Loading