Skip to content

Commit 405ef4e

Browse files
committed
Updated Cilbox to latest branch
1 parent aab3e3c commit 405ef4e

4 files changed

Lines changed: 42 additions & 15 deletions

File tree

Basis/Packages/com.cnlohr.cilbox/Cilbox.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//#define PER_INSTRUCTION_PROFILING
22

33
using UnityEngine;
4+
using UnityEngine.Serialization;
45
using System.Collections.Generic;
56
using System;
67
using System.Collections.Specialized;
@@ -197,9 +198,20 @@ public object Interpret( CilboxProxy ths, object [] parametersIn )
197198
try
198199
{
199200
ret = InterpretInner( stackBuffer, parameters ).AsObject();
200-
} catch( Exception e )
201+
}
202+
catch( Exception e )
201203
{
202204
parentClass.box.InterpreterExit();
205+
206+
if (e is CilboxUnhandledInterpretedException uhe)
207+
{
208+
// strip the throwee just in case, and re-throw a normal runtime exception
209+
string exceptionTypeName = uhe.Throwee?.GetType().FullName ?? "null";
210+
string reason = $"Exception of type {exceptionTypeName} was unhandled in interpreted code";
211+
parentClass.box.DisableWithReason(reason); // CilboxUnhandledInterpretedException bypasses the box disable
212+
throw new CilboxInterpreterRuntimeException(reason, uhe.ClassName, uhe.MethodName, uhe.PC);
213+
}
214+
203215
Debug.Log( e.ToString() );
204216
throw;
205217
}
@@ -1541,10 +1553,7 @@ private StackElement InterpretInner( ArraySegment<StackElement> stackBufferIn, A
15411553
catch( Exception e )
15421554
{
15431555
string fullError = $"Breakwarn: {e.ToString()} Class: {parentClass.className}, Function: {methodName}, Bytecode: {pc}";
1544-
Debug.LogError( fullError );
1545-
box.disabledReason = fullError;
1546-
box.disabled = true;
1547-
//box.InterpreterExit();
1556+
box.DisableWithReason(fullError);
15481557

15491558
if (e is CilboxInterpreterRuntimeException)
15501559
{
@@ -1936,7 +1945,15 @@ abstract public class Cilbox : MonoBehaviour
19361945
public String disabledReason = "";
19371946
public bool disabled = false;
19381947

1939-
public long timeoutLengthUs = 500000; // 500ms Can be changed by specific Cilbox application.
1948+
[SerializeField][FormerlySerializedAs("timeoutLengthUs")] private long desiredTimeoutLengthUs = 500000; // 500ms Can be changed by specific Cilbox instance.
1949+
public long timeoutLengthUs
1950+
{
1951+
get => desiredTimeoutLengthUs;
1952+
set => desiredTimeoutLengthUs = Math.Min(value, MaxTimeoutLengthUs);
1953+
}
1954+
1955+
public virtual long MaxTimeoutLengthUs => 1000000; // 1 second. Can be overridden by specific Cilbox application.
1956+
19401957
[HideInInspector] public uint interpreterAccountingDepth = 0;
19411958
[HideInInspector] public long interpreterAccountingDropDead = 0;
19421959
[HideInInspector] public long interpreterAccountingCumulitiveTicks = 0;
@@ -1970,6 +1987,7 @@ public void BoxInitialize( bool bSimulate = false )
19701987
if( initialized ) return;
19711988
initialized = true;
19721989
//Debug.Log( "Cilbox Initialize Metadata:" + assemblyData.Length );
1990+
timeoutLengthUs = desiredTimeoutLengthUs; // make sure min is applied once.
19731991

19741992
Dictionary< String, Serializee > assemblyRoot = new Serializee( Convert.FromBase64String( assemblyData ), Serializee.ElementType.Map ).AsMap();
19751993
Dictionary< String, Serializee > classData = assemblyRoot["classes"].AsMap();
@@ -2328,6 +2346,14 @@ void Update()
23282346
{
23292347
usSpentLastFrame = Interlocked.Exchange( ref interpreterAccountingCumulitiveTicks, 0 ) / interpreterTicksInUs;
23302348
}
2349+
2350+
internal void DisableWithReason(string reason)
2351+
{
2352+
Debug.LogError( reason );
2353+
this.disabledReason = reason;
2354+
this.disabled = true;
2355+
//this.InterpreterExit();
2356+
}
23312357
}
23322358

23332359

@@ -3025,10 +3051,11 @@ public enum ImportFunctionID
30253051
Update,
30263052
Start,
30273053
Awake,
3028-
OnTriggerEnter,
3029-
OnTriggerExit,
30303054
OnEnable,
30313055
OnDisable,
3056+
OnDestroy,
3057+
OnTriggerEnter,
3058+
OnTriggerExit,
30323059
OnCollisionEnter,
30333060
OnCollisionExit
30343061
}

Basis/Packages/com.cnlohr.cilbox/CilboxAvatar.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Cilbox
1111
[CilboxTarget]
1212
public class CilboxAvatar : Cilbox
1313
{
14+
public override long MaxTimeoutLengthUs => 5000; // 5ms. Avatas need to be restrictive.
15+
1416
static HashSet<String> whiteListType = new HashSet<String>(){
1517
"Cilbox.CilboxPublicUtils",
1618
"System.Array",
@@ -71,11 +73,6 @@ public class CilboxAvatar : Cilbox
7173
"UnityEngine.Vector3.z",
7274
};
7375

74-
public CilboxAvatar()
75-
{
76-
timeoutLengthUs = 5000; // Limit avatars to 5ms.
77-
}
78-
7976
static public HashSet<String> GetWhiteListTypes() { return whiteListType; }
8077

8178
// This is called by CilboxUsage to decide of a type is allowed.

Basis/Packages/com.cnlohr.cilbox/CilboxProxy.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,11 @@ void Start() {
472472
box.InterpretIID( cls, this, ImportFunctionID.Awake, null );
473473
box.InterpretIID( cls, this, ImportFunctionID.Start, null );
474474
}
475+
void FixedUpdate() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.FixedUpdate, null ); }
476+
void Update() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.Update, null ); }
475477
void OnEnable() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.OnEnable, null ); }
476478
void OnDisable() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.OnDisable, null ); }
477-
void Update() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.Update, null ); }
478-
void FixedUpdate() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.FixedUpdate, null ); }
479+
void OnDestroy() { if( proxyWasSetup ) box.InterpretIID( cls, this, ImportFunctionID.OnDestroy, null ); }
479480
void OnTriggerEnter(Collider c) { if (proxyWasSetup) box.InterpretIID(cls, this, ImportFunctionID.OnTriggerEnter, new object[] { c }); }
480481
void OnTriggerExit(Collider c) { if (proxyWasSetup) box.InterpretIID(cls, this, ImportFunctionID.OnTriggerExit, new object[] { c }); }
481482
void OnCollisionEnter(Collision c) { if (proxyWasSetup) box.InterpretIID(cls, this, ImportFunctionID.OnCollisionEnter, new object[] { c }); }

Basis/Packages/com.cnlohr.cilbox/CilboxScene.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Cilbox
1111
[CilboxTarget]
1212
public class CilboxScene : Cilbox
1313
{
14+
public override long MaxTimeoutLengthUs => 10000000; // 10 seconds. Let scenes go crazy.
15+
1416
static HashSet<String> whiteListType = new HashSet<String>(){
1517
"Cilbox.CilboxPublicUtils",
1618
"System.Array",

0 commit comments

Comments
 (0)