tarray_indexing.nim 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. discard """
  2. output: '''allocating 1048576 65536
  3. filling page from 1048576 len 65536'''
  4. cmd: '''nim c --gc:arc $file'''
  5. """
  6. # bug #12669
  7. type
  8. MemState* = enum
  9. memPrivate
  10. MemPermisison* = enum
  11. memperm_Read
  12. MemInfo* = ref object
  13. base*, size*: uint32
  14. state*: MemState
  15. perm*: set[MemPermisison]
  16. MemBlock = ref object
  17. info: MemInfo
  18. data: seq[byte]
  19. UserProcessMemory* = ref object
  20. pageAccess: array[0x40000, ptr UncheckedArray[byte]]
  21. pages: array[0x40000, MemInfo]
  22. blocks: seq[owned MemBlock]
  23. proc allocMemory*(mem: UserProcessMemory, base, size: uint32) =
  24. let
  25. roundedBase = base and not(0xFFF'u32)
  26. roundedSize = (size + 0xFFF) and not(0xFFF'u32)
  27. echo "allocating ", base, " ", size
  28. for i in (roundedBase shr 12)..<((roundedBase + roundedSize) shr 12):
  29. #echo "span ", i
  30. doAssert mem.pages[i] == nil
  31. # TODO: beserer fehler
  32. let memBlock = MemBlock(
  33. info: MemInfo(
  34. base: roundedBase,
  35. size: roundedSize,
  36. state: memPrivate,
  37. perm: {memperm_Read}
  38. ),
  39. data: newSeq[byte](roundedSize))
  40. for i in 0..<(roundedSize shr 12):
  41. mem.pages[i + (roundedBase shr 12)] = memBlock.info
  42. #echo cast[uint64](addr mem.pageAccess[i + (roundedBase shr 12)])
  43. mem.pageAccess[i + (roundedBase shr 12)] = cast[ptr UncheckedArray[byte]](addr memBlock.data[i * 0x1000])
  44. mem.blocks.add memBlock
  45. #for i in (roundedBase shr 12)..<((roundedBase + roundedSize) shr 12):
  46. # assert mem.pageAccess[i] != nil
  47. proc fillPages*(mem: UserProcessMemory, start: uint32, data: seq[byte]) =
  48. echo "filling page from ", start, " len ", data.len
  49. assert (start and not(0xFFF'u32)) == start
  50. assert (uint32(data.len) and not(0xFFF'u32)) == uint32(data.len)
  51. for i in (start shr 12)..<((start + uint32(data.len)) shr 12):
  52. #echo cast[uint64](addr mem.pageAccess[i])
  53. let page = mem.pageAccess[i]
  54. assert page != nil
  55. #copyMem(page, addr data[i * 0x1000 - start], 0x1000)
  56. const base = 0x00100000
  57. proc a(): owned UserProcessMemory =
  58. result = UserProcessMemory()
  59. result.allocMemory(base, 0x1000 * 16)
  60. result.fillPages(base, newSeq[byte](0x1000 * 16))
  61. discard a()