Skip to content

Fix handling VAR data type #3169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025

Conversation

josesimoes
Copy link
Member

@josesimoes josesimoes commented May 9, 2025

Description

  • Now it's parsing it in the code instead of calling FindGenericParamAtTypeSpec().
  • Remove declaration and implementation of FindGenericParamAtTypeSpec().

Motivation and Context

  • The existing code was only able to deal with method-generic parameters. Now it can handle also type-level generic argument out of the signature blob directly.

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • Refactor
    • Improved internal handling of generic type parameters, resulting in more robust and maintainable code for resolving generic parameter types.
  • Chores
    • Removed unused internal methods and streamlined related logic for better efficiency.

@josesimoes josesimoes added the Area: Interpreter Everything related with the interpreter, execution engine and such label May 9, 2025
@nfbot nfbot added the Type: bug label May 9, 2025
Copy link

coderabbitai bot commented May 9, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes refactor how generic type parameter resolution (for DATATYPE_VAR) is handled in the runtime, debugger, and type system. The previous approach used an assembly method to directly resolve generic parameters; this is replaced by explicit signature parsing using CLR_RT_SignatureParser. Associated helper methods and declarations for direct lookup are removed.

Changes

File(s) Change Summary
src/CLR/Core/Execution.cpp Refactored DATATYPE_VAR handling in CLR_RT_ExecutionEngine::InitializeLocals to use explicit signature parsing with CLR_RT_SignatureParser instead of calling FindGenericParamAtTypeSpec.
src/CLR/Core/TypeSystem.cpp Removed assertion in CLR_RT_SignatureParser::Advance. In CLR_RT_TypeDef_Instance::ResolveToken, replaced DATATYPE_VAR handling to parse the signature directly for the generic parameter, removing the call to FindGenericParamAtTypeSpec. Deleted the CLR_RT_Assembly::FindGenericParamAtTypeSpec method entirely.
src/CLR/Debugger/Debugger.cpp Refactored generic type parameter (DATATYPE_VAR) resolution in Debugging_Value_GetStack to use explicit signature parsing with CLR_RT_SignatureParser rather than the assembly helper method.
src/CLR/Include/nanoCLR_Runtime.h Removed the declaration of CLR_RT_Assembly::FindGenericParamAtTypeSpec from the struct interface.

Sequence Diagram(s)

sequenceDiagram
    participant ExecutionEngine
    participant SignatureParser
    participant TypeSystem
    participant Debugger

    ExecutionEngine->>SignatureParser: Initialize parser for locals signature
    ExecutionEngine->>SignatureParser: Advance to generic parameter position
    SignatureParser-->>ExecutionEngine: Return class and data type

    Debugger->>SignatureParser: Initialize parser for method/locals signature
    Debugger->>SignatureParser: Advance to argument/local index, then generic param
    SignatureParser-->>Debugger: Return class and data type

    TypeSystem->>SignatureParser: Initialize parser for TypeSpec signature
    TypeSystem->>SignatureParser: Advance to generic parameter position
    SignatureParser-->>TypeSystem: Return TypeDef_Index and data type
Loading

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@josesimoes josesimoes force-pushed the fix-processing-datatype-var branch from 95cbf83 to 06fb34d Compare May 9, 2025 16:08
@josesimoes
Copy link
Member Author

@coderabbitai review

Copy link

coderabbitai bot commented May 9, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cfc6d81 and 06fb34d.

📒 Files selected for processing (4)
  • src/CLR/Core/Execution.cpp (1 hunks)
  • src/CLR/Core/TypeSystem.cpp (1 hunks)
  • src/CLR/Debugger/Debugger.cpp (2 hunks)
  • src/CLR/Include/nanoCLR_Runtime.h (0 hunks)
💤 Files with no reviewable changes (1)
  • src/CLR/Include/nanoCLR_Runtime.h
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ORGPAL_PALX)
  • GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ORGPAL_PALTHREE)
  • GitHub Check: nf-interpreter (Check_Code_Style)
  • GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
  • GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (3)
src/CLR/Core/Execution.cpp (1)

1974-1994: Improved generic parameter resolution for type-level generics.

The change enhances handling of DATATYPE_VAR by directly parsing generic parameters from the signature blob instead of using the removed FindGenericParamAtTypeSpec() function. This properly resolves type-level generic parameters in locals signatures.

src/CLR/Debugger/Debugger.cpp (1)

2854-2864: Unchecked Advance() results may crash the debugger

Every call to parser.Advance(elem) returns an HRESULT.
In the new loop that walks to GenericParamPosition the return value is ignored:

for (int i = 0; i < res.GenericParamPosition; i++)
{
    parser.Advance(elem);   // <- ignored
}

If the signature is malformed or shorter than expected the function will return a failing HRESULT, leaving elem undefined and propagating bogus targetClass/targetDataType.
Always propagate the error:

for (int i = 0; i < res.GenericParamPosition; i++)
{
-    parser.Advance(elem);
+    NANOCLR_CHECK_HRESULT(parser.Advance(elem));
}
src/CLR/Core/TypeSystem.cpp (1)

989-1018: Implementation improved for generic parameter resolution in VAR data types

The new implementation directly parses the signature stream to resolve generic parameters, which is a more robust approach than the previous method. This enables proper support for type-level generic arguments beyond just method-generic parameters.

The code now:

  1. Creates a signature parser initialized with the TypeSpec
  2. Advances once to consume the GENERICINST or VAR entry
  3. Walks forward through the signature elements to the correct generic parameter position
  4. Extracts the corresponding TypeDef_Index directly from the signature
  5. Properly populates all instance fields with the resolved type information

This implementation properly handles error cases and includes helpful comments that explain the parsing logic.

Comment on lines +2866 to 2869
// this element.Class is the TypeDef_Index for 'T'
targetClass = elem.Class;
targetDataType = elem.DataType;
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

targetClass / targetDataType may remain uninitialised

When any of the above Advance operations fail the two out-parameters are left untouched yet later used to patch tmp.
Ensure they are initialised (e.g. to CLR_RT_TypeDef_Index::Invalid() / DATATYPE_VOID) right before the parsing starts, and only used when the parsing was successful.

Comment on lines 2828 to 2845
if (cmd->m_kind == CLR_DBG_Commands::Debugging_Value_GetStack::c_Argument)
{
parser.Initialize_MethodSignature(&md);

// skip the return value entry
iElement++;

// if there's an implicit 'this', adjust index
if (parser.Flags & PIMAGE_CEE_CS_CALLCONV_HASTHIS)
{
if (iElement == 0)
{
// requested the 'this' argument – invalid for a primitive
return false;
}
iElement--;
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

iElement is reused after it has under-flown – element selection becomes undefined

iElement was decremented to 0 in the first walk (do { Advance } while(iElement--)).
After the loop it holds UINT32_MAX; re-using it here:

iElement++;      // 0x00000000 → 0x00000001 ?? NO – value is 0xFFFFFFFF
...
if (parser.Flags & PIMAGE_CEE_CS_CALLCONV_HASTHIS)
{
    if (iElement == 0) ...
    iElement--;
}

means the subsequent element navigation is performed with an invalid large index, so the newly built parser will advance way past the end of the signature, or – worse – trap on the first Advance.

Fix by using a fresh local variable for the second traversal:

-// advance to the requested argument/local
-do { parser.Advance(elem); } while (iElement-- > 0);
+uint32_t localIndex = cmd->m_index; // fresh copy
+if (kind == c_Argument) { localIndex++; /* skip ret */ }
+...
+while (localIndex-- > 0) { NANOCLR_CHECK_HRESULT(parser.Advance(elem)); }

Comment on lines 2824 to 2833
// type‐level generic parameter in a generic TypeDef: re‐parse the signature
CLR_RT_SignatureParser parser;

// isGenericInstance = true;
// for arguments, initialize over the method signature and skip the return slot
if (cmd->m_kind == CLR_DBG_Commands::Debugging_Value_GetStack::c_Argument)
{
parser.Initialize_MethodSignature(&md);

// skip the return value entry
iElement++;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

parser is re-declared and shadows an outer variable – easy to mis-read and introduces lifetime bugs

There is already a CLR_RT_SignatureParser parser{} defined a few lines above (2787).
Declaring a second variable with the same name inside this inner scope:

CLR_RT_SignatureParser parser;   // shadows outer parser

silently hides the outer instance and makes the code harder to reason about.
If a future maintainer inserts code below this block that relies on the first parser, the compiler will accept it but the logic will be wrong.

-CLR_RT_SignatureParser parser;
+CLR_RT_SignatureParser sigParser;

Rename the inner parser (or simply re-use the outer one after Initialize_*()), so that shadowing cannot occur.

@josesimoes josesimoes force-pushed the fix-processing-datatype-var branch 6 times, most recently from 68c3e43 to 88c6138 Compare May 9, 2025 18:36
- Now it's parsing it in the code instead of calling FindGenericParamAtTypeSpec().
- Remove declaration and implementation of FindGenericParamAtTypeSpec().
@josesimoes josesimoes force-pushed the fix-processing-datatype-var branch from 88c6138 to 4391171 Compare May 9, 2025 18:47
@josesimoes josesimoes merged commit f12acf5 into nanoframework:develop May 9, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Interpreter Everything related with the interpreter, execution engine and such Type: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants