void
vmxon_ut::test_start_check_vmx_capabilities_msr_addr_width_failure()
{
    MockRepository mocks;
    auto in = bfn::mock_shared<intrinsics_intel_x64>(mocks);
    auto intrinsics = in.get();
    auto l_mm = bfn::mock_shared<memory_manager>(mocks);
    mm = l_mm.get();

    // is_vmx_operation_enabled
    mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0);

    // check_cpuid_vmx_supported
    mocks.OnCall(intrinsics, intrinsics_intel_x64::cpuid_ecx).With(1).Return((1 << 5));

    // check_vmx_capabilities_msr
    auto basic = ((1ULL << 55) | ((~(1ULL << 48))));
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_BASIC_MSR).Return(basic);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        vmxon_intel_x64 vmxon(in);

        EXPECT_EXCEPTION(vmxon.start(), bfn::vmxon_capabilities_failure_error);
    });
}
void
vmxon_ut::test_stop_vmxoff_check_failure()
{
    MockRepository mocks;
    auto in = bfn::mock_shared<intrinsics_intel_x64>(mocks);
    auto intrinsics = in.get();
    auto l_mm = bfn::mock_shared<memory_manager>(mocks);
    mm = l_mm.get();

    // execute_vmxoff
    mocks.OnCall(intrinsics, intrinsics_intel_x64::vmxoff).Return(true);

    // disable_vmx_operation
    Call &cr4_0 = mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::write_cr4).Do(stubbed_write_cr4);

    // is_vmx_operation_enabled
    mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).After(cr4_0).Return(CR4_VMXE_VMX_ENABLE_BIT);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        vmxon_intel_x64 vmxon(in);

        EXPECT_EXCEPTION(vmxon.stop(), bfn::vmxon_failure_error);
    });
}
void
driver_entry_ut::test_common_dump_resolve_symbol_failed()
{
    EXPECT_TRUE(common_add_module(m_dummy1, m_dummy1_length) == BF_SUCCESS);
    EXPECT_TRUE(common_add_module(m_dummy2, m_dummy2_length) == BF_SUCCESS);
    EXPECT_TRUE(common_add_module(m_dummy3, m_dummy3_length) == BF_SUCCESS);
    EXPECT_TRUE(common_load_vmm() == BF_SUCCESS);

    // We need to wrap the mocks variable in a temporary stack frame as the
    // current implementation of HippoMocks does not have a function to undo
    // it's hooks into c-style functions as the functionality to do this is
    // in the destructor.

    {
        MockRepository mocks;

        mocks.OnCallFunc(resolve_symbol).Return(-1);

        RUN_UNITTEST_WITH_MOCKS(mocks, [&]
        {
            EXPECT_TRUE(common_dump_vmm() == BF_ERROR_FAILED_TO_DUMP_DR);
        });
    }

    EXPECT_TRUE(common_unload_vmm() == BF_SUCCESS);
}
void
vmxon_ut::test_start_check_ia32_feature_control_msr()
{
    MockRepository mocks;
    auto in = bfn::mock_shared<intrinsics_intel_x64>(mocks);
    auto intrinsics = in.get();
    auto l_mm = bfn::mock_shared<memory_manager>(mocks);
    mm = l_mm.get();

    // is_vmx_operation_enabled
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0);

    // check_cpuid_vmx_supported
    mocks.OnCall(intrinsics, intrinsics_intel_x64::cpuid_ecx).With(1).Return((1 << 5));

    // check_vmx_capabilities_msr
    auto basic = ((1ULL << 55) | ((6ULL << 50) & (~(1ULL << 48))));
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_BASIC_MSR).Return(basic);

    // check_ia32_vmx_cr0_fixed_msr
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr0).Return((0xffffffff));
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR0_FIXED0_MSR).Return(0);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR0_FIXED1_MSR).Return(0xffffffff);

    // check_ia32_feature_control_msr
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_FEATURE_CONTROL_MSR).Return(0);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        vmxon_intel_x64 vmxon(in);

        EXPECT_EXCEPTION(vmxon.start(), bfn::vmxon_failure_error);
    });
}
void
vmxon_ut::test_start_check_ia32_vmx_cr4_fixed0_msr_failure()
{
    MockRepository mocks;
    auto in = bfn::mock_shared<intrinsics_intel_x64>(mocks);
    auto intrinsics = in.get();
    auto l_mm = bfn::mock_shared<memory_manager>(mocks);
    mm = l_mm.get();

    // is_vmx_operation_enabled
    Call &cr4_0 = mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0);

    // check_cpuid_vmx_supported
    mocks.OnCall(intrinsics, intrinsics_intel_x64::cpuid_ecx).With(1).Return((1 << 5));

    // check_vmx_capabilities_msr
    auto basic = ((1ULL << 55) | ((6ULL << 50) & (~(1ULL << 48))));
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_BASIC_MSR).Return(basic);

    // check_ia32_vmx_cr0_fixed_msr
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr0).Return((0xffffffff));
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR0_FIXED0_MSR).Return(0);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR0_FIXED1_MSR).Return(0xffffffff);

    // check_ia32_feature_control_msr
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_FEATURE_CONTROL_MSR).Return((1 << 0));

    // check_v8086_disabled
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_rflags).Return(~RFLAGS_VM_VIRTUAL_8086_MODE);

    // create_vmxon_region
    mocks.OnCallFunc(memory_manager::instance).Do(fake_memory_manager);
    mocks.OnCall(mm, memory_manager::malloc_aligned).Do(stubbed_malloc_aligned);
    mocks.OnCall(mm, memory_manager::virt_to_phys).Return((void *)0xDEADBEEFDEAF1000);

    // enable_vmx_operation
    Call &cr4_1 = mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).After(cr4_0).Return(0);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::write_cr4).Do(stubbed_write_cr4);

    // is_vmx_operation_enabled
    Call &cr4_2 = mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).After(cr4_1).Return(CR4_VMXE_VMX_ENABLE_BIT);

    // check_ia32_vmx_cr4_fixed_msr ***
    mocks.ExpectCall(intrinsics, intrinsics_intel_x64::read_cr4).After(cr4_2).Return(0xDEADBEEF);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR4_FIXED0_MSR).Return(0xFEEDBEEF);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR4_FIXED1_MSR).Return(0xDEADBEEF);

    // Handle rollbacks
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::vmxoff).Return(true);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::write_cr4).Do(stubbed_write_cr4);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        vmxon_intel_x64 vmxon(in);

        EXPECT_EXCEPTION(vmxon.start(), bfn::vmxon_fixed_msr_failure_error);
    });
}
void
memory_manager_ut::test_root_page_table_x64_init_success()
{
    MockRepository mocks;
    setup_mm(mocks);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        this->expect_no_exception([&] { root_page_table_x64::instance(); });
    });
}
void
memory_manager_ut::test_root_page_table_x64_cr3()
{
    MockRepository mocks;
    setup_mm(mocks);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        this->expect_true(g_pt->cr3() == 0x0000000ABCDEF001F);
    });
}
void
driver_entry_ut::test_common_load_loader_init_failed()
{
    MockRepository mocks;

    mocks.OnCallFunc(bfelf_loader_init).Return(-1);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(common_load_vmm() == -1);
    });
}
void
driver_entry_ut::test_common_load_status_running()
{
    MockRepository mocks;

    mocks.OnCallFunc(vmm_status).Return(VMM_RUNNING);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(common_load_vmm() == BF_ERROR_VMM_INVALID_STATE);
    });
}
void
driver_entry_ut::test_common_load_status_loaded()
{
    MockRepository mocks;

    mocks.OnCallFunc(vmm_status).Return(VMM_LOADED);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(common_load_vmm() == BF_SUCCESS);
    });
}
void
driver_entry_ut::test_common_load_status_corrupt()
{
    MockRepository mocks;

    mocks.OnCallFunc(vmm_status).Return(VMM_CORRUPT);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(common_load_vmm() == BF_ERROR_VMM_CORRUPTED);
    });
}
void
driver_entry_ut::test_common_dump_status_unloaded()
{
    MockRepository mocks;

    mocks.OnCallFunc(vmm_status).Return(VMM_UNLOADED);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(common_dump_vmm() == BF_ERROR_VMM_INVALID_STATE);
    });
}
void
memory_manager_ut::test_root_page_table_x64_map_failure()
{
    MockRepository mocks;
    setup_mm(mocks);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        this->expect_exception([&] { g_pt->map(0, 0x54323000, x64::memory_attr::rw_wb); }, ""_ut_ffe);
        this->expect_exception([&] { g_pt->map(0x12347000, 0, x64::memory_attr::rw_wb); }, ""_ut_ffe);
        this->expect_exception([&] { g_pt->map(0x12347000, 0x54323000, 0); }, ""_ut_lee);
    });
}
void
memory_manager_ut::test_root_page_table_x64_map_add_md_failure()
{
    MockRepository mocks;
    auto &&mm = setup_mm(mocks);

    mocks.OnCall(mm, memory_manager_x64::add_md).Throw(std::runtime_error("error"));

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        this->expect_exception([&] { g_pt->map(0x12347000, 0x54323000, x64::memory_attr::rw_wb); }, ""_ut_ree);
    });
}
void
memory_manager_ut::test_root_page_table_x64_map_unmap_twice_success()
{
    MockRepository mocks;
    setup_mm(mocks);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        this->expect_no_exception([&] { g_pt->map(0x12347000, 0x54323000, x64::memory_attr::rw_wb); });
        this->expect_no_exception([&] { g_pt->unmap(0x12347000); });
        this->expect_no_exception([&] { g_pt->unmap(0x12347000); });
    });
}
Exemple #16
0
void
entry_ut::test_start_vmm_success()
{
    MockRepository mocks;
    auto vcm = mocks.Mock<vcpu_manager>();
    mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);

    mocks.OnCall(vcm, vcpu_manager::init);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_NO_EXCEPTION(start_vmm(0));
    });
}
Exemple #17
0
void
entry_ut::test_init_vmm_throws_any_exception()
{
    MockRepository mocks;
    auto vcm = mocks.Mock<vcpu_manager>();
    mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);

    mocks.OnCall(vcm, vcpu_manager::init).Throw(10);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_NO_EXCEPTION(init_vmm(0));
    });
}
void
memory_manager_ut::test_root_page_table_x64_init_failure()
{
    MockRepository mocks;
    auto &&mm = setup_mm(mocks);

    mocks.OnCall(mm, memory_manager_x64::add_md).With(0xDEADBEEF, _, _).Throw(std::runtime_error("error"));

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        this->expect_no_exception([&] { root_page_table_x64::instance(); });
        this->expect_true(g_terminate_called);
    });
}
Exemple #19
0
void
entry_ut::test_stop_vmm_throws_general_exception()
{
    MockRepository mocks;
    auto vcm = mocks.Mock<vcpu_manager>();
    mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);

    mocks.OnCall(vcm, vcpu_manager::hlt_vcpu);
    mocks.OnCall(vcm, vcpu_manager::delete_vcpu).Throw(bfn::general_exception());

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_NO_EXCEPTION(stop_vmm(0));
    });
}
Exemple #20
0
void
entry_ut::test_start_vmm_throws_standard_exception()
{
    MockRepository mocks;
    auto vcm = mocks.Mock<vcpu_manager>();
    mocks.OnCallFunc(vcpu_manager::instance).Return(vcm);

    mocks.OnCall(vcm, vcpu_manager::create_vcpu).Throw(std::exception());
    mocks.OnCall(vcm, vcpu_manager::run_vcpu);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_NO_EXCEPTION(start_vmm(0));
    });
}
void
vmm_ut::test_verify_vmx_capabilities_msr_failed_invalid_memory_type()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_BASIC_MSR).Return(0);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_vmx_capabilities_msr() == vmm_error::not_supported);
    });
}
void
vmm_ut::test_verify_cpuid_vmx_supported_success()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::cpuid_ecx).Return((1 << 5));

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_cpuid_vmx_supported() == vmm_error::success);
    });
}
void
vmm_ut::test_verify_vmx_capabilities_msr_success()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_BASIC_MSR).Return((6ll << 50));

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_vmx_capabilities_msr() == vmm_error::success);
    });
}
void
vmm_ut::test_verify_vmx_operation_disabled_success()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_vmx_operation_disabled() == vmm_error::success);
    });
}
void
vmm_ut::test_execute_vmxoff_already_off()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.NeverCall(intrinsics, intrinsics_intel_x64::vmxoff);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.execute_vmxoff() == vmm_error::success);
    });
}
void
vmm_ut::test_verify_v8086_disabled_failed()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_rflags).Return(RFLAGS_VM_VIRTUAL_8086_MODE);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_v8086_disabled() == vmm_error::not_supported);
    });
}
void
vmm_ut::test_verify_ia32_feature_control_msr_success()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_FEATURE_CONTROL_MSR).Return(1);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_ia32_feature_control_msr() == vmm_error::success);
    });
}
void
vmm_ut::test_disable_vmx_operation_success()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0xFFFFFFFFFFFFFFFF);
    mocks.ExpectCall(intrinsics, intrinsics_intel_x64::write_cr4).With(~CR4_VMXE_VMX_ENABLE_BIT);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.disable_vmx_operation() == vmm_error::success);
    });
}
void
driver_entry_ut::test_common_load_loader_add_file_failed()
{
    MockRepository mocks;

    mocks.OnCallFunc(bfelf_loader_add).Return(-1);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(common_add_module(m_dummy1, m_dummy1_length) == BF_SUCCESS);
        EXPECT_TRUE(common_add_module(m_dummy2, m_dummy2_length) == BF_SUCCESS);
        EXPECT_TRUE(common_add_module(m_dummy3, m_dummy3_length) == BF_SUCCESS);
        EXPECT_TRUE(common_load_vmm() == -1);
        EXPECT_TRUE(common_unload_vmm() == BF_SUCCESS);
    });
}
void
vmm_ut::test_verify_ia32_vmx_cr4_fixed_msr_success()
{
    MockRepository mocks;
    intrinsics_intel_x64 *intrinsics = mocks.Mock<intrinsics_intel_x64>();

    vmm_intel_x64 vmm(intrinsics);

    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_cr4).Return(0x1);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR4_FIXED0_MSR).Return(0x1);
    mocks.OnCall(intrinsics, intrinsics_intel_x64::read_msr).With(IA32_VMX_CR4_FIXED1_MSR).Return(0xFFFFFFFFFFFFFFF7);

    RUN_UNITTEST_WITH_MOCKS(mocks, [&]
    {
        EXPECT_TRUE(vmm.verify_ia32_vmx_cr4_fixed_msr() == vmm_error::success);
    });
}