0009-save-x28-before-clobbering-it-in-the-regex-compiler.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. From 903a79a1efff18fc7cc50db09a3fe5d768adc9a8 Mon 19 Mar 2018 09:58:06 +0100
  2. From: Lars T Hansen <lhansen@mozilla.com>
  3. Date: Fri, 23 Mar 2018 22:01:33 +0000
  4. Subject: [PATCH] save x28 before clobbering it in the regex compiler
  5. Fixes: https://bugzilla.mozilla.org/show_bug.cgi?id=1445907
  6. Upsream-status: Applied
  7. See: https://hg.mozilla.org/mozilla-central/rev/903a79a1efff
  8. Signed-off-by: Lars T Hansen <lhansen@mozilla.com>
  9. Signed-off-by: Adam Duskett <aduskett@gmail.com>
  10. ---
  11. diff --git a/js/src/irregexp/NativeRegExpMacroAssembler.cpp b/js/src/irregexp/NativeRegExpMacroAssembler.cpp
  12. --- a/js/src/irregexp/NativeRegExpMacroAssembler.cpp
  13. +++ b/js/src/irregexp/NativeRegExpMacroAssembler.cpp
  14. @@ -118,17 +118,25 @@ NativeRegExpMacroAssembler::GenerateCode
  15. Label return_temp0;
  16. // Finalize code - write the entry point code now we know how many
  17. // registers we need.
  18. masm.bind(&entry_label_);
  19. #ifdef JS_CODEGEN_ARM64
  20. - // ARM64 communicates stack address via sp, but uses a pseudo-sp for addressing.
  21. + // ARM64 communicates stack address via SP, but uses a pseudo-sp (PSP) for
  22. + // addressing. The register we use for PSP may however also be used by
  23. + // calling code, and it is nonvolatile, so save it. Do this as a special
  24. + // case first because the generic save/restore code needs the PSP to be
  25. + // initialized already.
  26. + MOZ_ASSERT(PseudoStackPointer64.Is(masm.GetStackPointer64()));
  27. + masm.Str(PseudoStackPointer64, vixl::MemOperand(sp, -16, vixl::PreIndex));
  28. +
  29. + // Initialize the PSP from the SP.
  30. masm.initStackPtr();
  31. #endif
  32. // Push non-volatile registers which might be modified by jitcode.
  33. size_t pushedNonVolatileRegisters = 0;
  34. for (GeneralRegisterForwardIterator iter(savedNonVolatileRegisters); iter.more(); ++iter) {
  35. masm.Push(*iter);
  36. pushedNonVolatileRegisters++;
  37. @@ -416,17 +424,32 @@ NativeRegExpMacroAssembler::GenerateCode
  38. masm.pop(temp0);
  39. masm.movePtr(temp0, StackPointer);
  40. #endif
  41. // Restore non-volatile registers which were saved on entry.
  42. for (GeneralRegisterBackwardIterator iter(savedNonVolatileRegisters); iter.more(); ++iter)
  43. masm.Pop(*iter);
  44. +#ifdef JS_CODEGEN_ARM64
  45. + // Now restore the value that was in the PSP register on entry, and return.
  46. +
  47. + // Obtain the correct SP from the PSP.
  48. + masm.Mov(sp, PseudoStackPointer64);
  49. +
  50. + // Restore the saved value of the PSP register, this value is whatever the
  51. + // caller had saved in it, not any actual SP value, and it must not be
  52. + // overwritten subsequently.
  53. + masm.Ldr(PseudoStackPointer64, vixl::MemOperand(sp, 16, vixl::PostIndex));
  54. +
  55. + // Perform a plain Ret(), as abiret() will move SP <- PSP and that is wrong.
  56. + masm.Ret(vixl::lr);
  57. +#else
  58. masm.abiret();
  59. +#endif
  60. // Backtrack code (branch target for conditional backtracks).
  61. if (backtrack_label_.used()) {
  62. masm.bind(&backtrack_label_);
  63. Backtrack();
  64. }
  65. // Backtrack stack overflow code.
  66. diff --git a/js/src/jit-test/tests/regexp/bug1445907.js b/js/src/jit-test/tests/regexp/bug1445907.js
  67. new file mode 100644
  68. --- /dev/null
  69. +++ b/js/src/jit-test/tests/regexp/bug1445907.js
  70. @@ -0,0 +1,15 @@
  71. +// On ARM64, we failed to save x28 properly when generating code for the regexp
  72. +// matcher.
  73. +//
  74. +// There's wasm and Debugger code here because the combination forces the use of
  75. +// x28 and exposes the bug when running on the simulator.
  76. +
  77. +if (!wasmIsSupported())
  78. + quit();
  79. +
  80. +var g = newGlobal('');
  81. +var dbg = new Debugger(g);
  82. +g.eval(`var m = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary('(module (func (export "test")))')))`);
  83. +var re = /./;
  84. +dbg.onEnterFrame = function(frame) { re.exec("x") };
  85. +result = g.eval("m.exports.test()");
  86. --
  87. 2.23.0