Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8266626: Check that the target address of a native call is not NULL #530

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -28,6 +28,7 @@
import jdk.incubator.foreign.Addressable;
import jdk.incubator.foreign.CLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SegmentAllocator;
import jdk.internal.reflect.CallerSensitive;
@@ -39,17 +40,26 @@
import java.util.Objects;

public abstract class AbstractCLinker implements CLinker {

private static void checkSymbol(Addressable symbol) {
MemoryAddress symbolAddr = symbol.address();
if (symbolAddr.equals(MemoryAddress.NULL))
JornVernee marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("Symbol is NULL: " + symbolAddr);
Copy link
Collaborator

@mcimadamore mcimadamore May 7, 2021

Choose a reason for hiding this comment

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

IAE or NPE?

Copy link
Member Author

@JornVernee JornVernee May 7, 2021

Choose a reason for hiding this comment

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

I think IAE, in order to avoid confusion with the case where symbol is null (This is also why I added the MA instance to the error message). NPE seems more appropriate for the null case I think.

}

@CallerSensitive
public final MethodHandle downcallHandle(Addressable symbol, MethodType type, FunctionDescriptor function) {
Reflection.ensureNativeAccess(Reflection.getCallerClass());
Objects.requireNonNull(symbol);
checkSymbol(symbol);
return MethodHandles.insertArguments(downcallHandle(type, function), 0, symbol);
}

@CallerSensitive
public final MethodHandle downcallHandle(Addressable symbol, SegmentAllocator allocator, MethodType type, FunctionDescriptor function) {
Reflection.ensureNativeAccess(Reflection.getCallerClass());
Objects.requireNonNull(symbol);
checkSymbol(symbol);
Objects.requireNonNull(allocator);
MethodHandle downcall = MethodHandles.insertArguments(downcallHandle(type, function), 0, symbol);
if (type.returnType().equals(MemorySegment.class)) {
@@ -90,11 +90,7 @@ public class ProgrammableInvoker {
methodType(Object.class, Addressable.class, SegmentAllocator.class, Object[].class, MethodHandle.class, Map.class, Map.class));
MH_WRAP_ALLOCATOR = lookup.findStatic(Binding.Context.class, "ofAllocator",
methodType(Binding.Context.class, SegmentAllocator.class));
MethodHandle MH_Addressable_address = lookup.findVirtual(Addressable.class, "address",
methodType(MemoryAddress.class));
MethodHandle MH_MemoryAddress_toRawLongValue = lookup.findVirtual(MemoryAddress.class, "toRawLongValue",
methodType(long.class));
MH_ADDR_TO_LONG = filterArguments(MH_MemoryAddress_toRawLongValue, 0, MH_Addressable_address);
MH_ADDR_TO_LONG = lookup.findStatic(ProgrammableInvoker.class, "unboxTargetAddress", methodType(long.class, Addressable.class));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
@@ -176,6 +172,14 @@ public MethodHandle getBoundMethodHandle() {
return handle;
}

private static long unboxTargetAddress(Addressable addr) {
MemoryAddress ma = addr.address();
if (ma.equals(MemoryAddress.NULL)) {
JornVernee marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("Target address is NULL: " + ma);
}
return ma.toRawLongValue();
}

// Funnel from type to Object[]
private static MethodHandle makeCollectorHandle(MethodType type) {
return type.parameterCount() == 0
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2021, 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.
*/

/*
* @test
* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
* @modules jdk.incubator.foreign
* @run testng/othervm
* --enable-native-access=ALL-UNNAMED
* TestNULLTarget
*/

import jdk.incubator.foreign.Addressable;
import jdk.incubator.foreign.CLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
import org.testng.annotations.Test;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

public class TestNULLTarget {

static final CLinker LINKER = CLinker.getInstance();

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNULLLinking() {
LINKER.downcallHandle(
MemoryAddress.NULL,
MethodType.methodType(void.class),
FunctionDescriptor.ofVoid());
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNULLVirtual() throws Throwable {
MethodHandle mh = LINKER.downcallHandle(
MethodType.methodType(void.class),
FunctionDescriptor.ofVoid());
mh.invokeExact((Addressable) MemoryAddress.NULL);
}
}
@@ -145,8 +145,8 @@ static <Z> void addDefaultMapping(Class<Z> carrier, Z value) {
addDefaultMapping(Charset.class, Charset.defaultCharset());
addDefaultMapping(Consumer.class, x -> {});
addDefaultMapping(MethodType.class, MethodType.methodType(void.class));
addDefaultMapping(MemoryAddress.class, MemoryAddress.NULL);
addDefaultMapping(Addressable.class, MemoryAddress.NULL);
addDefaultMapping(MemoryAddress.class, MemoryAddress.ofLong(1));
addDefaultMapping(Addressable.class, MemoryAddress.ofLong(1));
addDefaultMapping(MemoryLayout.class, MemoryLayouts.JAVA_INT);
addDefaultMapping(ValueLayout.class, MemoryLayouts.JAVA_INT);
addDefaultMapping(GroupLayout.class, MemoryLayout.structLayout(MemoryLayouts.JAVA_INT));