Skip to content

Commit d1efb0c

Browse files
author
Gerard Ziemski
committedJan 19, 2022
8267341: macos attempt_reserve_memory_at(arg1, arg2, true) failure
Reviewed-by: dcubed, dholmes
1 parent 6179e13 commit d1efb0c

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed
 

‎test/hotspot/gtest/runtime/test_os.cpp

+28-4
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,23 @@ TEST_VM(os, jio_snprintf) {
351351
test_snprintf(jio_snprintf, false);
352352
}
353353

354+
#ifdef __APPLE__
355+
// Not all macOS versions can use os::reserve_memory (i.e. anon_mmap) API
356+
// to reserve executable memory, so before attempting to use it,
357+
// we need to verify that we can do so by asking for a tiny executable
358+
// memory chunk.
359+
static inline bool can_reserve_executable_memory(void) {
360+
bool executable = true;
361+
size_t len = 128;
362+
char* p = os::reserve_memory(len, executable);
363+
bool exec_supported = (p != NULL);
364+
if (exec_supported) {
365+
os::release_memory(p, len);
366+
}
367+
return exec_supported;
368+
}
369+
#endif
370+
354371
// Test that os::release_memory() can deal with areas containing multiple mappings.
355372
#define PRINT_MAPPINGS(s) { tty->print_cr("%s", s); os::print_memory_mappings((char*)p, total_range_len, tty); }
356373
//#define PRINT_MAPPINGS
@@ -360,6 +377,13 @@ TEST_VM(os, jio_snprintf) {
360377
// (from multiple calls to os::reserve_memory)
361378
static address reserve_multiple(int num_stripes, size_t stripe_len) {
362379
assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");
380+
381+
#ifdef __APPLE__
382+
// Workaround: try reserving executable memory to figure out
383+
// if such operation is supported on this macOS version
384+
const bool exec_supported = can_reserve_executable_memory();
385+
#endif
386+
363387
size_t total_range_len = num_stripes * stripe_len;
364388
// Reserve a large contiguous area to get the address space...
365389
address p = (address)os::reserve_memory(total_range_len);
@@ -371,7 +395,11 @@ static address reserve_multiple(int num_stripes, size_t stripe_len) {
371395
address q = p + (stripe * stripe_len);
372396
// Commit, alternatingly with or without exec permission,
373397
// to prevent kernel from folding these mappings.
398+
#ifdef __APPLE__
399+
const bool executable = exec_supported ? (stripe % 2 == 0) : false;
400+
#else
374401
const bool executable = stripe % 2 == 0;
402+
#endif
375403
q = (address)os::attempt_reserve_memory_at((char*)q, stripe_len, executable);
376404
EXPECT_NE(q, (address)NULL);
377405
EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, executable));
@@ -413,11 +441,7 @@ struct NUMASwitcher {
413441
#endif
414442

415443
#ifndef _AIX // JDK-8257041
416-
#if defined(__APPLE__) && !defined(AARCH64) // See JDK-8267341.
417-
TEST_VM(os, DISABLED_release_multi_mappings) {
418-
#else
419444
TEST_VM(os, release_multi_mappings) {
420-
#endif
421445

422446
// With NMT enabled, this will trigger JDK-8263464. For now disable the test if NMT=on.
423447
if (MemTracker::tracking_level() > NMT_off) {

0 commit comments

Comments
 (0)
Please sign in to comment.