From 52a7ed5d19ab1260ed4781bf0e8f205e18ff2e73 Mon Sep 17 00:00:00 2001 From: vega Date: Fri, 16 Dec 2016 12:12:36 +0700 Subject: [PATCH 01/11] Fix overflow when casting int64 to int in cpuload.nim --- lib/pure/concurrency/cpuload.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pure/concurrency/cpuload.nim b/lib/pure/concurrency/cpuload.nim index b0fd002ed9..db5f474071 100644 --- a/lib/pure/concurrency/cpuload.nim +++ b/lib/pure/concurrency/cpuload.nim @@ -45,12 +45,12 @@ proc advice*(s: var ThreadPoolState): ThreadPoolAdvice = procKernelDiff = procKernel - s.prevProcKernel procUserDiff = procUser - s.prevProcUser - sysTotal = int(sysKernelDiff + sysUserDiff) - procTotal = int(procKernelDiff + procUserDiff) + sysTotal = sysKernelDiff + sysUserDiff + procTotal = procKernelDiff + procUserDiff # total CPU usage < 85% --> create a new worker thread. # Measurements show that 100% and often even 90% is not reached even # if all my cores are busy. - if sysTotal == 0 or procTotal / sysTotal < 0.85: + if sysTotal == 0 or procTotal.float / sysTotal.float < 0.85: result = doCreateThread s.prevSysKernel = sysKernel s.prevSysUser = sysUser From bda8a6c1b7dca04f3292afc853889ce678f9f1dc Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 00:03:47 +0100 Subject: [PATCH 02/11] fixes nimsuggest issue #41 --- compiler/sigmatch.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 15171874f1..0f31edf1a8 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -172,7 +172,9 @@ proc sumGeneric(t: PType): int = inc result of tyGenericInvocation, tyTuple, tyProc: result += ord(t.kind == tyGenericInvocation) - for i in 0 .. Date: Sun, 18 Dec 2016 00:06:46 +0100 Subject: [PATCH 03/11] fixes nimsuggest issue #40 --- compiler/semfold.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 42fa607813..36918b2e12 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -768,7 +768,7 @@ proc getConstExpr(m: PSym, n: PNode): PNode = of nkCast: var a = getConstExpr(m, n.sons[1]) if a == nil: return - if n.typ.kind in NilableTypes: + if n.typ != nil and n.typ.kind in NilableTypes: # we allow compile-time 'cast' for pointer types: result = a result.typ = n.typ From 31bc063f2d66cf0463d700b041f250ab920c8217 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 00:08:11 +0100 Subject: [PATCH 04/11] significantly better stack traces when templates are involved --- compiler/cgen.nim | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 3217b86e4d..0cb1de142c 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -191,21 +191,25 @@ proc freshLineInfo(p: BProc; info: TLineInfo): bool = result = true proc genLineDir(p: BProc, t: PNode) = - var line = t.info.safeLineNm + var tt = t + while tt.kind in {nkStmtListExpr}+nkCallKinds: + tt = tt.lastSon + let line = tt.info.safeLineNm + if optEmbedOrigSrc in gGlobalOptions: - add(p.s(cpsStmts), ~"//" & t.info.sourceLine & rnl) - genCLineDir(p.s(cpsStmts), t.info.toFullPath, line) + add(p.s(cpsStmts), ~"//" & tt.info.sourceLine & rnl) + genCLineDir(p.s(cpsStmts), tt.info.toFullPath, line) if ({optStackTrace, optEndb} * p.options == {optStackTrace, optEndb}) and (p.prc == nil or sfPure notin p.prc.flags): - if freshLineInfo(p, t.info): + if freshLineInfo(p, tt.info): linefmt(p, cpsStmts, "#endb($1, $2);$n", - line.rope, makeCString(toFilename(t.info))) + line.rope, makeCString(toFilename(tt.info))) elif ({optLineTrace, optStackTrace} * p.options == {optLineTrace, optStackTrace}) and - (p.prc == nil or sfPure notin p.prc.flags) and t.info.fileIndex >= 0: - if freshLineInfo(p, t.info): + (p.prc == nil or sfPure notin p.prc.flags) and tt.info.fileIndex >= 0: + if freshLineInfo(p, tt.info): linefmt(p, cpsStmts, "nimln($1, $2);$n", - line.rope, t.info.quotedFilename) + line.rope, tt.info.quotedFilename) proc postStmtActions(p: BProc) {.inline.} = add(p.s(cpsStmts), p.module.injectStmt) From bf1f1eab1f3784131c66a772684cabf245d89741 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 01:58:16 +0100 Subject: [PATCH 05/11] revert line info generation change; produces worse results for other cases --- compiler/cgen.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 0cb1de142c..81d13140d2 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -192,8 +192,8 @@ proc freshLineInfo(p: BProc; info: TLineInfo): bool = proc genLineDir(p: BProc, t: PNode) = var tt = t - while tt.kind in {nkStmtListExpr}+nkCallKinds: - tt = tt.lastSon + #while tt.kind in {nkStmtListExpr}+nkCallKinds: + # tt = tt.lastSon let line = tt.info.safeLineNm if optEmbedOrigSrc in gGlobalOptions: From 7bbf74e2a3b0a45f4fe49e3abd0e3ccf0c3b5c8a Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 18 Dec 2016 14:07:05 +0100 Subject: [PATCH 06/11] fixes nimsuggest bug #43 --- compiler/suggest.nim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/suggest.nim b/compiler/suggest.nim index 39689099a5..9646c7399f 100644 --- a/compiler/suggest.nim +++ b/compiler/suggest.nim @@ -57,10 +57,11 @@ proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo; result.qualifiedPath = @[] if not isLocal and s.kind != skModule: let ow = s.owner - if ow.kind != skModule and ow.owner != nil: + if ow != nil and ow.kind != skModule and ow.owner != nil: let ow2 = ow.owner result.qualifiedPath.add(ow2.origModuleName) - result.qualifiedPath.add(ow.origModuleName) + if ow != nil: + result.qualifiedPath.add(ow.origModuleName) result.qualifiedPath.add(s.name.s) if s.typ != nil: From 6d10b365feaf6c5634a9698caa4e885f8a47daae Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 20:21:03 +0100 Subject: [PATCH 07/11] debug output: show line info earlier --- compiler/astalgo.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/astalgo.nim b/compiler/astalgo.nim index 7c07b29952..affbdffd96 100644 --- a/compiler/astalgo.nim +++ b/compiler/astalgo.nim @@ -386,6 +386,7 @@ proc debugTree(n: PNode, indent: int, maxRecDepth: int; var istr = rspaces(indent + 2) result = "{$N$1\"kind\": $2" % [istr, makeYamlString($n.kind)] + addf(result, ",$N$1\"info\": $2", [istr, lineInfoToStr(n.info)]) if maxRecDepth != 0: case n.kind of nkCharLit..nkUInt64Lit: @@ -418,7 +419,6 @@ proc debugTree(n: PNode, indent: int, maxRecDepth: int; addf(result, "$N$1$2", [rspaces(indent + 4), debugTree(n.sons[i], indent + 4, maxRecDepth - 1, renderType)]) addf(result, "$N$1]", [istr]) - addf(result, ",$N$1\"info\": $2", [istr, lineInfoToStr(n.info)]) addf(result, "$N$1}", [rspaces(indent)]) proc debug(n: PSym) = From 91935fd915ce643472c81e11a04d1531eacad6e9 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 20:21:50 +0100 Subject: [PATCH 08/11] fixes #4308, fixes #4905 --- compiler/cgen.nim | 2 ++ compiler/evaltempl.nim | 21 ++++++++++++++++++++- compiler/sem.nim | 1 + tests/errmsgs/tdont_show_system.nim | 13 +++++++++++++ tests/errmsgs/tproper_stacktrace.nim | 11 +++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/errmsgs/tdont_show_system.nim create mode 100644 tests/errmsgs/tproper_stacktrace.nim diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 81d13140d2..085e089900 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -194,6 +194,8 @@ proc genLineDir(p: BProc, t: PNode) = var tt = t #while tt.kind in {nkStmtListExpr}+nkCallKinds: # tt = tt.lastSon + if tt.kind in nkCallKinds and tt.len > 1: + tt = tt.sons[1] let line = tt.info.safeLineNm if optEmbedOrigSrc in gGlobalOptions: diff --git a/compiler/evaltempl.nim b/compiler/evaltempl.nim index 96ede44fdc..59f04ca84c 100644 --- a/compiler/evaltempl.nim +++ b/compiler/evaltempl.nim @@ -104,6 +104,25 @@ proc evalTemplateArgs(n: PNode, s: PSym; fromHlo: bool): PNode = var evalTemplateCounter* = 0 # to prevent endless recursion in templates instantiation +proc wrapInComesFrom*(info: TLineInfo; res: PNode): PNode = + when true: + result = res + result.info = info + if result.kind in {nkStmtList, nkStmtListExpr}: + result.lastSon.info = info + when false: + # this hack is required to + var x = result + while x.kind == nkStmtListExpr: x = x.lastSon + if x.kind in nkCallKinds: + for i in 1.. 100: @@ -132,5 +151,5 @@ proc evalTemplate*(n: PNode, tmpl, genSymOwner: PSym; fromHlo=false): PNode = #if ctx.instLines: result.info = n.info for i in countup(0, safeLen(body) - 1): evalTemplateAux(body.sons[i], args, ctx, result) - + result = wrapInComesFrom(n.info, result) dec(evalTemplateCounter) diff --git a/compiler/sem.nim b/compiler/sem.nim index 02c779ef02..069b7ea76c 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -369,6 +369,7 @@ proc semMacroExpr(c: PContext, n, nOrig: PNode, sym: PSym, result = evalMacroCall(c.module, c.cache, n, nOrig, sym) if efNoSemCheck notin flags: result = semAfterMacroCall(c, result, sym, flags) + result = wrapInComesFrom(nOrig.info, result) popInfoContext() proc forceBool(c: PContext, n: PNode): PNode = diff --git a/tests/errmsgs/tdont_show_system.nim b/tests/errmsgs/tdont_show_system.nim new file mode 100644 index 0000000000..6963a8a3fe --- /dev/null +++ b/tests/errmsgs/tdont_show_system.nim @@ -0,0 +1,13 @@ +discard """ + errormsg: "value of type 'bool' has to be discarded" + line: 13 + file: "tdont_show_system.nim" +""" + +# bug #4308 + +#proc getGameTile: int = +# 1 > 0 + +# bug #4905 subsumes the problem of #4308: +true notin {false} diff --git a/tests/errmsgs/tproper_stacktrace.nim b/tests/errmsgs/tproper_stacktrace.nim new file mode 100644 index 0000000000..57e65fa6fb --- /dev/null +++ b/tests/errmsgs/tproper_stacktrace.nim @@ -0,0 +1,11 @@ +discard """ + outputsub: '''tproper_stacktrace.nim(7) tproper_stacktrace''' + exitcode: 1 +""" + +template fuzzy(x) = + echo x[] != 9 + +var p: ptr int +fuzzy p + From 39ca8b8c8e55033d79b848be519fc4a8d34b337e Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 23:11:53 +0100 Subject: [PATCH 09/11] fixes #4818 --- lib/system/alloc.nim | 24 ++++++++++++++++++++++-- tests/system/tdeepcopy.nim | 1 - tests/system/trealloc.nim | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/system/trealloc.nim diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index 3a8e8a1b69..5b0955132b 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -100,7 +100,8 @@ type freeChunksList: PBigChunk # XXX make this a datastructure with O(1) access chunkStarts: IntSet root, deleted, last, freeAvlNodes: PAvlNode - locked: bool # if locked, we cannot free pages. + locked, blockChunkSizeIncrease: bool # if locked, we cannot free pages. + nextChunkSize: int {.deprecated: [TLLChunk: LLChunk, TAvlNode: AvlNode, TMemRegion: MemRegion].} # shared: @@ -275,9 +276,25 @@ proc pageAddr(p: pointer): PChunk {.inline.} = #sysAssert(Contains(allocator.chunkStarts, pageIndex(result))) proc requestOsChunks(a: var MemRegion, size: int): PBigChunk = + if not a.blockChunkSizeIncrease: + a.nextChunkSize = + if a.currMem < 64 * 1024: PageSize*4 + else: a.nextChunkSize*2 + var size = size + + if size > a.nextChunkSize: + result = cast[PBigChunk](osAllocPages(size)) + else: + result = cast[PBigChunk](osTryAllocPages(a.nextChunkSize)) + if result == nil: + result = cast[PBigChunk](osAllocPages(size)) + a.blockChunkSizeIncrease = true + else: + size = a.nextChunkSize + incCurrMem(a, size) inc(a.freeMem, size) - result = cast[PBigChunk](osAllocPages(size)) + sysAssert((cast[ByteAddress](result) and PageMask) == 0, "requestOsChunks 1") #zeroMem(result, size) result.next = nil @@ -432,6 +449,9 @@ proc getBigChunk(a: var MemRegion, size: int): PBigChunk = splitChunk(a, result, size) else: result = requestOsChunks(a, size) + # if we over allocated split the chunk: + if result.size > size: + splitChunk(a, result, size) result.prevSize = 0 # XXX why is this needed? result.used = true incl(a, a.chunkStarts, pageIndex(result)) diff --git a/tests/system/tdeepcopy.nim b/tests/system/tdeepcopy.nim index 5a582425ab..f7a6e87fa4 100644 --- a/tests/system/tdeepcopy.nim +++ b/tests/system/tdeepcopy.nim @@ -1,6 +1,5 @@ discard """ output: "ok" - disabled: "true" """ import tables, lists diff --git a/tests/system/trealloc.nim b/tests/system/trealloc.nim new file mode 100644 index 0000000000..dc5f712d60 --- /dev/null +++ b/tests/system/trealloc.nim @@ -0,0 +1,21 @@ +discard """ + output: '''success''' +""" + +# bug #4818 + +# Test that this completes without OOM. + +const BUFFER_SIZE = 5000 +var buffer = cast[ptr uint16](alloc(BUFFER_SIZE)) + +var total_size: int64 = 0 +for i in 0 .. 4000: + let size = BUFFER_SIZE * i + #echo "requesting ", size + total_size += size.int64 + buffer = cast[ptr uint16](realloc(buffer, size)) + #echo totalSize, " total: ", getTotalMem(), " occupied: ", getOccupiedMem(), " free: ", getFreeMem() + +dealloc(buffer) +echo "success" From 3e5e18bc5d3391cc9bb7fe5e0a4856dad09cbd71 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 23:20:22 +0100 Subject: [PATCH 10/11] make tests green again --- compiler/evaltempl.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/evaltempl.nim b/compiler/evaltempl.nim index 59f04ca84c..87745f7edf 100644 --- a/compiler/evaltempl.nim +++ b/compiler/evaltempl.nim @@ -108,7 +108,7 @@ proc wrapInComesFrom*(info: TLineInfo; res: PNode): PNode = when true: result = res result.info = info - if result.kind in {nkStmtList, nkStmtListExpr}: + if result.kind in {nkStmtList, nkStmtListExpr} and result.len > 0: result.lastSon.info = info when false: # this hack is required to From c130a2af17e0d5e948d21e6cb5bd0aca02ddb977 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 18 Dec 2016 23:32:15 +0100 Subject: [PATCH 11/11] make tests green --- tests/misc/tnot.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/misc/tnot.nim b/tests/misc/tnot.nim index 8c75c6bc06..5c268981ef 100644 --- a/tests/misc/tnot.nim +++ b/tests/misc/tnot.nim @@ -1,6 +1,6 @@ discard """ - tfile: "tnot.nim" - tline: 14 + file: "tnot.nim" + line: 14 errormsg: "type mismatch" """ # BUG: following compiles, but should not: