Skip to content

Commit

Permalink
Update NFI unit tests to use native permission.
Browse files Browse the repository at this point in the history
  • Loading branch information
rschatz authored and chumer committed Apr 8, 2018
1 parent bef3fbb commit 7254d77
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2018, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 com.oracle.truffle.nfi.test;

import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.tck.TruffleRunner;
import org.graalvm.polyglot.Context;
import org.junit.Rule;
import org.junit.Test;

public class ForbiddenNFITest {

private final String nativeTestLib = System.getProperty("native.test.lib");

@Rule public TruffleRunner.RunWithPolyglotRule runWithPolyglot = new TruffleRunner.RunWithPolyglotRule(Context.newBuilder().allowNativeAccess(false));

private TruffleObject eval(String format, Object... args) {
Source source = Source.newBuilder(String.format(format, args)).name("ForbiddenNFITest").mimeType("application/x-native").build();
return (TruffleObject) runWithPolyglot.getTruffleTestEnv().parse(source).call();
}

@Test(expected = UnsatisfiedLinkError.class)
public void loadDefault() {
eval("default");
}

@Test(expected = UnsatisfiedLinkError.class)
public void loadTestLib() {
eval("load '%s'", nativeTestLib);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2018, 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 @@ -42,10 +42,11 @@
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.tck.TruffleRunner;
import org.graalvm.polyglot.Context;

public class NFITest {

@ClassRule public static TruffleRunner.RunWithPolyglotRule runWithPolyglot = new TruffleRunner.RunWithPolyglotRule();
@ClassRule public static TruffleRunner.RunWithPolyglotRule runWithPolyglot = new TruffleRunner.RunWithPolyglotRule(Context.newBuilder().allowNativeAccess(true));

protected static TruffleObject defaultLibrary;
protected static TruffleObject testLibrary;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2018, 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 All @@ -24,13 +24,12 @@
*/
package com.oracle.truffle.nfi.test;

import com.oracle.truffle.api.CallTarget;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import com.oracle.truffle.api.TruffleOptions;
Expand All @@ -39,7 +38,8 @@
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.java.*;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.vm.*;
import com.oracle.truffle.tck.TruffleRunner;
import org.graalvm.polyglot.Context;

/**
* This test is to be removed with the PolyglotEngine. Truffle NFI is currently no longer accessible
Expand All @@ -48,7 +48,8 @@
@SuppressWarnings("deprecation")
public class StringAsInterfaceNFITest {
private static StdLib stdlib;
private static PolyglotEngine engine;

@ClassRule public static TruffleRunner.RunWithPolyglotRule runWithPolyglot = new TruffleRunner.RunWithPolyglotRule(Context.newBuilder().allowNativeAccess(true));

@BeforeClass
public static void loadLibraries() {
Expand All @@ -57,20 +58,13 @@ public static void loadLibraries() {
return;
}

engine = PolyglotEngine.newBuilder().build();
stdlib = engine.eval(Source.newBuilder("default {\n" + //
CallTarget load = runWithPolyglot.getTruffleTestEnv().parse(Source.newBuilder("default {\n" + //
" strdup(string):string;\n" + //
" malloc(UINT32):pointer;\n" + //
" free(pointer):void;\n" + //
"}" //
).name("(load default)").mimeType("application/x-native").build()).as(StdLib.class);
}

@AfterClass
public static void cleanUp() {
if (engine != null) {
engine.dispose();
}
).name("(load default)").language("nfi").build());
stdlib = JavaInterop.asJavaObject(StdLib.class, (TruffleObject) load.call());
}

interface StdLib {
Expand Down Expand Up @@ -105,18 +99,17 @@ public void testAllocAndReleaseWithInvoke() throws Exception {
TruffleObject rawStdLib = JavaInterop.asTruffleObject(stdlib);
Object mem = ForeignAccess.sendInvoke(Message.createInvoke(1).createNode(), rawStdLib, "malloc", 512);
assertNotNull("some memory allocated", mem);
Object res = ForeignAccess.sendInvoke(Message.createInvoke(1).createNode(), rawStdLib, "free", mem);
assertTrue("It is number", res instanceof Number);
assertEquals("Zero return code", 0, ((Number) res).intValue());
ForeignAccess.sendInvoke(Message.createInvoke(1).createNode(), rawStdLib, "free", mem);
}

@Test
public void canViewDefaultLibraryAsAnotherInterface() {
Assume.assumeFalse("disable test on AOT", TruffleOptions.AOT);
Strndup second = engine.eval(Source.newBuilder("default {\n" + //
CallTarget load = runWithPolyglot.getTruffleTestEnv().parse(Source.newBuilder("default {\n" + //
" strndup(string, UINT32):string;\n" + //
"}" //
).name("(load default 2nd time)").mimeType("application/x-native").build()).as(Strndup.class);
).name("(load default)").language("nfi").build());
Strndup second = JavaInterop.asJavaObject(Strndup.class, (TruffleObject) load.call());

String copy = stdlib.strdup("Hello World!");
String hello = second.strndup(copy, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ TruffleObject findSymbol(String name) {
void preBindSymbol(String name, TruffleObject symbol) {
symbols.put(name, symbol);
}

@TruffleBoundary
String[] getSymbols() {
return symbols.keySet().toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.interop.java.JavaInterop;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.nfi.NFILibraryMessageResolutionFactory.CachedLookupSymbolNodeGen;
import com.oracle.truffle.nfi.NFILibraryMessageResolutionFactory.IdentToStringNodeGen;
Expand Down Expand Up @@ -104,6 +105,14 @@ public Object access(NFILibrary receiver, String symbol, Object... args) {
}
}

@Resolve(message = "KEYS")
abstract static class KeysNode extends Node {

public Object access(NFILibrary receiver) {
return JavaInterop.asTruffleObject(receiver.getSymbols());
}
}

@Resolve(message = "KEY_INFO")
abstract static class KeyInfoNode extends Node {

Expand Down

0 comments on commit 7254d77

Please sign in to comment.