nim-stint/mem_corrupt_bug.nim

37 lines
883 B
Nim
Raw Permalink Normal View History

import ./mem_corrupt_bug_add,
./mem_corrupt_bug_mul
when isMainModule:
import typetraits
import ./mem_corrupt_bug_convert
2018-02-15 22:28:31 +00:00
let a = toMpUint(10'u32)
2018-02-15 22:28:31 +00:00
echo "a: " & $a
echo "a+a: " & $(a+a)
let z = a * a
2018-03-20 15:12:42 +00:00
echo "a * a: " & $z # How did the result value change?
echo "a * a type: " & $z.type.name
2018-03-20 15:14:31 +00:00
# Compile without release: memory corruption
# In release: no corruption
# Comment out the "naiveMul" in mul_impl: no corruption
2018-03-20 15:12:42 +00:00
echo "Is memory corrupted: " & $(z != toMpUint(100'u32))
2018-03-20 15:16:09 +00:00
# Output on my machine
#
# a: (lo: 10, hi: 0)
# +: (lo: 20, hi: 0)
# a+a: (lo: 20, hi: 0)
# naiveMul cast16:(lo: 100, hi: 0)
# naiveMul cast16:(lo: 0, hi: 0)
# naiveMul cast16:(lo: 0, hi: 0)
# +: (lo: 0, hi: 0)
# Within `*` result: (lo: 100, hi: 0)
# Within `*` result type: MpUint[32]
# a * a: (lo: 100, hi: 3924)
2018-03-20 15:16:09 +00:00
# a * a type: MpUint[32]
# Is memory corrupted: true