test_nacl_irt_stack_alignment.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ppapi/tests/test_nacl_irt_stack_alignment.h"
  5. #include <stddef.h>
  6. #include "ppapi/c/pp_var.h"
  7. #include "ppapi/c/ppb_var.h"
  8. #include "ppapi/cpp/instance.h"
  9. #include "ppapi/cpp/module.h"
  10. #include "ppapi/cpp/var.h"
  11. #include "ppapi/tests/testing_instance.h"
  12. // This whole test is really only meant for x86-32 NaCl (not PNaCl).
  13. //
  14. // This is a regression test for the IRT code being sensitive to stack
  15. // alignment. The de jure ABI is that the stack should be aligned to
  16. // 16 bytes at call sites. However, the de facto ABI is that the IRT
  17. // worked in the past when called with misaligned stack. NaCl code is
  18. // now compiled to expect the proper 16-byte alignment, but the IRT
  19. // code must remain compatible with old binaries that failed to do so.
  20. #if defined(__i386__)
  21. REGISTER_TEST_CASE(NaClIRTStackAlignment);
  22. bool TestNaClIRTStackAlignment::Init() {
  23. var_interface_ = static_cast<const PPB_Var*>(
  24. pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
  25. return var_interface_ && CheckTestingInterface();
  26. }
  27. void TestNaClIRTStackAlignment::RunTests(const std::string& filter) {
  28. RUN_TEST(MisalignedCallVarAddRef, filter);
  29. }
  30. // This calls the given function with the stack explicitly misaligned.
  31. // If the function (in the IRT) was compiled wrongly, it will crash.
  32. void MisalignedCall(void (*func)(PP_Var), const PP_Var* arg)
  33. asm("MisalignedCall") __attribute__((regparm(2)));
  34. // regparm(2) means: First argument in %eax, second argument in %edx.
  35. // Writing this with an inline asm would require explaining all the
  36. // call-clobbers register behavior in the asm clobber list, which is a
  37. // lot with all the SSE and FPU state. It's far simpler just to make
  38. // it a function call the compiler knows is a function call, and then
  39. // write the function itself in pure assembly.
  40. asm("MisalignedCall:\n"
  41. // Use an SSE register to copy the 16 bytes of memory.
  42. // Note this instruction does not care about alignment.
  43. // The pointer is not necessarily aligned to 16 bytes.
  44. "movups (%edx), %xmm0\n"
  45. // Set up a frame so we can recover the stack pointer after alignment.
  46. "push %ebp\n"
  47. "mov %esp, %ebp\n"
  48. // Align the stack properly to 16 bytes.
  49. "andl $-16, %esp\n"
  50. // Now make space for the 16 bytes of argument data,
  51. // plus another 4 bytes so the stack pointer is misaligned.
  52. "subl $20, %esp\n"
  53. // Copy the argument onto the (misaligned) top of stack.
  54. "movups %xmm0, (%esp)\n"
  55. // Now call into the IRT, and hilarity ensues.
  56. "naclcall %eax\n"
  57. // Standard epilogue.
  58. "mov %ebp, %esp\n"
  59. "pop %ebp\n"
  60. "naclret");
  61. std::string TestNaClIRTStackAlignment::TestMisalignedCallVarAddRef() {
  62. PP_Var var;
  63. var.type = PP_VARTYPE_INT32;
  64. var.padding = 0;
  65. var.value.as_int = 23;
  66. ASSERT_EQ(sizeof(var), static_cast<size_t>(16));
  67. // This will crash if the test fails.
  68. MisalignedCall(var_interface_->AddRef, &var);
  69. MisalignedCall(var_interface_->Release, &var);
  70. PASS();
  71. }
  72. #endif // defined(__i386__)