Skip to content

Commit

Permalink
Cleanup Coverity Scan Bug (#221)
Browse files Browse the repository at this point in the history
Coverity scan is getting mad about our use of new / delete
in our unit tests, even though we mimic the standard library
(it's a false positive), so this patch moves this code to a
system header to prevent the scanner from see it. It also
will provide a generic way to do this for other unit tests
in the future.

[ISSUE]: #193

Signed-off-by: “Rian <“rianquinn@gmail.com”>
  • Loading branch information
rianquinn authored and brendank310 committed Sep 22, 2016
1 parent a644f4d commit 42637a1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ matrix:
- COVERALLS=true make
- make test
after_success:
- coveralls --build-root makefiles -e build_libbfc -e build_libcxx -e build_libcxxabi -e build_newlib -e build_scripts -e common -e doc -e extensions -e source_libbfc -e source_libcxx -e source_libcxxabi -e source_llvm -e source_newlib -e sysroot -e tools -e bfc -e bfcxx -e bfdrivers/src/arch -e bfm/src/arch -e include/hippomocks.h -e include/unittest.h -e bfunwind -e bfvmm/src/vcpu_factory/src/vcpu_factory.cpp -e bfvmm/include/intrinsics/intrinsics_x64.h -e bfvmm/include/intrinsics/intrinsics_intel_x64.h -e include/gsl -e include/exception.h -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_controls.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_guest.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_host.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_misc.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_debug.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_host_vm_state.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_promote.asm -e bfvmm/src/vmcs/src/vmcs_intel_x64_resume.asm -e bfvmm/src/vmcs/src/vmcs_intel_x64_vmm_state.cpp -e bfvmm/include/vmcs/vmcs_intel_x64_host_vm_state.h -e bfvmm/include/vmcs/vmcs_intel_x64_promote.h -e bfvmm/include/vmcs/vmcs_intel_x64_resume.h -e bfvmm/include/vmcs/vmcs_intel_x64_state.h -e bfvmm/include/vmcs/vmcs_intel_x64_vmm_state.h --gcov-options '\-lp'
- coveralls --build-root makefiles -e build_libbfc -e build_libcxx -e build_libcxxabi -e build_newlib -e build_scripts -e common -e doc -e extensions -e source_libbfc -e source_libcxx -e source_libcxxabi -e source_llvm -e source_newlib -e sysroot -e tools -e bfc -e bfcxx -e bfdrivers/src/arch -e bfm/src/arch -e include/hippomocks.h -e include/unittest.h -e include/new_delete.h -e bfunwind -e bfvmm/src/vcpu_factory/src/vcpu_factory.cpp -e bfvmm/include/intrinsics/intrinsics_x64.h -e bfvmm/include/intrinsics/intrinsics_intel_x64.h -e include/gsl -e include/exception.h -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_controls.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_guest.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_host.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_check_misc.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_debug.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_host_vm_state.cpp -e bfvmm/src/vmcs/src/vmcs_intel_x64_promote.asm -e bfvmm/src/vmcs/src/vmcs_intel_x64_resume.asm -e bfvmm/src/vmcs/src/vmcs_intel_x64_vmm_state.cpp -e bfvmm/include/vmcs/vmcs_intel_x64_host_vm_state.h -e bfvmm/include/vmcs/vmcs_intel_x64_promote.h -e bfvmm/include/vmcs/vmcs_intel_x64_resume.h -e bfvmm/include/vmcs/vmcs_intel_x64_state.h -e bfvmm/include/vmcs/vmcs_intel_x64_vmm_state.h --gcov-options '\-lp'

#
# Coverity
Expand Down
78 changes: 1 addition & 77 deletions bfvmm/src/vmcs/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,86 +20,10 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include <test.h>

size_t g_new_throws_bad_alloc = 0;

static void *
malloc_aligned(std::size_t size)
{
int ret = 0;
void *ptr = nullptr;

ret = posix_memalign(&ptr, MAX_PAGE_SIZE, size);
(void) ret;

return ptr;
}

static void *
custom_new(std::size_t size)
{
if (size == g_new_throws_bad_alloc)
throw std::bad_alloc();

if ((size & (MAX_PAGE_SIZE - 1)) == 0)
return malloc_aligned(size);

return malloc(size);
}

static void
custom_delete(void *ptr)
{
free(ptr);
}

void *
operator new[](std::size_t size)
{
return custom_new(size);
}

void *
operator new(std::size_t size)
{
return custom_new(size);
}

void
operator delete(void *ptr, std::size_t /* size */) throw()
{
custom_delete(ptr);
}

void
operator delete(void *ptr) throw()
{
custom_delete(ptr);
}

void
operator delete[](void *ptr) throw()
{
custom_delete(ptr);
}

void
operator delete[](void *ptr, std::size_t /* size */) throw()
{
custom_delete(ptr);
}
#include <new_delete.h>

vmcs_ut::vmcs_ut()
{
auto mem1 = new char;
auto mem2 = new char[10];
delete mem1;
delete[] mem2;

operator delete(nullptr);
operator delete(nullptr, sizeof(char));
operator delete[](nullptr);
operator delete[](nullptr, sizeof(char));
}

bool
Expand Down
107 changes: 107 additions & 0 deletions include/new_delete.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// Bareflank Hypervisor
//
// Copyright (C) 2015 Assured Information Security, Inc.
// Author: Rian Quinn <quinnr@ainfosec.com>
// Author: Brendan Kerrigan <kerriganb@ainfosec.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef NEW_DELETE_H
#define NEW_DELETE_H

#pragma GCC system_header

// Note:
//
// This file provides a generic replacement for new / delete for unit testing
// so that things like bad_alloc and aligned memory can be unit tested. This
// file can only be included once in a unit test, and it should be included
// as a system header to prevent scanners from getting upset that we are using
// malloc / free which is exactly what the standard libraries are doing.

#include <stdlib.h>
#include <stddef.h>
#include <exception>

size_t g_new_throws_bad_alloc = 0;

static void *
malloc_aligned(std::size_t size)
{
int ret = 0;
void *ptr = nullptr;

ret = posix_memalign(&ptr, MAX_PAGE_SIZE, size);
(void) ret;

return ptr;
}

static void *
custom_new(std::size_t size)
{
if (size == g_new_throws_bad_alloc)
throw std::bad_alloc();

if ((size & (MAX_PAGE_SIZE - 1)) == 0)
return malloc_aligned(size);

return malloc(size);
}

static void
custom_delete(void *ptr)
{
free(ptr);
}

void *
operator new[](std::size_t size)
{
return custom_new(size);
}

void *
operator new(std::size_t size)
{
return custom_new(size);
}

void
operator delete(void *ptr, std::size_t /* size */) throw()
{
custom_delete(ptr);
}

void
operator delete(void *ptr) throw()
{
custom_delete(ptr);
}

void
operator delete[](void *ptr) throw()
{
custom_delete(ptr);
}

void
operator delete[](void *ptr, std::size_t /* size */) throw()
{
custom_delete(ptr);
}

#endif

0 comments on commit 42637a1

Please sign in to comment.