KernelNewbies:

Compiler Multiverse support

This page documents the proposal for a compile feature to enable run time multiverse support.

TableOfContents(4)

gcc could be extended to generalize the binary patching technique used in the Linux kernel for use for any application. What this provides is variable optimized run time multiverse support.

Technical feasibility

Its known in theory this should technically be possible given that:

Example

Let's consider the simple case of using a bool which only has two possible values:

Code that would want to make use of this would annotate that the bool can be optimized for binary patching using a special attribute, for instance:

static __attribute__((multiverse)) bool some_value;

int foo(void)
{
    if (some_value) {
        do_a();
    } else {
        do_b();
    }
}

int main(int argc, char **argv)
{
    if (argc > 1)
        some_value = true;

    gcc_runtime_optimize_variable(&some_value);
}

In this example when gcc would see some_value has the multiverse annotation, it would clone the foo() function twice, for two different possible universes, one where some_val is true, and one where some_val is false. The references to the some_val locations in code are also stored for later use (generic some_val use, where some_val = true, some_val = false) in a special section to allow for run time patching.

Applications get to hint during run time when a variable is deemed stable, this means that the variable value will not change for either:

If an application, or the kernel, determines that a configuration variable is now deemed "stable", it can enable the application to put itself into an application specific safe state (for instance perhaps disallow concurrent threads, disable preemption, perhaps machine_stop() in the kernel). After this application specific safe-state call, a generic function would be called that would run time patch either the first few bytes of some generic functions or all invocations of the generic functions with the specialized variant for the current value of some_val.

Benefits

Benefits once this happens:

Negative effects

Expected negative effects to consider:

Potential uses

Expected potential uses:

Feature inception

This idea was suggested by Alexander Graf while discussing an alternative to [http://kernelnewbies.org/KernelProjects/kernel-sandboxing Linker Table solution] with Luis Rodriguez to address [http://kernelnewbies.org/KernelProjects/kernel-sandboxing dead code] in the Linux kernel. This is a development idea for an extension to gcc to generalize dead-code support with compiler support.

Interested developers

If you are interested in helping complete it on gcc please reach out to:

KernelNewbies: KernelProjects/compiler-multiverse (last edited 2016-05-09 18:37:28 by mcgrof)