Section 14.27

Function wrapper

Definition

A function wrapper is a function that is called in place of another function, the wrapped function, and that calls the wrapped function itself. The wrapper runs extra code before the call, after it, or both, and passes the wrapped function’s result back to its caller, which uses the wrapper exactly as it would use the wrapped function. Wrappers count, time or check calls without editing the caller or the wrapped function. A wrapper sees only the calls that are routed to it.

Where it fits

Main entry: Runtime instrumentation (§14.24).

The Runtime instrumentation entry treats probes in general; a wrapper places a probe at a function call. What is specific to a wrapper is the routing that sends calls to it.

--wrap=malloc misses a call inside the compiled file that defines malloc, where the name is defined, not undefined; a call that a library makes through an internal function instead of malloc; a call inside a shared library linked earlier, because --wrap changes only the files of the current link; and an allocation the compiler inlined or removed. These gaps also prevent double counting. If malloc and calloc are both wrapped, and the library’s calloc gets its memory through an internal function, each calloc call is counted once, by its own wrapper. Had calloc called malloc through an undefined reference, one allocation would pass both wrappers and be counted twice. The symbol table §12.8 of each compiled file, its list of defined and used names, shows which case holds.

A wrapper must keep the wrapped function’s contract: the same parameters and result, the same calling convention of the ABI §12.47 (application binary interface), meaning the rules for passing arguments and results in registers and on the stack, the same error reporting, such as the C variable errno, and the same order of side effects. It must be safe wherever the wrapped function runs: on several threads at once, in interrupt handlers §6.12, and within itself. A wrapper that logs with printf can call itself, because printf may call malloc.

Questions answered

Do inlined calls, alternate symbols, or direct backend calls bypass the wrapper?

Yes, unless the routing mechanism also redirects them, and the usual mechanisms do not. A wrapper runs only for calls routed to it: link-time wrapping such as --wrap catches only undefined references to the wrapped name in the files of that link, and load-time replacement only calls that the dynamic linker resolves. An inlined call is no call at all, because the compiler copied the function’s body into the caller. A call through an alternate symbol, another name for the same code or an internal function, is not a call to the wrapped name. A direct call to the backend, the lower-level function that does the real work, skips the wrapped function and its wrapper. To establish coverage, list every route to the operation, then search the symbol tables and machine code for calls that avoid the wrapper.

Related entries: Runtime instrumentation (§14.24), Static versus dynamic instrumentation (§14.25), Probe and hook (§14.26), Instruction set architecture (ISA) (§4.38), Reentrancy (§9.27), Linker (§12.4), Symbol table (§12.8).