1 /*
2  * Copyright (c) 1998, 2021, 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 "memory/resourceArea.hpp"
27 #include "oops/oop.hpp"
28 #include "runtime/handles.inline.hpp"
29 #include "utilities/preserveException.hpp"
30 
PreserveExceptionMark(Thread * thread)31 PreserveExceptionMark::PreserveExceptionMark(Thread* thread) {
32   _thread = thread;
33   _preserved_exception_oop = Handle(thread, _thread->pending_exception());
34   _preserved_exception_line = _thread->exception_line();
35   _preserved_exception_file = _thread->exception_file();
36   _thread->clear_pending_exception(); // Pending exceptions are checked in the destructor
37 }
38 
39 
~PreserveExceptionMark()40 PreserveExceptionMark::~PreserveExceptionMark() {
41   if (_thread->has_pending_exception()) {
42     oop exception = _thread->pending_exception();
43     _thread->clear_pending_exception(); // Needed to avoid infinite recursion
44     ResourceMark rm(_thread);
45     assert(false, "PreserveExceptionMark destructor expects no pending exceptions %s",
46                   exception->print_string());
47   }
48 
49   if (_preserved_exception_oop() != NULL) {
50     _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
51   }
52 }
53 
preserve()54 void WeakPreserveExceptionMark::preserve() {
55   _preserved_exception_oop = Handle(_thread, _thread->pending_exception());
56   _preserved_exception_line = _thread->exception_line();
57   _preserved_exception_file = _thread->exception_file();
58   _thread->clear_pending_exception();
59 }
60 
restore()61 void WeakPreserveExceptionMark::restore() {
62   if (!_thread->has_pending_exception()) {
63     _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
64   }
65 }
66