fix: avoid attr_value_t conversion of new_command result during module init - #387
Merged
Merged
Conversation
Module initialization registers the `init-tsffs` CLI command by passing a
`new_command(...)` call to `run_python`. `SIM_run_python` converts the value
of a trailing expression to an `attr_value_t`. Newer Simics Base versions
return a `CliCommand` object from `new_command` instead of `None`, and that
object has no `attr_value_t` representation, so the conversion raises
`SimExc_Type`. The command is registered successfully; only the marshalling
of the return value fails.
`#[simics_exception]` turns that pending exception into an `Err`, which the
error path then mishandles:
let tsffs = Tsffs::create()?; // *mut ConfClass
...
.map_err(|e| { error!(tsffs, "{e}"); e })
`simics::log!` casts its object argument with `$obj as *mut ConfObject`.
`ConfClass` and `ConfObject` are distinct opaque types, so this raw pointer
cast compiles silently and `VT_log_error` reads a `conf_class_t` as a
`conf_object_t`. The result is a SIGSEGV inside `VT_effective_log_level`
while loading the module, which hides the real error entirely:
intel#3 VT_effective_log_level
intel#4 VT_log_message64
intel#5 VT_log_error
intel#6 simics::api::logging::log_error
intel#7 _simics_module_init
Bind the result of `new_command` to a variable so the snippet is a statement
rather than an expression. No value is returned, so no conversion is
attempted. Drop the `error!` call that passed a `ConfClass` where a
`ConfObject` was required; the remaining `.expect()` reports any genuine
failure without dereferencing an invalid pointer.
Verified by loading the rebuilt module: `load-module tsffs` followed by
`init-tsffs` now succeeds and creates the `tsffs` object.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
The focused fix addresses both documented crash causes and is covered by existing module-loading integration paths.
Pull request overview
Fixes a module-load crash on newer Simics versions.
Changes:
- Prevents
new_commandresults from being marshalled toattr_value_t. - Removes unsafe error logging with a
ConfClasspointer.
File summaries
| File | Description |
|---|---|
src/lib.rs |
Safely registers init-tsffs during module initialization. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Loading the
tsffsmodule crashes with a SIGSEGV on newer Simics Base versions, before any target is even set up.load-module tsffsalone is enough to reproduce it — no model required.Root cause
Two independent issues compound here.
1. The trigger.
init()registers theinit-tsffsCLI command by handing a barenew_command(...)call torun_python.SIM_run_pythonconverts the value of a trailing expression to anattr_value_t. Newer Simics Base versions return aCliCommandobject fromnew_commandrather thanNone, and that object has noattr_value_trepresentation:The command itself registers fine — I confirmed this by wrapping
new_commandand observing it return normally. Only the marshalling of the return value fails.#[simics_exception]then promotes the pending exception into anErr.2. The segfault that hides it. The error path is itself unsound:
simics::log!casts its object argument with$obj as *mut ConfObject.ConfClassandConfObjectare distinct opaque types, so this raw pointer cast compiles silently, andVT_log_errorgoes on to read aconf_class_tas aconf_object_t. That is the crash inVT_effective_log_level, and it swallows the actual error message.Fix
Bind the result of
new_commandto a variable so the snippet is a statement rather than an expression. Nothing is returned, so no conversion is attempted.Drop the
error!call that passed aConfClasswhere aConfObjectwas required. The remaining.expect()surfaces any genuine failure without dereferencing an invalid pointer.Verification
Rebuilt with
cargo simics-build -rand loaded the resulting module:Previously this sequence segfaulted at
load-module.Note for maintainers
The unchecked
$obj as *mut simics::ConfObjectcast in thelog!/error!macros insimulator-bindingsmakes passing aConfClassa silent, guaranteed segfault. Constraining that parameter toAsConfObject/*mut ConfObjectwould have turned this into a compile error rather than a crash in module init. Might be worth a separate issue there.