1#!/bin/bash
2#
3# Copyright (C) 2017 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
17UNSUPPORTED_SDK_VERSION=25
18SUPPORTED_SDK_VERSION=26
19
20# Expect failure with unsupported SDK version
21EXPECTED_STATUS[${UNSUPPORTED_SDK_VERSION}]=1
22
23# Expect success with supported SDK version
24EXPECTED_STATUS[${SUPPORTED_SDK_VERSION}]=0
25
26DX_OUTPUT=dx.log
27rm -f ${DX_OUTPUT} 2>/dev/null
28
29for SDK_VERSION in ${UNSUPPORTED_SDK_VERSION} ${SUPPORTED_SDK_VERSION}; do
30  echo Trying SDK version ${SDK_VERSION} with invoke-custom.
31  dx --min-sdk-version=${SDK_VERSION} --dex --output=invokecustom.dex \
32     --verbose-dump --dump-to=- --dump-width=1000 invokecustom.jar 2>&1
33  STATUS=$?
34  if [[ ${STATUS} != ${EXPECTED_STATUS[$SDK_VERSION]} ]]; then
35    echo Unexpected status ${STATUS} for SDK version ${SDK_VERSION}.
36    exit 1
37  fi
38done | tee -a ${DX_OUTPUT}
39
40JAVAP_OUTPUT=invokecustom.InvokeCustom.txt
41javap -c -v -cp invokecustom.jar invokecustom.InvokeCustom > ${JAVAP_OUTPUT}
42
43# Check each invokedynamic instruction produced one invoke-custom
44INVOKEDYNAMIC_COUNT=$( grep "invokedynamic #" ${JAVAP_OUTPUT} | wc -l )
45INVOKE_CUSTOM_COUNT=$( grep ": invoke-custom" ${DX_OUTPUT} | wc -l )
46if [ "${INVOKEDYNAMIC_COUNT}" -ne "${INVOKE_CUSTOM_COUNT}" ]; then
47    echo Found ${INVOKEDYNAMIC_COUNT} uses of invokedynamic but ${INVOKE_CUSTOM_COUNT} uses of invoke-custom.
48    exit 1
49fi
50
51# Check there is a 1:1 correspondance between the number of call site ids and invoke-custom bytecodes.
52CALL_SITE_ID_COUNT=$( sed -n -e '/call_site_off:/ p' ${DX_OUTPUT} | wc -l )
53if [ ${CALL_SITE_ID_COUNT} -gt ${INVOKE_CUSTOM_COUNT} ]; then
54    echo Found ${CALL_SITE_ID_COUNT} call sites but ${INVOKE_CUSTOM_COUNT} uses of invoke-custom.
55    exit 1
56fi
57
58# Check number of invokedynamic constants matches the number of unique call sites
59CST_INDY_COUNT=$( sed -n -e 's@.*: invokedynamic #\([0-9]*\),.*@\1@p' ${JAVAP_OUTPUT} | \
60                  sort | \
61                  uniq -c | \
62                  wc -l )
63CALL_SITE_COUNT=$( sed -n -e 's@.*call_site_off: \([0-9a-f]\+\)@\1@p' ${DX_OUTPUT} | \
64                   uniq -c | \
65                   wc -l )
66if [ ${CST_INDY_COUNT} -ne ${CALL_SITE_COUNT} ]; then
67    echo Found ${CST_INDY_COUNT} invokedynamic constants but ${CALL_SITE_COUNT} call sites.
68    exit 1
69fi
70