{"id":22143745,"url":"https://github.com/invpe/gp-iot","last_synced_at":"2025-07-26T00:33:07.741Z","repository":{"id":242043327,"uuid":"808526503","full_name":"invpe/GP-IOT","owner":"invpe","description":"General Purpose Things eXperiment - dynamically loading and executing binaries while running a Sketch on esp32.","archived":false,"fork":false,"pushed_at":"2024-07-19T18:27:50.000Z","size":67,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-07-19T23:16:19.682Z","etag":null,"topics":["esp32","esp32-arduino","experiment","iot"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/invpe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-31T08:53:54.000Z","updated_at":"2024-07-19T18:27:53.000Z","dependencies_parsed_at":"2024-06-10T10:10:46.274Z","dependency_job_id":"ee14c720-8c50-435b-ab87-d01b64de8b56","html_url":"https://github.com/invpe/GP-IOT","commit_stats":null,"previous_names":["invpe/gp-iot"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/invpe%2FGP-IOT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/invpe%2FGP-IOT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/invpe%2FGP-IOT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/invpe%2FGP-IOT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/invpe","download_url":"https://codeload.github.com/invpe/GP-IOT/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227635459,"owners_count":17796972,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esp32","esp32-arduino","experiment","iot"],"created_at":"2024-12-01T22:15:50.739Z","updated_at":"2024-12-01T22:15:51.571Z","avatar_url":"https://github.com/invpe.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"![image](https://github.com/invpe/GP-IOT/assets/106522950/b72d07c3-76c9-4583-8f40-53f39d463f7a)\n\n# General Purpose Things - eXperiment\n\nAs part of an initiative to enhance the [GridShell](https://github.com/invpe/GridShell) project, i thought about executing binaries instead of scripts by dynamically loading and executing them on ESP32 nodes.\n\nThis repo called GP-IOT - General Purpose (Internet of) Things is my workbench, to get me to a place where i can do it issues free, easily and in secure manner.\n\nGeneral Purpose Things - draw inspiration from established distributed computing systems like BOINC, focusing on executing binaries on nodes. This repository introduces a Sketch named `Runner`, designed to dynamically load binaries from SPIFFS, and a `Task` source code that is an example binary. It locates the function address within the binary and executes it in a separate thread, showcasing a practical example of dynamic binary execution on IoT devices.\n\n# Demo\n\nThe repository holds a ready-to-use solution, including `Runner` sketch and a simple `task`.\n\n1. First upload the `Runner` sketch.\n2. Then go to `Example` and perform compilation, linkage, binary extraction and spiffs upload with `./task.sh`\n3. Open up terminal and reboot your device, you should end up binary being exexuted :\n\n```\n[19:55:29:407] Connecting to WiFi...␍␊\n[19:55:29:407] Connected to WiFi␍␊\n[19:55:29:523] Loading from SPIFFS␍␊\n[19:55:29:527] BIN SIZE: 152␍␊\n[19:55:29:527] Free heap size: 201324␍␊\n[19:55:29:527] MALLOC OK␍␊\n[19:55:29:545] SUBMITTING TASK␍␊\n[19:55:29:545] .␍␊\n[19:55:29:545] Task Function Address           : 0x14␊\n[19:55:29:545] Program buffer address          : 0x40092C9C␊\n[19:55:29:545] Calculated Task Function Address: 0x40092CB0␊\n[19:55:29:545] Return value from task: AAAAAAAAAA␊\n```   \n \n# How\n\nThere are few conditions that have to be met in order for the example to work:\n\n1. Binary we want to start needs to have a special `metadata` structure, that is placed at the END of the binary itself with the use of `.ld` script.\n   \n   This metadata holds a `uint32_t` address of the function we want to execute, in this case: `taskFunction`\n\n```\nstruct TaskMetadata {    \n    uint32_t taskFunctionAddress;       \n    char dummyText[16];  \n}; \nstruct TaskMetadata __attribute__((section(\".task_metadata\"))) taskMetadata = {    \n    (uint32_t)\u0026taskFunction,\n    \"AAAAAAAAAAAAAAA\"  // Initialize dummy text to see if our metadata is seen at the top of the binary\n};\n```\n\n2. The compilation and later link process need to ensure this `metadata` is placed at the bottom of the binary, because `runner` sketch will pick this up and retrieve the `taskFunctionAddress`. I am using a template `.ld` file for that, might not be perfect but works.\n \n3. The binary needs to have the same function definition as the `runner` so that we call it with the same arguments, and expect the same resunts (if anyhting is returned)\n\n`typedef void (*task_func_t)(const char*, char*);`\n\n`void taskFunction(const char* input, char* output)`\n\n\n4. The binary once compiled must have a decent size, so that we can allocate the memory for it, there is a check performed in the `RUNNER` sketch, that bails out if execution is impossible due to memory issues.\n\n```\n  // Check if there is enough free heap memory\n  if (freeHeap \u003c fileSize) {\n    Serial.println(\"Insufficient free heap memory\");\n    taskFile.close();\n    return;\n  }\n```\n\n5. The binary needs to be placed on SPIFFS, so ensure the SPIFFS is ready for use - that's handled by `Runner` sketch, which formats and initializes it properly.\n\n6. The runner (once started) will then dynamically load and execute the binary, but it can be also done in a separate thread to avoid blocking the main one if necessary.\n   \n   `xTaskCreate(taskRunner, \"TaskRunner\", 8192, program, 1, NULL);`\n\n7. The code is executed in IRAM, remember variables and addresses are relative to the start of the binary as there is no o/s initialization for this binary blob.\n\n8. The CPU can access data via the data bus in a byte-, half-word-, or word-aligned manner. The CPU can also access data via the instruction bus, but only in a word-aligned manner; non-word-aligned access will cause a CPU exception.\n9. And probably more.. ;-)\n   \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finvpe%2Fgp-iot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finvpe%2Fgp-iot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finvpe%2Fgp-iot/lists"}