1 /*
 2  * Copyright (c) 2022, 2026, 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 #include "downcallLinker.hpp"
25 #include "runtime/interfaceSupport.inline.hpp"
26 
27 #include <cerrno>
28 #ifdef _WIN64
29 #include <Windows.h>
30 #include <Winsock2.h>
31 #endif
32 
33 // keep in synch with jdk.internal.foreign.abi.CapturableState
34 enum PreservableValues {
35   NONE = 0,
36   GET_LAST_ERROR = 1,
37   WSA_GET_LAST_ERROR = 1 << 1,
38   ERRNO = 1 << 2
39 };
40 
41 // We call this from _thread_in_native, right before a downcall
42 JVM_LEAF(void, DowncallLinker::capture_state_pre(int32_t* value_ptr, int captured_state_mask))
43 #ifdef _WIN64
44   if (captured_state_mask & GET_LAST_ERROR) {
45     SetLastError(*value_ptr);
46   }
47   value_ptr++;
48   if (captured_state_mask & WSA_GET_LAST_ERROR) {
49     WSASetLastError(*value_ptr);
50     *value_ptr = WSAGetLastError();
51   }
52   value_ptr++;
53 #endif
54   if (captured_state_mask & ERRNO) {
55     errno = *value_ptr;
56   }
57 JVM_END
58 
59 // We call this from _thread_in_native, right after a downcall
60 JVM_LEAF(void, DowncallLinker::capture_state_post(int32_t* value_ptr, int captured_state_mask))
61 #ifdef _WIN64
62   if (captured_state_mask & GET_LAST_ERROR) {
63     *value_ptr = GetLastError();
64   }
65   value_ptr++;
66   if (captured_state_mask & WSA_GET_LAST_ERROR) {
67     *value_ptr = WSAGetLastError();
68   }
69   value_ptr++;
70 #endif
71   if (captured_state_mask & ERRNO) {
72     *value_ptr = errno;
73   }
74 JVM_END
75 
76 void DowncallLinker::StubGenerator::add_offsets_to_oops(GrowableArray<VMStorage>& java_regs, VMStorage tmp1, VMStorage tmp2) const {
77   int reg_idx = 0;
78   for (int sig_idx = 0; sig_idx < _num_args; sig_idx++) {
79     if (_signature[sig_idx] == T_OBJECT) {
80       assert(_signature[sig_idx + 1] == T_LONG, "expected offset after oop");
81       VMStorage reg_oop = java_regs.at(reg_idx++);
82       VMStorage reg_offset = java_regs.at(reg_idx++);
83       sig_idx++; // skip offset
84       pd_add_offset_to_oop(reg_oop, reg_offset, tmp1, tmp2);
85     } else if (_signature[sig_idx] != T_VOID) {
86       reg_idx++;
87     }
88   }
89 }