Stubbing noreturn Functions

From OC Systems Wiki!
Revision as of 18:03, 3 April 2017 by GVincentCastellano (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


In Aprobe versions 4.4.6b and 4.4.6c coverage.ual does its own probe on abort() to trigger a coverage snapshot, and deallocates some internal data structures at that point so that a core dump may occur on subsequent abort calls. A fix is targeted for Aprobe version 4.4.6d. It is also possible to rebuild the UAL locally to correct the problem.


The gcc compiler supports applying the "noreturn" attribute to functions, as described at This attribute enables the compiler to "optimize" callers of such functions by treating any statements following the call as unreachable code and eliminating them.

Two examples of functions carrying this attribute are exit() and abort().

This optimization is so aggressive that even the return sequence for calling functions can be eliminated if the call is at the end of the routine. Thus an attempt to stub such a routine with aprobe's ap_StubRoutine will effectively result in a branch to random instructions, which in some cases may not have been intended as executable code at all.

Following are code for a simple program to demonstrate the issue, and a probe to allow the noreturn function to be stubbed.

Note that because the compiler has deprived the caller of abort (in this example, fatal()) of a return sequence, we manipulate the instruction and frame pointer registers to effect a return to the caller of fatal(), in this case main().

APC Source

// stub_abort.apc
probe thread {
  probe "abort" in "" {
    on_entry {
      ap_ExecutionContextPtr->ReturnAddress = (ap_Uint32)(*(int *)($$EBP+4));
      // Get abort's caller's return address from the stack and use
      // it as the address this probe will return to.
      $$EBP = *(int *)($$EBP);
      // Pop the frame pointer so it's correct when we return to abort's
      // caller's caller.
      // Perform the usual stubbing.

C Source

// stub_abort.c
#include <stdlib.h>
#include <stdio.h>

void fatal()
   printf("I returned from abort()?\n");

int main ()
   printf("I will survive!\n");