1#!/bin/bash
2
3#
4# Copyright (C) 2019 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# 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
18set -Eeuo pipefail
19
20TEST_NAME=$1
21SPEC_NAME=${TEST_NAME//[\/.]/_}
22
23GENERATOR_DIR=$ANDROID_BUILD_TOP/frameworks/ml/nn/tools/test_generator
24NNTEST_DIR=$ANDROID_PRODUCT_OUT/data/nativetest
25
26# Create tmp dir for spec dump.
27LOG_DIR=$(mktemp -d)/nnapi-fuzzing-logs
28mkdir -p $LOG_DIR
29echo Creating logs in $LOG_DIR
30
31# Save the fuzzer settings.
32DUMPSPEC=$(adb shell getprop debug.nn.fuzzer.dumpspec)
33LOGLEVEL=$(adb shell getprop debug.nn.fuzzer.log)
34
35if [[ "$DUMPSPEC" == "" ]]; then
36    DUMPSPEC="0"
37fi
38
39if [[ "$LOGLEVEL" == "" ]]; then
40    LOGLEVEL="SILENCE"
41fi
42
43# Set to dump the spec file after the test.
44adb shell setprop debug.nn.fuzzer.dumpspec 1
45adb shell setprop debug.nn.fuzzer.log silence
46
47# Run test.
48adb push $NNTEST_DIR/NeuralNetworksTest_static_fuzzing/NeuralNetworksTest_static_fuzzing /data/local/tmp
49adb shell /data/local/tmp/NeuralNetworksTest_static_fuzzing --gtest_filter=$TEST_NAME
50
51# Apply the previous settings.
52adb shell setprop debug.nn.fuzzer.dumpspec $DUMPSPEC
53adb shell setprop debug.nn.fuzzer.log $LOGLEVEL
54
55# Pull spec file, and generate HTML for visualization.
56adb pull /data/local/tmp/${SPEC_NAME}.mod.py $LOG_DIR
57$GENERATOR_DIR/spec_visualizer.py $LOG_DIR/${SPEC_NAME}.mod.py -o $LOG_DIR/${SPEC_NAME}.html
58
59google-chrome $LOG_DIR/${SPEC_NAME}.html
60