diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/EmbeddedConsoleHandler.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/EmbeddedConsoleHandler.java index 750367a6f0..d8ed066ab8 100644 --- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/EmbeddedConsoleHandler.java +++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/EmbeddedConsoleHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2022, 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 diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java index 16962d882b..cca1e99073 100644 --- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java +++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java @@ -2764,7 +2764,7 @@ public Object FASTR_DATAPTR(Object x) { @Override public Object FASTR_GlobalVarAlloc(RContext context) { - return context.stateglobalNativeVar.allocGlobalVarDescr(); + return GlobalNativeVarContext.allocGlobalVarDescr(); } @Override diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/FastRUpCalls.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/FastRUpCalls.java index 81844b566a..d1fb2f01a3 100644 --- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/FastRUpCalls.java +++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/upcalls/FastRUpCalls.java @@ -30,8 +30,9 @@ import com.oracle.truffle.r.runtime.context.RContext; /** - * Up-calls specific to FastR used in FastR native code. Most of the functions are not exported in any API, - * except for the global var API ({@code FASTR_GlobalVar*}, which is exported in {@code Rinternals.h}. + * Up-calls specific to FastR used in FastR native code. Most of the functions are not exported in + * any API, except for the global var API ({@code FASTR_GlobalVar*}, which is exported in + * {@code Rinternals.h}. */ public interface FastRUpCalls { // Checkstyle: stop method name check @@ -71,9 +72,9 @@ public interface FastRUpCalls { Object FASTR_GlobalVarAlloc(@RFFIInject RContext context); /** - * Initializes the {@code globVarDescr} with the information about the current context. - * i.e. assign an index into the per-context array of global native variables. - * Must be called at most once for every context. + * Initializes the {@code globVarDescr} with the information about the current context. i.e. + * assign an index into the per-context array of global native variables. Must be called at most + * once for every context. */ void FASTR_GlobalVarInit(Object globVarDescr, @RFFIInject RContext context); diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java index 1c953b089b..4e4b0d3c7e 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java @@ -4,7 +4,7 @@ * Copyright (c) 1998--2014, The R Core Team * Copyright (c) 2002--2010, The R Foundation * Copyright (C) 2005--2006, Morten Welinder - * Copyright (c) 2014, 2019, Oracle and/or its affiliates + * Copyright (c) 2014, 2022, Oracle and/or its affiliates * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/com.oracle.truffle.r.native/fficall/src/truffle_nfi/rffiutils.c b/com.oracle.truffle.r.native/fficall/src/truffle_nfi/rffiutils.c index fe2d15d6d3..a50e2cc216 100644 --- a/com.oracle.truffle.r.native/fficall/src/truffle_nfi/rffiutils.c +++ b/com.oracle.truffle.r.native/fficall/src/truffle_nfi/rffiutils.c @@ -48,16 +48,7 @@ static __thread int callErrorJmpBufStackIndex = 0; static __thread int exceptionFlag = 0; void exitCall() { - if (callErrorJmpBufStackIndex > 0 && peekJmpBuf() != NULL) { - jmp_buf *error_jmpbuf = peekJmpBuf(); - if (error_jmpbuf == NULL) { - fatalError("exitCall: peekJmpBuf() should return non-null"); - } - longjmp(*error_jmpbuf, 1); - } - // callErrorJmpBufStackIndex == 0 can happen when there is bad pairing of push/pop to errorjmpBufStack. - // We do not consider it a fatal error here, since there should already be a Java exception - // raised in the JVM, therefore, should be handled after this downcall returns. + longjmp(*callErrorJmpBufStack[callErrorJmpBufStackIndex - 1], 1); } void checkExitCall() { diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/Engine.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/Engine.java index d59288226d..be01e65e16 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/Engine.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/Engine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2022, 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 diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/GlobalNativeVarContext.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/GlobalNativeVarContext.java index cae368ada1..9d062737dd 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/GlobalNativeVarContext.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/GlobalNativeVarContext.java @@ -1,3 +1,25 @@ +/* + * Copyright (c) 2022, 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 3 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 3 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 + * 3 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.r.runtime.context; import com.oracle.truffle.api.CompilerDirectives; @@ -17,12 +39,13 @@ import java.util.HashMap; import java.util.Map; -public class GlobalNativeVarContext implements RContext.ContextState { +public final class GlobalNativeVarContext implements RContext.ContextState { /** * We have to wrap {@link GlobalVarDescriptor descriptors} in {@link RForeignObjectWrapper} and - * {@link com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeMirror} in this class, because - * we expect all the descriptors to have static life cycle, whereas, usually, foreign object - * wrappers and native mirrors have a life cycle tied to the life cycle of a specific context. + * {@link com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeMirror} in this class, + * because we expect all the descriptors to have static life cycle, whereas, usually, foreign + * object wrappers and native mirrors have a life cycle tied to the life cycle of a specific + * context. * * Note that the static life cycle of descriptors is expected, because the descriptors are * allocated once per every DLL load. @@ -49,7 +72,7 @@ public static GlobalNativeVarContext newContextState(RContext context) { * wrapper, and assigns it a native mirror. */ @CompilerDirectives.TruffleBoundary - public RForeignObjectWrapper allocGlobalVarDescr() { + public static RForeignObjectWrapper allocGlobalVarDescr() { var foreignWrapper = new RForeignObjectWrapper(new GlobalVarDescriptor()); NativeDataAccess.createNativeMirror(foreignWrapper); globalNativeVarDescriptors.add(foreignWrapper); @@ -85,25 +108,25 @@ public Object getGlobalVar(Object descr, InteropLibrary interop) { } @Override - public void beforeFinalize(RContext context) { - callAllDestructors(context, InteropLibrary.getUncached()); + public void beforeFinalize(RContext ctx) { + callAllDestructors(ctx, InteropLibrary.getUncached()); } /** * Removes {@code context} from all teh global native variable descriptors. * - * @param context Context used as the key for global native variable descriptors. + * @param ctx Context used as the key for global native variable descriptors. */ @Override - public void beforeDispose(RContext context) { + public void beforeDispose(RContext ctx) { for (int i = 0; i < globalNativeVarDescriptors.size(); i++) { GlobalVarDescriptor descriptor = getDesriptorAt(i); // Some descriptors were created only for a particular context, so it is possible // that some descriptors do not have any information about some contexts. // Therefore, we have to check for `containsKey`. - if (descriptor.containsKey(context.getId())) { + if (descriptor.containsKey(ctx.getId())) { try { - descriptor.removeHashEntry(context.getId()); + descriptor.removeHashEntry(ctx.getId()); } catch (UnknownKeyException e) { throw RInternalError.shouldNotReachHere(e); } @@ -111,7 +134,7 @@ public void beforeDispose(RContext context) { } } - public void callAllDestructors(RContext context, InteropLibrary interop) { + public void callAllDestructors(RContext ctx, InteropLibrary interop) { // Do not wrap the argument to the native function. boolean[] whichArgToWrap = {false}; for (int i = 0; i < destructors.size(); i++) { @@ -119,7 +142,7 @@ public void callAllDestructors(RContext context, InteropLibrary interop) { Object ptrForDestructor = getGlobalVar(destructor.globalVarDescr, interop); interop.toNative(ptrForDestructor); assert interop.isPointer(ptrForDestructor); - Object ret = context.getRFFI().callNativeFunction(destructor.nativeFunc, destructor.nativeFuncType, Destructor.SIGNATURE, + Object ret = ctx.getRFFI().callNativeFunction(destructor.nativeFunc, destructor.nativeFuncType, Destructor.SIGNATURE, new Object[]{ptrForDestructor}, whichArgToWrap); assert interop.isNull(ret); } @@ -134,8 +157,8 @@ public static void printAllDescriptors(RContext context) { for (int i = 0; i < globalNativeVarDescriptors.size(); i++) { var descrWrapper = globalNativeVarDescriptors.get(i); context.getConsole().println( - String.format(" RForeignObjectWrapper{nativeMirror = %s, delegate = %s}, ", - descrWrapper.getNativeMirror(), descrWrapper.getDelegate())); + String.format(" RForeignObjectWrapper{nativeMirror = %s, delegate = %s}, ", + descrWrapper.getNativeMirror(), descrWrapper.getDelegate())); } context.getConsole().println("]"); } @@ -178,6 +201,7 @@ static final class GlobalVarDescriptor implements TruffleObject { private final Map contextIndexes = new HashMap<>(); @ExportMessage + @SuppressWarnings("static-method") boolean hasHashEntries() { return true; } @@ -196,7 +220,7 @@ boolean isHashEntryReadable(Object key) { @ExportMessage @CompilerDirectives.TruffleBoundary Object readHashValue(Object key) { - return contextIndexes.get((Integer) key); + return contextIndexes.get(key); } @ExportMessage @@ -206,14 +230,15 @@ boolean isHashEntryModifiable(Object key) { } @ExportMessage - boolean isHashEntryInsertable(Object key) { + @SuppressWarnings("static-method") + boolean isHashEntryInsertable(@SuppressWarnings("unused") Object key) { return true; } @ExportMessage @CompilerDirectives.TruffleBoundary void writeHashEntry(Object key, Object value) { - contextIndexes.put(((Integer) key), (Integer) value); + contextIndexes.put((Integer) key, (Integer) value); } @ExportMessage @@ -225,20 +250,21 @@ boolean isHashEntryRemovable(Object key) { @ExportMessage @CompilerDirectives.TruffleBoundary void removeHashEntry(Object key) throws UnknownKeyException { - Integer previousValue = contextIndexes.remove((Integer) key); + Integer previousValue = contextIndexes.remove(key); if (previousValue == null) { throw UnknownKeyException.create(key); } } @ExportMessage + @SuppressWarnings("static-method") Object getHashEntriesIterator() { return null; } private boolean containsKey(Object key) { assert key instanceof Integer; - return contextIndexes.containsKey((Integer) key); + return contextIndexes.containsKey(key); } @Override diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java index ff02244472..a4bce93c76 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java @@ -253,6 +253,7 @@ default void beforeDispose(RContext context) { * context finalization happens before context disposal, and generally, it should be safe to * call native functions in the context finalization. */ + @SuppressWarnings("unused") default void beforeFinalize(RContext context) { // default empty implementation } diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RForeignObjectWrapper.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RForeignObjectWrapper.java index 498cda34be..37cdfd3621 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RForeignObjectWrapper.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RForeignObjectWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2022, 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 @@ -24,7 +24,6 @@ import com.oracle.truffle.api.interop.InteropLibrary; import com.oracle.truffle.api.interop.TruffleObject; -import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.RType; import com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeMirror; diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIMaterializeNode.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIMaterializeNode.java index c8cc4faba6..cd204be4e7 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIMaterializeNode.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIMaterializeNode.java @@ -33,7 +33,6 @@ import com.oracle.truffle.r.runtime.RInternalError; import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.context.RContext; -import com.oracle.truffle.r.runtime.data.NativeDataAccess; import com.oracle.truffle.r.runtime.data.RBaseObject; import com.oracle.truffle.r.runtime.data.RComplex; import com.oracle.truffle.r.runtime.data.RDataFactory; diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIToNativeMirrorNode.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIToNativeMirrorNode.java index 152bc29d87..24651cc119 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIToNativeMirrorNode.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIToNativeMirrorNode.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2022, 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 diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIWrap.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIWrap.java index 1b67c7e2a3..5c33a6359f 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIWrap.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/FFIWrap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, 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 diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIContext.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIContext.java index aaa7a14bf3..85de416fa8 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIContext.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIContext.java @@ -146,8 +146,15 @@ public final void registerReferenceUsedInNative(Object obj, ConditionProfile nop * Calls a native function in slow-path. This method is a simpler alternative to * {@link com.oracle.truffle.r.runtime.ffi.DownCallNodeFactory.DownCallNode} machinery. Use it * once the native function is not known during FastR build, i.e., whenever you cannot use - * {@link NativeFunction}, e.g., when calling a native function that wass passed to the - * Java side as a pointer. + * {@link NativeFunction}, e.g., when calling a native function that wass passed to the Java + * side as a pointer. + *

+ * Note that unlike {@code DownCallNode} machinery, it is not safe to use the native R API + * from the native function, i.e., it is not safe to do any upcalls. This is because + * {@code DownCallNode} machinery uses trampolines that sets the longjump in case of errors from + * upcalls, whereas the native functions called by this method cannot use this trampoline + * mechanism, because these native functions are expected to be provided by the user, and + * therefore, we cannot modify their sources. * * @param nativeFunc (Truffle) object representing the native function to be called. * @param nativeFuncType Type of the native function - can be either {@code LLVM} or diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractInteropTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractInteropTest.java index 6a2e6c416e..e471cb15aa 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractInteropTest.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractInteropTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, 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 diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RForeignObjectWrapperInteropTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RForeignObjectWrapperInteropTest.java index 741dc35b91..068772bf2c 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RForeignObjectWrapperInteropTest.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RForeignObjectWrapperInteropTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022, 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 diff --git a/mx.fastr/mx_fastr_edinclude.py b/mx.fastr/mx_fastr_edinclude.py index 6d3b60d6e5..09c7b3c29f 100644 --- a/mx.fastr/mx_fastr_edinclude.py +++ b/mx.fastr/mx_fastr_edinclude.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2017, 2022, 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