#include "poca_console.hh" #include #include #include #include #include "abi_smoke.hh" #include "esp_console.h" #include "esp_elf.h" #include "esp_heap_caps.h" #include "poca_plugin.hh" #include "poca_sched.hh" #include "private/elf_platform.h" #include "sdkconfig.h" #ifndef CONFIG_ELF_LOADER_LOAD_PSRAM #define CONFIG_ELF_LOADER_LOAD_PSRAM 0 #endif #ifndef CONFIG_ELF_LOADER_CACHE_OFFSET #define CONFIG_ELF_LOADER_CACHE_OFFSET 0 #endif #ifndef CONFIG_ELF_LOADER_SET_MMU #define CONFIG_ELF_LOADER_SET_MMU 0 #endif #ifndef CONFIG_ELF_DYNAMIC_LOAD_SHARED_OBJECT #define CONFIG_ELF_DYNAMIC_LOAD_SHARED_OBJECT 0 #endif namespace frame::poca { namespace { const char* on_off(int flag) { return flag != 0 ? "y" : "n"; } void print_loader_config() { std::printf("elf_loader: vendored espressif 1.3.3 @6526c5b1 (esp-iot-solution)\n"); std::printf(" ELF_LOADER=y LOAD_PSRAM=%s CACHE_OFFSET=%s SET_MMU=%s DLSO=%s\n", on_off(CONFIG_ELF_LOADER_LOAD_PSRAM), on_off(CONFIG_ELF_LOADER_CACHE_OFFSET), on_off(CONFIG_ELF_LOADER_SET_MMU), on_off(CONFIG_ELF_DYNAMIC_LOAD_SHARED_OBJECT)); std::printf(" esp_elf_t state size: %u bytes\n", static_cast(sizeof(esp_elf_t))); std::printf(" loader entries: esp_elf_init=%p esp_elf_relocate=%p\n", reinterpret_cast(&esp_elf_init), reinterpret_cast(&esp_elf_relocate)); } void print_heap_caps(const char* label, uint32_t caps) { multi_heap_info_t info{}; heap_caps_get_info(&info, caps); const unsigned size = static_cast(info.total_free_bytes + info.total_allocated_bytes); std::printf("heap %-9s size=%10u free=%10u largest=%10u min_ever=%10u\n", label, size, static_cast(info.total_free_bytes), static_cast(info.largest_free_block), static_cast(info.minimum_free_bytes)); } int poca_command(int argc, char** argv) { if (argc == 2 && std::strcmp(argv[1], "status") == 0) { print_loader_config(); std::printf(" registered host imports: poca_host_sentinel poca_host_add\n"); std::printf(" entry queries so far: %u (execution canary)\n", static_cast(poca_entry_queries())); std::printf(" iram cache syncs so far: %u (p5 counter)\n", static_cast(esp_elf_iram_cache_sync_count())); print_heap_caps("internal", MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); print_heap_caps("iram-exec", MALLOC_CAP_EXEC | MALLOC_CAP_INTERNAL); print_heap_caps("psram", MALLOC_CAP_SPIRAM); return 0; } if (argc == 2 && std::strcmp(argv[1], "abi") == 0) { return run_abi_smoke(); } if ((argc == 2 || argc == 3) && std::strcmp(argv[1], "verify") == 0) { return cmd_poca_verify(argc == 3 ? argv[2] : "baseline"); } if ((argc == 2 || argc == 3) && std::strcmp(argv[1], "load") == 0) { return cmd_poca_load(argc == 3 ? argv[2] : "baseline"); } if (argc == 2 && std::strcmp(argv[1], "activate") == 0) { return cmd_poca_activate(); } if (argc == 2 && std::strcmp(argv[1], "unload") == 0) { return cmd_poca_unload(); } if ((argc == 2 || argc == 3) && std::strcmp(argv[1], "iram") == 0) { unsigned iterations = 1; if (argc == 3) { const int parsed = std::atoi(argv[2]); if (parsed < 1 || parsed > 100000) { std::printf("[poca-iram] FAIL iteration count out of range (1..100000)\n"); return 1; } iterations = static_cast(parsed); } return cmd_poca_iram(iterations); } if (argc == 2 && std::strcmp(argv[1], "coexist") == 0) { return cmd_poca_coexist(); } if ((argc == 3 || argc == 4) && std::strcmp(argv[1], "soak") == 0) { const int parsed = std::atoi(argv[2]); if (parsed < 1 || parsed > 10000) { std::printf("[poca-soak] FAIL cycle count out of range (1..10000)\n"); return 1; } return cmd_poca_soak(static_cast(parsed), argc == 4 ? argv[3] : "default"); } if (argc == 3 && std::strcmp(argv[1], "sched") == 0) { return cmd_poca_sched(argv[2]); } if (argc == 3 && std::strcmp(argv[1], "capacity") == 0) { const int parsed = std::atoi(argv[2]); if (parsed < 1 || parsed > 8) { std::printf("[poca-cap] FAIL count out of range (1..8)\n"); return 1; } return cmd_poca_capacity(static_cast(parsed)); } if (argc == 3 && std::strcmp(argv[1], "fcbegin") == 0) { char* end = nullptr; const long parsed = std::strtol(argv[2], &end, 10); if (end == argv[2] || *end != '\0' || parsed < 1 || parsed > 512 * 1024) { std::printf("[fc] FAIL size out of range (1..%d)\n", 512 * 1024); return 1; } return cmd_poca_fcbegin(static_cast(parsed)); } if (argc == 3 && std::strcmp(argv[1], "fcwr") == 0) { return cmd_poca_fcwr(argv[2]); } if (argc == 5 && std::strcmp(argv[1], "fcgo") == 0) { const int parsed = std::atoi(argv[4]); if (parsed == 0 && std::strcmp(argv[4], "0") != 0) { std::printf("[FC] FAIL expected code not an integer: %s\n", argv[4]); return 1; } return cmd_poca_fcgo(argv[2], argv[3], parsed); } std::printf("usage: poca status | poca abi | poca verify [name] | poca load [name] | " "poca activate | poca unload | poca iram [n] | poca coexist | poca soak " "[mix] | poca sched | poca capacity | poca fcbegin | " "poca fcwr | poca fcgo \n"); return 1; } } // namespace esp_err_t start_console() { const esp_console_cmd_t poca_cmd{ .command = "poca", .help = "poca status | abi | verify [name] | load [name] | activate | unload | iram [n] | " "coexist | soak [mix] | sched | capacity | fcbegin | " "fcwr | fcgo ", .hint = nullptr, .func = &poca_command, .argtable = nullptr, .func_w_context = nullptr, .context = nullptr, }; esp_err_t result = esp_console_cmd_register(&poca_cmd); if (result != ESP_OK) { return result; } esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); repl_config.prompt = "poca> "; /* 600 (T14): the fail-closed corpus streams fixtures as hex chunks on * single console lines ("poca fcwr " + 2*N hex chars); 128-byte chunks * need 266 characters, and the extra headroom keeps the chunk size a * runner-side choice. */ repl_config.max_cmdline_length = 600; repl_config.max_cmdline_args = 8; repl_config.task_stack_size = kPocaReplStackBytes; repl_config.task_priority = 5; repl_config.task_core_id = 1; esp_console_dev_usb_serial_jtag_config_t device_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT(); esp_console_repl_t* repl = nullptr; result = esp_console_new_repl_usb_serial_jtag(&device_config, &repl_config, &repl); return result == ESP_OK ? esp_console_start_repl(repl) : result; } } // namespace frame::poca