/*
 * Copyright (C) 2023 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 <elf.h>
#include <gtest/gtest.h>
#include <mntent.h>

#include <regex>
#include <set>

#include <elfutils/iter.h>
#include <libdm/dm.h>

#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android/api-level.h>

constexpr char kLowRamProp[] = "ro.config.low_ram";
constexpr char kVendorApiLevelProp[] = "ro.vendor.api_level";
// 16KB by default (unsupported devices must explicitly opt-out)
constexpr size_t kRequiredMaxSupportedPageSize = 0x4000;

static inline std::string escapeForRegex(const std::string& str) {
    // Regex metacharacters to be escaped
    static const std::regex specialChars(R"([.^$|(){}\[\]+*?\\])");

    // Replace each special character with its escaped version
    return std::regex_replace(str, specialChars, R"(\$&)");
}

static inline bool startsWithPattern(const std::string& str, const std::string& pattern) {
    std::regex _pattern("^" + pattern + ".*");
    return std::regex_match(str, _pattern);
}

static std::set<std::string> getMounts() {
    std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
    std::set<std::string> exclude({"/", "/config", "/data", "/data_mirror", "/dev", "/linkerconfig",
                                   "/mnt", "/proc", "/storage", "/sys"});
    std::set<std::string> mounts;

    if (fp == nullptr) {
        return mounts;
    }

    mntent* mentry;
    while ((mentry = getmntent(fp.get())) != nullptr) {
        std::string mountDir(mentry->mnt_dir);

        std::string dir = "/" + android::base::Split(mountDir, "/")[1];

        if (exclude.find(dir) != exclude.end()) {
            continue;
        }

        mounts.insert(dir);
    }

    return mounts;
}

using ::android::elfutils::ElfFile;

class ElfAlignmentTest : public ::testing::TestWithParam<std::string> {
  protected:
    static void loadAlignmentCb(ElfFile& elfFile) {
        using namespace android::elfutils;

        static std::array ignored_directories{
                // Ignore VNDK APEXes. They are prebuilts from old branches, and would
                // only be used on devices with old vendor images.
                escapeForRegex("/apex/com.android.vndk.v"),
                // Ignore Trusty VM images under */etc/vm/trusty_vm as they don't run
                // in userspace, so 16K is not required. See b/365240530 and b/406626518
                // for more context.
                ".*" + escapeForRegex("/etc/vm/trusty_vm"),
                // Ignore non-Android firmware images.
                escapeForRegex("/odm/firmware/"), escapeForRegex("/vendor/firmware/"),
                escapeForRegex("/vendor/firmware_mnt/image"),
                // Ignore TEE binaries ("glob: /apex/com.*.android.authfw.ta*")
                escapeForRegex("/apex/com.") + ".*" + escapeForRegex(".android.authfw.ta")};

        // Don't check 32-bit ELFs
        if (elfFile.is32Bit()) return;

        std::string path = elfFile.getPath();
        for (const auto& pattern : ignored_directories) {
            if (startsWithPattern(path, pattern)) {
                return;
            }
        }

        // Ignore ART Odex files for now. They are not 16K aligned.
        // b/376814207
        if (path.ends_with(".odex")) {
            return;
        }

        if (auto minAlign = elfFile.getMinLoadSegmentAlignment()) {
            EXPECT_GE(*minAlign, kRequiredMaxSupportedPageSize)
                    << " " << path << " is not at least 16KiB aligned";
        }
    };

    static bool isLowRamDevice() { return android::base::GetBoolProperty(kLowRamProp, false); }

    static int vendorApiLevel() {
        // "ro.vendor.api_level" is added in Android T. Undefined indicates S or below
        return android::base::GetIntProperty(kVendorApiLevelProp, __ANDROID_API_S__);
    }

    void SetUp() override {
        if (vendorApiLevel() < 202404) {
            GTEST_SKIP() << "16kB support is only required on V and later releases.";
        } else if (isLowRamDevice()) {
            GTEST_SKIP() << "Low Ram devices only support 4kB page size";
        }
    }
};

using ::android::elfutils::ElfIterator;

// @VsrTest = 3.14.1
TEST_P(ElfAlignmentTest, VerifyLoadSegmentAlignment) {
    ElfIterator::forEachElfFromDir(GetParam(), &loadAlignmentCb);
}

INSTANTIATE_TEST_SUITE_P(ElfTestPartitionsAligned, ElfAlignmentTest,
                         ::testing::ValuesIn(getMounts()));

int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
