Skip to content

Rust: Make statics safe (1/3)#4380

Merged
eljobe merged 5 commits intomasterfrom
pmikolajczyk/nit-4432-statics-1
Feb 13, 2026
Merged

Rust: Make statics safe (1/3)#4380
eljobe merged 5 commits intomasterfrom
pmikolajczyk/nit-4432-statics-1

Conversation

@pmikolajczyk41
Copy link
Member

@pmikolajczyk41 pmikolajczyk41 commented Feb 13, 2026

The caller-env library exposes static implementations of MemAccess and ExecEnv traits.

For the first one, a ZST (zero-size-type) StaticMem is provided. Since it doesn't use any static data, callers actually can always create a new cost-free instance of StaticMem, without any need for a common static variable. Changes:

  • removed pub static mut STATIC_MEM: StaticMem
  • used fresh object (StaticMem) instead of a static var in wasm libs; this allows to remove unsafe {} block declaration there

For ExecEnv trait we have another ZST StaticExecEnv. However, this one used two auxiliary mutable statics (TIME and RNG) to mimic Go runtime state. It turned out that we already have available struct GoRuntimeState (with exactly such two fields). Changes:

  • created a new static, immutable GO_RUNTIME_STATE: Lazy<Mutex<GoRuntimeState>>
  • removed TIME and RNG static vars
  • used fresh object (StaticEnv) instead of a static var in wasm libs

Also, moved GoRuntimeState from jit to caller-env and made create_pcg function private.

Note on thread-safety

The old code was not thread safe. What is more, it wasn't even single-thread safe (that's why the warnings about mutable references to mutable statics created). The new code is thread safe:

  • Lock is a thread safe mechanism for single initialization
  • Mutex is thread safe

However, they both come from the spin library, providing spin lock (without control yielding etc). This is because, caller-env is used in no-std environments, where we cannot use the std tools (like LazyLock or Mutex) and we don't have access to OS-level primitives.

In fact, we probably do not have to be thread safe here, as this code is rather for single threaded execution. However:

  • a lot of other arbitrator code already uses Mutexes (including prover and user-test crates)
  • if we were about using single-thread things like RefCell, we would have to wrap them anyway in some auxiliary struct and do unsafe impl<T> Sync for Wrapper<T> {} to tell compiler that we are sure that this will be single threaded execution so that it allows us to use it in a static variable

What is more, in single-threaded environment, spin lock should not incur visible overhead.


part of NIT-4432

@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 33.09%. Comparing base (e15a401) to head (cf9be6f).
⚠️ Report is 15 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4380      +/-   ##
==========================================
+ Coverage   32.98%   33.09%   +0.11%     
==========================================
  Files         489      489              
  Lines       58019    58063      +44     
==========================================
+ Hits        19137    19217      +80     
+ Misses      35542    35481      -61     
- Partials     3340     3365      +25     

@pmikolajczyk41 pmikolajczyk41 marked this pull request as ready for review February 13, 2026 15:53
@github-actions
Copy link
Contributor

github-actions bot commented Feb 13, 2026

❌ 8 Tests Failed:

Tests completed Failed Passed Skipped
4264 8 4256 0
View the top 3 failed tests by shortest run time
TestRedisProduceComplex/one_producer,_all_consumers_are_active
Stack Traces | 1.240s run time
... [CONTENT TRUNCATED: Keeping last 20 lines]
�[36mDEBUG�[0m[02-13|19:08:25.632] consumer: setting result                 �[36mcid�[0m=90fe1fcc-0065-4fe5-87dc-d66b80f901dc �[36mmsgIdInStream�[0m=1771009704511-0  �[36mresultKeyInRedis�[0m=result-key:stream:09330684-e021-4c7d-bb51-0221b3641244.1771009704511-0
�[36mDEBUG�[0m[02-13|19:08:25.632] consumer: xack                           �[36mcid�[0m=90fe1fcc-0065-4fe5-87dc-d66b80f901dc �[36mmessageId�[0m=1771009704511-0
�[36mDEBUG�[0m[02-13|19:08:25.632] consumer: xack                           �[36mcid�[0m=6f0fa70c-f7a5-48a7-97ff-82628c6460d9 �[36mmessageId�[0m=1771009704511-1
�[36mDEBUG�[0m[02-13|19:08:25.637] consumer: xdel                           �[36mcid�[0m=d2c52780-873a-4d1d-99fc-b9ccc9a4cc62 �[36mmessageId�[0m=1771009704510-17
�[36mDEBUG�[0m[02-13|19:08:25.638] consumer: xdel                           �[36mcid�[0m=90fe1fcc-0065-4fe5-87dc-d66b80f901dc �[36mmessageId�[0m=1771009704511-0
�[36mDEBUG�[0m[02-13|19:08:25.638] consumer: xdel                           �[36mcid�[0m=6f0fa70c-f7a5-48a7-97ff-82628c6460d9 �[36mmessageId�[0m=1771009704511-1
�[36mDEBUG�[0m[02-13|19:08:25.638] consumer: xack                           �[36mcid�[0m=1bc61050-cee6-43e4-859c-9019483c45d6 �[36mmessageId�[0m=1771009704510-19
�[36mDEBUG�[0m[02-13|19:08:25.639] consumer: xdel                           �[36mcid�[0m=1bc61050-cee6-43e4-859c-9019483c45d6 �[36mmessageId�[0m=1771009704510-19
�[36mDEBUG�[0m[02-13|19:08:25.640] trimming                                 �[36mxTrimMinID�[0m=1771009704510-18 �[36mtrimmed�[0m=0 �[36mtrim-err�[0m=&lt;nil&gt;
�[36mDEBUG�[0m[02-13|19:08:25.640] consumer: xack                           �[36mcid�[0m=4b503e1e-f8d7-4362-841c-3c0ccedba70b �[36mmessageId�[0m=1771009704510-18
�[36mDEBUG�[0m[02-13|19:08:25.648] consumer: xdel                           �[36mcid�[0m=4b503e1e-f8d7-4362-841c-3c0ccedba70b �[36mmessageId�[0m=1771009704510-18
�[33mWARN �[0m[02-13|19:08:25.648] XClaimJustID returned empty response when indicating heartbeat �[33mmsgID�[0m=1771009704510-18
�[36mDEBUG�[0m[02-13|19:08:25.681] checkResponses                           �[36mresponded�[0m=76 �[36merrored�[0m=0 �[36mchecked�[0m=97
�[36mDEBUG�[0m[02-13|19:08:25.689] redis producer: check responses starting
�[31mERROR�[0m[02-13|19:08:25.710] Error from XpendingExt in getting PEL for auto claim �[31merr�[0m="context canceled" �[31mpendingLen�[0m=0
�[31mERROR�[0m[02-13|19:08:25.710] Error from XpendingExt in getting PEL for auto claim �[31merr�[0m="context canceled" �[31mpendingLen�[0m=0
�[31mERROR�[0m[02-13|19:08:25.710] Error from XpendingExt in getting PEL for auto claim �[31merr�[0m="context canceled" �[31mpendingLen�[0m=0
�[31mERROR�[0m[02-13|19:08:25.710] Error from XpendingExt in getting PEL for auto claim �[31merr�[0m="context canceled" �[31mpendingLen�[0m=0
�[36mDEBUG�[0m[02-13|19:08:25.711] checkResponses                           �[36mresponded�[0m=21 �[36merrored�[0m=0 �[36mchecked�[0m=21
--- FAIL: TestRedisProduceComplex/one_producer,_all_consumers_are_active (1.24s)
TestNitroNodeVersionAlerter
Stack Traces | 1.790s run time
... [CONTENT TRUNCATED: Keeping last 20 lines]
INFO [02-13|19:19:06.830] Deploying event bridge
INFO [02-13|19:19:06.834] Starting work on payload                 id=0x039ffccb44a52803
INFO [02-13|19:19:06.834] Submitted contract creation              hash=0xda837eb5d89c8a7331ebb941aabbf3a663c473b7c19443bdbdfe04df8639d4f4 from=0x57Ff0F473737a1c161bfF9efDF016F7991585088 nonce=6   contract=0x8A9dA5e5455B307e99515702E3E753809e26A71b value=0
INFO [02-13|19:19:06.834] Updated payload                          id=0x039ffccb44a52803 number=7  hash=1a3c6a..144a64 txs=1   withdrawals=0 gas=580,911   fees=5.80911e-07  root=90e6e8..0ad0d1 elapsed="668.99µs"
INFO [02-13|19:19:06.835] Stopping work on payload                 id=0x039ffccb44a52803 reason=delivery
INFO [02-13|19:19:06.836] Imported new potential chain segment     number=7  hash=1a3c6a..144a64 blocks=1  txs=1   mgas=0.581 elapsed="903.348µs" mgasps=643.064  triediffs=21.44KiB  triedirty=0.00B
INFO [02-13|19:19:06.836] Chain head was updated                   number=7  hash=1a3c6a..144a64 root=90e6e8..0ad0d1 elapsed="198.2µs"
WARN [02-13|19:19:06.843] XClaimJustID returned empty response when indicating heartbeat msgID=1771010346667-0
WARN [02-13|19:19:06.865] XClaimJustID returned empty response when indicating heartbeat msgID=1771010346667-0
WARN [02-13|19:19:06.882] XClaimJustID returned empty response when indicating heartbeat msgID=1771010346667-0
INFO [02-13|19:19:06.887] Deploying event bridge
INFO [02-13|19:19:06.888] Deploying event bridge
INFO [02-13|19:19:06.890] Submitted contract creation              hash=0xda837eb5d89c8a7331ebb941aabbf3a663c473b7c19443bdbdfe04df8639d4f4 from=0x57Ff0F473737a1c161bfF9efDF016F7991585088 nonce=6   contract=0x8A9dA5e5455B307e99515702E3E753809e26A71b value=0
INFO [02-13|19:19:06.891] Submitted contract creation              hash=0xda837eb5d89c8a7331ebb941aabbf3a663c473b7c19443bdbdfe04df8639d4f4 from=0x57Ff0F473737a1c161bfF9efDF016F7991585088 nonce=6   contract=0x8A9dA5e5455B307e99515702E3E753809e26A71b value=0
INFO [02-13|19:19:06.893] Starting work on payload                 id=0x03398dea4a410049
INFO [02-13|19:19:06.894] Starting work on payload                 id=0x03d8d67be956098e
INFO [02-13|19:19:06.894] Updated payload                          id=0x03398dea4a410049 number=7  hash=f5b10b..89301a txs=1   withdrawals=0 gas=580,911   fees=5.80911e-07  root=5b5d9a..c5a102 elapsed="729.566µs"
INFO [02-13|19:19:06.894] Stopping work on payload                 id=0x03398dea4a410049 reason=delivery
--- FAIL: TestNitroNodeVersionAlerter (1.79s)
INFO [02-13|19:19:06.895] Updated payload                          id=0x03d8d67be956098e number=7  hash=5f6eda..0d743d txs=1   withdrawals=0 gas=580,911   fees=5.80911e-07  root=33c1fb..962d28 elapsed="671.202µs"
TestBlocksReExecutorCommitState
Stack Traces | 2.580s run time
... [CONTENT TRUNCATED: Keeping last 20 lines]
DEBUG[02-13|19:14:12.106] Executing EVM call finished              runtime="173.114µs"
DEBUG[02-13|19:14:12.106] Served eth_call                          reqid=334  duration="221.062µs"
TRACE[02-13|19:14:12.106] Handled RPC response                     reqid=334  duration="1.302µs"
DEBUG[02-13|19:14:12.107] Executing EVM call finished              runtime="103.323µs"
DEBUG[02-13|19:14:12.107] Served eth_call                          reqid=335  duration="134.41µs"
TRACE[02-13|19:14:12.107] Handled RPC response                     reqid=335  duration=791ns
TRACE[02-13|19:14:12.107] P2P networking is spinning down
DEBUG[02-13|19:14:12.107] Executing EVM call finished              runtime="188.071µs"
DEBUG[02-13|19:14:12.107] Served eth_call                          reqid=336  duration="222.034µs"
TRACE[02-13|19:14:12.107] Handled RPC response                     reqid=336  duration="1.072µs"
DEBUG[02-13|19:14:12.107] Executing EVM call finished              runtime="182.801µs"
DEBUG[02-13|19:14:12.107] Served eth_call                          reqid=337  duration="218.949µs"
TRACE[02-13|19:14:12.107] Handled RPC response                     reqid=337  duration=631ns
DEBUG[02-13|19:14:12.107] Served eth_getBlockByNumber              reqid=338  duration="43.15µs"
TRACE[02-13|19:14:12.107] Handled RPC response                     reqid=338  duration=721ns
DEBUG[02-13|19:14:12.109] Served eth_getBlockByNumber              reqid=289  duration="67.937µs"
TRACE[02-13|19:14:12.109] Handled RPC response                     reqid=289  duration="2.155µs"
DEBUG[02-13|19:14:12.109] Served eth_getBlockByNumber              reqid=284  duration="56.875µs"
TRACE[02-13|19:14:12.109] Handled RPC response                     reqid=284  duration="1.834µs"
--- FAIL: TestBlocksReExecutorCommitState (2.58s)

📣 Thoughts on this report? Let Codecov know! | Powered by Codecov

Copy link
Contributor

@bragaigor bragaigor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bragaigor bragaigor assigned eljobe and unassigned bragaigor Feb 13, 2026
@eljobe eljobe enabled auto-merge February 13, 2026 19:19
@eljobe eljobe added this pull request to the merge queue Feb 13, 2026
Merged via the queue into master with commit d29fc5c Feb 13, 2026
27 checks passed
@eljobe eljobe deleted the pmikolajczyk/nit-4432-statics-1 branch February 13, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants