test: use CHECK instead of EXPECT where necessary

GetPageSize() and OverrunGuardedBuffer currently use non-fatal EXPECT_*
macros because GoogleTest does not allow the fatal variants ASSERT_* in
non-void returning functions (i.e., in this file, nowhere outside of the
TEST itself).

The EXPECT_* macros continue execution upon failure, but we really don't
want that (and static analysis apparently does not like it either).
Since we cannot use GoogleTest's ASSERT_* here, use our own CHECK_*
instead of EXPECT_* outside of the TEST. Hopefully, this will finally
pacify static analysis.

Refs: https://github.com/nodejs/node/pull/44666

PR-URL: https://github.com/nodejs/node/pull/44795
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
This commit is contained in:
Tobias Nießen 2022-09-26 10:00:38 +00:00 committed by Michael Dawson
parent 5be8a30cc3
commit fcf27b12e3

View File

@ -32,7 +32,7 @@
#if defined(USE_MPROTECT) #if defined(USE_MPROTECT)
size_t GetPageSize() { size_t GetPageSize() {
int page_size = sysconf(_SC_PAGE_SIZE); int page_size = sysconf(_SC_PAGE_SIZE);
EXPECT_GE(page_size, 1); CHECK_GE(page_size, 1);
return page_size; return page_size;
} }
#elif defined(USE_VIRTUALPROTECT) #elif defined(USE_VIRTUALPROTECT)
@ -49,32 +49,32 @@ class OverrunGuardedBuffer {
OverrunGuardedBuffer() { OverrunGuardedBuffer() {
#if defined(USE_MPROTECT) || defined(USE_VIRTUALPROTECT) #if defined(USE_MPROTECT) || defined(USE_VIRTUALPROTECT)
size_t page = GetPageSize(); size_t page = GetPageSize();
EXPECT_GE(page, N); CHECK_GE(page, N);
#endif #endif
#ifdef USE_MPROTECT #ifdef USE_MPROTECT
// Place the packet right before a guard page, which, when accessed, causes // Place the packet right before a guard page, which, when accessed, causes
// a segmentation fault. // a segmentation fault.
alloc_base = static_cast<uint8_t*>(aligned_alloc(page, 2 * page)); alloc_base = static_cast<uint8_t*>(aligned_alloc(page, 2 * page));
EXPECT_NE(alloc_base, nullptr); CHECK_NOT_NULL(alloc_base);
uint8_t* second_page = alloc_base + page; uint8_t* second_page = alloc_base + page;
EXPECT_EQ(mprotect(second_page, page, PROT_NONE), 0); CHECK_EQ(mprotect(second_page, page, PROT_NONE), 0);
data_base = second_page - N; data_base = second_page - N;
#elif defined(USE_VIRTUALPROTECT) #elif defined(USE_VIRTUALPROTECT)
// On Windows, it works almost the same way. // On Windows, it works almost the same way.
alloc_base = static_cast<uint8_t*>( alloc_base = static_cast<uint8_t*>(
VirtualAlloc(nullptr, 2 * page, MEM_COMMIT, PAGE_READWRITE)); VirtualAlloc(nullptr, 2 * page, MEM_COMMIT, PAGE_READWRITE));
EXPECT_NE(alloc_base, nullptr); CHECK_NOT_NULL(alloc_base);
uint8_t* second_page = alloc_base + page; uint8_t* second_page = alloc_base + page;
DWORD old_prot; DWORD old_prot;
EXPECT_NE(VirtualProtect(second_page, page, PAGE_NOACCESS, &old_prot), 0); CHECK_NE(VirtualProtect(second_page, page, PAGE_NOACCESS, &old_prot), 0);
EXPECT_EQ(old_prot, PAGE_READWRITE); CHECK_EQ(old_prot, PAGE_READWRITE);
data_base = second_page - N; data_base = second_page - N;
#else #else
// Place the packet in a regular allocated buffer. The bug causes undefined // Place the packet in a regular allocated buffer. The bug causes undefined
// behavior, which might crash the process, and when it does not, address // behavior, which might crash the process, and when it does not, address
// sanitizers and valgrind will catch it. // sanitizers and valgrind will catch it.
alloc_base = static_cast<uint8_t*>(malloc(N)); alloc_base = static_cast<uint8_t*>(malloc(N));
EXPECT_NE(alloc_base, nullptr); CHECK_NOT_NULL(alloc_base);
data_base = alloc_base; data_base = alloc_base;
#endif #endif
} }
@ -92,7 +92,7 @@ class OverrunGuardedBuffer {
#ifdef USE_MPROTECT #ifdef USE_MPROTECT
// Revert page protection such that the memory can be free()'d. // Revert page protection such that the memory can be free()'d.
uint8_t* second_page = alloc_base + page; uint8_t* second_page = alloc_base + page;
EXPECT_EQ(mprotect(second_page, page, PROT_READ | PROT_WRITE), 0); CHECK_EQ(mprotect(second_page, page, PROT_READ | PROT_WRITE), 0);
#endif #endif
free(alloc_base); free(alloc_base);
#endif #endif