Skip to content

Architecture pass fixes: cppia - #478

Open
dimensionscape wants to merge 3 commits into
mainfrom
fix/arch-pass-3
Open

dimensionscape wants to merge 3 commits into
mainfrom
fix/arch-pass-3

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

The cppia fixes from the architecture pass in #154. Each commit has a regression test that fails without its fix and passes with it on Windows x64.

  • cppia: the interpreter and the JIT disagree on array builtins #468: the interpreter and the JIT gave different results for array builtins. Under the JIT, Array<Dynamic> compound assignment stored the right hand side alone, its ++, insert and toString did nothing, Bools in an Array<Any> became Ints, bitwise compound assignment on Array<Null<Int>> did nothing, an out of range Int read as a String was "null", and ++d[i] on a Dynamic as a statement decremented. Interpreted, indexOf used as a Float gave 0 and join or toString as a statement did not run. Both engines now match the same code built as C++. Test: CppiaArrayEngines.
  • cppia: malformed files that still crash the host #467: malformed cppia files that crashed the host: MAIN with no types, unknown or miscalled script enum constructors, a duplicate class type id, string and file ids past the string table, an enum parameter index past the value, break or continue outside a loop under the JIT, and a super constructor call to a class that is not the super class. Each fails to load, or is read safely. Test: CppiaMalformedLoads, ten files.
  • cppia: adoptInstances moves instances that code then calls or reads with the wrong layout #464: adoptInstances moved instances whose methods changed slots, so old code still running called the wrong method or crashed, and moved classes whose fields could hold instances that stayed. Methods and script interface slots must match now, field types compare by name, and a class stays with the classes its fields hold. docs/CppiaModuleRelease.md describes the rule. Test: CppiaAdoptLayouts.

Also run locally: the cppia suite interpreted and with the JIT, and the Cppia regression tests.

Fixes #464
Fixes #467
Fixes #468

Compared with the same code built as C++, which both now match:

JIT:
- Array<Dynamic> and Array<Any> compound assignment stored the right
  hand side alone, and ++, --, insert and toString had no JIT code at
  all, so they did nothing ("Unknown ArrayBuiltinAny"). They call
  helpers sharing the interpreter's arithmetic, which catch, and the
  jitted code checks after each.
- A Bool pushed, unshifted or set into an Array<Any> was stored as an
  Int. It goes in boxed.
- Bitwise compound operators on an Array<Null<Int>> did nothing: the
  bitwise cases fell through to the element's type for lack of a break.
- An Int array's element read out of range as a String was "null", not
  "0". It reads the element type's zero.
- ++d[i] on a Dynamic used as a statement decremented: the test was
  crement==coPostInc || crement==coPostInc.

Interpreter:
- A typed indexOf or lastIndexOf used as a Float returned 0 without
  running.
- join and toString used as a statement, and iterator and
  keyValueIterator, were skipped, receiver and element toString calls
  with them.

Test: CppiaArrayEngines, twelve lines, each wrong in one engine before
and right in both now, interpreted and under the JIT on Windows x64.

Fixes #468
SECURITY.md promises a load error for a malformed .cppia file, but
these crashed the host, interpreted, under the JIT, or both:

- MAIN in a module with no types: ScriptCallable::link read the type
  of its return, 0, from an empty table. Return type ids are checked.
- An unknown script enum constructor left a null value that was
  dereferenced, and a call with the wrong number of arguments wrote
  past the enum value under the JIT. Both fail to link.
- Two classes with one type id: the second overwrote the first's and
  wrote outside the vtable made for it. It fails to load.
- A string constant id, and the text format's file id, were read
  unchecked, and the string table grew to them: by 100000000 entries,
  1.4 GB, for a file id, and the JIT's addresses of its strings were
  left dangling. They are checked as other string ids are.
- An enum parameter index past the value read past it. A script enum's
  constructors bound it at link, and otherwise the interpreter and the
  JIT check the value's own parameter count.
- break or continue outside a loop made sljit assert under the JIT,
  ending the process. The function fails to compile instead, checked
  on the pass that emits code, since the measuring pass has no labels.
- A super constructor call to a script class other than the super
  class ran that constructor on an instance laid out for this one. It
  fails to link, after the check for a class with no constructor.
  CppiaMalformedConstructors' control, such a call from a static main,
  is refused now too.

Test: CppiaMalformedLoads, ten such files, loaded and run interpreted
and with the JIT compiling as it loads. Before, the host crashed on
the first; now each fails to load or runs safely. The cppia suite and
the 60 Cppia regression tests pass, interpreted and under the JIT, on
Windows x64.

Fixes #467
adoptInstances compared only field names, storage types and offsets,
and docs/CppiaModuleRelease.md said methods could differ in any way.
But a call's vtable slot is fixed when its code links, so old code
still running, which the docs say finishes normally, called a moved
instance by the old slots: with a method added first, foo.b() ran the
new a(), and foo.a() reached x(), whose Int was read as a String,
crashing the interpreter and giving the JIT a String of garbage. And a
moved class's field could hold an instance of a class that stayed,
since its layout changed, which new code then read with the new layout,
past the object.

Now:
- methods, their super classes' included, must have the same names in
  the same slots, and so must the script interfaces the class
  implements;
- fields compare their type names, so a retyped field within one
  storage type, Array<Int> to Array<Float>, keeps its instances, as the
  docs said it did;
- a class with a field that can hold an instance of a class that stays
  stays too, to a fixed point.

The report says which rule kept a class, and the docs describe the
rule.

Test: CppiaAdoptLayouts, the two cases, interpreted and under the JIT.
Before, the first crashed the interpreter and the second read past the
object; now neither class moves and old code keeps its own results.
CppiaAdoptInstances, whose classes change only method bodies, still
moves them, on Windows x64.

Fixes #464
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant