1#!/bin/bash 2 3# Copyright (C) 2023 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Check that vendor ELF files have the required 16KB [or 64KB] load segment 18# alignment on devices. 19 20# Requirement added in U (34) 21MIN_VENDOR_API_LEVEL=34 22 23DEFAULT_VENDOR_API_LEVEL=0 24 25# Default to 64KB max page size unless otherwise specified. 26DEFAULT_MAX_PAGE_SIZE=65536 27 28# Device is only low RAM if explicitly stated 29DEFAULT_CONFIG_LOW_RAM=false 30 31fail() { #msg 32 echo "FAILED: $1" 33 exit 1 34} 35 36pass() { #msg 37 echo "PASSED: $1" 38 exit 0 39} 40 41skip() { #msg 42 echo "SKIPPED: $1" 43 exit 0 44} 45 46# Skip test if vendor API level < U (34) 47vendor_api_level="$(adb shell getprop ro.vendor.api_level $DEFAULT_VENDOR_API_LEVEL)" 48if [ "$vendor_api_level" -lt "$MIN_VENDOR_API_LEVEL" ]; then 49 skip "Vendor API level ($vendor_api_level) < Min vendor API level ($MIN_VENDOR_API_LEVEL)" 50fi 51 52# Android Go and other low RAM devices do not support larger than 4KB page size 53config_low_ram="$(adb shell getprop ro.config.low_ram $DEFAULT_CONFIG_LOW_RAM)" 54if [ "$config_low_ram" != "$DEFAULT_CONFIG_LOW_RAM" ]; then 55 skip "Low RAM devices only support 4096 max page size" 56fi 57 58# Some devices may choose to opt out of 64KB max page size support 59max_page_size="$(adb shell getprop ro.product.cpu.pagesize.max $DEFAULT_MAX_PAGE_SIZE)" 60if [ $max_page_size -lt $DEFAULT_MAX_PAGE_SIZE ]; then 61 skip "Device only supports $max_page_size max page size" 62fi 63 64 65unaligned_elfs=() 66 67get_unaligned_elfs() { 68 adb shell ' 69 # Find all vendor ELF files 70 paths=() 71 for i in `find /vendor -type f -exec file {} \; | grep ELF | awk -F: "{ print \\$1 }"`; do 72 paths+=( $i ) 73 done 74 75 unaligned=() 76 for path in "${paths[@]}"; do 77 load_alignment=$( readelf -l $path | grep LOAD | head -n1 | awk "{ print \$NF }" ) 78 79 # Require 64KB alignment for future proofing. Android uses sparse files so 80 # the real disk space impact is not significant. 81 if [ "$load_alignment" != "0x10000" ]; then 82 unaligned+=( $path ) 83 fi 84 done 85 86 echo "${unaligned[@]}"' 87} 88 89print_unaligned_elfs() { # arr_unaligned_elfs 90 elfs=("$@") 91 92 echo "" 93 echo "=== Unaligned vendor ELF files found ===" 94 echo "" 95 for elf in ${elfs[@]}; do 96 echo " $elf" 97 done 98 echo "" 99 echo "Please rebuild the above artifacts with 64KB aligned load segments." 100 echo "" 101 echo " This can be done by specifying the following linker flag:" 102 echo " -Wl,-z,max-page-size=65536" 103 echo "" 104 echo "This is required in devices with Vendor API Level >= $MIN_VENDOR_API_LEVEL" 105 echo "" 106} 107 108# @VsrTest = 3.3-005 109vendor_elf_alignment_test() { 110 unaligned_elfs+=( $(get_unaligned_elfs) ) 111 nr_unaligned="${#unaligned_elfs[@]}" 112 113 if [ "$nr_unaligned" == "0" ]; then 114 pass "All vendor ELF files have the required load segment alignment" 115 else 116 print_unaligned_elfs "${unaligned_elfs[@]}" 117 fail "Vendor ELF files with unaligned load segments found" 118 fi 119} 120 121vendor_elf_alignment_test 122