| 1 | /** |
|---|
| 2 | * FreeRTOS Stack Overflow Demo |
|---|
| 3 | * |
|---|
| 4 | * Generate a stack overflow on purpose. |
|---|
| 5 | * |
|---|
| 6 | * Date: December 5, 2020 |
|---|
| 7 | * Author: Shawn Hymel |
|---|
| 8 | * License: 0BSD |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | // Use only core 1 for demo purposes |
|---|
| 12 | #if CONFIG_FREERTOS_UNICORE |
|---|
| 13 | static const BaseType_t app_cpu = 0; |
|---|
| 14 | #else |
|---|
| 15 | static const BaseType_t app_cpu = 1; |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | // Task: Perform some mundane task |
|---|
| 19 | void testTask(void *parameter) { |
|---|
| 20 | while (1) { |
|---|
| 21 | int a = 1; |
|---|
| 22 | int b[100]; |
|---|
| 23 | |
|---|
| 24 | // Do something with array so it's not optimized out by the compiler |
|---|
| 25 | for (int i = 0; i < 100; i++) { |
|---|
| 26 | b[i] = a + 1; |
|---|
| 27 | } |
|---|
| 28 | //Serial.println(b[0]); |
|---|
| 29 | |
|---|
| 30 | // Print out remaining stack memory (words) |
|---|
| 31 | Serial.print("High water mark (words): "); |
|---|
| 32 | Serial.println(uxTaskGetStackHighWaterMark(NULL)); |
|---|
| 33 | |
|---|
| 34 | // Print out number of free heap memory bytes before malloc |
|---|
| 35 | Serial.print("Heap before malloc (bytes): "); |
|---|
| 36 | Serial.println(xPortGetFreeHeapSize()); |
|---|
| 37 | int *ptr = (int*)pvPortMalloc(1024 * sizeof(int)); |
|---|
| 38 | |
|---|
| 39 | // One way to prevent heap overflow is to check the malloc output |
|---|
| 40 | if (ptr == NULL) { |
|---|
| 41 | Serial.println("Not enough heap."); |
|---|
| 42 | vPortFree(NULL); |
|---|
| 43 | } else { |
|---|
| 44 | |
|---|
| 45 | // Do something with the memory so it's not optimized out by the compiler |
|---|
| 46 | for (int i = 0; i < 1024; i++) { |
|---|
| 47 | ptr[i] = 3; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | // Print out number of free heap memory bytes after malloc |
|---|
| 52 | Serial.print("Heap after malloc (bytes): "); |
|---|
| 53 | Serial.println(xPortGetFreeHeapSize()); |
|---|
| 54 | |
|---|
| 55 | // Free up our allocated memory |
|---|
| 56 | //vPortFree(ptr); |
|---|
| 57 | |
|---|
| 58 | // Wait for a while |
|---|
| 59 | vTaskDelay(100 / portTICK_PERIOD_MS); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void setup() { |
|---|
| 64 | |
|---|
| 65 | // Configure Serial |
|---|
| 66 | Serial.begin(115200); |
|---|
| 67 | |
|---|
| 68 | // Wait a moment to start (so we don't miss Serial output) |
|---|
| 69 | vTaskDelay(1000 / portTICK_PERIOD_MS); |
|---|
| 70 | Serial.println(); |
|---|
| 71 | Serial.println("---FreeRTOS Memory Demo---"); |
|---|
| 72 | |
|---|
| 73 | // Start the only other task |
|---|
| 74 | xTaskCreatePinnedToCore(testTask, |
|---|
| 75 | "Test Task", |
|---|
| 76 | 1500, |
|---|
| 77 | NULL, |
|---|
| 78 | 1, |
|---|
| 79 | NULL, |
|---|
| 80 | app_cpu); |
|---|
| 81 | |
|---|
| 82 | // Delete "setup and loop" task |
|---|
| 83 | vTaskDelete(NULL); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void loop() { |
|---|
| 87 | // Execution should never get here |
|---|
| 88 | } |
|---|