Your browser does not support JavaScript! Skip to main content
Free 30-day trial DO-178C Handbook RapiCoupling Preview DO-178C Multicore Training Multicore Resources
Rapita Systems
 

Industry leading verification tools

Rapita Verification Suite (RVS)

RapiTest - Functional testing for critical software RapiCover - Low-overhead coverage analysis for critical software RapiTime - In-depth execution time analysis for critical software RapiTask - RTOS scheduling visualization RapiCoverZero - Zero-footprint coverage analysis RapiTimeZero - Zero-footprint timing analysis RapiTaskZero - Zero-footprint event-level scheduling analysis RVS Qualification Kits - Tool qualification for DO-178 B/C and ISO 26262 projects RapiCouplingPreview - DCCC analysis

Multicore Verification

MACH178 - Multicore Avionics Certification for High-integrity DO-178C projects MACH178 Foundations - Lay the groundwork for A(M)C 20-193 compliance RapiDaemons - Analyze interference in multicore systems

Other

RTBx - The ultimate data logging solution Sim68020 - Simulation for the Motorola 68020 microprocessor

RVS Software Policy

Software licensing Product life cycle policy RVS Assurance issue policy RVS development roadmap

Industry leading verification services

Engineering Services

Data Coupling & Control Coupling Object code verification Qualification Training Consultancy Tool Integration Support

Latest from Rapita HQ

Latest news

RVS 3.23 Launched
Rapita System Announces New Distribution Partnership with COONTEC
Rapita partners with Asterios Technologies to deliver solutions in multicore certification
SAIF Autonomy to use RVS to verify their groundbreaking AI platform
View News

Latest from the Rapita blog

How to measure stack usage through stack painting with RapiTest
What does AMACC Rev B mean for multicore certification?
How emulation can reduce avionics verification costs: Sim68020
Multicore timing analysis: to instrument or not to instrument
View Blog

Latest discovery pages

Processor How to achieve multicore DO-178C certification with Rapita Systems
Plane How to achieve DO-178C certification with Rapita Systems
Military Drone Certifying Unmanned Aircraft Systems
control_tower DO-278A Guidance: Introduction to RTCA DO-278 approval
View Discovery pages

Upcoming events

DASC 2025
2025-09-18
Certification-Ready Rust: GNAT Pro & RVS for Avionics Standards
2025-09-30
DO-178C Multicore In-person Training (Fort Worth, TX)
2025-10-01
Avionics Certification Q&A: CERT TALK (with Consunova and Visure)
2025-10-01
View Events

Technical resources for industry professionals

Latest White papers

Mitigation of interference in multicore processors for A(M)C 20-193
Sysgo WP
Developing DO-178C and ED-12C-certifiable multicore software
DO178C Handbook
Efficient Verification Through the DO-178C Life Cycle
View White papers

Latest Videos

Certification-Ready Rust: GNAT Pro & RVS for Avionics Standards
Accelerated software verification with RVS 3.23
Getting started with RVS
Requirements traceability with RapiTest and Polarion ALM
View Videos

Latest Case studies

Case Study Front Cover
Multicore timing analysis support for ECSS-E-ST-40C R&D with MACH178
GMV case study front cover
GMV verify ISO26262 automotive software with RVS
Kappa: Verifying Airborne Video Systems for Air-to-Air Refueling using RVS
View Case studies

Other Resources

 Webinars

 Brochures

 Product briefs

 Technical notes

 Research projects

 Multicore resources

Discover Rapita

About us

The company menu

  • Customers
  • Partners & Distributors
  • Research projects
  • Contact us
  • Careers
  • Working at Rapita
  • Subscribe to newsletter

Industries

  Civil Aviation (DO-178C)   Automotive (ISO 26262)   Military & Defense   Space

Standards

  DO-178C   A(M)C 20-193

US office

+1 248-957-9801
info@rapitasystems.com Rapita Systems, Inc., 41131 Vincenti Ct., Novi, MI 48375, USA

UK office

+44 (0)1904 413945
info@rapitasystems.com Rapita Systems Ltd., Atlas House, Osbaldwick Link Road, York, YO10 3JB, UK

Spain office

+34 93 351 02 05
info@rapitasystems.com Rapita Systems S.L., Parc UPC, Edificio K2M, c/ Jordi Girona, 1-3, Barcelona 08034, Spain
Back to Top Contact Us

How to measure stack usage through stack painting with RapiTest

Antoine Colin
2025-09-15

Accurately measuring stack usage is essential in safety-critical and embedded software projects. Oversized stacks waste precious memory, while undersized stacks risk catastrophic overflows.

In this post, we’ll explore how you can implement the classic stack painting technique with RapiTest support functions to measure and report task stack usage in your tests.

Why measure stack usage?

In embedded and safety-critical environments, stack allocation is usually static. Each task or function must be given a stack large enough to handle worst-case conditions, but not so large that memory is wasted. Underestimating stack requirements risks stack overflow and unpredictable failures, while overestimating wastes memory resources that are often scarce.

Certification standards such as DO-178C and ISO 26262 require clear evidence that stack sizes are justified and safe. Measuring stack usage is therefore not just good engineering practice, it’s a compliance requirement.

What is stack painting?

Stack painting (or stack watermarking) is a runtime technique for estimating the maximum stack depth a task has reached during execution. It involves the following stages:

  1. Paint the stack: At initialization, fill the stack memory with a known pattern (e.g.0x99).
  2. Run the task: As the program executes, stack operations overwrite this pattern.
  3. Measure usage: After execution, scan the stack from its base until you find the first unmodified pattern. The distance from the base to this point shows how deep the stack grew, the high-water mark.

Stack analysis by stack painting

This method is simple, portable, and widely used in embedded systems. Note that the stack usage observed depends on how well tests execute the code – to observe the absolute worst-case stack usage, a test needs to exist that exercises the code in a sufficient manner.

What are RapiTest support functions?

RapiTest support functions let you write reusable C/C++ logic that can be shared across multiple test cases. Instead of duplicating initialization or measurement code, you can encapsulate it once and invoke it wherever needed.

Typical uses include:

  • Test setup and teardown routines
  • Complex validation checks
  • Mocking and stubbing code
  • Custom measurement utilities (like measuring stack usage)

By using support functions for stack painting, you gain both reuse (of the same logic across different tests) and traceability (consistent reporting within RapiTest).

Example implementation

Below is an example implementation of stack painting and measurement using RapiTest support functions:


// ---------------------------------------------------------------------------
// Support functions for measuring maximum stack usage
// ---------------------------------------------------------------------------

static volatile unsigned char *stack_end;
volatile int idx;

/* RVS_ini_stack — Paint the stack with a known pattern.
 * @param max  Number of bytes to paint downward from the current stack pointer.
 */
void RVS_ini_stack(unsigned int max)
{
    volatile unsigned char marker = 0xFF;  // Current stack position
    stack_end = ▮                   // Save stack base
    // Fill stack with pattern
    for (idx = 0; idx < max; idx++) {
        stack_end--;        // Move downward (stacks grow down)
        *stack_end = 0x99;  // Paint memory with known pattern
    }
}

/* RVS_measure_stack — Measure maximum stack usage since last initialization.
 * @param used  Output variable that receives the number of bytes used.
 */
void RVS_measure_stack(unsigned int *used)
{
    volatile unsigned char marker = 0xFF;  // Current stack position
    // Skip untouched bytes
    while (*stack_end == 0x99) {stack_end++;}
    // Calculate usage
    *used = (unsigned int)(&marker - stack_end);
}

Using the stack support functions in a RapiTest test

The example below shows how you might use these support functions in a test case.

RapiTest tests to measure stack usage

This test will:

1. Paint the stack with a pattern.
2. Run the code under test (the LCD task).
3. Measure how much stack space was used.
4. Validate the measured stack usage against the specified threshold.

Reporting results in RapiTest

When you run these tests in RapiTest, the results integrate seamlessly into your usual reports. 

In the example below, a test case checks whether stack usage remains under a limit of 256 bytes. RapiTest automatically logs the measured high-water mark (295 bytes in this case) and flags the assertion as failed.

Viewing stack analysis results in RapiTest

This makes it easy to track stack usage alongside other test results, with clear evidence.

By embedding stack usage checks in your RapiTest campaigns, you can detect potential overflows early and maintain traceable evidence of stack usage behavior throughout your project.

Benefits of this approach

  • Reusable: As support functions are encapsulated, you can use them across multiple tests.
  • Portable: Works in any C/C++ environment supported by RapiTest.
  • Traceable: Stack usage measurements are captured and reported within your RapiTest results.
  • Certification-ready: Provides evidence of stack behavior, supporting compliance with safety standards.
  • Efficient: Easy to integrate with existing test campaigns.

Conclusion

By implementing ‘stack painting’ via RapiTest support functions, you can efficiently measure stack usage as part of your automated test campaigns.

Want to learn more about how RapiTest can support your software verification projects? You can find out more on our website or sign up to a free trial today.

DO-178C webinars

DO178C webinars

White papers


Mitigation of interference in multicore processors for A(M)C 20-193
Sysgo WP
Developing DO-178C and ED-12C-certifiable multicore software
DO178C Handbook
Efficient Verification Through the DO-178C Life Cycle

A Commercial Solution for Safety-Critical Multicore Timing Analysis
  • Solutions
    • Rapita Verification Suite
    • RapiTest
    • RapiCover
    • RapiTime
    • RapiTask
    • MACH178

    • Verification and Validation Services
    • Qualification
    • Training
    • Integration
  • Latest
  • Latest menu

    • News
    • Blog
    • Events
    • Videos
  • Downloads
  • Downloads menu

    • Brochures
    • Webinars
    • White Papers
    • Case Studies
    • Product briefs
    • Technical notes
    • Software licensing
  • Company
  • Company menu

    • About Rapita
    • Careers
    • Customers
    • Distributors
    • Industries
    • Locations
    • Partners
    • Research projects
    • Contact
  • Discover
    • Multicore Timing Analysis
    • Worst Case Execution Time
    • WCET Tools
    • Code coverage for Ada, C & C++
    • MC/DC Coverage
    • Verifying additional code for DO-178C
    • Data Coupling & Control Coupling
    • DO-178C
    • AC 20-193 and AMC 20-193
    • Certifying eVTOL
    • Certifying UAS

All materials © Rapita Systems Ltd. 2025 - All rights reserved | Privacy information | Trademark notice Subscribe to our newsletter