Skip to content

refactor(virtio): replace zone_mem raw array with struct; fix MAX_RAMS=4 overflow#100

Merged
agicy merged 2 commits into
syswonder:mainfrom
agicy:refactor/zone-mem-struct
Jun 18, 2026
Merged

refactor(virtio): replace zone_mem raw array with struct; fix MAX_RAMS=4 overflow#100
agicy merged 2 commits into
syswonder:mainfrom
agicy:refactor/zone-mem-struct

Conversation

@agicy

@agicy agicy commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Changes

  • Replace unsigned long long[4] + 4 macros with named structs (zone_mem_region, zone_mem)
  • Use CONFIG_MAX_MEMORY_REGIONS (matching hvisor kernel constant) instead of hardcoded 4
  • Add bounds check: reject num_mems > MAX_RAMS with clear error instead of silent overflow
  • Inline get_zone_ram_index() into get_virt_addr(), delete the function and its header declaration
  • Delete dead code (stray ; after continue)

References

Closes: #99

agicy added 2 commits June 15, 2026 09:08
Replace the `unsigned long long zone_mem[MAX_ZONES][MAX_RAMS][4]` raw
array and its 4 #define index macros (VIRT_ADDR, ZONE0_IPA, ZONEX_IPA,
MEM_SIZE) with properly named structs:

  struct zone_mem_region { virt_addr, zone0_ipa, zonex_ipa, mem_size }
  struct zone_mem { regions[CONFIG_MAX_MEMORY_REGIONS], num_regions }

Also:
- Use CONFIG_MAX_MEMORY_REGIONS directly (matching hvisor constant)
- Inline get_zone_ram_index() into get_virt_addr(), remove the function
- Delete the 4 index macros
- Delete dead `;` statement after `continue`
Reject JSON configs whose memory_region count exceeds
CONFIG_MAX_MEMORY_REGIONS instead of silently overflowing the array.
@agicy

agicy commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

The alias MAX_RAMS was found unnecessary: it appeared in only 3 places, all in the same file, and CONFIG_MAX_MEMORY_REGIONS is already self-documenting. I will delete the alias and use CONFIG_MAX_MEMORY_REGIONS directly.

@agicy agicy force-pushed the refactor/zone-mem-struct branch from fc971f2 to 674fc54 Compare June 15, 2026 09:12

@Inquisitor-201 Inquisitor-201 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@agicy agicy merged commit 31ef380 into syswonder:main Jun 18, 2026
1 check passed
@agicy agicy deleted the refactor/zone-mem-struct branch June 18, 2026 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zone_mem: buffer overflow when num_mems > MAX_RAMS=4; raw array with magic-index macros

2 participants