/* * Copyright (c) 2022 SAP SE. All rights reserved. * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// Check NMT header for integrity, as well as expected type and size. staticvoid check_expected_malloc_header(constvoid* payload, MEMFLAGS type, size_t size) { const MallocHeader* hdr = MallocTracker::malloc_header(payload);
hdr->assert_block_integrity();
EXPECT_EQ(hdr->size(), size);
EXPECT_EQ(hdr->flags(), type);
}
// Check that a malloc with an overflowing size is rejected.
TEST_VM(NMT, malloc_failure1) { void* p = os::malloc(SIZE_MAX, mtTest);
EXPECT_NULL(p);
}
// Check that gigantic mallocs are rejected, even if no size overflow happens.
TEST_VM(NMT, malloc_failure2) { void* p = os::malloc(SIZE_MAX - M, mtTest);
EXPECT_NULL(p);
}
// We test this with both NMT enabled and disabled. bool nmt_enabled = MemTracker::enabled(); const size_t first_size = 0x100;
void* p = os::malloc(first_size, mtTest);
EXPECT_NOT_NULL(p); if (nmt_enabled) {
check_expected_malloc_header(p, mtTest, first_size);
}
GtestUtils::mark_range(p, first_size);
// should fail void* p2 = os::realloc(p, failing_request_size, mtTest);
EXPECT_NULL(p2);
// original allocation should still be intact
GtestUtils::check_range(p, first_size); if (nmt_enabled) {
check_expected_malloc_header(p, mtTest, first_size);
}
EXPECT_NOT_NULL(p); if (check_nmt_header) {
check_expected_malloc_header(p, mtTest, old_size);
}
void* p2 = os::realloc(p, new_size, mtTest);
EXPECT_NOT_NULL(p2); if (check_nmt_header) {
check_expected_malloc_header(p2, mtTest, new_size);
}
// Check old content, and possibly zapped area (if block grew) if (old_size < new_size) {
GtestUtils::check_range((char*)p2, old_size, old_content); #ifdef ASSERT
GtestUtils::check_range((char*)p2 + old_size, new_size - old_size, uninitBlockPad); #endif
} else {
GtestUtils::check_range((char*)p2, new_size, old_content);
}
return p2;
}
// Check a random sequence of reallocs. For enlarging reallocs, we expect the // newly allocated memory to be zapped (in debug) while the old section should be // left intact.
TEST_VM(NMT, random_reallocs) {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.