Nim/tests/vm
c-blake a0b33f9408
Add hashWangYi1 (#13823)
* Unwind just the "pseudorandom probing" (whole hash-code-keyed variable
stride double hashing) part of recent sets & tables changes (which has
still been causing bugs over a month later (e.g., two days ago
https://github.com/nim-lang/Nim/issues/13794) as well as still having
several "figure this out" implementation question comments in them (see
just diffs of this PR).

This topic has been discussed in many places:
  https://github.com/nim-lang/Nim/issues/13393
  https://github.com/nim-lang/Nim/pull/13418
  https://github.com/nim-lang/Nim/pull/13440
  https://github.com/nim-lang/Nim/issues/13794

Alternative/non-mandatory stronger integer hashes (or vice-versa opt-in
identity hashes) are a better solution that is more general (no illusion
of one hard-coded sequence solving all problems) while retaining the
virtues of linear probing such as cache obliviousness and age-less tables
under delete-heavy workloads (still untested after a month of this change).

The only real solution for truly adversarial keys is a hash keyed off of
data unobservable to attackers.  That all fits better with a few families
of user-pluggable/define-switchable hashes which can be provided in a
separate PR more about `hashes.nim`.

This PR carefully preserves the better (but still hard coded!) probing
of the  `intsets` and other recent fixes like `move` annotations, hash
order invariant tests, `intsets.missingOrExcl` fixing, and the move of
`rightSize` into `hashcommon.nim`.

* Fix `data.len` -> `dataLen` problem.

* This is an alternate resolution to https://github.com/nim-lang/Nim/issues/13393
(which arguably could be resolved outside the stdlib).

Add version1 of Wang Yi's hash specialized to 8 byte integers.  This gives
simple help to users having trouble with overly colliding hash(key)s.  I.e.,
  A) `import hashes; proc hash(x: myInt): Hash = hashWangYi1(int(x))`
      in the instantiation context of a `HashSet` or `Table`
or
  B) more globally, compile with `nim c -d:hashWangYi1`.

No hash can be all things to all use cases, but this one is A) vetted to
scramble well by the SMHasher test suite (a necessarily limited but far
more thorough test than prior proposals here), B) only a few ALU ops on
many common CPUs, and C) possesses an easy via "grade school multi-digit
multiplication" fall back for weaker deployment contexts.

Some people might want to stampede ahead unbridled, but my view is that a
good plan is to
  A) include this in the stdlib for a release or three to let people try it
     on various key sets nim-core could realistically never access/test
     (maybe mentioning it in the changelog so people actually try it out),
  B) have them report problems (if any),
  C) if all seems good, make the stdlib more novice friendly by adding
     `hashIdentity(x)=x` and changing the default `hash() = hashWangYi1`
     with some `when defined` rearranging so users can `-d:hashIdentity`
     if they want the old behavior back.
This plan is compatible with any number of competing integer hashes if
people want to add them.  I would strongly recommend they all *at least*
pass the SMHasher suite since the idea here is to become more friendly to
novices who do not generally understand hashing failure modes.

* Re-organize to work around `when nimvm` limitations; Add some tests; Add
a changelog.md entry.

* Add less than 64-bit CPU when fork.

* Fix decl instead of call typo.

* First attempt at fixing range error on 32-bit platforms; Still do the
arithmetic in doubled up 64-bit, but truncate the hash to the lower
32-bits, but then still return `uint64` to be the same.  So, type
correct but truncated hash value.  Update `thashes.nim` as well.

* A second try at making 32-bit mode CI work.

* Use a more systematic identifier convention than Wang Yi's code.

* Fix test that was wrong for as long as `toHashSet` used `rightSize` (a
very long time, I think).  `$a`/`$b` depend on iteration order which
varies with table range reduced hash order which varies with range for
some `hash()`.  With 3 elements, 3!=6 is small and we've just gotten
lucky with past experimental `hash()` changes.  An alternate fix here
would be to not stringify but use the HashSet operators, but it is not
clear that doesn't alter the "spirit" of the test.

* Fix another stringified test depending upon hash order.

* Oops - revert the string-keyed test.

* Fix another stringify test depending on hash order.

* Add a better than always zero `defined(js)` branch.

* It turns out to be easy to just work all in `BigInt` inside JS and thus
guarantee the same low order bits of output hashes (for `isSafeInteger`
input numbers).  Since `hashWangYi1` output bits are equally random in
all their bits, this means that tables will be safely scrambled for table
sizes up to 2**32 or 4 gigaentries which is probably fine, as long as the
integer keys are all < 2**53 (also likely fine).  (I'm unsure why the
infidelity with C/C++ back ends cut off is 32, not 53 bits.)

Since HashSet & Table only use the low order bits, a quick corollary of
this is that `$` on most int-keyed sets/tables will be the same in all
the various back ends which seems a nice-to-have trait.

* These string hash tests fail for me locally.  Maybe this is what causes
the CI hang for testament pcat collections?

* Oops. That failure was from me manually patching string hash in hashes.  Revert.

* Import more test improvements from https://github.com/nim-lang/Nim/pull/13410

* Fix bug where I swapped order when reverting the test.  Ack.

* Oh, just accept either order like more and more hash tests.

* Iterate in the same order.

* `return` inside `emit` made us skip `popFrame` causing weird troubles.

* Oops - do Windows branch also.

* `nimV1hash` -> multiply-mnemonic, type-scoped `nimIntHash1` (mnemonic
resolutions are "1 == identity", 1 for Nim Version 1, 1 for
first/simplest/fastest in a series of possibilities.  Should be very
easy to remember.)

* Re-organize `when nimvm` logic to be a strict `when`-`else`.

* Merge other changes.

* Lift constants to a common area.

* Fall back to identity hash when `BigInt` is unavailable.

* Increase timeout slightly (probably just real-time perturbation of CI
system performance).
2020-04-15 20:11:18 +02:00
..
meta.nim make more tests green 2018-08-13 13:02:52 +02:00
mevalffi.nim enable testing -d:nimHasLibFFI mode (#13091) 2020-02-04 14:08:56 +01:00
t2574.nim require errormsg to be specified before file. 2018-12-11 21:23:21 +01:00
tableinstatic.nim make tests green 2019-05-28 07:31:25 +02:00
taddrof.nim VM: improvements for var T/addr (#12667); fixes #12489 2019-11-28 09:56:02 +01:00
tanonproc.nim VM: improvements for var T/addr (#12667); fixes #12489 2019-11-28 09:56:02 +01:00
tarrayboundeval.nim the parser finally parses 'echo {1,2}' as it should 2017-09-30 21:35:30 +02:00
tasmparser.nim Remove expr/stmt (#5857) 2017-07-25 09:28:23 +02:00
tbitops.nim Fix vm signed xor (#10519) 2019-02-01 12:12:10 +01:00
tcastint.nim Fix spellings (#12277) [backport] 2019-09-27 07:02:54 +02:00
tcgemptycallarg.nim Field checks for everybody (#8957) 2018-10-09 19:54:12 +02:00
tcompilesetting.nim fix nim doc subdir/foo which was generating broken css; + other fixes (#13647) 2020-03-17 13:44:37 +01:00
tcompiletimerange.nim fixes tcompiletimerange [bugfix] (#11720) 2019-07-12 15:37:54 +02:00
tcompiletimesideeffects.nim Check there are no side effects before optimizing away compile time expressions. (#9934) 2018-12-31 14:41:24 +01:00
tcompiletimetable.nim allows access to .compileTime vars at runtime (#12128) 2019-09-05 11:56:32 +02:00
tcomponent.nim make more tests green 2018-08-13 13:02:52 +02:00
tconst_float_as_int.nim fixes #4619 2016-08-23 01:10:34 +02:00
tconsteval.nim updated tests to be executed 2018-11-23 11:58:28 +01:00
tconstobj.nim Fix VM conversion to var type [bugfix] (#11866) 2019-08-01 17:44:12 +02:00
tconstprocassignments.nim Fixes #11045. Assigning a proc to const and invoking. (#11076) 2019-05-04 22:57:15 +02:00
tconsttable.nim tables work in 'const' sections; echo supports 'nil' strings; minor cleanups 2015-02-09 00:08:55 +01:00
tconsttable2.nim cleanup tests; don't use non-working 'msg' spec field 2018-11-23 13:16:45 +01:00
tcopy_global_var.nim fixes #5269 2017-02-05 08:51:43 +01:00
teval1.nim Fixes #10514 (#12268) 2019-09-28 19:50:20 +02:00
texception.nim VM exception fixes (#11868) 2019-08-02 22:59:04 +01:00
texcl.nim Allow void macro result (#11286) 2019-05-21 21:31:40 +02:00
textensionmap.nim fixes #5237 2017-01-19 13:26:22 +01:00
tfarjump.nim Increased TInstr field sizes: allow long jumps and 65535 VM registers (#12777) 2019-12-10 19:04:02 +01:00
tfile_rw.nim fixes #13013, reverts previous changes to readLines() (#13036) [backport] 2020-01-05 09:22:41 +01:00
tforwardproc.nim lots of small changes 2018-12-11 21:23:22 +01:00
tgloballetfrommacro.nim fixes #11903 (#11908) 2019-08-31 19:32:59 +02:00
tgorge.bat Fixes #4871 2016-10-09 18:02:48 +02:00
tgorge.nim add getCurrentCompilerExe to vmops (eg allows to get nim compiler at CT); add tests for vmops (#9925) 2018-12-18 09:07:12 +01:00
tgorge.sh make run the default action of a test in tester 2018-11-23 11:58:28 +01:00
tgorgeex.bat added system.gorgeEx that includes the exitCode; refs #4874; fixes #1994 2017-01-07 02:38:26 +01:00
tgorgeex.sh make run the default action of a test in tester 2018-11-23 11:58:28 +01:00
tinheritance.nim [bugfix] VM: finally do inheritance properly 2019-07-12 12:15:30 +02:00
tissues.nim add isNamedTuple; make $(1, 2) be (1, 2) instead of (Field0: 1, Field1: 2) which leaked implementation detail (#10070) 2019-01-08 18:37:25 -08:00
tldconst.nim tests: Trim .nim files trailing whitespace 2015-09-04 23:04:32 +02:00
tmanyregs.nim Increased TInstr field sizes: allow long jumps and 65535 VM registers (#12777) 2019-12-10 19:04:02 +01:00
tmaxloopiterations.nim VM: allow overriding MaxLoopIterations without rebuilding nim (#13233) 2020-01-23 10:53:30 +01:00
tmisc_vm.nim fixes #12488 [backport] 2019-11-28 23:00:34 +01:00
tmitems_vm.nim VM: improvements for var T/addr (#12667); fixes #12489 2019-11-28 09:56:02 +01:00
tnilref.nim Improve vm support for ref types 2018-06-26 21:25:27 +02:00
tnimnode.nim Improve dumpLisp macro (#9515) 2018-10-27 14:10:05 +01:00
toverflowopcaddimmint.nim Add underflow/overflow tests. 2014-03-31 14:50:44 +02:00
toverflowopcaddint.nim Add underflow/overflow tests. 2014-03-31 14:50:44 +02:00
toverflowopcmulint.nim int128 on firstOrd, lastOrd and lengthOrd (#11701) 2019-08-07 15:53:16 +02:00
toverflowopcsubimmint.nim Add underflow/overflow tests. 2014-03-31 14:50:44 +02:00
toverflowopcsubint.nim Add underflow/overflow tests. 2014-03-31 14:50:44 +02:00
tquadplus.nim fixes #1023 2014-03-22 21:18:56 +01:00
tref.nim fix https://github.com/timotheecour/Nim/issues/88 (#13865) [backport:1.2] 2020-04-06 00:24:35 +02:00
treset.nim added system.default in order to prevent the abstraction inversion that 'template default(T): untyped = (var x: T; x)' causes 2019-03-05 09:54:59 +01:00
triangle_array.nim fixes #1781 2014-12-28 00:42:24 +01:00
tseq_badinit.nim make tests green again 2018-08-13 23:16:03 +02:00
tsetlen.nim fix #9872: setLen now works properly at CT [backport] 2018-12-08 14:33:42 -08:00
tsignaturehash.nim Compiler plugin for implementing incremental computation in user space (#10819) 2019-04-11 23:09:11 +02:00
tsimpleglobals.nim cleanup tests; don't use non-working 'msg' spec field 2018-11-23 13:16:45 +01:00
tslow_tables.nim Add hashWangYi1 (#13823) 2020-04-15 20:11:18 +02:00
tslurp.nim updated tests to be executed 2018-11-23 11:58:28 +01:00
tstaticprintseq.nim Allow void macro result (#11286) 2019-05-21 21:31:40 +02:00
tstring_openarray.nim Fix operations on string as openarray in VM. (#6257) 2017-09-15 13:35:57 +02:00
tstringnil.nim make more tests green 2018-08-13 13:02:52 +02:00
tswap.nim cleanup tests; don't use non-working 'msg' spec field 2018-11-23 13:16:45 +01:00
ttouintconv.nim makes more tests green 2019-09-02 22:03:06 +02:00
tvarsection.nim Fix compileTime pragma applying to whole var/let section (#10389) 2019-01-22 07:55:11 +01:00
tvmmisc.nim fixes #10886 [backport] (#10897) 2019-03-23 14:44:53 +01:00
tvmops.nim 32 bit fixes (#10608) 2019-02-13 23:30:14 +01:00
twrong_concat.nim fixes #3804 2016-04-04 01:41:14 +02:00
twrongarray.nim fixes #1343 2014-08-15 09:57:03 +02:00
twrongconst.nim Remove expr/stmt (#5857) 2017-07-25 09:28:23 +02:00
twrongwhen.nim make some tests green 2014-03-16 20:42:49 +02:00
tyaytypedesc.nim fixes #3357 2015-09-23 21:46:33 +02:00
tzero_extend.nim makes test green again 2019-09-02 14:23:05 +02:00