Skip to content

Stack usage during serialization is significantly higher than v6 #2046

Closed
@willmmiles

Description

@willmmiles

Describe the bug

Serialization using ArduinoJSON v7 on ESP8266 seems to be using substantially more stack than v6 for common serialization tasks. With a large configuration structure, this can result in a stack overflow, causing difficult to diagnose crashes.

Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:

  1. The program uses ArduinoJson 7
  2. The issue happens at run time
  3. The issue concerns serialization
  4. Program crashes
  5. Program uses PROGMEM
  6. Casting the pointer doesn't solve the issue

Environment
Here is the environment that I used:

  • Microcontroller: ESP8266
  • Core/runtime: ESP8266 core for Arduino v3.1.2 (PlatformIO espressif8266@4.2.1)
  • IDE: VSCode/PlatformIO

Reproduction

[TODO] Working on it - I should be able to provide a better (complete and buildable) example in a few days. Serialization code is of the form:

void serializeConfig() {
  JsonDocument doc;
  JsonObject root = doc.to<JsonObject>();

  JsonArray rev = root[F("rev")].to<JsonArray>();
  rev.add(1); //major settings revision
  rev.add(0); //minor settings revision

  root[F("vid")] = VERSION;

  JsonObject id = root[F("id")].to<JsonObject>();
  id[F("mdns")] = cmDNS;
  id[F("name")] = serverDescription;
  id[F("sui")] = simplifiedUI;
  // and so on ...

  File f = LittleFS.open("/cfg-v7.json", "w");
  if (f) serializeJson(root, f);
  f.close();
}

void measureStackUsage() {
    cont_check(g_pcont);   // Validate we haven't already overflowed
    cont_repaint_stack(g_pcont);  // Fill unused stack with guard value

    auto unused_stack_before = cont_get_free_stack(g_pcont);    // Counts stack filled with guard value
    serializeConfig();

    cont_check(g_pcont);   // Validate we haven't overflowed
    auto unused_stack_after = cont_get_free_stack(g_pcont);

    Serial.printf("Stack used by serializeConfig: %d\n", unused_stack_after - unused_stack_before);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv7ArduinoJson 7

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions