|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include "precompiled.hpp" |
| 26 | +#include "runtime/os.hpp" |
| 27 | +#include "runtime/globals.hpp" |
| 28 | +#include "runtime/stackOverflow.hpp" |
| 29 | +#include "utilities/align.hpp" |
| 30 | +#include "utilities/globalDefinitions.hpp" |
| 31 | +#include "utilities/ostream.hpp" |
| 32 | +#include "unittest.hpp" |
| 33 | + |
| 34 | + |
| 35 | +TEST_VM(StackOverflow, basics) { |
| 36 | + StackOverflow so; |
| 37 | + |
| 38 | + // Make up a stack range. No need to allocate anything. Size has to be large enough |
| 39 | + // to fit sum of all zones into them. |
| 40 | + address base = (address) 0x40000000; |
| 41 | + const size_t size = os::vm_page_size() * 100; |
| 42 | + address end = base - size; |
| 43 | + so.initialize(base, end); |
| 44 | + |
| 45 | + // Walking down the "stack" check for consistency of the three "in_stack_xxx" functions |
| 46 | + enum { normal_stack, reserved_or_yellow_zone, red_zone } where = normal_stack; |
| 47 | + for (address p = base - 1; p >= end; p -= os::vm_page_size()) { |
| 48 | + // tty->print_cr(PTR_FORMAT " %d %d %d", p2i(p), |
| 49 | + // (int)so.in_stack_reserved_zone(p), |
| 50 | + // (int)so.in_stack_yellow_reserved_zone(p), |
| 51 | + // (int)so.in_stack_red_zone(p)); |
| 52 | + switch (where) { |
| 53 | + case normal_stack: |
| 54 | + ASSERT_FALSE(so.in_stack_red_zone(p)); |
| 55 | + if (so.in_stack_yellow_reserved_zone(p)) { |
| 56 | + if (StackReservedPages > 0) { |
| 57 | + ASSERT_TRUE(so.in_stack_reserved_zone(p)); |
| 58 | + } else { |
| 59 | + ASSERT_FALSE(so.in_stack_reserved_zone(p)); |
| 60 | + } |
| 61 | + where = reserved_or_yellow_zone; |
| 62 | + } else { |
| 63 | + ASSERT_FALSE(so.in_stack_reserved_zone((p))); |
| 64 | + } |
| 65 | + break; |
| 66 | + case reserved_or_yellow_zone: |
| 67 | + if (so.in_stack_red_zone(p)) { |
| 68 | + ASSERT_FALSE(so.in_stack_yellow_reserved_zone(p)); |
| 69 | + where = red_zone; |
| 70 | + } else { |
| 71 | + ASSERT_TRUE(so.in_stack_yellow_reserved_zone(p)); |
| 72 | + } |
| 73 | + break; |
| 74 | + case red_zone: |
| 75 | + ASSERT_TRUE(so.in_stack_red_zone(p)); |
| 76 | + ASSERT_FALSE(so.in_stack_yellow_reserved_zone(p)); |
| 77 | + ASSERT_FALSE(so.in_stack_reserved_zone((p))); |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + ASSERT_EQ(where, red_zone); |
| 82 | + |
| 83 | + // Check bases. |
| 84 | + ASSERT_FALSE(so.in_stack_red_zone(so.stack_red_zone_base())); |
| 85 | + ASSERT_TRUE(so.in_stack_red_zone(so.stack_red_zone_base() - 1)); |
| 86 | + ASSERT_TRUE(so.in_stack_yellow_reserved_zone(so.stack_red_zone_base())); |
| 87 | + ASSERT_FALSE(so.in_stack_reserved_zone(so.stack_reserved_zone_base())); |
| 88 | + if (so.stack_reserved_zone_size() > 0) { |
| 89 | + ASSERT_TRUE(so.in_stack_reserved_zone(so.stack_reserved_zone_base() - 1)); |
| 90 | + } |
| 91 | +} |
0 commit comments