Skip to content

Fix asserts #3074

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
Jan 22, 2025
Merged

Fix asserts #3074

merged 1 commit into from
Jan 22, 2025

Conversation

josesimoes
Copy link
Member

@josesimoes josesimoes commented Jan 22, 2025

Description

  • Rework some asserts.
  • Add missing assert variations for 64 bits build.
  • Add new asserts.
  • Remove unnecessary asserts.

Motivation and Context

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

  • Bug Fixes

    • Enhanced memory validation checks in the garbage collector to prevent potential memory corruption.
    • Improved error reporting and debugging information for memory block validation, including detailed messages for boundary and overlap checks.
  • Refactor

    • Updated assertions and boundary checks in memory management methods.
    • Added null reference handling in memory relocation processes.

These changes focus on improving the robustness and reliability of memory management within the system's core runtime components.

@josesimoes josesimoes added the Area: Common libs Everything related with common libraries label Jan 22, 2025
Copy link

coderabbitai bot commented Jan 22, 2025

Walkthrough

The pull request introduces debug enhancements across multiple core memory management and garbage collection files. The changes focus on improving error reporting and validation checks in the CLR (Common Language Runtime) memory management system. Specifically, the modifications add more detailed debug print statements and boundary checks in methods responsible for heap block validation, memory relocation, and cluster validation. These changes aim to provide more comprehensive logging and error detection during memory operations without altering the fundamental control flow of the existing code.

Changes

File Change Summary
src/CLR/Core/CLR_RT_HeapCluster.cpp Added conditional debug print statements in ValidateBlock method for enhanced error reporting, with 64-bit Windows-specific formatting.
src/CLR/Core/GarbageCollector_Compaction.cpp Updated memory relocation assertions to check against specific heap cluster payload boundaries; added null reference handling in Heap_Relocate method.
src/CLR/Core/GarbageCollector_Info.cpp Enhanced ValidateCluster method with boundary checks and overlap detection for memory blocks.

Sequence Diagram

sequenceDiagram
    participant HeapCluster as Heap Cluster
    participant GarbageCollector as Garbage Collector
    participant MemoryBlock as Memory Block

    HeapCluster->>MemoryBlock: Validate Block
    MemoryBlock-->>HeapCluster: Validation Results
    
    GarbageCollector->>MemoryBlock: Check Boundaries
    GarbageCollector->>MemoryBlock: Check Overlaps
    MemoryBlock-->>GarbageCollector: Validation Status
Loading

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.

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. (Beta)
  • @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.

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: 1

🧹 Nitpick comments (5)
src/CLR/Core/GarbageCollector_Info.cpp (1)

114-117: Use Consistent Format Specifiers for Pointer Printing

In the debug print statements, you're using different format specifiers based on the _WIN64 definition. To ensure cross-platform compatibility and correct formatting on both 32-bit and 64-bit systems, consider using the PRIxPTR format specifier uniformly and casting pointers to (uintptr_t).

Apply this diff:

 #ifdef _WIN64
     CLR_Debug::Printf("Block exceeds cluster boundary: 0x%016" PRIxPTR "\r\n", (uintptr_t)ptr);
 #else
-    CLR_Debug::Printf("Block exceeds cluster boundary: %08x\r\n", ptr);
+    CLR_Debug::Printf("Block exceeds cluster boundary: 0x%08" PRIxPTR "\r\n", (uintptr_t)ptr);
 #endif

 // Similarly update other print statements:
-    CLR_Debug::Printf("Overlapping blocks detected: Next block of %08x is overlapping it.\r\n", ptr);
+    CLR_Debug::Printf("Overlapping blocks detected: Next block of 0x%08" PRIxPTR " is overlapping it.\r\n", (uintptr_t)ptr);

-    CLR_Debug::Printf("Overlapping blocks detected: Previous block of  %08x is overlapping it.\r\n", ptr);
+    CLR_Debug::Printf("Overlapping blocks detected: Previous block of 0x%08" PRIxPTR " is overlapping it.\r\n", (uintptr_t)ptr);

Also applies to: 134-140, 155-163

src/CLR/Core/CLR_RT_HeapCluster.cpp (2)

339-356: Standardize Pointer Printing Across Platforms

In the ValidateBlock method, debug print statements use different formats for 32-bit and 64-bit systems. To maintain consistency and avoid potential issues, use PRIxPTR with (uintptr_t) casting for pointer values on all platforms.

Apply this diff:

 #ifdef _WIN64
     CLR_Debug::Printf(
         "Block beyond cluster limits: 0x%016" PRIxPTR " [0x%016" PRIxPTR " : 0x%016" PRIxPTR "-0x%016" PRIxPTR "]\r\n",
         (uintptr_t)ptr,
         (uintptr_t)this,
         (uintptr_t)m_payloadStart,
         (uintptr_t)m_payloadEnd);
 #else
-    CLR_Debug::Printf(
-        "Block beyond cluster limits: %08x [%08x : %08x-%08x]\r\n",
-        (size_t)ptr,
-        (size_t)this,
-        (size_t)m_payloadStart,
-        (size_t)m_payloadEnd);
+    CLR_Debug::Printf(
+        "Block beyond cluster limits: 0x%08" PRIxPTR " [0x%08" PRIxPTR " : 0x%08" PRIxPTR "-0x%08" PRIxPTR "]\r\n",
+        (uintptr_t)ptr,
+        (uintptr_t)this,
+        (uintptr_t)m_payloadStart,
+        (uintptr_t)m_payloadEnd);
 #endif

 // Similarly update other print statements for 'Bad Block Type', 'Bad Block null-size', and 'Bad Block size'.

Also applies to: 363-371, 386-393, 407-414


386-393: Handle Zero DataSize with Detailed Diagnostics

A DataSize of zero is an anomaly that could indicate memory corruption. Instead of just breaking and invoking NANOCLR_DEBUG_STOP(), provide detailed diagnostics to aid in troubleshooting.

Consider adding information about the block's state and surrounding memory to help identify the cause.

src/CLR/Core/GarbageCollector_Compaction.cpp (2)

479-480: Add Null Pointer Check Before Dereferencing

In the Heap_Relocate_Pass method:

_ASSERTE(ptr >= hc->m_payloadStart && ptr <= hc->m_payloadEnd);

Ensure that ptr is not nullptr before performing the assertion and dereferencing ptr in ptr->DataSize(). Although ptr is expected to be valid within this context, adding a null check enhances safety.


539-548: Streamline Null Reference Handling in Heap_Relocate

The check for dst == nullptr is enclosed within a debug trace condition. To maintain consistent behavior regardless of the debug trace settings, move the null check outside of any conditional compilation blocks.

Apply this diff:

     {
-#if defined(NANOCLR_TRACE_MEMORY_STATS)
         if (dst == nullptr)
         {
             // nothing to do here
-            CLR_Debug::Printf("\r\nGC: Skipping relocation as referenced object is null.\r\n");
             return;
         }
-        else
-        {
-            CLR_Debug::Printf("\r\nGC: Relocating Heap\r\n");
-        }
-#endif
+        // Proceed with relocation only if dst is not null
+        if (dst == nullptr)
+        {
+            // Nothing to relocate
+            return;
+        }
     }

This ensures that null references are handled correctly in all builds.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bd4d280 and 630d0af.

📒 Files selected for processing (3)
  • src/CLR/Core/CLR_RT_HeapCluster.cpp (1 hunks)
  • src/CLR/Core/GarbageCollector_Compaction.cpp (4 hunks)
  • src/CLR/Core/GarbageCollector_Info.cpp (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ST_NUCLEO64_F091RC)
  • 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 (2)
src/CLR/Core/GarbageCollector_Info.cpp (1)

125-142: Review Overlapping Block Checks for Completeness

The overlapping block checks added for the nextPtr and prevPtr aim to detect overlaps in memory blocks. However, the current conditions may not cover all edge cases of memory corruption or misalignment.

  • Data Types: Limiting the checks to blocks that are not of DATATYPE_VALUETYPE or DATATYPE_CLASS might exclude other data types that could also cause overlaps if corrupted.
  • Boundary Conditions: Ensure that when comparing nextPtr and prevPtr to ptr, all possibilities of overlapping due to incorrect DataSize() or pointer arithmetic errors are accounted for.

Consider expanding the checks to include all relevant data types and thoroughly verify that no overlapping occurs due to any form of memory corruption. This enhances the robustness of the validation.

Also applies to: 146-165

src/CLR/Core/GarbageCollector_Compaction.cpp (1)

261-265: Ensure Debug Assertions are Active in Debug Builds

The added _ASSERTE statements are crucial for validating memory boundaries during compaction. Ensure that the DEBUG macro is defined during debug builds so these assertions are active. If custom build configurations are used, verify that _ASSERTE is correctly mapped to an assert function.

- Rework some asserts.
- Add missing assert variations for 64 bits build.
- Add new asserts.
- Remove unnecessary asserts.
@josesimoes josesimoes force-pushed the fix-asserts-code-style branch from 630d0af to 3d349dc Compare January 22, 2025 03:40
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: 0

🧹 Nitpick comments (2)
src/CLR/Core/GarbageCollector_Info.cpp (2)

122-142: Consider enhancing debug messages with block sizes.

The overlap detection logic is solid. However, when overlaps are detected, including the sizes of the overlapping blocks in the debug message would help in debugging.

-                    CLR_Debug::Printf(
-                        "Overlapping blocks detected. Next block of 0x%016" PRIxPTR " is overlapping it.\r\n",
-                        (uintptr_t)ptr);
+                    CLR_Debug::Printf(
+                        "Overlapping blocks detected. Next block of 0x%016" PRIxPTR " (size: %d) is overlapping current block (size: %d).\r\n",
+                        (uintptr_t)ptr,
+                        nextPtr->DataSize(),
+                        ptr->DataSize());

144-165: Maintain consistency with the previous enhancement.

The previous block overlap checks mirror the next block checks perfectly. Apply the same debug message enhancement here for consistency.

-                    CLR_Debug::Printf(
-                        "Overlapping blocks detected: Previous block of 0x%016" PRIxPTR " is overlapping it.\r\n",
-                        (uintptr_t)ptr);
+                    CLR_Debug::Printf(
+                        "Overlapping blocks detected: Previous block of 0x%016" PRIxPTR " (size: %d) is overlapping current block (size: %d).\r\n",
+                        (uintptr_t)ptr,
+                        prevPtr->DataSize(),
+                        ptr->DataSize());
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 630d0af and 3d349dc.

📒 Files selected for processing (3)
  • src/CLR/Core/CLR_RT_HeapCluster.cpp (1 hunks)
  • src/CLR/Core/GarbageCollector_Compaction.cpp (4 hunks)
  • src/CLR/Core/GarbageCollector_Info.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/CLR/Core/GarbageCollector_Compaction.cpp
  • src/CLR/Core/CLR_RT_HeapCluster.cpp
🧰 Additional context used
📓 Learnings (1)
src/CLR/Core/GarbageCollector_Info.cpp (2)
Learnt from: josesimoes
PR: nanoframework/nf-interpreter#3074
File: src/CLR/Core/GarbageCollector_Info.cpp:107-167
Timestamp: 2025-01-22T03:38:57.394Z
Learning: In nanoFramework's memory management code, DataSize() validation is comprehensively handled through CLR_RT_HeapCluster::ValidateBlock() and other caller code. Additional size checks in ValidateCluster() are redundant as the validation is already performed at multiple levels.
Learnt from: josesimoes
PR: nanoframework/nf-interpreter#3074
File: src/CLR/Core/GarbageCollector_Info.cpp:107-167
Timestamp: 2025-01-22T03:38:57.394Z
Learning: In CLR_RT_GarbageCollector::ValidateCluster, DataSize() validation is already handled by ValidateBlock() and other caller code, making additional size checks redundant.
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: nf-interpreter (Check_Code_Style)
  • GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ST_NUCLEO64_F091RC)
  • 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 (Nightly build) (Check_Build_Options)
  • GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (2)
src/CLR/Core/GarbageCollector_Info.cpp (2)

104-106: LGTM! Good practice to validate blocks first.

The early validation of blocks helps detect memory corruption before proceeding with further checks.


107-120: Well-implemented boundary checks with proper architecture support.

The boundary validation is essential for memory safety, and the implementation correctly handles both 32-bit and 64-bit architectures with appropriate debug messages.

@josesimoes josesimoes merged commit a74f248 into nanoframework:main Jan 22, 2025
28 checks passed
@josesimoes josesimoes deleted the fix-asserts-code-style branch January 22, 2025 04:21
This was referenced Jan 31, 2025
josesimoes added a commit that referenced this pull request Mar 17, 2025
***NO_CI***

(cherry picked from commit a74f248)
@coderabbitai coderabbitai bot mentioned this pull request Jul 7, 2025
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Common libs Everything related with common libraries Type: bug Type: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants