OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WASM_RUN_UTILS_H | 5 #ifndef WASM_RUN_UTILS_H |
6 #define WASM_RUN_UTILS_H | 6 #define WASM_RUN_UTILS_H |
7 | 7 |
8 #include <setjmp.h> | 8 #include <setjmp.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 } | 659 } |
660 } | 660 } |
661 | 661 |
662 ReturnType Call(ParamTypes... p) { | 662 ReturnType Call(ParamTypes... p) { |
663 DCHECK(compiled_); | 663 DCHECK(compiled_); |
664 if (interpret()) return CallInterpreter(p...); | 664 if (interpret()) return CallInterpreter(p...); |
665 | 665 |
666 // Use setjmp/longjmp to deal with traps in WebAssembly code. | 666 // Use setjmp/longjmp to deal with traps in WebAssembly code. |
667 // Make the return value volatile, to give defined semantics if accessed | 667 // Make the return value volatile, to give defined semantics if accessed |
668 // after setjmp. | 668 // after setjmp. |
669 volatile ReturnType return_value = | 669 ReturnType return_value = static_cast<ReturnType>(0xdeadbeefdeadbeef); |
670 static_cast<ReturnType>(0xdeadbeefdeadbeef); | 670 static int setjmp_ret; |
671 int jump_value = setjmp(WasmRunnerBase::jump_buffer); | 671 setjmp_ret = setjmp(WasmRunnerBase::jump_buffer); |
672 // jump_value == 0 --> first return; jump_value == 1 --> longjmp happened. | 672 // setjmp returns 0 on the first return, 1 (passed to longjmp) after trap. |
673 if (!jump_value) DoCall(&return_value, p...); | 673 if (setjmp_ret == 0) { |
| 674 DoCall(static_cast<void*>(&p)..., static_cast<void*>(&return_value)); |
| 675 } |
674 return return_value; | 676 return return_value; |
675 } | 677 } |
676 | 678 |
677 ReturnType CallInterpreter(ParamTypes... p) { | 679 ReturnType CallInterpreter(ParamTypes... p) { |
678 WasmInterpreter::Thread* thread = interpreter()->GetThread(0); | 680 WasmInterpreter::Thread* thread = interpreter()->GetThread(0); |
679 thread->Reset(); | 681 thread->Reset(); |
680 std::array<WasmVal, sizeof...(p)> args{{WasmVal(p)...}}; | 682 std::array<WasmVal, sizeof...(p)> args{{WasmVal(p)...}}; |
681 thread->PushFrame(function(), args.data()); | 683 thread->PushFrame(function(), args.data()); |
682 if (thread->Run() == WasmInterpreter::FINISHED) { | 684 if (thread->Run() == WasmInterpreter::FINISHED) { |
683 WasmVal val = thread->GetReturnValue(); | 685 WasmVal val = thread->GetReturnValue(); |
684 possible_nondeterminism_ |= thread->PossibleNondeterminism(); | 686 possible_nondeterminism_ |= thread->PossibleNondeterminism(); |
685 return val.to<ReturnType>(); | 687 return val.to<ReturnType>(); |
686 } else if (thread->state() == WasmInterpreter::TRAPPED) { | 688 } else if (thread->state() == WasmInterpreter::TRAPPED) { |
687 // TODO(titzer): return the correct trap code | 689 // TODO(titzer): return the correct trap code |
688 int64_t result = 0xdeadbeefdeadbeef; | 690 int64_t result = 0xdeadbeefdeadbeef; |
689 return static_cast<ReturnType>(result); | 691 return static_cast<ReturnType>(result); |
690 } else { | 692 } else { |
691 // TODO(titzer): falling off end | 693 // TODO(titzer): falling off end |
692 return ReturnType{0}; | 694 return ReturnType{0}; |
693 } | 695 } |
694 } | 696 } |
695 | 697 |
696 private: | 698 private: |
697 // Don't inline this function. The setjmp above should be followed immediately | 699 // Don't inline this function. The setjmp above should be followed immediately |
698 // by a call. | 700 // by a call. |
699 V8_NOINLINE void DoCall(volatile ReturnType* return_value, ParamTypes... p) { | 701 template <typename... Ptrs> |
| 702 V8_NOINLINE void DoCall(Ptrs... ptrs) { |
700 auto trap_callback = []() -> void { | 703 auto trap_callback = []() -> void { |
701 set_trap_callback_for_testing(nullptr); | 704 set_trap_callback_for_testing(nullptr); |
702 longjmp(WasmRunnerBase::jump_buffer, 1); | 705 longjmp(WasmRunnerBase::jump_buffer, 1); |
703 }; | 706 }; |
704 set_trap_callback_for_testing(trap_callback); | 707 set_trap_callback_for_testing(trap_callback); |
705 | 708 |
706 wrapper_.SetInnerCode( | 709 wrapper_.SetInnerCode( |
707 module_.GetFunctionCode(functions_[0]->function_index())); | 710 module_.GetFunctionCode(functions_[0]->function_index())); |
708 CodeRunner<int32_t> runner(CcTest::InitIsolateOnce(), | 711 CodeRunner<int32_t> runner(CcTest::InitIsolateOnce(), |
709 wrapper_.GetWrapperCode(), wrapper_.signature()); | 712 wrapper_.GetWrapperCode(), wrapper_.signature()); |
710 ReturnType return_value_local; | 713 int32_t result = runner.Call(ptrs...); |
711 int32_t result = runner.Call(static_cast<void*>(&p)..., | |
712 static_cast<void*>(&return_value_local)); | |
713 *return_value = return_value_local; | |
714 // If we arrive here, no trap happened. | 714 // If we arrive here, no trap happened. |
715 CHECK_EQ(WASM_WRAPPER_RETURN_VALUE, result); | 715 CHECK_EQ(WASM_WRAPPER_RETURN_VALUE, result); |
716 } | 716 } |
717 }; | 717 }; |
718 | 718 |
719 // Declare static variable. | 719 // Declare static variable. |
720 jmp_buf WasmRunnerBase::jump_buffer; | 720 jmp_buf WasmRunnerBase::jump_buffer; |
721 | 721 |
722 // A macro to define tests that run in different engine configurations. | 722 // A macro to define tests that run in different engine configurations. |
723 #define WASM_EXEC_TEST(name) \ | 723 #define WASM_EXEC_TEST(name) \ |
724 void RunWasm_##name(WasmExecutionMode execution_mode); \ | 724 void RunWasm_##name(WasmExecutionMode execution_mode); \ |
725 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ | 725 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ |
726 TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ | 726 TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ |
727 void RunWasm_##name(WasmExecutionMode execution_mode) | 727 void RunWasm_##name(WasmExecutionMode execution_mode) |
728 | 728 |
729 #if V8_CC_MSVC | |
730 #define WASM_EXEC_TEST_WITH_TRAP(name) WASM_EXEC_TEST(name) | |
731 #else | |
732 #define WASM_EXEC_TEST_WITH_TRAP(name) \ | 729 #define WASM_EXEC_TEST_WITH_TRAP(name) \ |
733 void RunWasm_##name(WasmExecutionMode execution_mode); \ | 730 void RunWasm_##name(WasmExecutionMode execution_mode); \ |
734 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ | 731 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ |
735 void RunWasm_##name(WasmExecutionMode execution_mode); \ | 732 void RunWasm_##name(WasmExecutionMode execution_mode); \ |
736 TEST(RunWasmCompiledWithTrapIf_##name) { \ | 733 TEST(RunWasmCompiledWithTrapIf_##name) { \ |
737 bool trap_if = FLAG_wasm_trap_if; \ | 734 bool trap_if = FLAG_wasm_trap_if; \ |
738 FLAG_wasm_trap_if = true; \ | 735 FLAG_wasm_trap_if = true; \ |
739 RunWasm_##name(kExecuteCompiled); \ | 736 RunWasm_##name(kExecuteCompiled); \ |
740 FLAG_wasm_trap_if = trap_if; \ | 737 FLAG_wasm_trap_if = trap_if; \ |
741 } \ | 738 } \ |
742 TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ | 739 TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ |
743 void RunWasm_##name(WasmExecutionMode execution_mode) | 740 void RunWasm_##name(WasmExecutionMode execution_mode) |
744 #endif | |
745 | 741 |
746 #define WASM_EXEC_COMPILED_TEST(name) \ | 742 #define WASM_EXEC_COMPILED_TEST(name) \ |
747 void RunWasm_##name(WasmExecutionMode execution_mode); \ | 743 void RunWasm_##name(WasmExecutionMode execution_mode); \ |
748 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ | 744 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ |
749 void RunWasm_##name(WasmExecutionMode execution_mode) | 745 void RunWasm_##name(WasmExecutionMode execution_mode) |
750 | 746 |
751 } // namespace | 747 } // namespace |
752 | 748 |
753 #endif | 749 #endif |
OLD | NEW |