From 6f459782d9953117864922be07d541ed5de5332b Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 19:46:49 +0200 Subject: [PATCH 01/16] Remove unused parameter stack --- src/Primitives/primitives.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Primitives/primitives.h b/src/Primitives/primitives.h index 03b08146..df70c25a 100644 --- a/src/Primitives/primitives.h +++ b/src/Primitives/primitives.h @@ -32,7 +32,7 @@ std::vector get_io_state(Module *m); void restore_external_state(Module *m, std::vector external_state); -inline void create_stack(std::vector *stack) {} +inline void create_stack(std::vector*) {} template void create_stack(std::vector *stack, T value, Ts... args) { From f831d1f7037d984978bd37c70d932c250517f684 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 19:54:11 +0200 Subject: [PATCH 02/16] Make psram [[maybe_unused]] since it is not used on some targets --- src/Memory/mem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Memory/mem.cpp b/src/Memory/mem.cpp index 922eff6d..1a0f1b5e 100644 --- a/src/Memory/mem.cpp +++ b/src/Memory/mem.cpp @@ -10,7 +10,7 @@ #endif // Assert calloc -void *acalloc(size_t nmemb, size_t size, const char *name, bool psram) { +void *acalloc(size_t nmemb, size_t size, const char *name, [[maybe_unused]] bool psram) { if ((int)(nmemb * size) == 0) { return nullptr; } else { @@ -38,7 +38,7 @@ void *acalloc(size_t nmemb, size_t size, const char *name, bool psram) { // Assert realloc/calloc void *arecalloc(void *ptr, size_t old_nmemb, size_t nmemb, size_t size, - const char *name, bool psram) { + const char *name, [[maybe_unused]] bool psram) { #ifdef ARDUINO void *res; if (psramInit() && psram) { From 19d431efe4d4a17cdeec93f85bc9399ebbf7da11 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 19:59:29 +0200 Subject: [PATCH 03/16] Use static_cast instead of c style cast --- src/Memory/mem.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Memory/mem.cpp b/src/Memory/mem.cpp index 1a0f1b5e..a5bef0b4 100644 --- a/src/Memory/mem.cpp +++ b/src/Memory/mem.cpp @@ -11,7 +11,7 @@ // Assert calloc void *acalloc(size_t nmemb, size_t size, const char *name, [[maybe_unused]] bool psram) { - if ((int)(nmemb * size) == 0) { + if (static_cast(nmemb * size) == 0) { return nullptr; } else { debug("IN Acalloc count: %zu, size: %zu for %s \n", nmemb, size, name); @@ -28,7 +28,7 @@ void *acalloc(size_t nmemb, size_t size, const char *name, [[maybe_unused]] bool debug("Done ... Acalloc\n"); if (res == nullptr) { debug("FAILED ... Acalloc\n"); - FATAL("Could not allocate %d bytes for %s \n", (int)(nmemb * size), + FATAL("Could not allocate %d bytes for %s \n", static_cast(nmemb * size), name); } debug("NOT FAILED ... Acalloc\n"); @@ -47,10 +47,10 @@ void *arecalloc(void *ptr, size_t old_nmemb, size_t nmemb, size_t size, res = (size_t *)calloc(nmemb, size); } #else - auto *res = (size_t *)calloc(nmemb, size); + auto *res = static_cast(calloc(nmemb, size)); #endif if (res == nullptr) { - FATAL("Could not allocate %d bytes for %s", (int)(nmemb * size), name); + FATAL("Could not allocate %d bytes for %s", static_cast(nmemb * size), name); } memset(res, 0, nmemb * size); // initialize memory with 0 memmove(res, ptr, old_nmemb * size); From dc1937d815de0d616a81c6a385db458deec96eea Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 20:00:50 +0200 Subject: [PATCH 04/16] Formatting --- src/Memory/mem.cpp | 10 ++++++---- src/Primitives/primitives.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Memory/mem.cpp b/src/Memory/mem.cpp index a5bef0b4..e2483501 100644 --- a/src/Memory/mem.cpp +++ b/src/Memory/mem.cpp @@ -10,7 +10,8 @@ #endif // Assert calloc -void *acalloc(size_t nmemb, size_t size, const char *name, [[maybe_unused]] bool psram) { +void *acalloc(size_t nmemb, size_t size, const char *name, + [[maybe_unused]] bool psram) { if (static_cast(nmemb * size) == 0) { return nullptr; } else { @@ -28,8 +29,8 @@ void *acalloc(size_t nmemb, size_t size, const char *name, [[maybe_unused]] bool debug("Done ... Acalloc\n"); if (res == nullptr) { debug("FAILED ... Acalloc\n"); - FATAL("Could not allocate %d bytes for %s \n", static_cast(nmemb * size), - name); + FATAL("Could not allocate %d bytes for %s \n", + static_cast(nmemb * size), name); } debug("NOT FAILED ... Acalloc\n"); return res; @@ -50,7 +51,8 @@ void *arecalloc(void *ptr, size_t old_nmemb, size_t nmemb, size_t size, auto *res = static_cast(calloc(nmemb, size)); #endif if (res == nullptr) { - FATAL("Could not allocate %d bytes for %s", static_cast(nmemb * size), name); + FATAL("Could not allocate %d bytes for %s", + static_cast(nmemb * size), name); } memset(res, 0, nmemb * size); // initialize memory with 0 memmove(res, ptr, old_nmemb * size); diff --git a/src/Primitives/primitives.h b/src/Primitives/primitives.h index df70c25a..07b523f3 100644 --- a/src/Primitives/primitives.h +++ b/src/Primitives/primitives.h @@ -32,7 +32,7 @@ std::vector get_io_state(Module *m); void restore_external_state(Module *m, std::vector external_state); -inline void create_stack(std::vector*) {} +inline void create_stack(std::vector *) {} template void create_stack(std::vector *stack, T value, Ts... args) { From a8694c4956c81c7af8afed3227b3a1a3324c2de3 Mon Sep 17 00:00:00 2001 From: jay-tux Date: Wed, 9 Oct 2024 20:04:35 +0200 Subject: [PATCH 05/16] Fix compiler warnings in src/Primitives/emulated.cpp --- src/Primitives/emulated.cpp | 46 +++++++++++++++++++------------------ src/Primitives/primitives.h | 2 +- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/Primitives/emulated.cpp b/src/Primitives/emulated.cpp index 71bab730..e429350f 100644 --- a/src/Primitives/emulated.cpp +++ b/src/Primitives/emulated.cpp @@ -57,20 +57,20 @@ double sensor_emu = 0; #define install_primitive_reverse(prim_name) \ { \ PrimitiveEntry *p = &primitives[prim_index - 1]; \ - p->f_reverse = &(prim_name##_reverse); \ - p->f_serialize_state = &(prim_name##_serialize); \ + p->f_reverse = &(prim_name## _reverse); \ + p->f_serialize_state = &(prim_name## _serialize); \ } #define def_prim(function_name, type) \ - Type function_name##_type = type; \ - bool function_name(Module *m) + Type function_name## _type = type; \ + bool function_name([[maybe_unused]] Module *m) #define def_prim_reverse(function_name) \ - void function_name##_reverse(Module *m, \ + void function_name## _reverse(Module *m, \ std::vector external_state) #define def_prim_serialize(function_name) \ - void function_name##_serialize( \ + void function_name## _serialize( \ std::vector &external_state) // TODO: use fp @@ -270,7 +270,7 @@ def_prim(test, oneToNoneU32) { Callback c = Callback(m, topic, fidx); CallbackHandler::add_callback(c); auto *payload = reinterpret_cast("TestPayload"); - CallbackHandler::push_event(topic, (const char *)payload, 11); + CallbackHandler::push_event(topic, reinterpret_cast(payload), 11); pop_args(1); return true; } @@ -318,11 +318,11 @@ def_prim(wifi_connected, NoneToOneU32) { def_prim(wifi_localip, twoToOneU32) { uint32_t buff = arg1.uint32; - uint32_t size = arg0.uint32; + // uint32_t size = arg0.uint32; // never used in emulator std::string ip = "192.168.0.181"; for (unsigned long i = 0; i < ip.length(); i++) { - m->memory.bytes[buff + i] = (uint32_t)ip[i]; + m->memory.bytes[buff + i] = *reinterpret_cast(&ip[i]); } pop_args(2); pushInt32(buff); @@ -345,7 +345,7 @@ def_prim(http_get, fourToOneU32) { return false; // TRAP } for (unsigned long i = 0; i < answer.length(); i++) { - m->memory.bytes[response + i] = (uint32_t)answer[i]; + m->memory.bytes[response + i] = *reinterpret_cast(&answer[i]); } // Pop args and return response address @@ -365,7 +365,7 @@ def_prim(http_post, tenToOneU32) { uint32_t authorization = arg3.uint32; uint32_t authorization_len = arg2.uint32; int32_t response = arg1.uint32; - uint32_t size = arg0.uint32; + // uint32_t size = arg0.uint32; // never used in emulator std::string url_parsed = parse_utf8_string(m->memory.bytes, url_len, url); std::string body_parsed = @@ -398,14 +398,14 @@ def_prim(chip_digital_write, twoToNoneU32) { } def_prim(chip_digital_read, oneToOneU32) { - uint8_t pin = arg0.uint32; + // uint8_t pin = arg0.uint32; // never used in emulator pop_args(1); pushUInt32(1); // HIGH return true; } def_prim(chip_analog_read, oneToOneI32) { - uint8_t pin = arg0.uint32; + // uint8_t pin = arg0.uint32; // never used in emulator pop_args(1); pushInt32(sin(sensor_emu) * 100); sensor_emu += .25; @@ -460,11 +460,11 @@ def_prim(write_spi_bytes_16, twoToNoneU32) { def_prim(subscribe_interrupt, threeToNoneU32) { uint8_t pin = arg2.uint32; // GPIOPin uint8_t tidx = arg1.uint32; // Table Idx pointing to Callback function - uint8_t mode = arg0.uint32; + // uint8_t mode = arg0.uint32; // never used in emulator debug("EMU: subscribe_interrupt(%u, %u, %u) \n", pin, tidx, mode); - if (tidx < 0 || m->table.size < tidx) { + if (m->table.size < tidx) { debug("subscribe_interrupt: out of range table index %i\n", tidx); return false; } @@ -554,6 +554,7 @@ void install_primitives() { //------------------------------------------------------ // resolving the primitives //------------------------------------------------------ +// ReSharper disable once CppDFAConstantFunctionResult bool resolve_primitive(char *symbol, Primitive *val) { debug("Resolve primitives (%d) for %s \n", ALL_PRIMITIVES, symbol); @@ -566,34 +567,35 @@ bool resolve_primitive(char *symbol, Primitive *val) { } } FATAL("Could not find primitive %s \n", symbol); - return false; + // return false; // unreachable } Memory external_mem{}; +// ReSharper disable once CppDFAConstantFunctionResult bool resolve_external_memory(char *symbol, Memory **val) { if (!strcmp(symbol, "memory")) { if (external_mem.bytes == nullptr) { external_mem.initial = 256; external_mem.maximum = 256; external_mem.pages = 256; - external_mem.bytes = (uint8_t *)acalloc( - external_mem.pages * PAGE_SIZE, sizeof(uint32_t), - "Module->memory.bytes primitive"); + external_mem.bytes = static_cast( + acalloc(external_mem.pages * PAGE_SIZE, sizeof(uint32_t), + "Module->memory.bytes primitive")); } *val = &external_mem; return true; } FATAL("Could not find memory %s \n", symbol); - return false; + // return false; // unreachable } //------------------------------------------------------ // Restore external state when restoring a snapshot //------------------------------------------------------ void restore_external_state(Module *m, - std::vector external_state) { + const std::vector &external_state) { uint8_t opcode = *m->pc_ptr; // TODO: Maybe primitives can also be called using the other call // instructions such as call_indirect @@ -617,7 +619,7 @@ void restore_external_state(Module *m, } } -std::vector get_io_state(Module *m) { +std::vector get_io_state(Module *) { std::vector ioState; for (auto &primitive : primitives) { if (primitive.f_serialize_state) { diff --git a/src/Primitives/primitives.h b/src/Primitives/primitives.h index 03b08146..acfc08fb 100644 --- a/src/Primitives/primitives.h +++ b/src/Primitives/primitives.h @@ -30,7 +30,7 @@ void install_primitives(); std::vector get_io_state(Module *m); void restore_external_state(Module *m, - std::vector external_state); + const std::vector &external_state); inline void create_stack(std::vector *stack) {} From 486dacc9f3b732be99bd12f11be8a5c1aed56bd4 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 20:30:13 +0200 Subject: [PATCH 06/16] Fix warnings + static_cast + fixed memory leak by using std::string copy constructor with length --- src/WARDuino/CallbackHandler.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/WARDuino/CallbackHandler.cpp b/src/WARDuino/CallbackHandler.cpp index 210fe794..9d5fdbdb 100644 --- a/src/WARDuino/CallbackHandler.cpp +++ b/src/WARDuino/CallbackHandler.cpp @@ -10,7 +10,7 @@ void push_guard(Module *m) { if (m == nullptr) { return; } - auto *guard = (Block *)malloc(sizeof(struct Block)); + auto *guard = static_cast(malloc(sizeof(Block))); guard->block_type = 255; guard->type = nullptr; guard->local_value_type = nullptr; @@ -69,12 +69,9 @@ size_t CallbackHandler::callback_count(const std::string &topic) { // WARNING: Push event functions should not use IO functions, since they can be // called from ISR callbacks void CallbackHandler::push_event(std::string topic, const char *payload, - unsigned int length) { - char *message = (char *)(malloc(sizeof(char) * length + 1)); - snprintf(message, length + 1, "%s", payload); - auto event = - new Event(std::move(topic), reinterpret_cast(message)); - CallbackHandler::push_event(event); + const unsigned int length) { + CallbackHandler::push_event( + new Event(std::move(topic), std::string(payload, length))); } void CallbackHandler::push_event(Event *event) { @@ -130,7 +127,7 @@ bool CallbackHandler::resolve_event(bool force) { push_guard(module); } else { // TODO handle error: event for non-existing iterator - printf("No handler found for %s (in %u items)!\n", event.topic.c_str(), + printf("No handler found for %s (in %lu items)!\n", event.topic.c_str(), CallbackHandler::callbacks->size()); } return !CallbackHandler::events->empty(); From cffe8d2f8c12fd2c45bc189a153f4ab5cb4d43ef Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 20:45:31 +0200 Subject: [PATCH 07/16] Use zu for size type --- src/WARDuino/CallbackHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WARDuino/CallbackHandler.cpp b/src/WARDuino/CallbackHandler.cpp index 9d5fdbdb..578c1395 100644 --- a/src/WARDuino/CallbackHandler.cpp +++ b/src/WARDuino/CallbackHandler.cpp @@ -127,7 +127,7 @@ bool CallbackHandler::resolve_event(bool force) { push_guard(module); } else { // TODO handle error: event for non-existing iterator - printf("No handler found for %s (in %lu items)!\n", event.topic.c_str(), + printf("No handler found for %s (in %zu items)!\n", event.topic.c_str(), CallbackHandler::callbacks->size()); } return !CallbackHandler::events->empty(); From 1a84c383d69efd9ff2fc31322cdd4975ba0d0048 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 20:51:43 +0200 Subject: [PATCH 08/16] Change one more lu to zu --- src/WARDuino/CallbackHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WARDuino/CallbackHandler.cpp b/src/WARDuino/CallbackHandler.cpp index 578c1395..28e0bc3b 100644 --- a/src/WARDuino/CallbackHandler.cpp +++ b/src/WARDuino/CallbackHandler.cpp @@ -113,7 +113,7 @@ bool CallbackHandler::resolve_event(bool force) { CallbackHandler::resolving_event = true; CallbackHandler::events->pop_front(); - debug("Resolving an event. (%lu remaining)\n", + debug("Resolving an event. (%zu remaining)\n", CallbackHandler::events->size()); auto iterator = CallbackHandler::callbacks->find(event.topic); From b066f2f837fa5fa5d0807c176b2978bbbe865782 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 20:52:37 +0200 Subject: [PATCH 09/16] Remove unused variables from socket.h --- src/Utils/sockets.cpp | 1 - src/Utils/sockets.h | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Utils/sockets.cpp b/src/Utils/sockets.cpp index 386e66ac..9547ec2a 100644 --- a/src/Utils/sockets.cpp +++ b/src/Utils/sockets.cpp @@ -80,7 +80,6 @@ int listenForIncomingConnection(int socket_fd, struct sockaddr_in address) { Sink::Sink(FILE *out) { this->outStream = out; - this->outDescriptor = fileno(out); } int Sink::write(const char *fmt, ...) const { diff --git a/src/Utils/sockets.h b/src/Utils/sockets.h index f05f59cc..6b88dc49 100644 --- a/src/Utils/sockets.h +++ b/src/Utils/sockets.h @@ -18,8 +18,8 @@ int listenForIncomingConnection(int socket_fd, struct sockaddr_in address); class Channel { public: virtual void open() {} - virtual int write(char const *fmt, ...) const { return 0; } - virtual ssize_t read(void *out, size_t size) { return 0; } + virtual int write(char const *, ...) const { return 0; } + virtual ssize_t read(void *, size_t) { return 0; } virtual void close() {} virtual ~Channel() = default; }; @@ -27,7 +27,6 @@ class Channel { class Sink : public Channel { private: FILE *outStream; - int outDescriptor; public: explicit Sink(FILE *out); From d7e2e4caf2ff8028c010dba8f64dc2304cf21520 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 20:59:00 +0200 Subject: [PATCH 10/16] Mark some things as maybe_unused when only used in debug mode --- src/Interpreter/instructions.cpp | 4 +++- src/Interpreter/interpreter.cpp | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Interpreter/instructions.cpp b/src/Interpreter/instructions.cpp index 06886d17..9046c0d0 100644 --- a/src/Interpreter/instructions.cpp +++ b/src/Interpreter/instructions.cpp @@ -1261,7 +1261,9 @@ bool i_instr_conversion(Module *m, uint8_t opcode) { /** * 0xe0 ... 0xe3 callback operations */ -bool i_instr_callback(Module *m, uint8_t opcode) { +// TODO: Remove [[maybe_unused]] when implemented +bool i_instr_callback([[maybe_unused]] Module *m, + [[maybe_unused]] uint8_t opcode) { // TODO return true; } diff --git a/src/Interpreter/interpreter.cpp b/src/Interpreter/interpreter.cpp index e3f368ae..b5f3f4fe 100644 --- a/src/Interpreter/interpreter.cpp +++ b/src/Interpreter/interpreter.cpp @@ -461,7 +461,8 @@ bool Interpreter::interpret(Module *m, bool waiting) { return success; } -void Interpreter::report_overflow(Module *m, uint8_t *maddr) { +void Interpreter::report_overflow([[maybe_unused]] Module *m, + [[maybe_unused]] uint8_t *maddr) { dbg_warn("memory start: %p, memory end: %p, maddr: %p\n", m->memory.bytes, m->memory.bytes + m->memory.pages * (uint32_t)PAGE_SIZE, maddr); sprintf(exception, "out of bounds memory access"); From 92941aa01fbf0c2d61656c5e2a25fe5705641108 Mon Sep 17 00:00:00 2001 From: MaartenS11 Date: Wed, 9 Oct 2024 21:00:22 +0200 Subject: [PATCH 11/16] Reformat --- src/Utils/sockets.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Utils/sockets.cpp b/src/Utils/sockets.cpp index 9547ec2a..4affeebd 100644 --- a/src/Utils/sockets.cpp +++ b/src/Utils/sockets.cpp @@ -78,9 +78,7 @@ int listenForIncomingConnection(int socket_fd, struct sockaddr_in address) { } #endif -Sink::Sink(FILE *out) { - this->outStream = out; -} +Sink::Sink(FILE *out) { this->outStream = out; } int Sink::write(const char *fmt, ...) const { va_list args; From 581ad2e2fb0ec78c69f91dab77150c6e8c52db6b Mon Sep 17 00:00:00 2001 From: jay-tux Date: Wed, 9 Oct 2024 21:13:49 +0200 Subject: [PATCH 12/16] Fix compiler warnings in src/Debug/debugger.cpp --- src/Debug/debugger.cpp | 231 +++++++++++++++++++++-------------------- src/Debug/debugger.h | 48 ++++----- src/Utils/sockets.cpp | 6 +- src/Utils/sockets.h | 12 ++- 4 files changed, 156 insertions(+), 141 deletions(-) diff --git a/src/Debug/debugger.cpp b/src/Debug/debugger.cpp index 0e1aa087..054e8f7a 100644 --- a/src/Debug/debugger.cpp +++ b/src/Debug/debugger.cpp @@ -39,17 +39,16 @@ void Debugger::addDebugMessage(size_t len, const uint8_t *buff) { this->parsedInterrupts.pop(); if (*data == interruptRecvCallbackmapping) { size_t startIdx = 0; - size_t endIdx; while (buff[startIdx] != '7' || buff[startIdx + 1] != '5' || buff[startIdx + 2] != '{') { startIdx++; } - endIdx = startIdx; + size_t endIdx = startIdx; while (buff[endIdx] != '\n') { endIdx++; } - auto *msg = (uint8_t *)acalloc(sizeof(uint8_t), (endIdx - startIdx), - "interrupt buffer"); + auto *msg = static_cast(acalloc( + sizeof(uint8_t), (endIdx - startIdx), "interrupt buffer")); memcpy(msg, buff + startIdx, (endIdx - startIdx) * sizeof(uint8_t)); *msg = *data; free(data); @@ -70,7 +69,7 @@ void Debugger::pushMessage(uint8_t *msg) { void Debugger::parseDebugBuffer(size_t len, const uint8_t *buff) { for (size_t i = 0; i < len; i++) { bool success = true; - int r = -1 /*undef*/; + int r = 0; // TODO replace by real binary switch (buff[i]) { @@ -91,9 +90,10 @@ void Debugger::parseDebugBuffer(size_t len, const uint8_t *buff) { if (this->interruptEven) { if (!this->interruptBuffer.empty()) { // done, send to process - auto data = (uint8_t *)acalloc(sizeof(uint8_t), - this->interruptBuffer.size(), - "interrupt buffer"); + // TODO: pointer gets leaked! + auto data = static_cast( + acalloc(sizeof(uint8_t), this->interruptBuffer.size(), + "interrupt buffer")); memcpy(data, this->interruptBuffer.data(), this->interruptBuffer.size() * sizeof(uint8_t)); this->parsedInterrupts.push(data); @@ -107,10 +107,10 @@ void Debugger::parseDebugBuffer(size_t len, const uint8_t *buff) { } else { // good parse if (!this->interruptEven) { this->interruptLastChar = - (this->interruptLastChar << 4u) + (uint8_t)r; + (this->interruptLastChar << 4u) + static_cast(r); this->interruptBuffer.push_back(this->interruptLastChar); } else { - this->interruptLastChar = (uint8_t)r; + this->interruptLastChar = static_cast(r); } this->interruptEven = !this->interruptEven; } @@ -132,6 +132,7 @@ void Debugger::addBreakpoint(uint8_t *loc) { this->breakpoints.insert(loc); } void Debugger::deleteBreakpoint(uint8_t *loc) { this->breakpoints.erase(loc); } +// ReSharper disable once CppParameterMayBeConstPtrOrRef // incorrect warning bool Debugger::isBreakpoint(uint8_t *loc) { return this->breakpoints.find(loc) != this->breakpoints.end() || this->mark == loc; @@ -139,7 +140,7 @@ bool Debugger::isBreakpoint(uint8_t *loc) { void Debugger::notifyBreakpoint(Module *m, uint8_t *pc_ptr) { this->mark = nullptr; - uint32_t bp = toVirtualAddress(pc_ptr, m); + const uint32_t bp = toVirtualAddress(pc_ptr, m); this->channel->write("AT %" PRIu32 "!\n", bp); } @@ -237,7 +238,7 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) { free(interruptData); break; case interruptUPDATEModule: - this->handleUpdateModule(m, interruptData); + handleUpdateModule(m, interruptData); this->channel->write("CHANGE Module!\n"); free(interruptData); break; @@ -305,7 +306,8 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) { } case interruptDUMPAllEvents: debug("InterruptDUMPEvents\n"); - size = (long)CallbackHandler::event_count(); + size = static_cast(CallbackHandler::event_count()); + [[fallthrough]]; case interruptDUMPEvents: // TODO get start and size from message this->channel->write("{"); @@ -341,35 +343,38 @@ bool Debugger::checkDebugMessages(Module *m, RunningState *program_state) { } // Private methods -void Debugger::printValue(StackValue *v, uint32_t idx, bool end = false) const { +void Debugger::printValue(const StackValue *v, const uint32_t idx, + const bool end = false) const { char buff[256]; +#define FMT(fmt0) "%" fmt0 + switch (v->value_type) { case I32: - snprintf(buff, 255, R"("type":"i32","value":%)" PRIi32, + snprintf(buff, 255, R"("type":"i32","value":)" FMT(PRIi32), v->value.uint32); break; case I64: - snprintf(buff, 255, R"("type":"i64","value":%)" PRIi64, + snprintf(buff, 255, R"("type":"i64","value":)" FMT(PRIi64), v->value.uint64); break; case F32: - snprintf(buff, 255, R"("type":"F32","value":"%)" PRIx32 "\"", + snprintf(buff, 255, R"("type":"F32","value":")" FMT(PRIx32) "\"", v->value.uint32); break; case F64: - snprintf(buff, 255, R"("type":"F64","value":"%)" PRIx64 "\"", + snprintf(buff, 255, R"("type":"F64","value":")" FMT(PRIx64) "\"", v->value.uint64); break; default: - snprintf(buff, 255, R"("type":"%02x","value":"%)" PRIx64 "\"", + snprintf(buff, 255, R"("type":"%02x","value":")" FMT(PRIx64) "\"", v->value_type, v->value.uint64); } this->channel->write(R"({"idx":%d,%s}%s)", idx, buff, end ? "" : ","); } -uint8_t *Debugger::findOpcode(Module *m, Block *block) { - auto find = +uint8_t *Debugger::findOpcode(Module *m, const Block *block) { + const auto find = std::find_if(std::begin(m->block_lookup), std::end(m->block_lookup), [&](const std::pair &pair) { return pair.second == block; @@ -385,19 +390,19 @@ uint8_t *Debugger::findOpcode(Module *m, Block *block) { return opcode; } -void Debugger::handleInvoke(Module *m, uint8_t *interruptData) { - uint32_t fidx = read_LEB_32(&interruptData); +void Debugger::handleInvoke(Module *m, uint8_t *interruptData) const { + const uint32_t fidx = read_LEB_32(&interruptData); - if (fidx < 0 || fidx >= m->function_count) { + if (fidx >= m->function_count) { debug("no function available for fidx %" PRIi32 "\n", fidx); return; } - Type func = *m->functions[fidx].type; + const Type func = *m->functions[fidx].type; StackValue *args = readWasmArgs(func, interruptData); WARDuino *instance = WARDuino::instance(); - RunningState current = instance->program_state; + const RunningState current = instance->program_state; instance->program_state = WARDUINOrun; WARDuino::instance()->invoke(m, fidx, func.param_count, args); @@ -405,7 +410,8 @@ void Debugger::handleInvoke(Module *m, uint8_t *interruptData) { this->dumpStack(m); } -void Debugger::handleInterruptRUN(Module *m, RunningState *program_state) { +void Debugger::handleInterruptRUN(const Module *m, + RunningState *program_state) { this->channel->write("GO!\n"); if (*program_state == WARDUINOpause && this->isBreakpoint(m->pc_ptr)) { this->skipBreakpoint = m->pc_ptr; @@ -413,13 +419,13 @@ void Debugger::handleInterruptRUN(Module *m, RunningState *program_state) { *program_state = WARDUINOrun; } -void Debugger::handleSTEP(Module *m, RunningState *program_state) { +void Debugger::handleSTEP(const Module *m, RunningState *program_state) { this->channel->write("STEP!\n"); *program_state = WARDUINOstep; this->skipBreakpoint = m->pc_ptr; } -void Debugger::handleSTEPOver(Module *m, RunningState *program_state) { +void Debugger::handleSTEPOver(const Module *m, RunningState *program_state) { this->skipBreakpoint = m->pc_ptr; uint8_t const opcode = *m->pc_ptr; if (opcode == 0x10) { // step over direct call @@ -466,14 +472,14 @@ void Debugger::dump(Module *m, bool full) const { this->channel->write(R"(, "locals": )"); this->dumpLocals(m); this->channel->write(", "); - this->dumpEvents(0, CallbackHandler::event_count()); + this->dumpEvents(0, static_cast(CallbackHandler::event_count())); } this->channel->write("}\n\n"); // fflush(stdout); } -void Debugger::dumpStack(Module *m) const { +void Debugger::dumpStack(const Module *m) const { this->channel->write("{\"stack\": ["); int32_t i = m->sp; while (0 <= i) { @@ -514,15 +520,15 @@ void Debugger::dumpCallstack(Module *m) const { auto toVA = [m](uint8_t *addr) { return toVirtualAddress(addr, m); }; this->channel->write("\"callstack\":["); for (int i = 0; i <= m->csp; i++) { - Frame *f = &m->callstack[i]; - uint8_t *callsite = nullptr; + const Frame *f = &m->callstack[i]; int callsite_retaddr = -1; int retaddr = -1; // first frame has no retrun address if (f->ra_ptr != nullptr) { + uint8_t *callsite = nullptr; callsite = f->ra_ptr - 2; // callsite of function (if type 0) - callsite_retaddr = toVA(callsite); - retaddr = toVA(f->ra_ptr); + callsite_retaddr = static_cast(toVA(callsite)); + retaddr = static_cast(toVA(f->ra_ptr)); } this->channel->write(R"({"type":%u,"fidx":"0x%x","sp":%d,"fp":%d,)", f->block->block_type, f->block->fidx, f->sp, @@ -534,7 +540,7 @@ void Debugger::dumpCallstack(Module *m) const { } } -void Debugger::dumpLocals(Module *m) const { +void Debugger::dumpLocals(const Module *m) const { // fflush(stdout); int firstFunFramePtr = m->csp; while (m->callstack[firstFunFramePtr].block->block_type != 0) { @@ -546,16 +552,16 @@ void Debugger::dumpLocals(Module *m) const { Frame *f = &m->callstack[firstFunFramePtr]; this->channel->write(R"({"count":%u,"locals":[)", f->block->local_count); // fflush(stdout); // FIXME: this is needed for ESP to properly print - char _value_str[256]; for (uint32_t i = 0; i < f->block->local_count; i++) { + char _value_str[256]; auto v = &m->stack[m->fp + i]; switch (v->value_type) { case I32: - snprintf(_value_str, 255, R"("type":"i32","value":%)" PRIi32, + snprintf(_value_str, 255, R"("type":"i32","value":)" FMT(PRIi32), v->value.uint32); break; case I64: - snprintf(_value_str, 255, R"("type":"i64","value":%)" PRIi64, + snprintf(_value_str, 255, R"("type":"i64","value":)" FMT(PRIi64), v->value.uint64); break; case F32: @@ -568,7 +574,7 @@ void Debugger::dumpLocals(Module *m) const { break; default: snprintf(_value_str, 255, - R"("type":"%02x","value":"%)" PRIx64 "\"", + R"("type":"%02x","value":")" FMT(PRIx64) "\"", v->value_type, v->value.uint64); } @@ -578,6 +584,7 @@ void Debugger::dumpLocals(Module *m) const { } this->channel->write("]}"); // fflush(stdout); +#undef FMT } void Debugger::dumpEvents(long start, long size) const { @@ -615,7 +622,7 @@ void Debugger::dumpCallbackmapping() const { * [0x10, index, ... new function body 0x0b] * Where index is the index without imports */ -bool Debugger::handleChangedFunction(Module *m, uint8_t *bytes) { +bool Debugger::handleChangedFunction(const Module *m, uint8_t *bytes) { // Check if this was a change request if (*bytes != interruptUPDATEFun) return false; @@ -625,16 +632,15 @@ bool Debugger::handleChangedFunction(Module *m, uint8_t *bytes) { uint32_t b = read_LEB_32(&pos); // read id Block *function = &m->functions[m->import_count + b]; - uint32_t body_size = read_LEB_32(&pos); + const uint32_t body_size = read_LEB_32(&pos); uint8_t *payload_start = pos; - uint32_t local_count = read_LEB_32(&pos); - uint8_t *save_pos; + const uint32_t local_count = read_LEB_32(&pos); + uint8_t *save_pos = pos; uint32_t tidx, lidx, lecount; // Local variable handling // Get number of locals for alloc - save_pos = pos; function->local_count = 0; for (uint32_t l = 0; l < local_count; l++) { lecount = read_LEB_32(&pos); @@ -645,8 +651,9 @@ bool Debugger::handleChangedFunction(Module *m, uint8_t *bytes) { if (function->local_count > 0) { function->local_value_type = - (uint8_t *)acalloc(function->local_count, sizeof(uint8_t), - "function->local_value_type"); + static_cast( + acalloc(function->local_count, sizeof(uint8_t), + "function->local_value_type")); } // Restore position and read the locals @@ -674,7 +681,7 @@ bool Debugger::handleChangedFunction(Module *m, uint8_t *bytes) { * @param bytes * @return */ -bool Debugger::handleChangedLocal(Module *m, uint8_t *bytes) const { +bool Debugger::handleChangedLocal(const Module *m, uint8_t *bytes) const { if (*bytes != interruptUPDATELocal) return false; uint8_t *pos = bytes + 1; this->channel->write("Local updates: %x\n", *pos); @@ -687,7 +694,7 @@ bool Debugger::handleChangedLocal(Module *m, uint8_t *bytes) const { v->value.uint32 = read_LEB_signed(&pos, 32); break; case I64: - v->value.int64 = read_LEB_signed(&pos, 64); + v->value.int64 = static_cast(read_LEB_signed(&pos, 64)); break; case F32: memcpy(&v->value.uint32, pos, 4); @@ -695,6 +702,8 @@ bool Debugger::handleChangedLocal(Module *m, uint8_t *bytes) const { case F64: memcpy(&v->value.uint64, pos, 8); break; + default: // nothing to do :( + break; } this->channel->write("Local %u changed to %u\n", localId, v->value.uint32); return true; @@ -714,7 +723,7 @@ bool Debugger::handlePushedEvent(char *bytes) const { return true; } -void Debugger::snapshot(Module *m) { +void Debugger::snapshot(Module *m) const { uint16_t numberBytes = 11; uint8_t state[] = {pcState, breakpointsState, @@ -730,7 +739,8 @@ void Debugger::snapshot(Module *m) { inspect(m, numberBytes, state); } -void Debugger::inspect(Module *m, uint16_t sizeStateArray, uint8_t *state) { +void Debugger::inspect(Module *m, const uint16_t sizeStateArray, + const uint8_t *state) const { debug("asked for inspect\n"); uint16_t idx = 0; auto toVA = [m](uint8_t *addr) { return toVirtualAddress(addr, m); }; @@ -763,13 +773,13 @@ void Debugger::inspect(Module *m, uint16_t sizeStateArray, uint8_t *state) { this->channel->write("%s\"callstack\":[", addComma ? "," : ""); addComma = true; for (int j = 0; j <= m->csp; j++) { - Frame *f = &m->callstack[j]; - uint8_t bt = f->block->block_type; - uint32_t block_key = (bt == 0 || bt == 0xff || bt == 0xfe) + const Frame *f = &m->callstack[j]; + const uint8_t bt = f->block->block_type; + const uint32_t block_key = (bt == 0 || bt == 0xff || bt == 0xfe) ? 0 : toVA(findOpcode(m, f->block)); - uint32_t fidx = bt == 0 ? f->block->fidx : 0; - int ra = f->ra_ptr == nullptr ? -1 : toVA(f->ra_ptr); + const uint32_t fidx = bt == 0 ? f->block->fidx : 0; + const auto ra = f->ra_ptr == nullptr ? -1 : toVA(f->ra_ptr); this->channel->write( R"({"type":%u,"fidx":"0x%x","sp":%d,"fp":%d,"idx":%d,)", bt, fidx, f->sp, f->fp, j); @@ -824,7 +834,7 @@ void Debugger::inspect(Module *m, uint16_t sizeStateArray, uint8_t *state) { break; } case memoryState: { - uint32_t total_elems = m->memory.pages * (uint32_t)PAGE_SIZE; + uint32_t total_elems = m->memory.pages * static_cast(PAGE_SIZE); this->channel->write( R"(%s"memory":{"pages":%d,"max":%d,"init":%d,"bytes":[)", addComma ? "," : "", m->memory.pages, m->memory.maximum, @@ -847,7 +857,7 @@ void Debugger::inspect(Module *m, uint16_t sizeStateArray, uint8_t *state) { } case eventsState: { this->channel->write("%s", addComma ? "," : ""); - this->dumpEvents(0, CallbackHandler::event_count()); + this->dumpEvents(0, static_cast(CallbackHandler::event_count())); addComma = true; break; } @@ -880,11 +890,11 @@ void Debugger::inspect(Module *m, uint16_t sizeStateArray, uint8_t *state) { this->channel->write("}\n"); } -void Debugger::enableSnapshots(uint8_t *interruptData) { +void Debugger::enableSnapshots(const uint8_t *interruptData) { asyncSnapshots = *interruptData; } -void Debugger::sendAsyncSnapshots(Module *m) { +void Debugger::sendAsyncSnapshots(Module *m) const { if (asyncSnapshots) { this->channel->write("SNAPSHOT "); snapshot(m); @@ -915,8 +925,8 @@ void Debugger::freeState(Module *m, uint8_t *interruptData) { debug("globals freeing state and then allocating\n"); if (m->global_count > 0) free(m->globals); if (amount > 0) - m->globals = (StackValue *)acalloc( - amount, sizeof(StackValue), "globals"); + m->globals = static_cast( + acalloc(amount, sizeof(StackValue), "globals")); } else { debug("globals setting existing state to zero\n"); for (uint32_t i = 0; i < m->global_count; i++) { @@ -939,8 +949,8 @@ void Debugger::freeState(Module *m, uint8_t *interruptData) { if (m->table.size != size) { debug("old table size %d\n", m->table.size); if (m->table.size != 0) free(m->table.entries); - m->table.entries = (uint32_t *)acalloc( - size, sizeof(uint32_t), "Module->table.entries"); + m->table.entries = static_cast(acalloc( + size, sizeof(uint32_t), "Module->table.entries")); } m->table.size = 0; // allows to accumulatively add entries break; @@ -958,8 +968,8 @@ void Debugger::freeState(Module *m, uint8_t *interruptData) { if (m->memory.bytes != nullptr) { free(m->memory.bytes); } - m->memory.bytes = (uint8_t *)acalloc(pages * PAGE_SIZE, 1, - "Module->memory.bytes"); + m->memory.bytes = static_cast( + acalloc(pages * PAGE_SIZE, 1, "Module->memory.bytes")); m->memory.pages = pages; // } // else{ @@ -968,10 +978,8 @@ void Debugger::freeState(Module *m, uint8_t *interruptData) { // } break; } - default: { + default: FATAL("freeState: receiving unknown command\n"); - break; - } } } debug("done with first msg\n"); @@ -1036,7 +1044,7 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { m->fp = f->sp + 1; } else if (block_type == 0xff || block_type == 0xfe) { debug("guard block %" PRIu8 "\n", block_type); - auto *guard = (Block *)malloc(sizeof(struct Block)); + auto *guard = static_cast(malloc(sizeof(struct Block))); guard->block_type = block_type; guard->type = nullptr; guard->local_value_type = nullptr; @@ -1103,7 +1111,7 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { } uint32_t total_bytes = limit - start + 1; uint8_t *mem_end = - m->memory.bytes + m->memory.pages * (uint32_t)PAGE_SIZE; + m->memory.bytes + m->memory.pages * static_cast(PAGE_SIZE); debug("will copy #%" PRIu32 " bytes from %" PRIu32 " to %" PRIu32 " (incl.)\n", total_bytes, start, limit); @@ -1162,16 +1170,17 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { } case callbacksState: { uint32_t numberMappings = read_B32(&program_state); - for (auto idx = 0; idx < numberMappings; ++idx) { + for (auto idx = 0u; idx < numberMappings; ++idx) { uint32_t callbackKeySize = read_B32(&program_state); - char *callbackKey = (char *)malloc(callbackKeySize + 1); - memcpy((void *)callbackKey, program_state, callbackKeySize); + auto *callbackKey = static_cast(malloc(callbackKeySize + 1)); + memcpy(callbackKey, program_state, callbackKeySize); callbackKey[callbackKeySize] = '\0'; program_state += callbackKeySize; + std::string key{callbackKey}; + free(callbackKey); uint32_t numberTableIndexes = read_B32(&program_state); - for (auto j = 0; j < numberTableIndexes; ++j) { + for (auto j = 0u; j < numberTableIndexes; ++j) { uint32_t tidx = read_B32(&program_state); - std::string key{callbackKey}; CallbackHandler::add_callback(Callback(m, key, tidx)); } } @@ -1179,22 +1188,23 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { } case eventsState: { uint32_t numberEvents = read_B32(&program_state); - for (auto idx = 0; idx < numberEvents; ++idx) { + for (auto idx = 0u; idx < numberEvents; ++idx) { // read topic uint32_t topicSize = read_B32(&program_state); - char *topic = (char *)malloc(topicSize + 1); - memcpy((void *)topic, program_state, topicSize); + auto *topic = static_cast(malloc(topicSize + 1)); + memcpy(topic, program_state, topicSize); topic[topicSize] = '\0'; program_state += topicSize; // read payload uint32_t payloadSize = read_B32(&program_state); - char *payload = (char *)malloc(payloadSize + 1); - memcpy((void *)payload, program_state, payloadSize); + auto *payload = static_cast(malloc(payloadSize + 1)); + memcpy(payload, program_state, payloadSize); payload[payloadSize] = '\0'; program_state += payloadSize; CallbackHandler::push_event(topic, payload, payloadSize); + free(topic); } break; } @@ -1206,13 +1216,13 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { for (int i = 0; i < io_state_count; i++) { IOStateElement state_elem; state_elem.key = ""; - char c = (char)*program_state++; + char c = static_cast(*program_state++); while (c != '\0') { state_elem.key += c; - c = (char)*program_state++; + c = static_cast(*program_state++); } state_elem.output = *program_state++; - state_elem.value = (int)read_B32(&program_state); + state_elem.value = static_cast(read_B32(&program_state)); external_state.emplace_back(state_elem); debug("pin %s(%s) = %d\n", state_elem.key.c_str(), state_elem.output ? "output" : "input", @@ -1226,12 +1236,12 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { } } } - auto done = (uint8_t)*program_state; - return done == (uint8_t)1; + auto done = *program_state; + return done == static_cast(1); } uintptr_t Debugger::readPointer(uint8_t **data) { - uint8_t len = (*data)[0]; + const uint8_t len = (*data)[0]; uintptr_t bp = 0x0; for (size_t i = 0; i < len; i++) { bp <<= sizeof(uint8_t) * 8; @@ -1246,8 +1256,8 @@ void Debugger::proxify() { this->proxy = new Proxy(); // TODO delete } -void Debugger::handleProxyCall(Module *m, RunningState *program_state, - uint8_t *interruptData) { +void Debugger::handleProxyCall(Module *m, RunningState *, + uint8_t *interruptData) const { if (this->proxy == nullptr) { dbg_info("No proxy available to send proxy call to.\n"); // TODO how to handle this error? @@ -1265,14 +1275,14 @@ void Debugger::handleProxyCall(Module *m, RunningState *program_state, this->proxy->pushRFC(m, rfc); } -RFC *Debugger::topProxyCall() { +RFC *Debugger::topProxyCall() const { if (proxy == nullptr) { return nullptr; } return this->proxy->topRFC(); } -void Debugger::sendProxyCallResult(Module *m) { +void Debugger::sendProxyCallResult(Module *m) const { if (proxy == nullptr) { return; } @@ -1281,17 +1291,17 @@ void Debugger::sendProxyCallResult(Module *m) { bool Debugger::isProxy() const { return this->proxy != nullptr; } -bool Debugger::isProxied(uint32_t fidx) const { +bool Debugger::isProxied(const uint32_t fidx) const { return this->supervisor != nullptr && this->supervisor->isProxied(fidx); } -void Debugger::handleMonitorProxies(Module *m, uint8_t *interruptData) { - uint32_t amount_funcs = read_B32(&interruptData); +void Debugger::handleMonitorProxies(const Module *m, uint8_t *interruptData) const { + const uint32_t amount_funcs = read_B32(&interruptData); printf("funcs_total %" PRIu32 "\n", amount_funcs); m->warduino->debugger->supervisor->unregisterAllProxiedCalls(); for (uint32_t i = 0; i < amount_funcs; i++) { - uint32_t fidx = read_B32(&interruptData); + const uint32_t fidx = read_B32(&interruptData); printf("registering fid=%" PRIu32 "\n", fidx); m->warduino->debugger->supervisor->registerProxiedCall(fidx); } @@ -1307,7 +1317,7 @@ void Debugger::startProxySupervisor(Channel *socket) { bool Debugger::proxy_connected() const { return this->connected_to_proxy; } -void Debugger::disconnect_proxy() { +void Debugger::disconnect_proxy() const { if (!this->proxy_connected()) { return; } @@ -1316,8 +1326,8 @@ void Debugger::disconnect_proxy() { this->supervisor->thread.join(); } -void Debugger::updateCallbackmapping(Module *m, const char *data) { - nlohmann::basic_json<> parsed = nlohmann::json::parse(data); +void Debugger::updateCallbackmapping(Module *m, const char *interruptData) { + nlohmann::basic_json<> parsed = nlohmann::json::parse(interruptData); CallbackHandler::clear_callbacks(); nlohmann::basic_json<> callbacks = *parsed.find("callbacks"); for (auto &array : callbacks.items()) { @@ -1338,51 +1348,52 @@ void Debugger::stop() { } // -void Debugger::pauseRuntime(Module *m) { +void Debugger::pauseRuntime(const Module *m) { m->warduino->program_state = WARDUINOpause; this->mark = nullptr; } bool Debugger::handleUpdateModule(Module *m, uint8_t *data) { uint8_t *wasm_data = data + 1; - uint32_t wasm_len = read_LEB_32(&wasm_data); - uint8_t *wasm = (uint8_t *)malloc(sizeof(uint8_t) * wasm_len); + const uint32_t wasm_len = read_LEB_32(&wasm_data); + auto *wasm = static_cast(malloc(sizeof(uint8_t) * wasm_len)); memcpy(wasm, wasm_data, wasm_len); WARDuino *wd = m->warduino; wd->update_module(m, wasm, wasm_len); return true; } -bool Debugger::handleUpdateGlobalValue(Module *m, uint8_t *data) { +bool Debugger::handleUpdateGlobalValue(const Module *m, uint8_t *data) const { this->channel->write("Global updates: %x\n", *data); - uint32_t index = read_LEB_32(&data); + const uint32_t index = read_LEB_32(&data); if (index >= m->global_count) return false; this->channel->write("Global %u being changed\n", index); StackValue *v = &m->globals[index]; - bool decodeType = false; + constexpr bool decodeType = false; deserialiseStackValue(data, decodeType, v); this->channel->write("Global %u changed to %u\n", index, v->value.uint32); return true; } -bool Debugger::handleUpdateStackValue(Module *m, uint8_t *data) { - uint32_t idx = read_LEB_32(&data); +bool Debugger::handleUpdateStackValue(const Module *m, uint8_t *bytes) const { + const uint32_t idx = read_LEB_32(&bytes); if (idx >= STACK_SIZE) { return false; } StackValue *sv = &m->stack[idx]; - bool decodeType = false; - if (!deserialiseStackValue(data, decodeType, sv)) { + // ReSharper disable once CppTooWideScopeInitStatement + constexpr bool decodeType = false; + if (!deserialiseStackValue(bytes, decodeType, sv)) { return false; } this->channel->write("StackValue %" PRIu32 " changed\n", idx); return true; } -bool Debugger::reset(Module *m) { - auto wasm = (uint8_t *)malloc(sizeof(uint8_t) * m->byte_count); +bool Debugger::reset(Module *m) const { + auto *wasm = static_cast(malloc(sizeof(uint8_t) * m->byte_count)); memcpy(wasm, m->bytes, m->byte_count); m->warduino->update_module(m, wasm, m->byte_count); this->channel->write("Reset WARDuino.\n"); diff --git a/src/Debug/debugger.h b/src/Debug/debugger.h index 444d03be..2b7e80e0 100644 --- a/src/Debug/debugger.h +++ b/src/Debug/debugger.h @@ -119,7 +119,7 @@ class Debugger { // Private methods - void printValue(StackValue *v, uint32_t idx, bool end) const; + void printValue(const StackValue *v, uint32_t idx, bool end) const; // TODO Move parsing to WARDuino class? void parseDebugBuffer(size_t len, const uint8_t *buff); @@ -128,15 +128,15 @@ class Debugger { //// Handle REPL interrupts - void handleInvoke(Module *m, uint8_t *interruptData); + void handleInvoke(Module *m, uint8_t *interruptData) const; //// Handle Interrupt Types - void handleInterruptRUN(Module *m, RunningState *program_state); + void handleInterruptRUN(const Module *m, RunningState *program_state); - void handleSTEP(Module *m, RunningState *program_state); + void handleSTEP(const Module *m, RunningState *program_state); - void handleSTEPOver(Module *m, RunningState *program_state); + void handleSTEPOver(const Module *m, RunningState *program_state); void handleInterruptBP(Module *m, uint8_t *interruptData); @@ -144,9 +144,9 @@ class Debugger { void dump(Module *m, bool full = false) const; - void dumpStack(Module *m) const; + void dumpStack(const Module *m) const; - void dumpLocals(Module *m) const; + void dumpLocals(const Module *m) const; void dumpBreakpoints(Module *m) const; @@ -158,27 +158,27 @@ class Debugger { void dumpCallbackmapping() const; - void inspect(Module *m, uint16_t sizeStateArray, uint8_t *state); + void inspect(Module *m, uint16_t sizeStateArray, const uint8_t *state) const; //// Handle live code update - static bool handleChangedFunction(Module *m, uint8_t *bytes); + static bool handleChangedFunction(const Module *m, uint8_t *bytes); - bool handleChangedLocal(Module *m, uint8_t *bytes) const; + bool handleChangedLocal(const Module *m, uint8_t *bytes) const; - bool handleUpdateModule(Module *m, uint8_t *data); + static bool handleUpdateModule(Module *m, uint8_t *data); - bool handleUpdateGlobalValue(Module *m, uint8_t *data); + bool handleUpdateGlobalValue(const Module *m, uint8_t *data) const; - bool handleUpdateStackValue(Module *m, uint8_t *bytes); + bool handleUpdateStackValue(const Module *m, uint8_t *bytes) const; - bool reset(Module *m); + bool reset(Module *m) const; //// Handle out-of-place debugging void freeState(Module *m, uint8_t *interruptData); - static uint8_t *findOpcode(Module *m, Block *block); + static uint8_t *findOpcode(Module *m, const Block *block); bool saveState(Module *m, uint8_t *interruptData); @@ -213,7 +213,7 @@ class Debugger { void stop(); - void pauseRuntime(Module *m); // pause runtime for given module + void pauseRuntime(const Module *m); // pause runtime for given module // Interrupts @@ -235,20 +235,20 @@ class Debugger { // Out-of-place debugging: EDWARD - void snapshot(Module *m); + void snapshot(Module *m) const; - void enableSnapshots(uint8_t *interruptData); + void enableSnapshots(const uint8_t *interruptData); - void sendAsyncSnapshots(Module *m); + void sendAsyncSnapshots(Module *m) const; void proxify(); void handleProxyCall(Module *m, RunningState *program_state, - uint8_t *interruptData); + uint8_t *interruptData) const; - RFC *topProxyCall(); + RFC *topProxyCall() const; - void sendProxyCallResult(Module *m); + void sendProxyCallResult(Module *m) const; bool isProxy() const; @@ -258,11 +258,11 @@ class Debugger { bool proxy_connected() const; - void disconnect_proxy(); + void disconnect_proxy() const; // Pull-based - void handleMonitorProxies(Module *m, uint8_t *interruptData); + void handleMonitorProxies(const Module *m, uint8_t *interruptData) const; // Push-based diff --git a/src/Utils/sockets.cpp b/src/Utils/sockets.cpp index 386e66ac..bd85a3bf 100644 --- a/src/Utils/sockets.cpp +++ b/src/Utils/sockets.cpp @@ -83,7 +83,7 @@ Sink::Sink(FILE *out) { this->outDescriptor = fileno(out); } -int Sink::write(const char *fmt, ...) const { +int Sink::write(const char *fmt, ...) { va_list args; va_start(args, fmt); int written = vfprintf(this->outStream, fmt, args); @@ -105,7 +105,7 @@ FileDescriptorChannel::FileDescriptorChannel(int fileDescriptor) { this->fd = fileDescriptor; } -int FileDescriptorChannel::write(const char *fmt, ...) const { +int FileDescriptorChannel::write(const char *fmt, ...) { va_list args; va_start(args, fmt); int written = vdprintf(this->fd, fmt, args); @@ -151,7 +151,7 @@ void ClientSocket::open() { this->socket = this->fileDescriptor; } -int WebSocket::write(const char *fmt, ...) const { +int WebSocket::write(const char *fmt, ...) { if (this->socket < 0) { return 0; } diff --git a/src/Utils/sockets.h b/src/Utils/sockets.h index f05f59cc..aa34d8ae 100644 --- a/src/Utils/sockets.h +++ b/src/Utils/sockets.h @@ -18,12 +18,16 @@ int listenForIncomingConnection(int socket_fd, struct sockaddr_in address); class Channel { public: virtual void open() {} - virtual int write(char const *fmt, ...) const { return 0; } + virtual int write(char const *fmt, ...) { return 0; } virtual ssize_t read(void *out, size_t size) { return 0; } virtual void close() {} virtual ~Channel() = default; }; +// note: Channel::write could be a const function, but since it's a proxy +// for writing to an output stream, it shouldn't be const (this also eliminates +// a lot of "expression result unused" warnings). + class Sink : public Channel { private: FILE *outStream; @@ -31,7 +35,7 @@ class Sink : public Channel { public: explicit Sink(FILE *out); - int write(char const *fmt, ...) const override; + int write(char const *fmt, ...) override; }; class Duplex : public Sink { @@ -51,7 +55,7 @@ class FileDescriptorChannel : public Channel { public: explicit FileDescriptorChannel(int fileDescriptor); - int write(char const *fmt, ...) const override; + int write(char const *fmt, ...) override; ssize_t read(void *out, size_t size) override; }; @@ -65,7 +69,7 @@ class WebSocket : public Channel { explicit WebSocket(int port); void open() override; - int write(char const *fmt, ...) const override; + int write(char const *fmt, ...) override; ssize_t read(void *out, size_t size) override; void close() override; }; From 3925265821ee7c8c2f9964087ea109156a11636f Mon Sep 17 00:00:00 2001 From: jay-tux Date: Wed, 9 Oct 2024 21:19:00 +0200 Subject: [PATCH 13/16] Ran clang-format --- src/Debug/debugger.cpp | 45 +++++++++++++++++++++++-------------- src/Primitives/emulated.cpp | 16 +++++++------ 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/Debug/debugger.cpp b/src/Debug/debugger.cpp index 054e8f7a..8828f2fe 100644 --- a/src/Debug/debugger.cpp +++ b/src/Debug/debugger.cpp @@ -557,11 +557,13 @@ void Debugger::dumpLocals(const Module *m) const { auto v = &m->stack[m->fp + i]; switch (v->value_type) { case I32: - snprintf(_value_str, 255, R"("type":"i32","value":)" FMT(PRIi32), + snprintf(_value_str, 255, + R"("type":"i32","value":)" FMT(PRIi32), v->value.uint32); break; case I64: - snprintf(_value_str, 255, R"("type":"i64","value":)" FMT(PRIi64), + snprintf(_value_str, 255, + R"("type":"i64","value":)" FMT(PRIi64), v->value.uint64); break; case F32: @@ -650,8 +652,7 @@ bool Debugger::handleChangedFunction(const Module *m, uint8_t *bytes) { } if (function->local_count > 0) { - function->local_value_type = - static_cast( + function->local_value_type = static_cast( acalloc(function->local_count, sizeof(uint8_t), "function->local_value_type")); } @@ -702,7 +703,7 @@ bool Debugger::handleChangedLocal(const Module *m, uint8_t *bytes) const { case F64: memcpy(&v->value.uint64, pos, 8); break; - default: // nothing to do :( + default: // nothing to do :( break; } this->channel->write("Local %u changed to %u\n", localId, v->value.uint32); @@ -775,9 +776,10 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray, for (int j = 0; j <= m->csp; j++) { const Frame *f = &m->callstack[j]; const uint8_t bt = f->block->block_type; - const uint32_t block_key = (bt == 0 || bt == 0xff || bt == 0xfe) - ? 0 - : toVA(findOpcode(m, f->block)); + const uint32_t block_key = + (bt == 0 || bt == 0xff || bt == 0xfe) + ? 0 + : toVA(findOpcode(m, f->block)); const uint32_t fidx = bt == 0 ? f->block->fidx : 0; const auto ra = f->ra_ptr == nullptr ? -1 : toVA(f->ra_ptr); this->channel->write( @@ -834,7 +836,8 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray, break; } case memoryState: { - uint32_t total_elems = m->memory.pages * static_cast(PAGE_SIZE); + uint32_t total_elems = + m->memory.pages * static_cast(PAGE_SIZE); this->channel->write( R"(%s"memory":{"pages":%d,"max":%d,"init":%d,"bytes":[)", addComma ? "," : "", m->memory.pages, m->memory.maximum, @@ -857,7 +860,8 @@ void Debugger::inspect(Module *m, const uint16_t sizeStateArray, } case eventsState: { this->channel->write("%s", addComma ? "," : ""); - this->dumpEvents(0, static_cast(CallbackHandler::event_count())); + this->dumpEvents( + 0, static_cast(CallbackHandler::event_count())); addComma = true; break; } @@ -1044,7 +1048,8 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { m->fp = f->sp + 1; } else if (block_type == 0xff || block_type == 0xfe) { debug("guard block %" PRIu8 "\n", block_type); - auto *guard = static_cast(malloc(sizeof(struct Block))); + auto *guard = + static_cast(malloc(sizeof(struct Block))); guard->block_type = block_type; guard->type = nullptr; guard->local_value_type = nullptr; @@ -1111,7 +1116,8 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { } uint32_t total_bytes = limit - start + 1; uint8_t *mem_end = - m->memory.bytes + m->memory.pages * static_cast(PAGE_SIZE); + m->memory.bytes + + m->memory.pages * static_cast(PAGE_SIZE); debug("will copy #%" PRIu32 " bytes from %" PRIu32 " to %" PRIu32 " (incl.)\n", total_bytes, start, limit); @@ -1172,7 +1178,8 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { uint32_t numberMappings = read_B32(&program_state); for (auto idx = 0u; idx < numberMappings; ++idx) { uint32_t callbackKeySize = read_B32(&program_state); - auto *callbackKey = static_cast(malloc(callbackKeySize + 1)); + auto *callbackKey = + static_cast(malloc(callbackKeySize + 1)); memcpy(callbackKey, program_state, callbackKeySize); callbackKey[callbackKeySize] = '\0'; program_state += callbackKeySize; @@ -1198,7 +1205,8 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { // read payload uint32_t payloadSize = read_B32(&program_state); - auto *payload = static_cast(malloc(payloadSize + 1)); + auto *payload = + static_cast(malloc(payloadSize + 1)); memcpy(payload, program_state, payloadSize); payload[payloadSize] = '\0'; program_state += payloadSize; @@ -1222,7 +1230,8 @@ bool Debugger::saveState(Module *m, uint8_t *interruptData) { c = static_cast(*program_state++); } state_elem.output = *program_state++; - state_elem.value = static_cast(read_B32(&program_state)); + state_elem.value = + static_cast(read_B32(&program_state)); external_state.emplace_back(state_elem); debug("pin %s(%s) = %d\n", state_elem.key.c_str(), state_elem.output ? "output" : "input", @@ -1295,7 +1304,8 @@ bool Debugger::isProxied(const uint32_t fidx) const { return this->supervisor != nullptr && this->supervisor->isProxied(fidx); } -void Debugger::handleMonitorProxies(const Module *m, uint8_t *interruptData) const { +void Debugger::handleMonitorProxies(const Module *m, + uint8_t *interruptData) const { const uint32_t amount_funcs = read_B32(&interruptData); printf("funcs_total %" PRIu32 "\n", amount_funcs); @@ -1393,7 +1403,8 @@ bool Debugger::handleUpdateStackValue(const Module *m, uint8_t *bytes) const { } bool Debugger::reset(Module *m) const { - auto *wasm = static_cast(malloc(sizeof(uint8_t) * m->byte_count)); + auto *wasm = + static_cast(malloc(sizeof(uint8_t) * m->byte_count)); memcpy(wasm, m->bytes, m->byte_count); m->warduino->update_module(m, wasm, m->byte_count); this->channel->write("Reset WARDuino.\n"); diff --git a/src/Primitives/emulated.cpp b/src/Primitives/emulated.cpp index e429350f..1b2c2345 100644 --- a/src/Primitives/emulated.cpp +++ b/src/Primitives/emulated.cpp @@ -57,20 +57,20 @@ double sensor_emu = 0; #define install_primitive_reverse(prim_name) \ { \ PrimitiveEntry *p = &primitives[prim_index - 1]; \ - p->f_reverse = &(prim_name## _reverse); \ - p->f_serialize_state = &(prim_name## _serialize); \ + p->f_reverse = &(prim_name##_reverse); \ + p->f_serialize_state = &(prim_name##_serialize); \ } #define def_prim(function_name, type) \ - Type function_name## _type = type; \ + Type function_name##_type = type; \ bool function_name([[maybe_unused]] Module *m) #define def_prim_reverse(function_name) \ - void function_name## _reverse(Module *m, \ + void function_name##_reverse(Module *m, \ std::vector external_state) #define def_prim_serialize(function_name) \ - void function_name## _serialize( \ + void function_name##_serialize( \ std::vector &external_state) // TODO: use fp @@ -270,7 +270,8 @@ def_prim(test, oneToNoneU32) { Callback c = Callback(m, topic, fidx); CallbackHandler::add_callback(c); auto *payload = reinterpret_cast("TestPayload"); - CallbackHandler::push_event(topic, reinterpret_cast(payload), 11); + CallbackHandler::push_event(topic, reinterpret_cast(payload), + 11); pop_args(1); return true; } @@ -345,7 +346,8 @@ def_prim(http_get, fourToOneU32) { return false; // TRAP } for (unsigned long i = 0; i < answer.length(); i++) { - m->memory.bytes[response + i] = *reinterpret_cast(&answer[i]); + m->memory.bytes[response + i] = + *reinterpret_cast(&answer[i]); } // Pop args and return response address From 0c0d8befd23c8e869c549b6cdfe35f0813092e4f Mon Sep 17 00:00:00 2001 From: jay-tux Date: Wed, 9 Oct 2024 21:20:10 +0200 Subject: [PATCH 14/16] Even more clang-format --- src/Debug/debugger.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Debug/debugger.h b/src/Debug/debugger.h index 2b7e80e0..0dbb1cca 100644 --- a/src/Debug/debugger.h +++ b/src/Debug/debugger.h @@ -158,7 +158,8 @@ class Debugger { void dumpCallbackmapping() const; - void inspect(Module *m, uint16_t sizeStateArray, const uint8_t *state) const; + void inspect(Module *m, uint16_t sizeStateArray, + const uint8_t *state) const; //// Handle live code update From f55fb93e3d7c500bcf9acaa7c21c006968efc308 Mon Sep 17 00:00:00 2001 From: jay-tux Date: Wed, 9 Oct 2024 21:31:15 +0200 Subject: [PATCH 15/16] Fix linker errors for obscure platforms --- src/Primitives/arduino.cpp | 2 +- src/Primitives/idf.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Primitives/arduino.cpp b/src/Primitives/arduino.cpp index 4a946d33..cd7983b9 100644 --- a/src/Primitives/arduino.cpp +++ b/src/Primitives/arduino.cpp @@ -1079,7 +1079,7 @@ bool resolve_external_memory(char *symbol, Memory **val) { // Restore external state when restoring a snapshot //------------------------------------------------------ void restore_external_state(Module *m, - std::vector external_state) { + const std::vector &external_state) { uint8_t opcode = *m->pc_ptr; // TODO: Maybe primitives can also be called using the other call // instructions such as call_indirect diff --git a/src/Primitives/idf.cpp b/src/Primitives/idf.cpp index 9db8acee..35873c5d 100644 --- a/src/Primitives/idf.cpp +++ b/src/Primitives/idf.cpp @@ -298,7 +298,7 @@ bool resolve_external_memory(char *symbol, Memory **val) { // Restore external state when restoring a snapshot //------------------------------------------------------ void restore_external_state(Module *m, - std::vector external_state) { + const std::vector &external_state) { uint8_t opcode = *m->pc_ptr; // TODO: Maybe primitives can also be called using the other call // instructions such as call_indirect From b6348ebaec0d5e17323b24ad259191e8ff6d68e0 Mon Sep 17 00:00:00 2001 From: jay-tux Date: Wed, 9 Oct 2024 21:32:05 +0200 Subject: [PATCH 16/16] Oops, we forgot Zephyr --- src/Primitives/zephyr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Primitives/zephyr.cpp b/src/Primitives/zephyr.cpp index 4da4f7d9..d7273b0d 100644 --- a/src/Primitives/zephyr.cpp +++ b/src/Primitives/zephyr.cpp @@ -316,7 +316,7 @@ bool resolve_external_memory(char *symbol, Memory **val) { // Restore external state when restoring a snapshot //------------------------------------------------------ void restore_external_state(Module *m, - std::vector external_state) { + const std::vector &external_state) { uint8_t opcode = *m->pc_ptr; // TODO: Maybe primitives can also be called using the other call // instructions such as call_indirect