Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 532b5606 authored by Branden Archer's avatar Branden Archer
Browse files

Add fuzzer for DistortionMapperFuzzer

This fuzzer is a distilled version of the DistortionMapperTest
unit test.

Bug: 170243740
Test: Builds and runs on emulator
Change-Id: I254a3bef5fc6bcf300fdb64833817645fba8e920
parent ebd91fc5
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
// Copyright 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

cc_fuzz {
    name: "libcameraservice_distortion_mapper_fuzzer",
    srcs: [
        "DistortionMapperFuzzer.cpp",
    ],
    shared_libs: [
        "libcameraservice",
        "libcamera_client",
    ],
}
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vector>

#include <fuzzer/FuzzedDataProvider.h>

#include "device3/DistortionMapper.h"
#include <camera/CameraMetadata.h>

using namespace android;
using namespace android::camera3;

int32_t testActiveArray[] = {100, 100, 1000, 750};
float testICal[] = { 1000.f, 1000.f, 500.f, 500.f, 0.f };
float identityDistortion[] = { 0.f, 0.f, 0.f, 0.f, 0.f};

void setupTestMapper(DistortionMapper *m,
        float distortion[5], float intrinsics[5],
        int32_t activeArray[4], int32_t preCorrectionActiveArray[4]) {
    CameraMetadata deviceInfo;

    deviceInfo.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE,
            preCorrectionActiveArray, 4);

    deviceInfo.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
            activeArray, 4);

    deviceInfo.update(ANDROID_LENS_INTRINSIC_CALIBRATION,
            intrinsics, 5);

    deviceInfo.update(ANDROID_LENS_DISTORTION,
            distortion, 5);

    m->setupStaticInfo(deviceInfo);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    FuzzedDataProvider fdp(data, size);

    DistortionMapper m;
    setupTestMapper(&m, identityDistortion, testICal,
        /*activeArray*/ testActiveArray,
        /*preCorrectionActiveArray*/ testActiveArray);

    bool clamp = fdp.ConsumeBool();
    bool simple = fdp.ConsumeBool();
    std::vector<int32_t> input;
    for (int index = 0; fdp.remaining_bytes() > 0; index++) {
        input.push_back(fdp.ConsumeIntegral<int32_t>());
    }

    // The size argument counts how many coordinate pairs there are, so
    // it is expected to be 1/2 the size of the input.
    m.mapCorrectedToRaw(input.data(), input.size()/2,  clamp, simple);

    return 0;
}