From ee0c9b868e5f8a648a6177ee835074288582f1e8 Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Wed, 11 Oct 2017 11:27:16 +0200
Subject: [PATCH] Initialize retrieve callbacks address on first use.

---
 .../truffle/r/ffi/impl/nfi/TruffleNFI_Context.java | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java
index 333ad65932..e5bb3244f4 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nfi/TruffleNFI_Context.java
@@ -31,6 +31,8 @@ import java.util.ArrayList;
 import java.util.EnumMap;
 
 import com.oracle.truffle.api.CompilerAsserts;
+import com.oracle.truffle.api.CompilerDirectives;
+import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.InteropException;
 import com.oracle.truffle.api.interop.Message;
@@ -169,7 +171,7 @@ final class TruffleNFI_Context extends RFFIContext {
         }
     }
 
-    private void initCallbacks(RContext context) {
+    private void initCallbacksAddress() {
         // get the address of the native thread local
         try {
             Node bind = Message.createInvoke(1).createNode();
@@ -179,7 +181,9 @@ final class TruffleNFI_Context extends RFFIContext {
         } catch (InteropException ex) {
             throw RInternalError.shouldNotReachHere(ex);
         }
+    }
 
+    private void initCallbacks(RContext context) {
         if (context.getKind() == ContextKind.SHARE_NOTHING) {
             // create and fill a new callbacks table
             callbacks = UnsafeAdapter.UNSAFE.allocateMemory(Callbacks.values().length * Unsafe.ARRAY_LONG_INDEX_SCALE);
@@ -203,10 +207,16 @@ final class TruffleNFI_Context extends RFFIContext {
     }
 
     private long callbacks;
-    private long callbacksAddress;
+    @CompilationFinal private long callbacksAddress;
 
     private long pushCallbacks() {
+        if (callbacksAddress == 0) {
+            CompilerDirectives.transferToInterpreterAndInvalidate();
+            initCallbacksAddress();
+        }
         long oldCallbacks = UnsafeAdapter.UNSAFE.getLong(callbacksAddress);
+        assert callbacks != 0L;
+        assert callbacksAddress != 0L;
         UnsafeAdapter.UNSAFE.putLong(callbacksAddress, callbacks);
         return oldCallbacks;
     }
-- 
GitLab