Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/hotspot/cpu/aarch64/upcallLinker_aarch64.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Arm Limited. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -330,7 +330,12 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
= UpcallStub::create(name,
&buffer,
receiver,
in_ByteSize(frame_data_offset));
in_ByteSize(frame_data_offset),
/*
* frame size should be in words,
* and also should include both saved FP and return address
*/
(frame_size / wordSize) + 2);
if (blob == nullptr) {
return nullptr;
}
Expand Down
9 changes: 7 additions & 2 deletions src/hotspot/cpu/ppc/upcallLinker_ppc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -338,7 +338,12 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
= UpcallStub::create(name,
&buffer,
receiver,
in_ByteSize(frame_data_offset));
in_ByteSize(frame_data_offset),
/*
* frame size should be in words,
* and also should include the frame
*/
(frame_size / wordSize) + 1);
if (blob == nullptr) {
return nullptr;
}
Expand Down
9 changes: 7 additions & 2 deletions src/hotspot/cpu/riscv/upcallLinker_riscv.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -350,7 +350,12 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
= UpcallStub::create(name,
&buffer,
receiver,
in_ByteSize(frame_data_offset));
in_ByteSize(frame_data_offset),
/*
* frame size should be in words,
* and also should include both saved FP and return address
*/
(frame_size / wordSize) + 2);
if (blob == nullptr) {
return nullptr;
}
Expand Down
9 changes: 7 additions & 2 deletions src/hotspot/cpu/s390/upcallLinker_s390.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Red Hat, Inc. All rights reserved.
* Copyright (c) 2020, 2024, Red Hat, Inc. 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
Expand Down Expand Up @@ -285,7 +285,12 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
= UpcallStub::create(name,
&buffer,
receiver,
in_ByteSize(frame_data_offset));
in_ByteSize(frame_data_offset),
/*
* frame size should be in words,
* and also should include return address
*/
(frame_size / wordSize) + 1);
if (blob == nullptr) {
return nullptr;
}
Expand Down
9 changes: 7 additions & 2 deletions src/hotspot/cpu/x86/upcallLinker_x86_64.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, 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
Expand Down Expand Up @@ -389,7 +389,12 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
= UpcallStub::create(name,
&buffer,
receiver,
in_ByteSize(frame_data_offset));
in_ByteSize(frame_data_offset),
/*
* frame size should be in words,
* and also should include both saved FP and return address
*/
(frame_size / wordSize) + 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I witnessed build failures for other CPU ports. Shouldn't all the callsites of UpcallStub::create be updated to reflect this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I fixed all of caller of UpcallStub::create, and then they passed build test.
(This PR branch starts with pr/, so GHA did not start automatically. Thus I started it in manual.)

Test for this PR was kicked in serviceability test, and it works fine on both x86_64 and aarch64.
In RISC-V, GHA did not kick the test, but I believe it would work fine because stack structure is similar with x86_64 (return address and FP are seemed to store on the stack)

I'm not sure on s390 and PPC64.
In PPC64, I checked UpcallLinker::make_upcall_stub. It seems to push into the stack once as following, but I'm not sure.

  address start = __ function_entry(); // called by C
  __ save_LR_CR(R0);
  assert((abi._stack_alignment_bytes % 16) == 0, "must be 16 byte aligned");
  // allocate frame (frame_size is also aligned, so stack is still aligned)
  __ push_frame(frame_size, tmp);

s390 also pushes address into the stack, but SA does not have s390 implementation, so we might be able to tackle this later when SA supports s390.

Copy link
Member

@RealFYang RealFYang Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can confirm that it works on RISC-V as well. Thanks for the update.

if (blob == nullptr) {
return nullptr;
}
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/share/code/codeBlob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,9 @@ SafepointBlob* SafepointBlob::create(
//----------------------------------------------------------------------------------------------------
// Implementation of UpcallStub

UpcallStub::UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset) :
UpcallStub::UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset, int frame_size) :
RuntimeBlob(name, CodeBlobKind::Upcall, cb, size, sizeof(UpcallStub),
CodeOffsets::frame_never_safe, 0 /* no frame size */,
CodeOffsets::frame_never_safe, frame_size,
/* oop maps = */ nullptr, /* caller must gc arguments = */ false),
_receiver(receiver),
_frame_data_offset(frame_data_offset)
Expand All @@ -607,14 +607,14 @@ void* UpcallStub::operator new(size_t s, unsigned size) throw() {
return CodeCache::allocate(size, CodeBlobType::NonNMethod);
}

UpcallStub* UpcallStub::create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset) {
UpcallStub* UpcallStub::create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset, int frame_size) {
ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock

UpcallStub* blob = nullptr;
unsigned int size = CodeBlob::allocation_size(cb, sizeof(UpcallStub));
{
MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
blob = new (size) UpcallStub(name, cb, size, receiver, frame_data_offset);
blob = new (size) UpcallStub(name, cb, size, receiver, frame_data_offset, frame_size);
}
if (blob == nullptr) {
return nullptr; // caller must handle this
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/code/codeBlob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ class UpcallStub: public RuntimeBlob {
jobject _receiver;
ByteSize _frame_data_offset;

UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset);
UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset, int frame_size);

void* operator new(size_t s, unsigned size) throw();

Expand All @@ -615,7 +615,7 @@ class UpcallStub: public RuntimeBlob {
FrameData* frame_data_for_frame(const frame& frame) const;
public:
// Creation
static UpcallStub* create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset);
static UpcallStub* create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset, int frame_size);

static void free(UpcallStub* blob);

Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/runtime/vmStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@
declare_type(nmethod, CodeBlob) \
declare_type(RuntimeStub, RuntimeBlob) \
declare_type(SingletonBlob, RuntimeBlob) \
declare_type(UpcallStub, RuntimeBlob) \
declare_type(SafepointBlob, SingletonBlob) \
declare_type(DeoptimizationBlob, SingletonBlob) \
declare_c2_type(ExceptionBlob, SingletonBlob) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public ImmutableOopMapSet getOopMaps() {

public boolean isRuntimeStub() { return false; }

public boolean isUpcallStub() { return false; }

public boolean isDeoptimizationStub() { return false; }

public boolean isUncommonTrapStub() { return false; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, 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
Expand Down Expand Up @@ -60,6 +60,7 @@ private static synchronized void initialize(TypeDataBase db) {
virtualConstructor.addMapping("AdapterBlob", AdapterBlob.class);
virtualConstructor.addMapping("MethodHandlesAdapterBlob", MethodHandlesAdapterBlob.class);
virtualConstructor.addMapping("VtableBlob", VtableBlob.class);
virtualConstructor.addMapping("UpcallStub", UpcallStub.class);
virtualConstructor.addMapping("SafepointBlob", SafepointBlob.class);
virtualConstructor.addMapping("DeoptimizationBlob", DeoptimizationBlob.class);
if (VM.getVM().isServerCompiler()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024, 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.
*
*/

package sun.jvm.hotspot.code;

import java.util.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.types.*;
import sun.jvm.hotspot.utilities.Observable;
import sun.jvm.hotspot.utilities.Observer;

public class UpcallStub extends RuntimeBlob {
static {
VM.registerVMInitializedObserver(new Observer() {
public void update(Observable o, Object data) {
initialize(VM.getVM().getTypeDataBase());
}
});
}

private static void initialize(TypeDataBase db) {
Type type = db.lookupType("UpcallStub");
}

public UpcallStub(Address addr) {
super(addr);
}

public boolean isUpcallStub() { return true; }
}
69 changes: 69 additions & 0 deletions test/hotspot/jtreg/serviceability/sa/LingeredAppWithFFMUpcall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

/*
* Copyright (c) 2024, 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.
*/

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;

import jdk.test.lib.apps.LingeredApp;
import jdk.test.lib.Utils;

public class LingeredAppWithFFMUpcall extends LingeredApp {

public static final String THREAD_NAME = "Upcall thread";

static {
System.loadLibrary("upcall");
}

public static void upcall() {
try {
Thread.sleep(600000); // 10 min
} catch (InterruptedException e) {
// Ignore
}
}

public static long createFunctionPointerForUpcall() throws NoSuchMethodException, IllegalAccessException {
var mh = MethodHandles.lookup()
.findStatic(LingeredAppWithFFMUpcall.class, "upcall", MethodType.methodType(void.class));
var stub = Linker.nativeLinker()
.upcallStub(mh, FunctionDescriptor.ofVoid(), Arena.global());
return stub.address();
}

public static native void callJNI(long upcallAddr);

public static void main(String[] args) {
try {
long upcallAddr = createFunctionPointerForUpcall();
(new Thread(() -> callJNI(upcallAddr), THREAD_NAME)).start();
LingeredApp.main(args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading