Skip to content

Commit 7484fe2

Browse files
committed
[GR-52676] Use Panama for upcalls
PullRequest: truffleruby/4187
2 parents cc2b0cd + 4c08267 commit 7484fe2

File tree

9 files changed

+52
-10
lines changed

9 files changed

+52
-10
lines changed

lib/cext/ABI_check.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1
1+
2

lib/truffle/truffle/cext.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ def self.init_libtrufflerubytrampoline(libtrampoline)
190190

191191
init_functions = libtrampoline[:rb_tr_trampoline_init_functions]
192192
init_functions = Primitive.interop_eval_nfi('(env,(string):pointer):void').bind(init_functions)
193-
init_functions.call(-> name { LIBTRUFFLERUBY[name] })
193+
if Truffle::Boot.get_option 'cexts-panama' and Primitive.vm_java_version >= 22 and !TruffleRuby.native?
194+
init_functions.call(-> name {
195+
closure = LIBTRUFFLERUBY[name].createNativeClosure('panama')
196+
keep_alive << closure
197+
closure
198+
})
199+
else
200+
init_functions.call(-> name { LIBTRUFFLERUBY[name] })
201+
end
194202

195203
init_constants = libtrampoline[:rb_tr_trampoline_init_global_constants]
196204
init_constants = Primitive.interop_eval_nfi('((string):pointer):void').bind(init_constants)
@@ -1784,6 +1792,14 @@ def rb_f_notimplement
17841792
raise NotImplementedError, "#{function}() function is unimplemented on this machine"
17851793
end
17861794

1795+
def rb_bug(message)
1796+
raise Exception, "rb_bug: #{message}"
1797+
end
1798+
1799+
def rb_fatal(message)
1800+
raise Exception, "rb_fatal: #{message}"
1801+
end
1802+
17871803
def test_kwargs(kwargs, raise_error)
17881804
return false if Primitive.nil?(kwargs)
17891805

spec/ruby/optional/capi/ext/kernel_spec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static VALUE kernel_spec_rb_eval_string_protect(VALUE self, VALUE str, VALUE ary
221221
VALUE kernel_spec_rb_sys_fail(VALUE self, VALUE msg) {
222222
errno = 1;
223223
if (msg == Qnil) {
224-
rb_sys_fail(0);
224+
rb_sys_fail(NULL);
225225
} else if (self != Qundef) {
226226
rb_sys_fail(StringValuePtr(msg));
227227
}

src/main/c/cext/exception.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ VALUE rb_syserr_new_str(int n, VALUE mesg) {
8585
return RUBY_CEXT_INVOKE("rb_syserr_new", INT2FIX(n), mesg);
8686
}
8787

88-
VALUE make_errno_exc_str(VALUE mesg) {
88+
static VALUE make_errno_exc_str(VALUE mesg) {
8989
int n = errno;
9090

9191
errno = 0;
@@ -156,11 +156,19 @@ void rb_eof_error(void) {
156156
}
157157

158158
void rb_tr_bug_va_list(const char *fmt, va_list args) {
159-
rb_tr_not_implemented("rb_bug");
159+
char buffer[1024];
160+
vsnprintf(buffer, 1024, fmt, args);
161+
VALUE message = rb_str_new_cstr(buffer);
162+
RUBY_CEXT_INVOKE_NO_WRAP("rb_bug", message);
163+
UNREACHABLE;
160164
}
161165

162166
void rb_tr_fatal_va_list(const char *fmt, va_list args) {
163-
rb_tr_not_implemented("rb_fatal");
167+
char buffer[1024];
168+
vsnprintf(buffer, 1024, fmt, args);
169+
VALUE message = rb_str_new_cstr(buffer);
170+
RUBY_CEXT_INVOKE_NO_WRAP("rb_fatal", message);
171+
UNREACHABLE;
164172
}
165173

166174
VALUE rb_make_exception(int argc, const VALUE *argv) {

src/main/java/org/truffleruby/cext/IsNativeObjectNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public abstract class IsNativeObjectNode extends RubyBaseNode {
2929

3030
/** Returns true if handle was natively allocated. */
31-
public abstract Object execute(Node node, Object handle);
31+
public abstract boolean execute(Node node, Object handle);
3232

3333
@Specialization
3434
static boolean isNativeObjectTaggedObject(long handle) {

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ protected boolean isExecutable() {
434434
}
435435

436436
@ExportMessage
437-
protected Object execute(Object[] arguments,
437+
protected ValueWrapper execute(Object[] arguments,
438438
@Cached WrapNode wrapNode) {
439439
return wrapNode.execute(arguments[0]);
440440
}
@@ -450,7 +450,7 @@ protected boolean isExecutable() {
450450
}
451451

452452
@ExportMessage
453-
protected Object execute(Object[] arguments,
453+
protected boolean execute(Object[] arguments,
454454
@Cached IsNativeObjectNode isNativeObjectNode,
455455
@Bind("$node") Node node) {
456456
return isNativeObjectNode.execute(node, arguments[0]);
@@ -467,7 +467,7 @@ protected boolean isExecutable() {
467467
}
468468

469469
@ExportMessage
470-
protected Object execute(Object[] arguments,
470+
protected long execute(Object[] arguments,
471471
@CachedLibrary(limit = "1") InteropLibrary values) throws UnsupportedMessageException {
472472
values.toNative(arguments[0]);
473473
return values.asPointer(arguments[0]);

src/main/java/org/truffleruby/options/Options.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public final class Options {
107107
public final boolean CEXTS;
108108
/** --cexts-lock=true */
109109
public final boolean CEXT_LOCK;
110+
/** --cexts-panama=false */
111+
public final boolean CEXTS_PANAMA;
110112
/** --options-log=false */
111113
public final boolean OPTIONS_LOG;
112114
/** --log-load=false */
@@ -250,6 +252,7 @@ public Options(Env env, OptionValues options, LanguageOptions languageOptions) {
250252
BACKTRACE_ON_NEW_FIBER = options.get(OptionsCatalog.BACKTRACE_ON_NEW_FIBER_KEY);
251253
CEXTS = options.get(OptionsCatalog.CEXTS_KEY);
252254
CEXT_LOCK = options.get(OptionsCatalog.CEXT_LOCK_KEY);
255+
CEXTS_PANAMA = options.get(OptionsCatalog.CEXTS_PANAMA_KEY);
253256
OPTIONS_LOG = options.get(OptionsCatalog.OPTIONS_LOG_KEY);
254257
LOG_LOAD = options.get(OptionsCatalog.LOG_LOAD_KEY);
255258
LOG_AUTOLOAD = options.get(OptionsCatalog.LOG_AUTOLOAD_KEY);
@@ -386,6 +389,8 @@ public Object fromDescriptor(OptionDescriptor descriptor) {
386389
return CEXTS;
387390
case "ruby.cexts-lock":
388391
return CEXT_LOCK;
392+
case "ruby.cexts-panama":
393+
return CEXTS_PANAMA;
389394
case "ruby.options-log":
390395
return OPTIONS_LOG;
391396
case "ruby.log-load":

src/options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ EXPERT:
133133
# C extension options
134134
CEXTS: [cexts, boolean, true, Enable use of C extensions]
135135
CEXT_LOCK: [cexts-lock, boolean, true, Use a Global Lock when running C extensions]
136+
CEXTS_PANAMA: [cexts-panama, boolean, false, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.']
136137

137138
# Debugging the values of options
138139
OPTIONS_LOG: [options-log, boolean, false, Log the final value of all options]

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public final class OptionsCatalog {
7171
public static final OptionKey<Boolean> BACKTRACE_ON_NEW_FIBER_KEY = new OptionKey<>(false);
7272
public static final OptionKey<Boolean> CEXTS_KEY = new OptionKey<>(true);
7373
public static final OptionKey<Boolean> CEXT_LOCK_KEY = new OptionKey<>(true);
74+
public static final OptionKey<Boolean> CEXTS_PANAMA_KEY = new OptionKey<>(false);
7475
public static final OptionKey<Boolean> OPTIONS_LOG_KEY = new OptionKey<>(false);
7576
public static final OptionKey<Boolean> LOG_LOAD_KEY = new OptionKey<>(false);
7677
public static final OptionKey<Boolean> LOG_AUTOLOAD_KEY = new OptionKey<>(false);
@@ -573,6 +574,14 @@ public final class OptionsCatalog {
573574
.usageSyntax("")
574575
.build();
575576

577+
public static final OptionDescriptor CEXTS_PANAMA = OptionDescriptor
578+
.newBuilder(CEXTS_PANAMA_KEY, "ruby.cexts-panama")
579+
.help("Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.")
580+
.category(OptionCategory.EXPERT)
581+
.stability(OptionStability.EXPERIMENTAL)
582+
.usageSyntax("")
583+
.build();
584+
576585
public static final OptionDescriptor OPTIONS_LOG = OptionDescriptor
577586
.newBuilder(OPTIONS_LOG_KEY, "ruby.options-log")
578587
.help("Log the final value of all options")
@@ -1421,6 +1430,8 @@ public static OptionDescriptor fromName(String name) {
14211430
return CEXTS;
14221431
case "ruby.cexts-lock":
14231432
return CEXT_LOCK;
1433+
case "ruby.cexts-panama":
1434+
return CEXTS_PANAMA;
14241435
case "ruby.options-log":
14251436
return OPTIONS_LOG;
14261437
case "ruby.log-load":
@@ -1665,6 +1676,7 @@ public static OptionDescriptor[] allDescriptors() {
16651676
BACKTRACE_ON_NEW_FIBER,
16661677
CEXTS,
16671678
CEXT_LOCK,
1679+
CEXTS_PANAMA,
16681680
OPTIONS_LOG,
16691681
LOG_LOAD,
16701682
LOG_AUTOLOAD,

0 commit comments

Comments
 (0)