Skip to content

ai review nits fixes of host functions#6963

Open
pwang200 wants to merge 2 commits intoXRPLF:ripple/wasmi-host-functionsfrom
pwang200:ripple/se/ai-review-nits-hfs
Open

ai review nits fixes of host functions#6963
pwang200 wants to merge 2 commits intoXRPLF:ripple/wasmi-host-functionsfrom
pwang200:ripple/se/ai-review-nits-hfs

Conversation

@pwang200
Copy link
Copy Markdown
Collaborator

@pwang200 pwang200 commented Apr 17, 2026

(1) Unchecked Expected dereference in isAmendmentEnabled_wrap
Location: src/libxrpl/tx/wasm/HostFuncWrapper.cpp:489

Impact: Low (+1 point)

Confidence: Medium

Category: Code quality / Defensive programming

Description: The code dereferences ret via *ret without checking whether the Expected contains a value. If hf->isAmendmentEnabled(uint256) ever returned an Unexpected (error), *ret would throw bad_expected_access. The current production implementation (WasmHostFunctionsImpl::isAmendmentEnabled) always returns a value (calling rules().enabled() which returns bool), so this cannot trigger today. However, the pattern is inconsistent with the defensive style used elsewhere in the host function wrappers.

Why fix: The current implementation can't return an error, so this is safe today. However, the pattern is genuinely inconsistent with every other host function wrapper in the file, which all check for errors before dereferencing. If someone changes isAmendmentEnabled to return an error in the future, this would crash the node with an uncaught exception. It's a real defensive coding gap worth closing.

Suggested Fix: Add ret && before the *ret == 1 check to guard against future changes that might return an error.

(2) Data race on j_ member before mutex acquisition
Location: src/libxrpl/tx/wasm/WasmiVM.cpp:759 and src/libxrpl/tx/wasm/WasmiVM.cpp:864

Impact: Low (+1 point)

Confidence: High

Category: Code quality / Theoretical UB

Branch: ripple/wasmi-host-functions

Description: Both WasmiEngine::run() and WasmiEngine::check() assign j_ = j BEFORE acquiring the mutex (acquired inside runHlp/checkHlp). Since WasmEngine is a process-wide singleton, concurrent calls create a data race on j_. However, Journal is just a single Sink* pointer — on 64-bit platforms pointer writes are hardware-atomic. Furthermore, j_ is only used for logging and never affects transaction outcomes. The reads of j_ inside the critical section (under mutex) are safe; the only concerning reads are in the catch blocks after the mutex is released, but even there the worst case is logging to a slightly wrong journal. Technically UB per the C++ standard, but practically harmless.

Suggested Fix: Move j_ = j inside runHlp/checkHlp after the mutex is acquired, or pass j as a parameter to eliminate the shared state.

Note — pattern comparison with rest of codebase: This is another area where WasmiEngine diverges from the established rippled pattern. In all normal transactors, beast::Journal is handled as:

PreflightContext, PreclaimContext: beast::Journal const j — a const member, passed as parameter, per-context instance

Transactor: beast::Journal const j_ — a const member, initialized in constructor, per-instance

Every normal transactor gets its own instance with its own journal. There is no shared mutable journal anywhere in the transaction processing pipeline. WasmiEngine is the only place in the tx processing code where j_ is a mutable member on a singleton, overwritten on every call. The fix aligns with the rest of the codebase: pass j as a parameter through the call chain rather than storing it on shared state.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.0%. Comparing base (8cc2169) to head (9bbef92).
⚠️ Report is 1 commits behind head on ripple/wasmi-host-functions.

Additional details and impacted files

Impacted file tree graph

@@                     Coverage Diff                     @@
##           ripple/wasmi-host-functions   #6963   +/-   ##
===========================================================
  Coverage                         82.0%   82.0%           
===========================================================
  Files                             1026    1026           
  Lines                            78199   78199           
  Branches                          7663    7661    -2     
===========================================================
+ Hits                             64144   64146    +2     
+ Misses                           14055   14053    -2     
Files with missing lines Coverage Δ
include/xrpl/tx/wasm/WasmiVM.h 94.1% <ø> (ø)
src/libxrpl/tx/wasm/HostFuncWrapper.cpp 98.4% <100.0%> (ø)
src/libxrpl/tx/wasm/WasmiVM.cpp 92.7% <100.0%> (ø)

... and 2 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown

This PR has conflicts, please resolve them in order for the PR to be reviewed.

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.

1 participant