mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-27 09:01:37 +00:00
orc: fix overflow checking regression (#25089)
Raising exceptions halfway through a memory allocation is undefined
behavior since exceptions themselves require multiple allocations and
the allocator functions are not reentrant.
It is of course also expensive performance-wise to introduce lots of
exception-raising code everywhere since it breaks many optimisations and
bloats the code.
Finally, performing pointer arithmetic with signed integers is incorrect
for example on on a 32-bit systems that allows up to 3gb of address
space for applications (large address extensions) and unnecessary
elsewhere - broadly, stuff inside the memory allocator is generated by
the compiler or controlled by the standard library meaning that
applications should not be forced to pay this price.
If we wanted to check for overflow, the right way would be in the
initial allocation location where both the size and count of objects is
known.
The code is updated to use the same arithmetic operator style as for
refc with unchecked operations rather than disabling overflow checking
wholesale in the allocator module - there are reasons for both, but
going with the existing flow seems like an easier place to start.
(cherry picked from commit 8b9972c8b6)
This commit is contained in:
@@ -319,12 +319,6 @@ when hasAlloc and not defined(js):
|
||||
|
||||
include bitmasks
|
||||
|
||||
template `+!`(p: pointer, s: SomeInteger): pointer =
|
||||
cast[pointer](cast[int](p) +% int(s))
|
||||
|
||||
template `-!`(p: pointer, s: SomeInteger): pointer =
|
||||
cast[pointer](cast[int](p) -% int(s))
|
||||
|
||||
proc alignedAlloc(size, align: Natural): pointer =
|
||||
if align <= MemAlign:
|
||||
when compileOption("threads"):
|
||||
@@ -334,32 +328,21 @@ when hasAlloc and not defined(js):
|
||||
else:
|
||||
# allocate (size + align - 1) necessary for alignment,
|
||||
# plus 2 bytes to store offset
|
||||
when compileOption("threads"):
|
||||
let base = allocShared(size + align - 1 + sizeof(uint16))
|
||||
else:
|
||||
let base = alloc(size + align - 1 + sizeof(uint16))
|
||||
let base =
|
||||
when compileOption("threads"):
|
||||
allocShared(cast[Natural](size +% align -% 1 +% sizeof(uint16)))
|
||||
else:
|
||||
alloc(cast[Natural](size +% align -% 1 +% sizeof(uint16)))
|
||||
# memory layout: padding + offset (2 bytes) + user_data
|
||||
# in order to deallocate: read offset at user_data - 2 bytes,
|
||||
# then deallocate user_data - offset
|
||||
let offset = align - (cast[int](base) and (align - 1))
|
||||
cast[ptr uint16](base +! (offset - sizeof(uint16)))[] = uint16(offset)
|
||||
let offset = align -% cast[int](cast[uint](base) and uint(align -% 1))
|
||||
result = base +! offset
|
||||
cast[ptr uint16](result -! sizeof(uint16))[] = uint16(offset)
|
||||
|
||||
proc alignedAlloc0(size, align: Natural): pointer =
|
||||
if align <= MemAlign:
|
||||
when compileOption("threads"):
|
||||
result = allocShared0(size)
|
||||
else:
|
||||
result = alloc0(size)
|
||||
else:
|
||||
# see comments for alignedAlloc
|
||||
when compileOption("threads"):
|
||||
let base = allocShared0(size + align - 1 + sizeof(uint16))
|
||||
else:
|
||||
let base = alloc0(size + align - 1 + sizeof(uint16))
|
||||
let offset = align - (cast[int](base) and (align - 1))
|
||||
cast[ptr uint16](base +! (offset - sizeof(uint16)))[] = uint16(offset)
|
||||
result = base +! offset
|
||||
result = alignedAlloc(size, align)
|
||||
zeroMem(result, size)
|
||||
|
||||
proc alignedDealloc(p: pointer, align: int) {.compilerproc.} =
|
||||
if align <= MemAlign:
|
||||
@@ -395,7 +378,7 @@ when hasAlloc and not defined(js):
|
||||
else:
|
||||
result = alignedAlloc(newSize, align)
|
||||
copyMem(result, p, oldSize)
|
||||
zeroMem(result +! oldSize, newSize - oldSize)
|
||||
zeroMem(result +! oldSize, newSize -% oldSize)
|
||||
alignedDealloc(p, align)
|
||||
|
||||
{.pop.}
|
||||
|
||||
Reference in New Issue
Block a user