Skip to content

Commit

Permalink
[GR-19220] Fail early for bad handle unwraps (#1777).
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/1092
  • Loading branch information
aardvark179 committed Oct 29, 2019
2 parents af7b1dd + 437bc45 commit cd2ab2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bug fixes:
* Fix `Tempfile#{size,length}` when the IO is not flushed (#1765, @rafaelfranca).
* Dump and load instance variables in subclasses of `Exception` (#1766, @rafaelfranca).
* Fix `Date._iso8601` and `Date._rfc3339` when the string is an invalid date (#1773, @rafaelfranca).
* Fail earlier for bad handle unwrapping (#1777, @chrisseaton).

Compatibility:

Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/truffleruby/cext/UnwrapNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import static org.truffleruby.cext.ValueWrapperManager.TRUE_HANDLE;
import static org.truffleruby.cext.ValueWrapperManager.UNDEF_HANDLE;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
import org.truffleruby.cext.UnwrapNodeGen.NativeToWrapperNodeGen;
Expand Down Expand Up @@ -78,15 +80,27 @@ protected long unwrapTaggedLong(long handle) {

@Specialization(guards = "isTaggedObject(handle)")
protected Object unwrapTaggedObject(long handle,
@CachedContext(RubyLanguage.class) RubyContext context) {
return context.getValueWrapperManager().getFromHandleMap(handle);
@CachedContext(RubyLanguage.class) RubyContext context,
@Cached BranchProfile noHandleProfile) {
final Object object = context.getValueWrapperManager().getFromHandleMap(handle);
if (object == null) {
noHandleProfile.enter();
raiseError(handle);
}
return object;
}

@TruffleBoundary
private void raiseError(long handle) {
throw new RuntimeException("dead handle 0x" + Long.toHexString(handle));
}

@Fallback
@TruffleBoundary
protected ValueWrapper unWrapUnexpectedHandle(long handle) {
// Avoid throwing a specialization exception when given an uninitialized or corrupt
// handle.
return null;
throw new RuntimeException("corrupt handle 0x" + Long.toHexString(handle));
}

public static UnwrapNativeNode create() {
Expand Down

0 comments on commit cd2ab2f

Please sign in to comment.