Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[regression] var param corruption and variable mutation when run the code with VM #12551

Closed
jangko opened this issue Oct 29, 2019 · 3 comments · Fixed by #19902
Closed

[regression] var param corruption and variable mutation when run the code with VM #12551

jangko opened this issue Oct 29, 2019 · 3 comments · Fixed by #19902
Labels
Regression VM see also `const` label

Comments

@jangko
Copy link
Contributor

jangko commented Oct 29, 2019

Very hard to shorten the example code, sorry if it's a bit intimidating.
It can be a single bug or two bugs.

Example

type
  StUint = object
    lo: uint64

func `+=`(x: var Stuint, y: Stuint) =
  x.lo += y.lo

func `-`(x, y: Stuint): Stuint =
  result.lo = x.lo - y.lo

func `+`(x, y: Stuint): Stuint =
  result.lo = x.lo + y.lo

func `-=`(x: var Stuint, y: Stuint) =
  x = x - y

func `<`(x, y: Stuint): bool=
  x.lo < y.lo

func `==`(x, y: Stuint): bool =
  x.lo == y.lo

func `<=`(x, y: Stuint): bool =
  x.lo <= y.lo

proc div3n2n(r: var Stuint, b: Stuint) =
  var d: Stuint
  r = d
  r += b

func div2n1n(r: var Stuint, b: Stuint) =
  div3n2n(r, b)

func divmodBZ(x, y: Stuint, r: var Stuint)=
  div2n1n(r, y)
  r.lo = 3

func `mod`(x, y: Stuint): Stuint =
  divmodBZ(x, y, result)

func doublemod_internal(a, m: Stuint): Stuint =
  result = a
  if a >= m - a:
    result -= m
  result += a

func mulmod_internal(a, b, m: Stuint): Stuint =
  var (a, b) = (a, b)
  swap(a, b)
  debugEcho "x1 a: ", a
  a = doublemod_internal(a, m)
  debugEcho "x2 a: ", a
  a = doublemod_internal(a, m)
  debugEcho "x3 a: ", a

func powmod_internal(a, m: Stuint): Stuint =
  var a = a
  debugEcho "z1 m: ", m
  debugEcho "z2 a: ", a
  result = mulmod_internal(result, a, m)
  debugEcho "z3 a: ", a
  a = mulmod_internal(a, a, m)

func powmod*(a, m: Stuint) =
  discard powmod_internal(a mod m, m)

static:
  var x = Stuint(lo: high(uint64))
  var y = Stuint(lo: 12)

  powmod(x, y)

Current Output

z1 m: (lo: 12)
z2 a: (lo: 3)
x1 a: (lo: 3)
x2 a: (lo: 6)
x3 a: (lo: 18446744073709551604) # -> corruption, should be 0
z3 a: (lo: 18446744073709551604) # -> mutation, should stay the same as z2
x1 a: (lo: 18446744073709551604)
x2 a: (lo: 18446744073709551568)
x3 a: (lo: 18446744073709551496)

Expected Output

z1 m: (lo: 12)
z2 a: (lo: 3)
x1 a: (lo: 3)
x2 a: (lo: 6)
x3 a: (lo: 0)
z3 a: (lo: 3)
x1 a: (lo: 3)
x2 a: (lo: 6)
x3 a: (lo: 0)

Additional Information

$ nim -v
Nim Compiler Version 1.0.2 [Windows: amd64]
Compiled at 2019-10-29
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: de5f6a07c239ccf5ca3d41d70350a37112025bcf
active boot switches: -d:release
@zah zah added Regression VM see also `const` label labels Oct 29, 2019
@jangko
Copy link
Contributor Author

jangko commented Nov 4, 2019

bad commit 162d74d

@timotheecour
Copy link
Member

@jangko can you try to minimize, and does that reduce to #15974 ?

@jangko
Copy link
Contributor Author

jangko commented Nov 21, 2020

iirc my example is very weird, insertion and removal of simple echo or other stmt could hide the bug.
I've try to minimize it at that time but could not reproduce. but because it has the same root and similarity with #15974, it could be the very same bug, but I could not prove it at this time.

Araq pushed a commit that referenced this issue Jun 22, 2022
 (#19902) [backport]

* revert #12217 since the root problem seems to have been fixed; fix #15974;fix #12551; fix #19464

* fix #16020; fix #16780

* fix tests and #16613

* fix #14553

* fix #19909; skip skipRegisterAddr

* fix #18641
narimiran pushed a commit that referenced this issue Jun 23, 2022
 (#19902) [backport]

* revert #12217 since the root problem seems to have been fixed; fix #15974;fix #12551; fix #19464

* fix #16020; fix #16780

* fix tests and #16613

* fix #14553

* fix #19909; skip skipRegisterAddr

* fix #18641

(cherry picked from commit 3cb2d7a)
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression VM see also `const` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants