History

Create specific exception when index is out of range. Update thread class to print out exception details. Throw exception when an invalid op-code is detected. Enforce mspawn instruction cannot start from a built-in module.

arr committed 10 years ago

Commit 266fad61 parent 9af409310 files · +63 −40Browse files

Showing the changes to src/vm/thread.cpp (1 of 10 files). Show all

Unified · Split

src/vm/thread.cpp+18−9
@@ -117,10 +117,9 @@ namespace · 116 unmodified lines
117#endif117#endif
118 }118 }
119 119
120 std::atomic<uint32_t> thread_id_counter(1);
121
122 uint32_t get_unique_thread_id()120 uint32_t get_unique_thread_id()
123 {121 {
122 static std::atomic<uint32_t> thread_id_counter{ 1 };
124 return thread_id_counter.fetch_add(1);123 return thread_id_counter.fetch_add(1);
125 }124 }
126}125}
@@ -206,15 +205,17 @@ vm_thread_t::vm_thread_t( · 79 unmodified lines
206 // Copy over the frame into this thread - pass arguments to the thread205 // Copy over the frame into this thread - pass arguments to the thread
207 current_frame->copy_frame_contents(initial_frame);206 current_frame->copy_frame_contents(initial_frame);
208 207
209 debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "init: vm thread: %d %d\n", _thread_id, _parent_thread_id);208 if (debug::is_component_tracing_enabled<debug::component_trace_t::thread>())
209 debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "init: vm thread: %d %d\n", _thread_id, _parent_thread_id);
210}210}
211 211
212vm_thread_t::~vm_thread_t()212vm_thread_t::~vm_thread_t()
213{213{
214 debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "destroy: vm thread: %d %d\n", _thread_id, _parent_thread_id);214 if (debug::is_component_tracing_enabled<debug::component_trace_t::thread>())
215 debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "destroy: vm thread: %d %d\n", _thread_id, _parent_thread_id);
215}216}
216 217
217vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, uint32_t quanta)218vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, const uint32_t quanta)
218{219{
219 _registers.current_thread_state = vm_thread_state_t::running;220 _registers.current_thread_state = vm_thread_state_t::running;
220 assert(_registers.pc != runtime_constants::invalid_program_counter);221 assert(_registers.pc != runtime_constants::invalid_program_counter);
@@ -236,9 +237,13 @@ vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, uint32_t quanta) · 15 unmodified lines
236 assert(static_cast<std::size_t>(_registers.pc) < code_section.size());237 assert(static_cast<std::size_t>(_registers.pc) < code_section.size());
237 238
238 const auto &inst = code_section[_registers.pc].op;239 const auto &inst = code_section[_registers.pc].op;
240 const auto op_code = static_cast<std::size_t>(inst.opcode);
241 if (static_cast<std::size_t>(opcode_t::last_opcode) < op_code)
242 throw vm_system_exception{ "Unknown op-code" };
243
239 decode_address(inst, _registers);244 decode_address(inst, _registers);
240 _registers.pc++;245 _registers.pc++;
241 vm_exec_table[static_cast<std::size_t>(inst.opcode)](_registers, virtual_machine);246 vm_exec_table[op_code](_registers, virtual_machine);
242 247
243 if (_registers.current_thread_state != vm_thread_state_t::running)248 if (_registers.current_thread_state != vm_thread_state_t::running)
244 break;249 break;
@@ -247,13 +252,17 @@ vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, uint32_t quanta) · 2 unmodified lines
247 catch (const unhandled_user_exception &uue)252 catch (const unhandled_user_exception &uue)
248 {253 {
249 // [TODO] Store the exception254 // [TODO] Store the exception
250 debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "broken: vm thread: unhandled: >>%s<< %d >>%s<<\n", uue.exception_id, uue.program_counter, uue.module_name);255 std::printf("%s - %s in %s at %d\n", uue.what(), uue.exception_id, uue.module_name, uue.program_counter);
256 _registers.current_thread_state = vm_thread_state_t::broken;
257 }
258 catch (const index_out_of_range_memory &ioor)
259 {
260 std::printf("%s - %d [%d,%d]\n", ioor.what(), ioor.invalid_value, ioor.valid_min, ioor.valid_max);
251 _registers.current_thread_state = vm_thread_state_t::broken;261 _registers.current_thread_state = vm_thread_state_t::broken;
252 }262 }
253 catch (const vm_user_exception &ue)263 catch (const vm_user_exception &ue)
254 {264 {
255 // [TODO] Store the exception265 std::printf("%s\n", ue.what());
256 debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "broken: vm thread: >>%s<<\n", ue.what());
257 _registers.current_thread_state = vm_thread_state_t::broken;266 _registers.current_thread_state = vm_thread_state_t::broken;
258 }267 }
259 268