From 405ef4e484eebd2bdc82382e1713b9796ff38c2f Mon Sep 17 00:00:00 2001 From: Mats Huesmann Date: Mon, 9 Mar 2026 03:50:46 +0100 Subject: [PATCH] Updated Cilbox to latest branch --- Basis/Packages/com.cnlohr.cilbox/Cilbox.cs | 43 +++++++++++++++---- .../com.cnlohr.cilbox/CilboxAvatar.cs | 7 +-- .../Packages/com.cnlohr.cilbox/CilboxProxy.cs | 5 ++- .../Packages/com.cnlohr.cilbox/CilboxScene.cs | 2 + 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Basis/Packages/com.cnlohr.cilbox/Cilbox.cs b/Basis/Packages/com.cnlohr.cilbox/Cilbox.cs index b2d58a81e..a02a5a3e3 100644 --- a/Basis/Packages/com.cnlohr.cilbox/Cilbox.cs +++ b/Basis/Packages/com.cnlohr.cilbox/Cilbox.cs @@ -1,6 +1,7 @@ //#define PER_INSTRUCTION_PROFILING using UnityEngine; +using UnityEngine.Serialization; using System.Collections.Generic; using System; using System.Collections.Specialized; @@ -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; } @@ -1541,10 +1553,7 @@ private StackElement InterpretInner( ArraySegment 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) { @@ -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; @@ -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(); @@ -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(); + } } @@ -3025,10 +3051,11 @@ public enum ImportFunctionID Update, Start, Awake, - OnTriggerEnter, - OnTriggerExit, OnEnable, OnDisable, + OnDestroy, + OnTriggerEnter, + OnTriggerExit, OnCollisionEnter, OnCollisionExit } diff --git a/Basis/Packages/com.cnlohr.cilbox/CilboxAvatar.cs b/Basis/Packages/com.cnlohr.cilbox/CilboxAvatar.cs index 2601bda3d..6e9d782ac 100644 --- a/Basis/Packages/com.cnlohr.cilbox/CilboxAvatar.cs +++ b/Basis/Packages/com.cnlohr.cilbox/CilboxAvatar.cs @@ -11,6 +11,8 @@ namespace Cilbox [CilboxTarget] public class CilboxAvatar : Cilbox { + public override long MaxTimeoutLengthUs => 5000; // 5ms. Avatas need to be restrictive. + static HashSet whiteListType = new HashSet(){ "Cilbox.CilboxPublicUtils", "System.Array", @@ -71,11 +73,6 @@ public class CilboxAvatar : Cilbox "UnityEngine.Vector3.z", }; - public CilboxAvatar() - { - timeoutLengthUs = 5000; // Limit avatars to 5ms. - } - static public HashSet GetWhiteListTypes() { return whiteListType; } // This is called by CilboxUsage to decide of a type is allowed. diff --git a/Basis/Packages/com.cnlohr.cilbox/CilboxProxy.cs b/Basis/Packages/com.cnlohr.cilbox/CilboxProxy.cs index 5f356c133..609a65f54 100644 --- a/Basis/Packages/com.cnlohr.cilbox/CilboxProxy.cs +++ b/Basis/Packages/com.cnlohr.cilbox/CilboxProxy.cs @@ -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 }); } diff --git a/Basis/Packages/com.cnlohr.cilbox/CilboxScene.cs b/Basis/Packages/com.cnlohr.cilbox/CilboxScene.cs index d84e861cb..a464528b4 100644 --- a/Basis/Packages/com.cnlohr.cilbox/CilboxScene.cs +++ b/Basis/Packages/com.cnlohr.cilbox/CilboxScene.cs @@ -11,6 +11,8 @@ namespace Cilbox [CilboxTarget] public class CilboxScene : Cilbox { + public override long MaxTimeoutLengthUs => 10000000; // 10 seconds. Let scenes go crazy. + static HashSet whiteListType = new HashSet(){ "Cilbox.CilboxPublicUtils", "System.Array",