fixed a serious code generation bug leading to wrong RTTI

This commit is contained in:
Araq 2012-10-19 01:59:28 +02:00
commit 3f82352c2e
7 changed files with 76 additions and 29 deletions

View file

@ -553,7 +553,7 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
sysAssert(allocInv(a), "rawAlloc: before listRemove test")
ListRemove(a.freeSmallChunks[s], c)
sysAssert(allocInv(a), "rawAlloc: end listRemove test")
sysAssert(((cast[TAddress](result) and PageMask) -% smallChunkOverhead()) %%
sysAssert(((cast[TAddress](result) and PageMask) - smallChunkOverhead()) %%
size == 0, "rawAlloc 21")
sysAssert(allocInv(a), "rawAlloc: end small size")
else:
@ -582,7 +582,7 @@ proc rawDealloc(a: var TMemRegion, p: pointer) =
# `p` is within a small chunk:
var c = cast[PSmallChunk](c)
var s = c.size
sysAssert(((cast[TAddress](p) and PageMask) -% smallChunkOverhead()) %%
sysAssert(((cast[TAddress](p) and PageMask) - smallChunkOverhead()) %%
s == 0, "rawDealloc 3")
var f = cast[ptr TFreeCell](p)
#echo("setting to nil: ", $cast[TAddress](addr(f.zeroField)))
@ -605,7 +605,7 @@ proc rawDealloc(a: var TMemRegion, p: pointer) =
ListRemove(a.freeSmallChunks[s div memAlign], c)
c.size = SmallChunkSize
freeBigChunk(a, cast[PBigChunk](c))
sysAssert(((cast[TAddress](p) and PageMask) -% smallChunkOverhead()) %%
sysAssert(((cast[TAddress](p) and PageMask) - smallChunkOverhead()) %%
s == 0, "rawDealloc 2")
else:
# set to 0xff to check for usage after free bugs:

View file

@ -60,7 +60,7 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
unsureAsgnRef(x, s2)
return
sysAssert(dest != nil, "genericAssignAux 3")
unsureAsgnRef(x, newObj(mt, seq.len * mt.base.size + GenericSeqSize))
unsureAsgnRef(x, newSeq(mt, seq.len))
var dst = cast[taddress](cast[ppointer](dest)[])
for i in 0..seq.len-1:
genericAssignAux(
@ -68,9 +68,6 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
cast[pointer](cast[taddress](s2) +% i *% mt.base.size +%
GenericSeqSize),
mt.Base, shallow)
var dstseq = cast[PGenericSeq](dst)
dstseq.len = seq.len
dstseq.reserved = seq.len
of tyObject:
# we need to copy m_type field for tyObject, as it could be empty for
# sequence reallocations:
@ -98,6 +95,35 @@ proc genericShallowAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} =
genericAssignAux(dest, src, mt, true)
GC_enable()
when false:
proc debugNimType(t: PNimType) =
if t.isNil:
cprintf("nil!")
return
var k: cstring
case t.kind
of tyBool: k = "bool"
of tyChar: k = "char"
of tyEnum: k = "enum"
of tyArray: k = "array"
of tyObject: k = "object"
of tyTuple: k = "tuple"
of tyRange: k = "range"
of tyPtr: k = "ptr"
of tyRef: k = "ref"
of tyVar: k = "var"
of tySequence: k = "seq"
of tyProc: k = "proc"
of tyPointer: k = "range"
of tyOpenArray: k = "openarray"
of tyString: k = "string"
of tyCString: k = "cstring"
of tyInt: k = "int"
of tyInt32: k = "int32"
else: k = "other"
cprintf("%s %ld\n", k, t.size)
debugNimType(t.base)
proc genericSeqAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} =
var src = src # ugly, but I like to stress the parser sometimes :-)
genericAssign(dest, addr(src), mt)

View file

@ -664,6 +664,9 @@ proc dbgRegisterWatchpoint(address: pointer, name: cstring,
Watchpoints[L].oldValue = genericHash(address, typ)
inc WatchpointsLen
proc dbgUnregisterWatchpoints*() =
WatchpointsLen = 0
proc dbgWriteStackTrace(f: PFrame) =
const
firstCalls = 32
@ -706,15 +709,22 @@ proc dbgWriteStackTrace(f: PFrame) =
write(stdout, tempFrames[j].procname)
write(stdout, "\n")
proc strstr(s1, s2: cstring): cstring {.importc, header: "<string.h>".}
proc interestingFilename(filename: cstring): bool =
#result = strstr(filename, "/rst.nim") == nil
result = true
proc checkWatchpoints =
let L = WatchpointsLen
for i in 0.. <L:
let newHash = genericHash(Watchpoints[i].address, Watchpoints[i].typ)
if newHash != Watchpoints[i].oldValue:
dbgWriteStackTrace(framePtr)
debugOut(Watchpoints[i].name)
if interestingFilename(framePtr.filename):
dbgWriteStackTrace(framePtr)
debugOut(Watchpoints[i].name)
Watchpoints[i].oldValue = newHash
proc endb(line: int) {.compilerproc.} =
# This proc is called before every Nimrod code line!
# Thus, it must have as few parameters as possible to keep the

View file

@ -192,11 +192,7 @@ proc quitOrDebug() {.inline.} =
else:
endbStep() # call the debugger
proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} =
e.name = ename
when hasSomeStackTrace:
e.trace = ""
rawWriteStackTrace(e.trace)
proc raiseExceptionAux(e: ref E_Base) =
if localRaiseHook != nil:
if not localRaiseHook(e): return
if globalRaiseHook != nil:
@ -205,7 +201,7 @@ proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} =
pushCurrentException(e)
c_longjmp(excHandler.context, 1)
elif e[] of EOutOfMemory:
writeToStdErr(ename)
writeToStdErr(e.name)
quitOrDebug()
else:
when hasSomeStackTrace:
@ -214,7 +210,7 @@ proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} =
add(buf, "Error: unhandled exception: ")
if not isNil(e.msg): add(buf, e.msg)
add(buf, " [")
add(buf, $ename)
add(buf, $e.name)
add(buf, "]\n")
writeToStdErr(buf)
else:
@ -230,16 +226,23 @@ proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} =
add(buf, "Error: unhandled exception: ")
if not isNil(e.msg): add(buf, e.msg)
add(buf, " [")
xadd(buf, ename, c_strlen(ename))
xadd(buf, e.name, c_strlen(e.name))
add(buf, "]\n")
writeToStdErr(buf)
quitOrDebug()
proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} =
e.name = ename
when hasSomeStackTrace:
e.trace = ""
rawWriteStackTrace(e.trace)
raiseExceptionAux(e)
proc reraiseException() {.compilerRtl.} =
if currException == nil:
raise newException(ENoExceptionToReraise, "no exception to reraise")
else:
raiseException(currException, currException.name)
raiseExceptionAux(currException)
proc WriteStackTrace() =
when hasSomeStackTrace: