Conversation
Implement configurable memory limits to prevent out-of-memory conditions when parsing large USD files. Memory usage is tracked during parsing and loading fails gracefully when limits are exceeded. USDC Parser: - Add memory tracking via CHECK_MEMORY_USAGE/REDUCE_MEMORY_USAGE macros - Track allocations in crate-reader.cc for tokens, arrays, and data structures - Pass memory limit from USDLoadOptions through USDCReaderConfig - Initialize _memoryUsage in CrateReader constructor USDA Parser: - Add max_memory_limit_in_mb to USDAReaderConfig (default 16GB) - Add memory tracking members to AsciiParser class (default 128GB) - Implement SetMaxMemoryLimit() method in AsciiParser - Track allocations for arrays, references, and string data - Add CHECK_MEMORY_USAGE calls in push_back operations Both parsers now: - Respect USDLoadOptions::max_memory_limit_in_mb setting - Provide clear error messages showing limit and current usage - Fail gracefully before system runs out of memory Testing: - Verified with suzanne-subd-lv5.usdc/usda at various limits (10MB, 50MB, 500MB) - Small files load successfully with 1MB limit - Memory limits properly enforced at different parsing stages This feature is especially useful for: - Mobile/embedded devices with limited memory - Server environments with resource quotas - Preventing DoS from malicious/corrupted USD files - Debugging memory usage patterns in USD files
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC ParsersI've completed a comprehensive review of the memory limit enforcement implementation. Here's my detailed analysis: Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices. Code Quality & Best Practices✅ Strengths:
|
There was a problem hiding this comment.
Pull Request Overview
This PR implements configurable memory limits for both USDA and USDC parsers to prevent out-of-memory conditions when parsing large USD files. The implementation adds memory tracking throughout the parsing process and fails gracefully when limits are exceeded.
Key changes:
- Added memory tracking macros and infrastructure to both USDA and USDC parsers
- Implemented configurable memory limits through USDLoadOptions with reasonable defaults (16GB for USDA, passed through for USDC)
- Added memory usage checks at key allocation points during parsing operations
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/usda-reader.hh | Adds max_memory_limit_in_mb configuration field with 16GB default |
| src/usda-reader.cc | Connects reader config to parser memory limit setting |
| src/tinyusdz.cc | Passes memory limits from USDLoadOptions to both USDA and USDC configs |
| src/crate-reader.cc | Initializes memory tracking in USDC reader and adds token allocation check |
| src/ascii-parser.hh | Adds memory tracking members and SetMaxMemoryLimit method with 128GB default |
| src/ascii-parser.cc | Implements CHECK_MEMORY_USAGE/REDUCE_MEMORY_USAGE macros and adds memory checks |
| src/ascii-parser-basetype.cc | Duplicates memory tracking macros and adds checks for array/reference allocations |
| StageMetas _stage_metas; | ||
|
|
||
| // Memory tracking | ||
| size_t _max_memory_limit_bytes{128ull * 1024ull * 1024ull * 1024ull}; // Default 128GB |
There was a problem hiding this comment.
The default memory limit of 128GB is inconsistent with the 16GB default in USDAReaderConfig. Consider using a consistent default value or making this configurable through the constructor to avoid confusion.
| size_t _max_memory_limit_bytes{128ull * 1024ull * 1024ull * 1024ull}; // Default 128GB | |
| size_t _max_memory_limit_bytes{16ull * 1024ull * 1024ull * 1024ull}; // Default 16GB (consistent with USDAReaderConfig) |
|
|
||
| #include "common-macros.inc" | ||
|
|
||
| #define CHECK_MEMORY_USAGE(__nbytes) do { \ |
There was a problem hiding this comment.
The CHECK_MEMORY_USAGE and REDUCE_MEMORY_USAGE macros are duplicated in both ascii-parser.cc and ascii-parser-basetype.cc. Consider moving these to a shared header file to avoid code duplication and ensure consistency.
|
|
||
| #include "external/fast_float/include/fast_float/fast_float.h" | ||
|
|
||
| #define CHECK_MEMORY_USAGE(__nbytes) do { \ |
There was a problem hiding this comment.
The CHECK_MEMORY_USAGE and REDUCE_MEMORY_USAGE macros are duplicated from ascii-parser.cc. Consider moving these to a shared header file to avoid code duplication and ensure consistency.
| #define REDUCE_MEMORY_USAGE(__nbytes) do { \ | ||
| if (_memory_usage >= (__nbytes)) { \ | ||
| _memory_usage -= (__nbytes); \ | ||
| } \ | ||
| } while(0) |
There was a problem hiding this comment.
The REDUCE_MEMORY_USAGE macro is defined but never used in the codebase. This suggests either incomplete implementation or dead code that should be removed.
| #define REDUCE_MEMORY_USAGE(__nbytes) do { \ | |
| if (_memory_usage >= (__nbytes)) { \ | |
| _memory_usage -= (__nbytes); \ | |
| } \ | |
| } while(0) |
| #define REDUCE_MEMORY_USAGE(__nbytes) do { \ | ||
| if (_memory_usage >= (__nbytes)) { \ | ||
| _memory_usage -= (__nbytes); \ | ||
| } \ | ||
| } while(0) |
There was a problem hiding this comment.
The REDUCE_MEMORY_USAGE macro is defined but never used in the codebase. This suggests either incomplete implementation or dead code that should be removed.
| #define REDUCE_MEMORY_USAGE(__nbytes) do { \ | |
| if (_memory_usage >= (__nbytes)) { \ | |
| _memory_usage -= (__nbytes); \ | |
| } \ | |
| } while(0) |
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC Parsers Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices with consistent patterns across both parsers. Key Findings🐛 Critical Bug FoundLocation: #define REDUCE_MEMORY_USAGE(__nbytes) do { \
if (_memoryUsage < (__nbytes)) { \ // ❌ BUG: Should be >= not <
_memoryUsage -= (__nbytes); \
} \
} while(0)Issue: This condition is inverted! It should check Fix: Change line 74 from
|
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC Parsers Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices with consistent patterns across both parsers. 🐛 Critical Bug FoundLocation: #define REDUCE_MEMORY_USAGE(__nbytes) do { \
if (_memoryUsage < (__nbytes)) { \ // ❌ BUG: Should be >= not <
_memoryUsage -= (__nbytes); \
} \
} while(0)Issue: This condition is inverted! It should check Fix: Change line 74 from Key Findings
|
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC Parsers Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices with consistent patterns across both parsers. 🐛 Critical Bug FoundLocation: #define REDUCE_MEMORY_USAGE(__nbytes) do { \
if (_memoryUsage < (__nbytes)) { \ // ❌ BUG: Should be >= not <
_memoryUsage -= (__nbytes); \
} \
} while(0)Issue: This condition is inverted! It should check Fix: Change line 74 from Key Findings
|
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC Parsers Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices with consistent patterns across both parsers. 🐛 Critical Bug FoundLocation: #define REDUCE_MEMORY_USAGE(__nbytes) do { \
if (_memoryUsage < (__nbytes)) { \ // ❌ BUG: Should be >= not <
_memoryUsage -= (__nbytes); \
} \
} while(0)Issue: This condition is inverted! It should check Fix: Change line 74 from Key Findings
|
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC Parsers Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices with consistent patterns across both parsers. 🐛 Critical Bug FoundLocation: #define REDUCE_MEMORY_USAGE(__nbytes) do { \
if (_memoryUsage < (__nbytes)) { \ // ❌ BUG: Should be >= not <
_memoryUsage -= (__nbytes); \
} \
} while(0)Issue: This condition is inverted! It should check Fix: Change line 74 from Key Findings
|
Use 128GB default for all USDA/USDC parser.
|
Claude finished @syoyo's task —— View job Code Review: Memory Limit Enforcement for USDA/USDC Parsers Todo List:
Overall Assessment ✅This is a well-implemented security feature that addresses a critical need for preventing out-of-memory conditions when parsing potentially malicious or corrupted USD files. The implementation follows good defensive programming practices with consistent patterns across both parsers. Key Findings
|
feat: Add memory limit enforcement for USDA/USDC parsers

Implement configurable memory limits to prevent out-of-memory conditions when
parsing large USD files. Memory usage is tracked during parsing and loading
fails gracefully when limits are exceeded.
USDC Parser:
USDA Parser:
Both parsers now:
Testing:
This feature is especially useful for: