Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions Basis/Packages/com.cnlohr.cilbox/Cilbox.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//#define PER_INSTRUCTION_PROFILING

using UnityEngine;
using UnityEngine.Serialization;
using System.Collections.Generic;
using System;
using System.Collections.Specialized;
Expand Down Expand Up @@ -197,9 +198,20 @@ public object Interpret( CilboxProxy ths, object [] parametersIn )
try
{
ret = InterpretInner( stackBuffer, parameters ).AsObject();
} catch( Exception e )
}
catch( Exception e )
{
parentClass.box.InterpreterExit();

if (e is CilboxUnhandledInterpretedException uhe)
{
// strip the throwee just in case, and re-throw a normal runtime exception
string exceptionTypeName = uhe.Throwee?.GetType().FullName ?? "null";
string reason = $"Exception of type {exceptionTypeName} was unhandled in interpreted code";
parentClass.box.DisableWithReason(reason); // CilboxUnhandledInterpretedException bypasses the box disable
throw new CilboxInterpreterRuntimeException(reason, uhe.ClassName, uhe.MethodName, uhe.PC);
}

Debug.Log( e.ToString() );
throw;
}
Expand Down Expand Up @@ -1541,10 +1553,7 @@ private StackElement InterpretInner( ArraySegment<StackElement> stackBufferIn, A
catch( Exception e )
{
string fullError = $"Breakwarn: {e.ToString()} Class: {parentClass.className}, Function: {methodName}, Bytecode: {pc}";
Debug.LogError( fullError );
box.disabledReason = fullError;
box.disabled = true;
//box.InterpreterExit();
box.DisableWithReason(fullError);

if (e is CilboxInterpreterRuntimeException)
{
Expand Down Expand Up @@ -1936,7 +1945,15 @@ abstract public class Cilbox : MonoBehaviour
public String disabledReason = "";
public bool disabled = false;

public long timeoutLengthUs = 500000; // 500ms Can be changed by specific Cilbox application.
[SerializeField][FormerlySerializedAs("timeoutLengthUs")] private long desiredTimeoutLengthUs = 500000; // 500ms Can be changed by specific Cilbox instance.
public long timeoutLengthUs
{
get => desiredTimeoutLengthUs;
set => desiredTimeoutLengthUs = Math.Min(value, MaxTimeoutLengthUs);
}

public virtual long MaxTimeoutLengthUs => 1000000; // 1 second. Can be overridden by specific Cilbox application.

[HideInInspector] public uint interpreterAccountingDepth = 0;
[HideInInspector] public long interpreterAccountingDropDead = 0;
[HideInInspector] public long interpreterAccountingCumulitiveTicks = 0;
Expand Down Expand Up @@ -1970,6 +1987,7 @@ public void BoxInitialize( bool bSimulate = false )
if( initialized ) return;
initialized = true;
//Debug.Log( "Cilbox Initialize Metadata:" + assemblyData.Length );
timeoutLengthUs = desiredTimeoutLengthUs; // make sure min is applied once.

Dictionary< String, Serializee > assemblyRoot = new Serializee( Convert.FromBase64String( assemblyData ), Serializee.ElementType.Map ).AsMap();
Dictionary< String, Serializee > classData = assemblyRoot["classes"].AsMap();
Expand Down Expand Up @@ -2328,6 +2346,14 @@ void Update()
{
usSpentLastFrame = Interlocked.Exchange( ref interpreterAccountingCumulitiveTicks, 0 ) / interpreterTicksInUs;
}

internal void DisableWithReason(string reason)
{
Debug.LogError( reason );
this.disabledReason = reason;
this.disabled = true;
//this.InterpreterExit();
}
}


Expand Down Expand Up @@ -3025,10 +3051,11 @@ public enum ImportFunctionID
Update,
Start,
Awake,
OnTriggerEnter,
OnTriggerExit,
OnEnable,
OnDisable,
OnDestroy,
OnTriggerEnter,
OnTriggerExit,
OnCollisionEnter,
OnCollisionExit
}
Expand Down
7 changes: 2 additions & 5 deletions Basis/Packages/com.cnlohr.cilbox/CilboxAvatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Cilbox
[CilboxTarget]
public class CilboxAvatar : Cilbox
{
public override long MaxTimeoutLengthUs => 5000; // 5ms. Avatas need to be restrictive.

static HashSet<String> whiteListType = new HashSet<String>(){
"Cilbox.CilboxPublicUtils",
"System.Array",
Expand Down Expand Up @@ -71,11 +73,6 @@ public class CilboxAvatar : Cilbox
"UnityEngine.Vector3.z",
};

public CilboxAvatar()
{
timeoutLengthUs = 5000; // Limit avatars to 5ms.
}

static public HashSet<String> GetWhiteListTypes() { return whiteListType; }

// This is called by CilboxUsage to decide of a type is allowed.
Expand Down
5 changes: 3 additions & 2 deletions Basis/Packages/com.cnlohr.cilbox/CilboxProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,11 @@ void Start() {
box.InterpretIID( cls, this, ImportFunctionID.Awake, null );
box.InterpretIID( cls, this, ImportFunctionID.Start, null );
}
void FixedUpdate() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.FixedUpdate, null ); }
void Update() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.Update, null ); }
void OnEnable() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.OnEnable, null ); }
void OnDisable() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.OnDisable, null ); }
void Update() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.Update, null ); }
void FixedUpdate() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.FixedUpdate, null ); }
void OnDestroy() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.OnDestroy, null ); }
void OnTriggerEnter(Collider c) { if (proxyWasSetup) box.InterpretIID(cls, this, ImportFunctionID.OnTriggerEnter, new object[] { c }); }
void OnTriggerExit(Collider c) { if (proxyWasSetup) box.InterpretIID(cls, this, ImportFunctionID.OnTriggerExit, new object[] { c }); }
void OnCollisionEnter(Collision c) { if (proxyWasSetup) box.InterpretIID(cls, this, ImportFunctionID.OnCollisionEnter, new object[] { c }); }
Expand Down
2 changes: 2 additions & 0 deletions Basis/Packages/com.cnlohr.cilbox/CilboxScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Cilbox
[CilboxTarget]
public class CilboxScene : Cilbox
{
public override long MaxTimeoutLengthUs => 10000000; // 10 seconds. Let scenes go crazy.

static HashSet<String> whiteListType = new HashSet<String>(){
"Cilbox.CilboxPublicUtils",
"System.Array",
Expand Down
Loading