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
117117 #endif
118118 }
119119
120− std::atomic<uint32_t> thread_id_counter(1);
121−
122120 uint32_t get_unique_thread_id()
123121 {
122+ static std::atomic<uint32_t> thread_id_counter{ 1 };
124123 return thread_id_counter.fetch_add(1);
125124 }
126125 }
@@ -206,15 +205,17 @@ vm_thread_t::vm_thread_t( · 79 unmodified lines
206205 // Copy over the frame into this thread - pass arguments to the thread
207206 current_frame->copy_frame_contents(initial_frame);
208207
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);
210210 }
211211
212212 vm_thread_t::~vm_thread_t()
213213 {
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);
215216 }
216217
217−vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, uint32_t quanta)
218+vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, const uint32_t quanta)
218219 {
219220 _registers.current_thread_state = vm_thread_state_t::running;
220221 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
236237 assert(static_cast<std::size_t>(_registers.pc) < code_section.size());
237238
238239 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+
239244 decode_address(inst, _registers);
240245 _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);
242247
243248 if (_registers.current_thread_state != vm_thread_state_t::running)
244249 break;
@@ -247,13 +252,17 @@ vm_thread_state_t vm_thread_t::execute(vm_t &virtual_machine, uint32_t quanta) · 2 unmodified lines
247252 catch (const unhandled_user_exception &uue)
248253 {
249254 // [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);
251261 _registers.current_thread_state = vm_thread_state_t::broken;
252262 }
253263 catch (const vm_user_exception &ue)
254264 {
255− // [TODO] Store the exception
256− debug::log_msg(debug::component_trace_t::thread, debug::log_level_t::debug, "broken: vm thread: >>%s<<\n", ue.what());
265+ std::printf("%s\n", ue.what());
257266 _registers.current_thread_state = vm_thread_state_t::broken;
258267 }
259268