Trace-Relating Compiler Correctness and Secure Compilation

Compiler correctness is, in its simplest form, defined as the inclusion of the set of traces of the compiled program into the set of traces of the original program, which is equivalent to the preservation of all trace properties. Here traces collect, for instance, the externally observable events of each execution. This definition requires, however, the set of traces of the source and target languages to be exactly the same, which is not the case when the languages are far apart or when observations are fine-grained. To overcome this issue, we study a generalized compiler correctness definition, which uses source and target traces drawn from potentially different sets and connected by an arbitrary relation. We set out to understand what guarantees this generalized compiler correctness definition gives us when instantiated with a non-trivial relation on traces. When this trace relation is not equality, it is no longer possible to preserve the trace properties of the source program unchanged. Instead, we provide a generic characterization of the target trace property ensured by correctly compiling a program that satisfies a given source property, and dually, of the source trace property one is required to show in order to obtain a certain target property for the compiled code. We show that this view on compiler correctness can naturally account for undefined behavior, resource exhaustion, different source and target values, side-channels, and various abstraction mismatches. Finally, we show that the same generalization also applies to many secure compilation definitions, which characterize the protection of a compiled program against linked adversarial code.


Introduction
Compiler correctness is an old idea [37,40,41] that has seen a significant revival in recent times. This new wave was started by the creation of the CompCert verified C compiler [33] and continued by the proposal of many significant extensions and variants of CompCert [8,9,12,23,29,30,42,52,56,57,61] and the success of many other milestone compiler verification projects, including Vellvm [64], Pilsner [45], CakeML [58], CertiCoq [4], etc. Yet, even for these verified compilers, the precise statement of correctness matters. Since proof assistants are used to conduct the verification, an external observer does not have to understand the proofs in order to trust them, but one still has to deeply understand the statement that was proved. And this is true not just for correct compilation, but also for secure compilation, which is the more recent idea that our compilation chains should do more to also ensure security of our programs [3,26].
Basic Compiler Correctness. The gold standard for compiler correctness is semantic preservation, which intuitively says that the semantics of a compiled program (in the target language) is compatible with the semantics of the original program (in the source language). For practical verified compilers, such as CompCert [33] and CakeML [58], semantic preservation is stated extrinsically, by referring to traces. In these two settings, a trace is an ordered sequence of events-such as inputs from and outputs to an external environment-that are produced by the execution of a program.
A basic definition of compiler correctness can be given by the set inclusion of the traces of the compiled program into the traces of the original program. Formally [33]: This definition says that for any whole 1 source program W, if we compile it (denoted W↓), execute it with respect to the semantics of the target language, and observe a trace t, then the original W can produce the same trace t with respect to the semantics of the source language. 2 This definition is simple and easy to understand, since it only references a few familiar concepts: a compiler between a source and a target language, each equipped with a trace-producing semantics (usually nondeterministic).
Beyond Basic Compiler Correctness. This basic compiler correctness definition assumes that any trace produced by a compiled program can be produced by the source program. This is a very strict requirement, and in particular implies that the source and target traces are drawn from the same set and that the same source trace corresponds to a given target trace. These assumptions are often too strong, and hence in practice verified compiler efforts use different formulations of compiler correctness: CompCert [33] The original compiler correctness theorem of CompCert [33] can be seen as an instance of basic compiler correctness, but it does not provide any guarantees for programs that can exhibit undefined behavior [53]. As allowed by the C standard, such unsafe programs are not even considered to be in the source language, so are not quantified over. This has important practical implications, since undefined behavior often leads to exploitable security vulnerabilities [13,24,25] and serious confusion even among experienced C and C++ developers [32,53,59,60]. As such, since 2010, CompCert provides an additional top-level correctness theorem 3 that better accounts for the presence of unsafe programs by providing guarantees for them up to the point when they encounter undefined behavior [53]. This new theorem goes beyond the basic correctness definition above, as a target trace need only correspond to a source trace up to the occurrence of undefined behavior in the source trace. CakeML [58] Compiler correctness for CakeML accounts for memory exhaustion in target executions. Crucially, memory exhaustion events cannot occur in source traces, only in target traces. Hence, dually to CompCert, compiler correctness only requires source and target traces to coincide up to the occurrence of a memory exhaustion event in the target trace.
Trace-Relating Compiler Correctness. Generalized formalizations of compiler correctness like the ones above can be naturally expressed as instances of a uniform definition, which we call trace-relating compiler correctness. This generalizes basic compiler correctness by (a) considering that source and target traces belong to possibly distinct sets Trace S and Trace T , and (b) being parameterized by an arbitrary trace relation ∼.
Definition 1.2 (Trace-Relating Compiler Correctness (CC ∼ )). A compiler ↓ is correct with respect to a trace relation ∼ ⊆ Trace S × Trace T iff ∀W.∀t. W↓ t ⇒∃s ∼ t. W s.
This definition requires that, for any target trace t produced by the compiled program W↓, there exist a source trace s that can be produced by the original program W and is related to t according to ∼ (i.e., s ∼ t). By choosing the trace relation appropriately, one can recover the different notions of compiler correctness presented above: Basic CC Take s ∼ t to be s = t. Trivially, the basic CC of Definition 1.1 is CC = .
CompCert Undefined behavior is modeled in CompCert as a trace-terminating event Goes_wrong that can occur in any of its languages (source, target, and all intermediate languages), so for a given phase (or composition thereof), we have Trace S = Trace T . Nevertheless, the relation between source and target traces with which to instantiate CC ∼ to obtain CompCert's current theorem is: s ∼ t ≡ s = t ∨ (∃m ≤ t. s = m·Goes_wrong). A compiler satisfying CC ∼ for this trace relation can turn a source trace ending in undefined behavior m·Goes_wrong (where "·" is concatenation) either into the same trace in the target (first disjunct), or into a target trace that starts with the prefix m but then continues arbitrarily (second disjunct, "≤" is the prefix relation). CakeML Here, target traces are sequences of symbols from an alphabet Σ T that has a specific trace-terminating event, Resource_limit_hit, which is not available in the source alphabet Σ S (i.e., Σ T = Σ S ∪ {Resource_limit_hit}. Then, the compiler correctness theorem of CakeML can be obtained by instantiating CC ∼ with the following ∼ relation: s ∼ t ≡ s = t ∨ (∃m. m ≤ s. t = m·Resource_limit_hit). The resulting CC ∼ instance relates a target trace ending in Resource_limit_hit after executing m to a source trace that first produces m and then continues in a way given by the semantics of the source program. Beyond undefined behavior and resource exhaustion, there are many other practical uses for CC ∼ : in this paper we show that it also accounts for differences between source and target values, for a single source output being turned into a series of target outputs, and for side-channels.
On the flip side, the compiler correctness statement and its implications can be more difficult to understand for CC ∼ than for CC = . The full implications of choosing a particular ∼ relation can be subtle. In fact, using a bad relation can make the compiler correctness statement trivial or unexpected. For instance, it should be easy to see that if one uses the total relation, which relates all source traces to all target ones, the CC ∼ property holds for every compiler, yet it might take one a bit more effort to understand that the same is true even for the following relation: Reasoning About Trace Properties. To understand more about a particular CC ∼ instance, we propose to also look at how it preserves trace properties-defined as sets of allowed traces [31]-from the source to the target. For instance, it is well known that CC = is equivalent to the preservation of all trace properties (where W |= π reads "W satisfies π" and stands for ∀t. W t ⇒ t ∈ π): CC = ≡ ∀π ∈ 2 Trace ∀W. W|=π ⇒ W↓|=π. However, to the best of our knowledge, similar results have not been formulated for trace relations beyond equality, when it is no longer possible to preserve the trace properties of the source program unchanged. For trace-relating compiler correctness, where source and target traces can be drawn from different sets and related by an arbitrary trace relation, there are two crucial questions to ask: 1. For a source trace property π S of a program-established for instance by formal verification-what is the strongest target property that any CC ∼ compiler is guaranteed to ensure for the produced target program? 2. For a target trace property π T , what is the weakest source property we need to show of the original source program to obtain π T for the result of any CC ∼ compiler? Far from being mere hypothetical questions, they can help the developer of a verified compiler to better understand the compiler correctness theorem they are proving, and we expect that any user of such a compiler will need to ask either one or the other if they are to make use of that theorem. In this work we provide a simple and natural answer to these questions, for any instance of CC ∼ . Building upon a bijection between relations and Galois connections [5,20,43], we observe that any trace relation ∼ corresponds to two property mappingsτ andσ, which are functions mapping source properties to target ones (τ standing for "to target") and target properties to source ones (σ standing for "to source"): τ (π S ) = {t | ∃s. s ∼ t ∧ s ∈ π S } ;σ(π T ) = {s | ∀t. s ∼ t ⇒ t ∈ π T } . The existential image of ∼,τ , answers the first question above by mapping a given source property π S to the target property that contains all target traces for which there exists a related source trace that satisfies π S . Dually, the universal image of ∼,σ, answers the second question by mapping a given target property π T to the source property that contains all source traces for which all related target traces satisfy π T . We introduce two new correct compilation definitions in terms of trace property preservation (TP): TPτ quantifies over all source trace properties and usesτ to obtain the corresponding target properties. TPσ quantifies over all target trace properties and usesσ to obtain the corresponding source properties. We prove that these two definitions are equivalent to CC ∼ , yielding a novel trinitarian view of compiler correctness ( Figure 1). Fig. 1: The equivalent compiler correctness definitions forming our trinitarian view.

Contributions.
We propose a new trinitarian view of compiler correctness that accounts for non-trivial trace relations. While, as discussed above, specific instances of the CC ∼ definition have already been used in practice, we seem to be the first to propose assessing the meaningfulness of CC ∼ instances in terms of how properties are preserved between the source and the target, and in particular by looking at the property mappingsσ andτ induced by the trace relation ∼. We prove that CC ∼ , TPσ, and TPτ are equivalent for any trace relation ( §2.2), as illustrated in Figure 1. In the opposite direction, we show that for every trace relation corresponding to a given Galois connection [20], an analogous equivalence holds. Finally, we extend these results ( §2.3) from the preservation of trace properties to the larger class of subset-closed hyperproperties (e.g., noninterference). We use CC ∼ compilers of various complexities to illustrate that our view on compiler correctness naturally accounts for undefined behavior ( §3.1), resource exhaustion ( §3.2), different source and target values ( §3.3), and differences in the granularity of data and observable events ( §3.4). We expect these ideas to apply to any other discrepancies between source and target traces. For each compiler we show how to choose the relation between source and target traces and how the induced property mappings preserve interesting trace properties and subset-closed hyperproperties. We look at the way particularσ andτ work on different kinds of properties and how the produced properties can be expressed for different kinds of traces. We analyze the impact of correct compilation on noninterference [22], showing what can still be preserved (and thus also what is lost) when target observations are finer than source ones, e.g., side-channel observations ( §4). We formalize the guarantee obtained by correct compilation of a noninterfering program as abstract noninterference [21], a weakening of target noninterference. Dually, we identify a family of declassifications of target noninterference for which source reasoning is possible. Finally, we show that the trinitarian view also extends to a large class of secure compilation definitions [2], formally characterizing the protection of the compiled program against linked adversarial code ( §5). For each secure compilation definition we again propose both a property-free characterization in the style of CC ∼ , and two characterizations in terms of preserving a class of source or target properties satisfied against arbitrary adversarial contexts. The additional quantification over contexts allows for finer distinctions when considering different property classes, so we study mapping classes not only of trace properties and hyperproperties, but also of relational hyperproperties [2]. An example secure compiler accounting for a target that can produce additional trace events that are not possible in the source illustrates this approach.
The traces considered in our examples are structured, usually as sequences of events. We notice however that unless explicitly mentioned, all our definitions and results are more general and make no assumption whatsoever about the structure of traces. Most of the theorems formally or informally mentioned in the paper were mechanized in the Coq proof assistant and are marked with . This development has around 10k lines of code, is described in the online appendix, and is available at the following address:

Trace-Relating Compiler Correctness
In this section, we start by generalizing the trace property preservation definitions at the end of the introduction to TP σ and TP τ , which depend on two arbitrary mappings σ and τ ( §2.1). We prove that, whenever σ and τ form a Galois connection, TP σ and TP τ are equivalent (Theorem 2.4). We then exploit a bijective correspondence between trace relations and Galois connections to close the trinitarian view ( §2.2), with two main benefits: first, it helps us assess the meaningfulness of a given trace relation by looking at the property mappings it induces; second, it allows us to construct new compiler correctness definitions starting from a desired mapping of properties. Finally, we generalize the classic result that compiler correctness (i.e., CC = ) is enough to preserve not just trace properties but also all subset-closed hyperproperties [14]. For this, we show that CC ∼ is also equivalent to subset-closed hyperproperty preservation, for which we also define both a version in terms ofσ and a version in terms ofτ ( §2.3).

Property Mappings
As explained in §1, trace-relating compiler correctness CC ∼ , by itself, lacks a crisp description of which trace properties are preserved by compilation. Since even the syntax of traces can differ between source and target, one can either look at trace properties of the source (but then one needs to interpret them in the target), or at trace properties of the target (but then one needs to interpret them in the source). Formally we need two property mappings, τ : 2 Trace S → 2 Trace T and σ : 2 Trace T → 2 Trace S , which lead us to the following generalization of trace property preservation (TP).
Definition 2.1 (TP σ and TP τ ). Given two property mappings, τ : 2 Trace S → 2 Trace T and σ : 2 Trace T → 2 Trace S , for a compilation chain ·↓ we define: For an arbitrary source program W, τ interprets a source property π S as the target guarantee for W↓. Dually, σ defines a source obligation sufficient for the satisfaction of a target property π T after compilation. Ideally: -Given π T , the target interpretation of the source obligation σ(π T ) should actually guarantee that π T holds, i.e., τ (σ(π T )) ⊆ π T ; -Dually for π S , we would not want the source obligation for τ (π S ) to be harder than π S itself, i.e., σ(τ (π S )) ⊇ π S . These requirements are satisfied when the two maps form a Galois connection between the posets of source and target properties ordered by inclusion. We briefly recall the definition and the characteristic property of Galois connections [16,38]. Definition 2.2 (Galois connection). Let (X, ) and (Y, ) be two posets. A pair of maps, α : X → Y , γ : Y → X is a Galois connection iff it satisfies the adjunction law: ∀x ∈ X. ∀y ∈ Y. α(x) y ⇐⇒ x γ(y). α (resp. γ) is the lower (upper) adjoint or abstraction (concretization) function and Y (X) the abstract (concrete) domain.
If two property mappings, τ and σ, form a Galois connection on trace properties ordered by set inclusion, Lemma 2.3 (with α = τ and γ = σ) tells us that they satisfy the ideal conditions we discussed above, i.e., τ (σ(π T )) ⊆ π T and σ(τ (π S )) ⊇ π S . 4 The two ideal conditions on τ and σ are sufficient to show the equivalence of the criteria they define, respectively TP τ and TP σ . Theorem 2.4 (TP τ and TP σ coincide ). Let τ : 2 Trace S 2 Trace T : σ be a Galois connection, with τ and σ the lower and upper adjoints (resp.). Then TP τ ⇐⇒ TP σ .

Trace Relations and Property Mappings
We now investigate the relation between CC ∼ , TP τ and TP σ . We show that for a trace relation and its corresponding Galois connection (Lemma 2.7), the three criteria are equivalent (Theorem 2.8). This equivalence offers interesting insights for both verification and design of a correct compiler. For a CC ∼ compiler, the equivalence makes explicit both the guarantees one has after compilation (τ ) and source proof obligations to ensure the satisfaction of a given target property (σ). On the other hand, a compiler designer might first determine the target guarantees the compiler itself must provide, i.e., τ , and then prove an equivalent statement, CC ∼ , for which more convenient proof techniques exist in the literature [7,58].
Definition 2.5 (Existential and Universal Image [20]). Given any two sets X and Y and a relation ∼ ⊆ A × B, define its existential or direct image,τ : 2 X → 2 Y and its universal image,σ : 2 Y → 2 X as follows: When trace relations are considered, the existential and universal images can be used to instantiate Definition 2.1 leading to the trinitarian view already mentioned in §1.  σ that maps a trace relation to its existential and universal images is a bijection between trace relations 2 Trace S ×Trace T and Galois connections on trace Proof. Gardiner et al. [20] show that the existential image is a functor from the category of sets and relations to the category of predicate transformers, mapping a set X → 2 X and a relation ∼ ⊆ X × Y →τ : 2 X → 2 Y . They also show that such a functor is an isomorphism -hence bijective -when one considers only monotonic predicate transformers that have a -unique -upper adjoint. The universal image of ∼,σ, is the unique adjoint ofτ ( ), hence ∼ →τ σ is itself bijective.
The bijection just introduced allows us to generalize Theorem 2.6 and switch between the three views of compiler correctness described earlier at will.
Theorem 2.8 (Correspondence of Criteria). For any trace relation ∼ and corresponding Galois connection τ σ, we have: Proof. For a trace relation ∼ and the Galois connectionτ σ, the result follows from Theorem 2.6. For a Galois connection τ σ and∼, use Lemma 2.7 to conclude that the existential and universal images of∼ coincide with τ and σ, respectively; the goal then follows from Theorem 2.6.
We conclude by explicitly noting that sometimes the lifted properties may be trivial: the target guarantee can be the true property (the set of all traces), or the source obligation the false property (the empty set of traces). This might be the case when source observations abstract away too much information ( §3.2 presents an example).

Preservation of Subset-Closed Hyperproperties
A CC = compiler ensures the preservation not only of trace properties, but also of all subset-closed hyperproperties, which are known to be preserved by refinement [14]. An example of a subset-closed hyperproperty is noninterference [14]; a CC = compiler thus guarantees that if W is noninterfering with respect to the inputs and outputs in the trace then so is W↓. To be able to talk about how (hyper)properties such as noninterference are preserved, in this section we propose another trinitarian view involving CC ∼ and preservation of subset-closed hyperproperties (Theorem 2.11), slightly weakened in that source and target property mappings will need to be closed under subsets.
First, recall that a program satisfies a hyperproperty when its complete set of traces, which from now on we will call its behavior, is a member of the hyperproperty [14].

Definition 2.9 (Hyperproperty Satisfaction). A program W satisfies a hyperproperty
Hyperproperty preservation is a strong requirement in general. Fortunately, many interesting hyperproperties are subset-closed (SCH for short), which simplifies their preservation since it suffices to show that the behaviors of the compiled program refine the behaviors of the source one, which coincides with the statement of CC = .
To talk about hyperproperty preservation in the trace-relating setting, we need an interpretation of source hyperproperties into the target and vice versa. The one we consider builds on top of the two trace property mappings τ and σ, which are naturally lifted to hyperproperty mappings. This way we are able to extract two hyperproperty mappings from a trace relation similarly to §2.2: Definition 2.10 (Lifting property mappings to hyperproperty mappings). Let τ : 2 Trace S → 2 Trace T and σ : 2 Trace T → 2 Trace S be arbitrary property mappings. The images of H S ∈ 2 2 Trace S , H T ∈ 2 2 Trace T under τ and σ are, respectively: Formally we are defining two new mappings, this time on hyperproperties, but by a small abuse of notation we still denote them by τ and σ. Interestingly, it is not possible to apply the argument used for CC = to show that a CC ∼ compiler guarantees W↓ |=τ (H S ) whenever W |= H S . This is in fact not true because direct images do not necessarily preserve subset-closure [36,44]. To fix this we close the image ofτ andσ under subsets (denoted as Cl ⊆ ) and obtain: Theorem 2.11 (Preservation of Subset-Closed Hyperproperties ). For any trace relation ∼ and its existential and universal images lifted to hyperproperties,τ andσ, and for Cl ⊆ (H) = {π | ∃π ∈ H. π ⊆ π }, we have: Theorem 2.11 makes us aware of the potential loss of precision when interested in preserving subset-closed hyperproperties through compilation. In §4 we focus on a security relevant subset-closed hyperproperty, noninterference, and show that such a loss of precision can be intended as a declassification of noninterference.

Instances of Trace-Relating Compiler Correctness
The trace-relating view of compiler correctness above can serve as a unifying framework for studying a range of interesting compilers. This section provides several representative instantiations of the framework: source languages with undefined behavior that compilation can turn into arbitrary target behavior ( §3.1), target languages with resource exhaustion that cannot happen in the source ( §3.2), changes in the representation of values ( §3.3), and differences in the granularity of data and observable events ( §3.4).

Undefined Behavior
We start by expanding upon the discussion of undefined behavior in §1. We first study the model of CompCert, where source and target alphabets are the same, including the event for undefined behavior. The trace relation weakens equality by allowing undefined behavior to be replaced with an arbitrary sequence of events.
Example 3.1 (CompCert-like Undefined Behavior Relation). Source and target traces are sequences of events drawn from Σ, where Goes_wrong ∈ Σ is a terminal event that represents an undefined behavior. We then use the trace relation from the introduction: Each trace of a target program produced by a CC ∼ compiler is either also a trace of the original source program or it has a finite prefix that the source program also produces, immediately before encountering undefined behavior. As explained in §1, one of the correctness theorems in CompCert can be rephrased as this variant of CC ∼ .
We proved that the property mappings induced by the relation can be written as ( ): These two mappings explain what a CC ∼ compiler ensures for the ∼ relation above. The target-to-source mappingσ states that to prove that a compiled program has a property π T using source-level reasoning, one has to prove that any trace produced by the source program must either be a target trace satisfying π T or have undefined behavior, but only provided that any continuation of the trace substituted for the undefined behavior satisfies π T . The source-to-target mappingτ states that by compiling a program satisfying a property π S we obtain a program that produces traces that satisfy the same property or that extend a source trace that ends in undefined behavior.
These definitions can help us reason about programs. For instance,σ specifies that, to prove that an event does not happen in the target, it is not enough to prove that it does not happen in the source: it is also necessary to prove that the source program is does not have any undefined behavior (second disjunct). Indeed, if it had an undefined behavior, its continuations could exhibit the unwanted event.
This relation can be easily generalized to other settings. For instance, consider the setting in which we compile down to a low-level language like machine code. Target traces can now contain new events that cannot occur in the source: indeed, in modern architectures like x86 a compiler typically uses only a fraction of the available instruction set. Some instructions might even perform dangerous operations, such as writing to the hard drive. Formally, the source and target do not have the same events any more. Thus, we consider a source alphabet Σ S = Σ ∪ {Goes_wrong}, and a target alphabet Σ T = Σ ∪ Σ . The trace relation is defined in the same way and we obtain the same property mappings as above, except that since target traces now have more events (some of which may be dangerous), and the arbitrary continuations of target traces get more interesting. For instance, consider a new event that represents writing data on the hard drive, and suppose we want to prove that this event cannot happen for a compiled program. Then, proving this property requires exactly proving that the source program exhibits no undefined behavior [11]. More generally, what one can prove about targetonly events can only be either that they cannot appear (because there is no undefined behavior) or that any of them can appear (in the case of undefined behavior).
In §5.2 we study a similar example, showing that even in a safe language linked adversarial contexts can cause dangerous target events that have no source correspondent.

Resource Exhaustion
Let us return to the discussion about resource exhaustion in §1.
Example 3.2 (Resource Exhaustion). We consider traces made of events drawn from Σ S in the source, and Σ T = Σ S ∪ {Resource_Limit_Hit} in the target. Recall the trace relation for resource exhaustion: Formally, this relation is similar to the one for undefined behavior, except this time it is the target trace that is allowed to end early instead of the source trace.
The induced trace property mappingsσ andτ are the following ( ): These capture the following intuitions. The target-to-source mappingσ states that to prove a property of the compiled program one has to show that the traces of the source program satisfy two conditions: (1) they must also satisfy the target property; and (2) the termination of every one of their prefixes by a resource exhaustion error must be allowed by the target property. This is rather restrictive: any property that prevents resource exhaustion cannot be proved using source-level reasoning. Indeed, if π T does not allow resource exhaustion, thenσ(π T ) = ∅. This is to be expected since resource exhaustion is simply not accounted for at the source level. The other mappingτ states that a compiled program produces traces that either belong to the same properties as the traces of the source program or end early due to resource exhaustion.
In this example, safety properties [31] are mapped (in both directions) to other safety properties ( ). This can be desirable for a relation: since safety properties are usually easier to reason about, one interested only in safety properties at the target can reason about them using source-level reasoning tools for safety properties.
The compiler correctness theorem in CakeML is an instance of CC ∼ for the ∼ relation above. We have also implemented two small compilers that are correct for this relation. The full details can be found in the Coq development in the supplementary materials. The first compiler ( ) goes from a simple expression language (similar to the one in §3.3 but without inputs) to the same language except that execution is bounded by some amount of fuel: each execution step consumes some amount of fuel and execution immediately halts when it runs out of fuel. The compiler is the identity.
The second compiler ( ) is more interesting: we proved this CC ∼ instance for a variant of a compiler from a WHILE language to a simple stack machine by Xavier Leroy [35]. We enriched the two languages with outputs and modified the semantics of the stack machine so that it falls into an error state if the stack reaches a certain size. The proof uses a standard forward simulation modified to account for failure.
We conclude this subsection by noting that the resource exhaustion relation and the undefined behavior relation from the previous subsection can easily be combined. Indeed, given a relation ∼ UB and a relation ∼ RE defined as above on the same sets of traces, we can build a new relation ∼ that allows both refinement of undefined behavior and resource exhaustion by taking their union: though the converse is not true.

Different Source and Target Values
We now illustrate trace-relating compilation for a translation mapping source-level booleans to target-level natural numbers. Given the simplicity of this compiler, most of the details of the formalization are deferred to the online appendix.
The source language is a pure, statically typed expression language whose expressions e include naturals n, booleans b, conditionals, arithmetic and relational operations, boolean inputs in b and natural inputs in n . A trace s is a list of inputs is paired with a result r, which can be a natural, a boolean, or an error. Well-typed programs never produce error ( ). Types ty are either N (naturals) or B (booleans); typing is standard. The source language has a standard big-step operational semantics (e is, r ) which tells how an expression e generates a trace is, r . The target language is analogous, except that it is untyped, only has naturals n and its only inputs are naturals in n . The semantics of the target language is also given in big-step style. Since we only have naturals and all expressions operate on them, no error result is possible in the target.
The compiler is homomorphic, translating a source expression to the same target expression; the only differences are natural numbers (and conditionals), as noted below. true↓ = 1 in b ↓ = in n e 1 ≤ e 2 ↓ = if e 1 ↓ ≤ e 2 ↓ then 1 else 0 false↓ = 0 in n ↓ = in n if e 1 then e 2 else e 3 ↓ = if e 1 ↓ ≤ 0 then e 3 ↓ else e 2 ↓ When compiling an if-then-else the target condition e 1 ↓ ≤ 0 is used to check that e 1 is false, and therefore the then and else branches of the source are swapped in the target. Relating Traces. We relate basic values (naturals and booleans) in a non-injective fashion as noted below. Then, we extend the relation to lists of inputs pointwise (Rules Empty and Cons) and lift that relation to traces (Rules Nat and Bool).
Property mappings. The property mappingsσ andτ induced by the trace relation ∼ defined above capture the intuition behind encoding booleans as naturals: the source-to-target mapping allows true to be encoded by any non-zero number; the target-to-source mapping requires that 0 be replaceable by both 0 and false. Compiler correctness. With the relation above, the compiler is proven to satisfy CC ∼ .
Simulations with different traces. The difficulty in proving Theorem 3.3 arises from the trace-relating compilation setting: For compilation chains that have the same source and target traces, it is customary to prove compiler correctness using a forward simulation (i.e., a simulation between source and target transition system); then, using determinacy [18,39] of the target language and input totality [19,63] (aka receptiveness) of the source, this forward simulation is flipped into a backward simulation (a simulation between target and source transition system), as described by Beringer et al. [7], Leroy [34]. This flipping is useful because forward simulations are often much easier to prove (by induction on the transitions of the source) than backward ones, as it is the case here.
We first give the main idea of the flipping proof, when the inputs are the same in the source and the target [7,34]. We only consider inputs, as it is the most interesting case, since with determinacy, nondeterminism only occurs on inputs. Given a forward simulation R, and a target program W T that simulates a source program W S , W T is able to perform an input iff so is W S : otherwise, say for instance that W S performs an output, by forward simulation W T would also perform an output, which is impossible because of determinacy. By input totality of the source, W S must be able to perform the exact same input as W T ; using forward simulation and determinacy, the resulting programs must be related.
By input totality k s + 3 By contradiction, using forward simulation and determinacy k s ∃W S1 R By forward simulation and determinacy · W T1 However, our trace relation is not injective (both 0 and false are mapped to 0), therefore these arguments do not apply: not all possible inputs of target programs are accounted for in the forward simulation. We thus have to strengthen the forward simulation assumption, requiring the following additional property to hold, for any source program W S and target program W T related by the forward simulation R.
We say that a forward simulation for which this property holds is flippable. For our example compiler, a flippable forward simulation works as follows: whenever a boolean input occurs in the source, the target program must perform every strictly positive input n (and not just 1, as suggested by the compiler). Using this property, determinacy of the target, input totality of the source, as well as the fact that any target input has an inverse image through the relation, we can indeed show that the forward simulation can be turned into a backward one: starting from W S R W T and an input i T2 , we show that there is i S1 and i T2 as in the diagram above, using the same arguments as when the inputs are the same; because the simulation is flippable, we can close the diagram, and obtain the existence of an adequate i S2 . From this we obtain CC ∼ .
In fact, we have proven a completely general 'flipping theorem', with this flippable hypothesis on the forward simulation ( ). We have also shown that if the relation ∼ defines a bijection between the inputs of the source and the target, then any forward simulation is flippable, hence reobtaining the usual proof technique [7,34] as a special case. This flipping theorem is further discussed in the online appendix.

Abstraction Mismatches
We now consider how to relate traces where a single source action is compiled to multiple target ones. To illustrate this, we take a pure, statically-typed source language that can output (nested) pairs of arbitrary size, and a pure, untyped target language where sent values have a fixed size. Concretely, the source is analogous to the language of §3.3, except that it does not have inputs or booleans and it has an expression send e, which can emit a (nested) pair e of values in a single action. That is, given that e reduces to a pair, e.g., v1, v2, v3 , expression send v1, v2, v3 emits action v1, v2, v3 . That expression is compiled into a sequence of individual sends in the target language send v1 ; send v2 ; send v3, since in the target, send e sends the value that e reduces to, but the language has no pairs. Due to space constraints we omit the full formalization of these simple languages and of the homomorphic compiler ( (·)  : e → e). The only interesting bit is the compilation of the send · expression, which relies on the gensend (·) function below. That function takes a source expression of a given type and returns a sequence of target send · instructions that send each element of the expression.
Relating Traces. We start with the trivial relation between numbers: n ∼ 0 n, i.e., numbers are related when they are the same. We cannot build a relation between single actions since a single source action is related to multiple target ones. Therefore, we define a relation between a source action M and a target trace t (a list of numbers), inductively on the structure of M (which is a pair of values, and values are natural numbers or pairs).
A pair of naturals is related to the two actions that send each element of the pair (Rule Trace-Rel-N-N). If a pair is made of sub-pairs, we require all such sub-pairs to be related (Rules Trace-Rel-N-M to Trace-Rel-M-M). We build on these rules to define the s ∼ t relation between source and target traces for which the compiler is correct (Theorem 3.4). Trivially, traces are related when they are both empty. Alternatively, given related traces, we can concatenate a source action and a second target trace provided that they are related (Rule Trace-Rel-Single).
With our trace relation, the trace property mappings capture the following intuitions: -The target-to-source mapping states that a source property can reconstruct target action as it sees fit. For example, trace 4 · 6 · 5 · 7 is related to 4, 6 · 5, 7 and 4, 6, 5, 7 (and many more variations). This gives freedom to the source implementation of a target behavior, which follows from the non-injectivity of ∼. 5 -The source-to-target mapping "forgets" about the way pairs are nested, but is faithful w.r.t. the values v i contained in a message. Notice that source safety properties are always mapped to target safety properties. For instance, if π S ∈ Safety S prescribes that some bad number is never sent, thenτ (π S ) prescribes the same number is never sent in the target andτ (π S ) ∈ Safety T . Of course if π S ∈ Safety S prescribes that a particular nested pairing like 4, 6, 5, 7 never happens, thenτ (π S ) is still a target safety property, but the trivial one, sinceτ (π S ) = ∈ Safety T .

Trace-Relating Compilation and Noninterference Preservation
When source and target observations are drawn from the same set, a correct compiler (CC = ) is enough to ensure the preservation of all subset-closed hyperproperties, in particular of noninterference (NI) [22], as also mentioned at the beginning of §2.3. In the scenario where target observations are strictly more informative than source observations, the best guarantee one may expect from a correct trace-relating compiler (CC ∼ ) is a weakening (or declassification) of target noninterference that matches the noninterference property satisfied in the source. To formalize this reasoning, this section applies the trinitarian view of trace-relating compilation to the general framework of abstract noninterference (ANI) [21]. We first define NI and explain the issue of preserving source NI via a CC ∼ compiler. We then introduce ANI, which allows characterizations of various forms of noninterference, and formulate a general theory of ANI preservation via CC ∼ . We also study how to deal with cases such as undefined behavior in the target. Finally, we answer the dual question, i.e., which source NI should be satisfied to guarantee that compiled programs are noninterfering with respect to target observers.
Intuitively, NI requires that publicly observable outputs do not reveal information about private inputs. To define this formally, we need a few additions to our setup. We indicate the (disjoint) input and output projections of a trace t as t • and t • respectively 6 . Denote with [t] low the equivalence class of a trace t, obtained using a standard lowequivalence relation that relates low (public) events only if they are equal, and ingores any difference between private events. Then, NI for source traces can be defined as: That is, source NI comprises the sets of traces that have equivalent low output projections as long as their low input projections are equivalent.
Trace-Relating Compilation and Noninterference. When additional observations are possible in the target, it is unclear whether a noninterfering source program is compiled to a noninterfering target program or not, and if so, whether the notion of NI in the target is the expected or desired one. We illustrate this issue considering a scenario where target traces extend source ones by exposing the execution time. While source noninterference NI S requires that private inputs do not affect public outputs, NI T additionally requires that the execution time is not affected by private inputs.
To model the scenario described, let Trace S denote the set of traces in the source, and Trace T = Trace S × N ω be the set of target traces, where N ω N ∪ {ω}. Target traces have two components: a source trace, and a natural number that denotes the time spent to produce the trace (ω if infinite). Notice that if two source traces Consider the following straightforward trace relation, which relates a source trace to any target trace whose first component is equal to it, irrespective of execution time: s ∼ t ≡ ∃n. t = (s, n). A compiler is CC ∼ if any trace that can be exhibited in the target can be simulated in the source in some amount of time. For such a compiler Theorem 2.11 says that if W satisfies NI S , then W↓ satisfies Cl ⊆ •τ (NI S ), which however is strictly weaker than NI T , as it contains, e.g., {(s 1 , 42), (s 2 , 42), (s 1 , 43), (s 2 , 43)}, and one cannot conclude that W↓ is noninterfering in the target. It is easy to prove that Cl ⊆ •τ (NI S ) = Cl ⊆ ({ π S × N ω | π S ∈ NI S }) = { π S × I | π S ∈ NI S ∧ I ⊆ N ω } , the first equality coming fromτ (π S ) = π S × N ω , and the second from NI S being subset-closed. As we will see, this hyperproperty can be characterized as a form of NI, which one might call timing-insensitive noninterference, and ensured only against attackers that cannot measure execution time. For this characterization, and to describe different forms of noninterference as well as formally analyze their preservation by a CC ∼ compiler, we rely on the general framework of abstract noninterference [21].
Abstract Noninterference. ANI [21] is a generalization of NI whose formulation relies on abstractions (in abstract interpretation sense [16]) in order to encompass arbitrary variants of NI. ANI is parameterized by an observer abstraction ρ, which denotes the distinguishing power of the attacker, and a selection abstraction φ, which specifies when to check NI, and therefore captures a form of declassification [54]. 7 Formally: low , we recover the standard noninterference defined above, where NI must hold for all low inputs (i.e., no declassification of private inputs), and the observational power of the attacker is limited to distinguishing low outputs.
The observational power of the attacker can be weakened by choosing a more liberal relation for ρ. For instance, one may limit the attacker to observe the parity of output integer values. Another way to weaken ANI is to use φ to specify that noninterference is only required to hold for a subset of low inputs.
To be formally precise, φ and ρ are defined over sets of (input and output projections of) traces, so when we write φ(t) above, this should be understood as a convenience notation for φ({t}). Likewise, φ = [·] low should be understood as φ = λπ. t∈π [t] low , i.e., the powerset lifting of [·] low . Additionally, φ and ρ are required to be upper-closed operators (uco)-i.e., monotonic, idempotent and extensive-on the poset that is the powerset of (input and output projections of) traces ordered by inclusion [21].
Trace-Relating Compilation and ANI for Timing. We can now reformulate our example with observable execution times in the target in terms of ANI. We have NI S = ANI φ S ρ S with φ S = ρ S = [·] low . In this case, we can formally describe the hyperproperty that a compiled program W↓ satisfies whenever W satisfies NI S as an instance of ANI: The definition of φ T tells us that the trace relation does not affect the selection abstraction. The definition of ρ T characterizes an observer that cannot distinguish execution times for noninterfering traces (notice that n 1 in the definition of ρ T is discarded). For instance, ρ T ({(s, n 1 )}) = ρ T ({(s, n 2 )}), for any s, n 1 , n 2 . Therefore, in this setting, we know explicitly through ρ T that a CC ∼ compiler degrades source noninterference to target timing-insensitive noninterference.
Trace-Relating Compilation and ANI in General. While the particular φ T and ρ T above can be discovered by intuition, we want to know whether there is a systematic way of obtaining them in general. In other words, for any trace relation ∼ and any notion of source NI, what property is guaranteed on noninterfering source programs by any CC ∼ compiler?
We can now answer this question generally (Theorem 4.1): any source notion of noninterference expressible as an instance of ANI is mapped to a corresponding instance of ANI in the target, whenever source traces are an abstraction of target ones (i.e., when ∼ is a total and surjective map). For this result we consider trace relations that can be split into input and output trace relations (denoted as ∼ The trace relation ∼ corresponds to a Galois connection between the sets of trace propertiesτ σ as described in §2.2. Similarly, the pair • ∼ and • ∼ corresponds to a pair of Galois connections,τ • σ • andτ • σ • , between the sets of input and output properties. In the timing example, time is an output so we have ∼ =, ∼ are both total maps from target to source traces, and where φ # T and ρ # T are defined as: S } (and both f • and g • are defined analogously).
For the example above we recover the definitions we justified intuitively, i.e., is at least as strong as the one that follows by just knowing that the compiler ↓ is CC ∼ . Noninterference and Undefined Behavior. As stated above, Theorem 4.1 does not apply to several scenarios from §3 such as undefined behavior ( §3.1), as in those cases the relation • ∼ is not a total map. Nevertheless, we can still exploit our framework to reason about the impact of compilation on noninterference.
Let us consider ∼ ∼ is any total and surjective map from target to source inputs (e.g., equality) and Intuitively, a CC ∼ compiler guarantees that no interference can be observed by a target attacker that cannot exploit undefined behavior to learn private information. This intuition can be made formal by the following theorem.
T is defined as in Theorem 4.1, and ρ # T is such that: Technically, instead of giving us a definition of ρ # T , the theorem gives a property of it. The property states that, given a target output trace t • , the attacker cannot distinguish it from any other target output traces produced by other possible compilations (τ • ) of the source trace s it relates to, up to the observational power of the source level attacker ρ S . Therefore, given a source attacker ρ S , the theorem characterizes a family of attackers that cannot observe any interference for a correctly compiled noninterfering program. Notice that the target attacker ρ # T = λ_. satisfies the premise of the theorem, but defines a trivial hyperproperty, so that we cannot prove in general that ANI shows that the family of attackers described in Theorem 4.2 is nonempty, and this ensures the existence of a most powerful attacker among them [21], whose explicit characterization we leave for future work. From Target NI to Source NI. We now explore the dual question: under what hypotheses does trace-relating compiler correctness alone allow target noninterference to be reduced to source noninterference? This is of practical interest, as one would be able to protect from target attackers by ensuring noninterference in the source. This task can be made easier if the source language has some static enforcement mechanism [1,36].
Let us consider the languages from §3.4 extended with inputting of (pairs of) values. It is easy to show that the compiler described in §3.4 is still CC ∼ . Assume that we want to satisfy a given notion of target noninterference after compilation, i.e., W↓|=ANI Recall that the observational power of the target attacker, ρ T , is expressed as a property of sequences of values. To express the same property (or attacker) in the source, we have to abstract the way pairs of values are nested. For instance, the source attacker should not distinguish v 1 , v 2 , v 3 and v 1 , v 2 , v 3 . In general (i.e., when • ∼ is not the identity), this argument is valid only when φ T can be represented in the source. More precisely, φ T must consider as equivalent all target inputs that are related to the same source one, because in the source it is not possible to have a finer distinction of inputs. This intuitive correspondence can be formalized as follows: and • ∼ a total and surjective map from source outputs to target ones and assume that ∀s t.
If ·↓ is a CC ∼ compiler and W satisfies ANI To wrap up the discussion about noninterference, the results presented in this section formalize and generalize some intuitive facts about compiler correctness and noninterference. Of course, they all place some restrictions on the shape of the noninterference instances that can be considered, because compiler correctness alone is in general not a strong enough criterion for dealing with many security properties [6,17].

Trace-Relating Secure Compilation
So far we have studied compiler correctness criteria for whole, standalone programs. However, in practice, programs do not exist in isolation, but in a context where they interact with other programs, libraries, etc. In many cases, this context cannot be assumed to be benign and could instead behave maliciously to try to disrupt a compiled program.
Hence, in this section we consider the following secure compilation scenario: a source program is compiled and linked with an arbitrary target-level context, i.e., one that may not be expressible as the compilation of a source context. Compiler correctness does not address this case, as it does not consider arbitrary target contexts, looking instead at whole programs (empty context [33]) or well-behaved target contexts that behave like source ones (as in compositional compiler correctness [27,30,45,57]).
To account for this scenario, Abate et al. [2] describe several secure compilation criteria based on the preservation of classes of (hyper)properties (e.g., trace properties, safety, hypersafety, hyperproperties, etc.) against arbitrary target contexts. For each of these criteria, they give an equivalent "property-free" criterion, analogous to the equivalence between TP and CC = . For instance, their robust trace property preservation criterion (RTP) states that, for any trace property π, if a source partial program P plugged into any context C S satisfies π, then the compiled program P↓ plugged into any target context C T satisfies π. Their equivalent criterion to RTP is RTC, which states that for any trace produced by the compiled program, when linked with any target context, there is a source context that produces the same trace. Formally (writing C [P ] to mean the whole program that results from linking partial program P with context C) they define: In the following we adopt the notation P |= R π to mean "P robustly satisfies π," i.e., P satisfies π irrespective of the contexts it is linked with. Thus, we write more compactly: RTP ≡ ∀π. ∀P. P |= R π ⇒ P↓ |= R π. All the criteria of Abate et al. [2] share this flavor of stating the existence of some source context that simulates the behavior of any given target context, with some variations depending on the class of (hyper)properties under consideration. All these criteria are stated in a setting where source and target traces are the same. In this section, we extend their result to our trace-relating setting, obtaining trintarian views for secure compilation. Despite the similarities with §2, more challenges show up, in particular when considering the robust preservation of proper sub-classes of trace properties. For example, after application ofσ orτ , a property may not be safety anymore, a crucial point for the equivalence with the property-free criterion for safety properties by Abate et al. [2]. We solve this by interpreting the class of safety properties as an abstraction of the class of all trace properties induced by a closure operator ( §5.1). The remaining subsections provide example compilation chains satisfying our trace-relating secure compilation criteria for trace properties ( §5.2) and for safety properties hypersafety ( §5.3).

Trace-Relating Secure Compilation: A Spectrum of Trinities
In this subsection we generalize many of the criteria of Abate et al. [2] using the ideas of §2. Before discussing how we solve the challenges for classes such as safety and hypersafety, we show the simple generalization of RTC to the trace-relating setting (RTC ∼ ) and its corresponding trinitarian view (Theorem 5.1): RTPτ ≡ ∀P ∀π S ∈ 2 Trace S . P |= R π S ⇒ P↓ |= Rτ (π S ); RTPσ ≡ ∀P ∀π T ∈ 2 Trace T . P |= Rσ (π T ) ⇒ P↓ |= R π T .
Abate et al. [2] propose many more equivalent pairs of criteria, each preserving different classes of (hyper)properties, which we briefly recap now. For trace properties, they also have criteria that preserve safety properties plus their version of liveness properties. For hyperproperties, they have criteria that preserve hypersafety properties, subset-closed hyperproperties, and arbitrary hyperproperties. Finally, they define relational hyperproperties, which are relations between the behaviors of multiple programs for expressing, e.g., that a program always runs faster than another. For relational hyperproperties, they have criteria that preserve arbitrary relational properties, relational safety properties, relational hyperproperties and relational subset-closed hyperproperties. Roughly speaking, the security guarantees due to robust preservation of trace properties regard only protecting the integrity of the program from the context, the guarantees of hyperproperties also regard data confidentiality, and the guarantees of relational hyperproperties even regard code confidentiality. Naturally, these stronger guarantees are increasingly harder to enforce and prove. While we have lifted the most significant criteria from Abate et al. [2] to our trinitarian view, due to space constraints we provide the formal definitions only for the two most interesting criteria. We summarize the generalizations of many other criteria in Figure 2, described at the end. Omitted definitions are available in the online appendix. Beyond Trace Properties: Robust Safety and Hyperproperty Preservation. We detail robust preservation of safety properties and of arbitrary hyperproperties since they are both relevant from a security point of view and their generalization is interesting.
Theorem 5.2 (Trinity for Robust Safety Properties ). For any trace relation ∼ and for the induced property mappingsτ andσ, we have: There is an interesting asymmetry between the last two characterizations above, which we explain now in more detail. RSPσ quantifies over target safety properties, while RTP Safe•τ quantifies over arbitrary source properties, but imposes the composition of τ with Safe, which maps an arbitrary target property π T to the target safety property that best over-approximates π T 8 (an analogous closure was needed for subset-closed hyperproperties in Theorem 2.11). More precisely, Safe is a closure operator on target properties, with Safety T = Safe(π T ) π T ∈ 2 Trace T . The mappings Safe •τ : 2 Trace S Safety T :σ determine a Galois connection between source trace properties and target safety properties, and ensure the equivalence RTP Safe•τ ⇐⇒ RSPσ ( ). This argument generalizes to arbitrary closure operators on target properties ( ) and on hyperproperties, as long as the corresponding class is a sub-class of subset-closed hyperproperties, and explains all but one of the asymmetries in Figure 2, the one that concerns the robust preservation of arbitrary hyperproperties: Theorem 5.3 (Weak Trinity for Robust Hyperproperties ). For a trace relation ∼ ⊆ Trace S × Trace T and induced property mappingsσ andτ , RHC ∼ is equivalent to RHPτ ; moreover, ifτ σ is a Galois insertion (i.e.,τ •σ = id), RHC ∼ implies RHPσ, while ifσ τ is a Galois reflection (i.e.,σ This trinity is weak since extra hypotheses are needed to prove some implications. While the equivalence RHC ∼ ⇐⇒ RHPτ holds unconditionally, the other two implications hold only under distinct, stronger assumptions. For RHPσ it is still possible and correct to deduce a source obligation for a given target hyperproperty H T when no information is lost in the the compositionτ •σ (i.e., the two maps are a Galois insertion). On the other hand, RHPτ is a consequence of RHPσ when no information is lost in composing in the other direction,σ •τ (i.e., the two maps are a Galois reflection). Navigating the Diagram. For a given trace relation ∼, Figure 2 orders the generalized criteria according to their relative strength. If a trinity implies another (denoted by ⇒), then the former provides stronger security for a compilation chain than the latter.
As mentioned, some property-full criteria regarding proper subclasses (i.e., subsetclosed hyperproperties, safety, hypersafety, 2-relational safety and 2-relational hyperproperties) quantify over arbitrary (relational) (hyper)properties and composeτ with an additional operator. We have already presented the Safe operator; other operators are Cl ⊆ , HSafe, and 2rSafe, which approximate the image ofτ with a subset-closed hyperproperty, a hypersafety and 2-relational safety respectively.
As a reading aid, when quantifying over arbitrary trace properties we use the shaded blue as background color, we use the red when quantifying over arbitrary subset-closed hyperproperties and green for arbitrary 2-relational properties.
We now describe how to interpret the acronyms in Figure 2. All criteria start with R meaning they refer to robust preservation. Criteria for relational hyperproperties-here only arity 2 is shown-contain 2r. Next, criteria names spell the class of hyperproperties they preserve: H for hyperproperties, SCH for subset-closed hyperproperties, HS for hypersafety, T for trace properties, and S for safety properties. Finally, property-free criteria end with a C while property-full ones involvingσ andτ end with P. Thus, robust (R) subset-closed hyperproperty-preserving (SCH) compilation (C) is RSCHC ∼ , robust (R) two-relational (2r) safety-preserving (S) compilation (C) is R2rSC ∼ , etc.

Instance of Trace-Relating Robust Preservation of Trace Properties
This subsection illustrates trace-relating secure compilation when the target language has strictly more events than the source that target contexts can exploit to break security. Source and Target Languages. The source and target languages used here are nearly identical expression languages, borrowing from the syntax of the source language of §3.3. Both languages add sequencing of expressions, two kinds of output events, and SCH subset-closed hyperproperties HS hypersafety T trace properties S safety properties P property-full criterion C property-free criterion based on σ and τ the expressions that generate them: out S n and out S n usable in source and target, respectively, and out T n usable only in the target, which is the only difference between source and target. The extra events in the target model the fact that the target language has an increased ability to perform certain operations, some of them potentially dangerous (such as writing to the hard drive), which cannot be performed by the source language, and against which source-level reasoning can therefore offer no protection. Both languages and compilation chains now deal with partial programs, contexts and linking of those two to produce whole programs. In this setting, a whole program is the combination of a main expression to be evaluated and a set of function definitions (with distinct names) that can refer to their argument symbolically and can be called by the main expression and by other functions. The set of functions of a whole program is the union of the functions of a partial program and a context; the latter also contains the main expression. The extensions of the typing rules and the operational semantics for whole programs are unsurprising and therefore elided. The trace model also follows closely that of §3.3: it consists of a list of regular events (including the new outputs) terminated by a result event. Finally, a partial program and a context can be linked into a whole program when their functions satisfy the requirements mentioned above.
Relating Traces. In the present model, source and target traces differ only in the fact that the target draws (regular) events from a strictly larger set than the source, i.e., Σ T ⊃ Σ S . A natural relation between source and target traces essentially maps to a given target trace t the source trace that erases from t those events that exist only at the target level. Let t| Σ S indicate trace t filtered to retain only those elements included in alphabet Σ S . We define the trace relation as: s ∼ t ≡ s = t| Σ S . In the opposite direction, a source trace s is related to many target ones, as any targetonly events can be inserted at any point in s. The induced mappings for ∼ are: τ (π S ) = {t | ∃s. s = t| Σ S ∧ s ∈ π S } ;σ(π T ) = {s | ∀t. s = t| Σ S ⇒ t ∈ π T } . That is, the target guarantee of a source property is that the target has the same source-level behavior, sprinkled with arbitrary target-level behavior. Conversely, the source-level obligation of a target property is the aggregate of those source traces all of whose target-level enrichments are in the target property.
Since R S and R T are very similar, it is simple to prove that the identity compiler ( ·↓) from R S to R T is secure according to the trace relation ∼ defined above.

Instances of Trace-Relating Robust Preservation of Safety and Hypersafety
To provide examples of cross-language trace-relations that preserve safety and hypersafety properties, we show how existing secure compilation results can be interpreted in our framework. This indicates how the more general theory developed here can already be instantiated to encompass existing results, and that existing proof techniques can be used in order to achieve the secure compilation criteria we define.
For the preservation of safety, Patrignani and Garg [50] study a compiler from a typed, concurrent WHILE language to an untyped, concurrent WHILE language with support for memory capabilities. As in §3.3, their source has bools and nats while their target only has nats. Additionally, their source has an ML-like memory (where the domain is locations ) while their target has an assembly-like memory (where the domain is natural numbers n). Their traces consider context-program interactions and as such they are concatenations of call and return actions with parameters, which can include booleans as well as locations. Because of the aforementioned differences, they need a cross-language relation to relate source and target actions.
Besides defining a relation on traces (i.e., an instance of ∼), they also define a relation between source and target safety properties. They provide an instantiation of τ that maps all safe source traces to the related target ones. This ensures that no additional target trace is introduced in the target property, and source safety properties are mapped to target safety ones by τ . Their compiler is then proven to generate code that respects τ , so they achieve a variation of RTP Safe•τ .
Concerning the preservation of hypersafety, Patrignani and Garg [49] consider compilers in a reactive setting where traces are sequences of input (α?) and output (α!) actions. In their setting, traces are different between source and target, so they define a cross-language relation on actions that is total on the source actions and injective. Additionally, their set of target output actions is strictly larger than the source one, as it includes a special action √ , which is how compiled code must respond to invalid target inputs (i.e., receiving a bool when a nat was expected). Starting from the relation on actions, they define TPC, which is an instance of what we call τ . Informally, given a set of source traces, TPC generates all target traces that are related (pointwise) to a source trace. Additionally, it generates all traces with interleavings of undesired inputs α? followed by √ as long as removing α? √ leaves a trace that relates to the source trace.
TPC preserves hypersafety across languages, i.e., it is an instance of RSCHP HSafe•τ mapping source hypersafety to target hypersafety (and safety to safety).

Related Work
We already discussed how our results relate to some existing work in correct compilation [33,58] and secure compilation [2,49,50]. We also already mentioned that most of our definitions and results make no assumptions about the structure of traces. One result that relies on the structure of traces is Theorem 5.2, which involves some finite prefix m, suggesting traces should be some sort of sequences of events (or states), as customary when one wants to refer to safety properties [14]. It is however sufficient to fix a topology on properties where safety properties coincide with closed sets [46]. Even for reasoning about safety, hypersafety, or arbitrary hyperproperties, traces can therefore be values, sequences of program states, or of input output events, or even the recently proposed interaction trees [62]. In the latter case we believe that the compilation from IMP to ASM proposed by Xia et al. [62] can be seen as an instance of HC ∼ , for the relation they call "trace equivalence." Compilers Where Our Work Could Be Useful. Our work should be broadly applicable to understanding the guarantees provided by many verified compilers. For instance, Wang et al. [61] recently proposed a CompCert variant that compiles all the way down to machine code, and it would be interesting to see if the model at the end of §3.1 applies there too. This and many other verified compilers [12,29,42,56] beyond CakeML [58] deal with resource exhaustion and it would be interesting to also apply the ideas of §3.2 to them. Hur and Dreyer [27] devised a correct compiler from an ML language to assembly using a cross-language logical relation to state their CC theorem. They do not have traces, though were one to add them, the logical relation on values would serve as the basis for the trace relation and therefore their result would attain CC ∼ . Switching to more informative traces capturing the interaction between the program and the context is often used as a proof technique for secure compilation [2,28,48]. Most of these results consider a cross-language relation, so they probably could be proved to attain one of the criteria from Figure 2.
Generalizations of Compiler Correctness. The compiler correctness definition of Morris [41] was already general enough to account for trace relations, since it considered a translation between the semantics of the source program and that of the compiled program, which he called "decode" in his diagram, reproduced in Figure 3 (left). And even some of the more recent compiler correctness definitions preserve this kind of flexibility [51]. While CC ∼ can be seen as an instance of a definition by Morris [41], we are not aware of any prior work that investigated the preservation of properties when the "decode translation" is neither the identity nor a bijection, and source properties need to be re-interpreted as target ones and vice versa. Correct Compilation and Galois Connections. Melton et al. [38] and Sabry and Wadler [55] expressed a strong variant of compiler correctness using the diagram of Figure 3 (right) [38,55]. They require that compiled programs parallel the computation steps of the original source programs, which can be proven showing the existence of a decompilation map # that makes the diagram commute, or equivalently, the existence of an adjoint for ↓ (W ≤ W ⇐⇒ W W for both source and target). The "parallel" intuition can be formalized as an instance of CC ∼ . Take source and target traces to be finite or infinite sequences of program states (maximal trace semantics [15]), and relate them exactly like Melton et al. [38] and Sabry and Wadler [55]. Translation Validation. Translation validation is an important alternative to proving that all runs of a compiler are correct. A variant of CC ∼ for translation validation can simply be obtained by specializing the definition to a particular W, and one can obtain again the same trinitarian view. Similarly for our other criteria, including our extensions of the secure compilation criteria of Abate et al. [2], which Busi et al. [10] seem to already be considering in the context of translation validation.

Conclusion and Future Work
We have extended the property preservation view on compiler correctness to arbitrary trace relations, and believe that this will be useful for understanding the guarantees various compilers provide. An open question is whether, given a compiler, there exists a most precise ∼ relation for which this compiler is correct. As mentioned in §1, every compiler is CC ∼ for some ∼, but under which conditions is there a most precise relation? In practice, more precision may not always be better though, as it may be at odds with compiler efficiency and may not align with more subjective notions of usefulness, leading to tradeoffs in the selection of suitable relations. Finally, another interesting direction for future work is studying whether using the relation to Galois connections allows to more easily compose trace relations for different purposes, say, for a compiler whose target language has undefined behavior, resource exhaustion, and side-channels. In particular, are there ways to obtain complex relations by combining simpler ones in a way that eases the compiler verification burden?