Skip to content

Commit f3b794e

Browse files
committed
Be robust when attempting to guard unexpected objects.
1 parent 1a7a0b3 commit f3b794e

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

lib/cext/include/ruby/ruby.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ bool RB_TYPE_P(VALUE value, int type);
500500

501501
#define RB_GC_GUARD(v) \
502502
(*__extension__ ({ \
503-
VALUE *rb_gc_guarded_ptr = rb_tr_gc_guard(v); \
503+
volatile VALUE *rb_gc_guarded_ptr = rb_tr_gc_guard(&v); \
504504
rb_gc_guarded_ptr; \
505505
}))
506506

lib/cext/include/truffleruby/truffleruby-pre.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ MUST_INLINE VALUE rb_tr_unwrap(VALUE object) {
5050

5151
// Needed for GC guarding
5252

53-
MUST_INLINE VALUE *rb_tr_gc_guard(VALUE v) {
54-
VALUE *ptr = &v;
55-
polyglot_invoke(RUBY_CEXT, "rb_tr_gc_guard", v);
53+
MUST_INLINE VALUE *rb_tr_gc_guard(VALUE *ptr) {
54+
polyglot_invoke(RUBY_CEXT, "rb_tr_gc_guard", *ptr);
5655
return ptr;
5756
}
5857

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.oracle.truffle.api.Truffle;
1515
import com.oracle.truffle.api.dsl.Cached;
1616
import com.oracle.truffle.api.dsl.CreateCast;
17+
import com.oracle.truffle.api.dsl.Fallback;
1718
import com.oracle.truffle.api.dsl.NodeChild;
1819
import com.oracle.truffle.api.dsl.NodeChildren;
1920
import com.oracle.truffle.api.dsl.Specialization;
@@ -1316,7 +1317,7 @@ protected WrapNode createWrapNode() {
13161317
public abstract static class UnwrapValueNode extends CoreMethodArrayArgumentsNode {
13171318

13181319
@Specialization
1319-
public Object unwrap(Object value,
1320+
public Object unwrap(TruffleObject value,
13201321
@Cached("create()") BranchProfile exceptionProfile,
13211322
@Cached("createUnwrapNode()") UnwrapNode unwrapNode) {
13221323
Object object = unwrapNode.execute(value);
@@ -1359,7 +1360,7 @@ protected void setThreadLocal() {
13591360
public abstract static class AddToMarkList extends CoreMethodArrayArgumentsNode {
13601361

13611362
@Specialization
1362-
public DynamicObject addToMarkList(VirtualFrame frmae, Object markedObject,
1363+
public DynamicObject addToMarkList(VirtualFrame frmae, TruffleObject markedObject,
13631364
@Cached("create()") BranchProfile exceptionProfile,
13641365
@Cached("create()") BranchProfile noExceptionProfile,
13651366
@Cached("create()") UnwrapNode.ToWrapperNode toWrapperNode) {
@@ -1388,21 +1389,21 @@ protected UnwrapNode createUnwrapNode() {
13881389
public abstract static class GCGuardNode extends CoreMethodArrayArgumentsNode {
13891390

13901391
@Specialization
1391-
public DynamicObject addToMarkList(VirtualFrame frmae, Object guardedObject,
1392-
@Cached("create()") BranchProfile exceptionProfile,
1392+
public DynamicObject addToMarkList(VirtualFrame frmae, TruffleObject guardedObject,
1393+
@Cached("create()") BranchProfile noExceptionProfile,
13931394
@Cached("create()") UnwrapNode.ToWrapperNode toWrapperNode) {
13941395
ValueWrapper wrappedValue = toWrapperNode.execute(guardedObject);
1395-
if (wrappedValue == null) {
1396-
exceptionProfile.enter();
1397-
throw new RaiseException(getContext(), coreExceptions().runtimeError(exceptionMessage(guardedObject), this));
1396+
if (wrappedValue != null) {
1397+
noExceptionProfile.enter();
1398+
getContext().getMarkingService().keepObject(guardedObject);
13981399
}
1399-
getContext().getMarkingService().keepObject(guardedObject);
14001400
return nil();
14011401
}
14021402

1403-
@TruffleBoundary
1404-
private String exceptionMessage(Object value) {
1405-
return String.format("native handle not found (%s) to guard it", value);
1403+
@Fallback
1404+
public DynamicObject addToMarkList(VirtualFrame frmae, Object guardedObject) {
1405+
// Do nothing for unexpected objects, no matter how unexpected.
1406+
return nil();
14061407
}
14071408

14081409
protected UnwrapNode createUnwrapNode() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static NativeToWrapperNode create() {
118118
@ImportStatic({ Message.class, ValueWrapperManager.class })
119119
public static abstract class ToWrapperNode extends RubyBaseNode {
120120

121-
public abstract ValueWrapper execute(Object value);
121+
public abstract ValueWrapper execute(TruffleObject value);
122122

123123
@Specialization
124124
public ValueWrapper wrappedValueWrapper(ValueWrapper value) {
@@ -143,7 +143,7 @@ public ValueWrapper unwrapTypeCastObject(TruffleObject value,
143143
return nativeToWrapperNode.execute(handle);
144144
} else {
145145
nonPointerProfile.enter();
146-
throw new RaiseException(getContext(), coreExceptions().argumentError("Not a handle or a pointer", this));
146+
throw null;
147147
}
148148
}
149149

@@ -152,7 +152,7 @@ public static ToWrapperNode create() {
152152
}
153153
}
154154

155-
public abstract Object execute(Object value);
155+
public abstract Object execute(TruffleObject value);
156156

157157
@Specialization
158158
public Object unwrapValue(ValueWrapper value) {

0 commit comments

Comments
 (0)