r/rust • u/ralfj miri • Jul 19 '18
Thoughts on Compile-Time Function Evaluation and Type Systems
https://www.ralfj.de/blog/2018/07/19/const.html19
u/eddyb Jul 20 '18 edited Jul 21 '18
I wish Ralf had linked to https://internals.rust-lang.org/t/mir-constant-evaluation/3143/47?u=eddyb which describes the real problem with non-determinism: no matter how much information you record, you'll always be able to break coherence (you can maybe prevent crates from being used together, which is closer to what I think Haskell without at-instance-definition-time typeclass coherence checks does, but it's not great by any measure - assuming it even works).
EDIT: Okay after a day of very confused comments on this threads I'll just come out and say it: some of these comments, even/especially the more helpful-sounding/"authoritative" ones, muddle the issue further and spread misinformation. Ralf (AFAIK) forgot to ask for my feedback, and wasn't aware of https://internals.rust-lang.org/t/mir-constant-evaluation/3143/47?u=eddyb which went into this 2.5yrs ago. Below follows an expanded version that I'll try to add to instead of replying everywhere in this thread:
Yes you could have input side-effects safely (i.e. reading from a file, but not writing it - technically output side-effects like writing files could also be safe but they're harder to judge because they make the compilation process significantly more determinism)
No, it's not more of a security problem than
include_str!- in fact, it's much safer than procedural macros because the compiler has to emulate IO so it can tell you exactly what's happening, via lints or some other method, and you could forbid it in your own cratesWhat you really do not want under any circumstance to do, is to break the type/trait-system (aka create "unsoundness"). It's literally UB (undefined behavior, of "nasal demons" reputation) for the type/trait-system, which means anything could happen (e.g. bypassing memory safety checks, or allowing two different types to be used interchangeably, both causing actual UB)
Side-effect-ish compile-time constants could break the type/trait-system if they ended up in a type, affecting both type-checking and the search for
implthat satisfy someType: Traitrequirement, and they weren't made deterministic-after-the-fact at every single stepThe easy case where the input side-effect doesn't depend on generic parameters (e.g.
read("foo.bin")) can be solved by doing the evaluation exactly once and recording the result as normative (i.e. not just a cache but rather replacing the call for evaluation purposes)The harder case, where generics are involved (e.g.
read(Self::PATH)in a trait), can be solved crate-locally, by doing the same thing from 5. but also keying it on the choice of generic parameters (again, make what'd normally be a cache, normative)The limited cross-crate case, where 6. is used in a downstream crate, can be solved by remembering the normative map of side-effect-ish results from 6., across crates
The general cross-crate case, with sibling crates, contains, however, the really bad problem.
You get 8. by combining 6. with this crate dependency graph:
a
/ \
b c
\ /
dIf a contains something generic that uses non-deterministic constants which depend on generic parameters, e.g.:
trait Trait<B> {}
impl<T> Trait<[u8; rand(0, size_of::<T>())]> for T {}and b and c both use it, then d must produce a "link-time error" if b and c got conflicting results out of the same evaluations.
That's because it's equivalent (in general, not just this example) to them having both specialized an impl from a, making different choices for associated/generic types or constants, and therefore anything that could reach that impl cannot have exactly one interpretation.
This crate graph would then be "incoherent", the same kind of typesystem incoherence that "trait coherence checking" prevents.
It's similar to Haskell's orphan/overlapping instances (Rust "impls"), and I believe Haskell's defaults are weaker than Rust.
Yes you can probably make it safe with link-time errors. But Rust has so far tried to and succeeded pretty well at getting rid of those. (further discussion should follow from this point instead of debating anything earlier)
1
u/DannoHung Jul 20 '18
Ok. Offhand, it seems like 80% (90%?) of the benefit is from 1 in terms of reading some file with "configuration" values for a bunch of types so you can populate said configuration in some simple mechanical way (as opposed to generating a Rust source file to get const's). The sort of thing I'm thinking of is building a parameter tuned version of a program and testing dozens/hundreds of versions. Or maybe building some hardcoded version of a binary that can only run in a given security context.
Is that the wrong intuition?
2
u/eddyb Jul 20 '18
FWIW if you just want to read a config file into a
&'static str, just useinclude_str!. Unless you mean theread(Self::PATH)example, but you could just do something with macros.The sort of thing I'm thinking of is building a parameter tuned version of a program and testing dozens/hundreds of versions.
I'm not sure I understand. "thinking of" in terms of what? As a usecase for constants computed from "input side-effects" (like reading a file)? Do you mean having a lot of tunable constants that are read from files or env vars?
Because you can use
include_str!for files andenv!for env vars, in that case. All you're missing is.parse::<i32>()or something being made aconst fnso you're not stuck with a&'static mut.1
u/tending Jul 20 '18
I don't see what's wrong with having link time errors. C++ has them so pretty much every linker used in practice must detect conflicting definitions of symbols nowadays. It may be desirable to do this anyway just to guard against accidental nondeterminism in the compiler, bad RAM causing bit flips when compiling different translation units, or inadvertent compile time behavior changes in the compiler across versions when you're mixing translation units from different versions.
(Also how does env! not have the same issue? It seems like a much much more error-prone idea, because people export things in their environment all the time to get random applications to work...)
2
u/eddyb Jul 20 '18
env!andinclude_str!and every proc macro invocation ever are expanded before the typesystem, therefore they're like 5. in my list but even simpler because you never type-check or interpret the invocation, it's expanded away before it has a chance to touch the type-system.
fn foo() -> &'static str { include_str!("foo.data") }has very different semantics from a version which usedstd::fs::read_to_string- theinclude_str!is expanded once into the source offoo, but the in-language IO fromstd::fshas to happen on every call tofoo.1
u/eddyb Jul 20 '18 edited Jul 20 '18
C++ has them so pretty much every linker used in practice must detect conflicting definitions of symbols nowadays.
What exactly do you mean by "conflicting definitions"? Just the signature? That's the least of our worries (although it is one way to weaponize the problem).
EDIT: do you mean ODR? That explicitly says "Other violations, particularly those that span translation units, are not required to be diagnosed", so across a TU you have UB and no errors - so like my 6., but without even denying 7. in any way, just assuming it never happens.
(for more information relevant to Rust please refer to "link-time errors" in Haskell & the ML family, rather than C++)
By "link-time error" we also refer to a Rust crate "linking" against another, or more accurately using another crate as a dependency. In my example, the error arises where trying to compile
dcauses the compiler to load dependenciesbandcand a mismatch would be detected between something in the history of everything that happened when compilingbvsc.Even if it may be possible to nail it down perfectly, imagine having to check every single type/trait query ever executed, no matter how small, between each pair of dependencies of each crate you ever compile. You could optimize it a bit, by doing taint tracking instead, but if you don't do that perfectly, you're instantly screwed (unsoundness aka typesystem UB).
1
u/tending Jul 21 '18
Modern linkers catch ODR violations, regardless of whether the standard requires it. Only weak symbols (templates and inline functions) can have multiple definitions appear at all (the linker will complain if there is more than one in a translation unit and if a definition of the same function appears in multiple translation units), and for those it will complain if the definitions are different. Two functions have the same symbol if they same monomorphized signature and name.
1
u/eddyb Jul 22 '18
In that case I don't expect it to catch anything:
If a Rust function is neither inline nor generic (you're not using "weak symbol" the same way linkers do AFAICT), it can't possible have multiple definitions (unlike C++, you can't get the same Rust symbol from different source definitions - we're much more in the camp of "compilation creates normative identities de novo"), so where would traditional ODR help?
A generic function in
acan already be instantiated inbandctoday and we don't have to check the two instantiations because we knowdcan usebandctogether (through trait coherence & whatnot).If we did want to apply something like ODR between instantiations at link time we would need to be able to check definitions for equivalence, which AFAIK linkers can't do.
You don't need any code generation in
a,borc, only type-level choices that were recorded (maybe something like a computed constant array), you could be combining incompatible types entirely withindand not realize it unless you checked everythingbandcdid for compatibility.1
u/tending Jul 22 '18
In that case I don't expect it to catch anything:
If a Rust function is neither inline nor generic (you're not using "weak symbol" the same way linkers do AFAICT), it can't possible have multiple definitions (unlike C++, you can't get the same Rust symbol from different source definitions - we're much more in the camp of "compilation creates normative identities de novo"), so where would traditional ODR help?
I think you're confused. To be thorough I was explaining that modern linkers catch both defining the same regular symbol in multiple translation units and defining a weak symbol differently in multiple translation units. It maybe true Rust doesn't have to worry about the former at all, but the latter is what this discussion is about. As long as your language has monomorphized generics the linker must have the smarts at the very least to throw out duplicate definitions (two crates both invoke a generic method with the same compile time arguments). Modern linkers due to C++ support go further and compare equivalence in order to catch mismatches.
I may not be aware enough of how Rust changes the compilation process but I don't see how it can avoid needing this anyway. I have a crate C with a generic method foo used by crates A and B with the same compile time arguments. I compile A, change the definition of foo inside C, then compile B. When I use A and B in my final binary something needs to catch that A and B have different monomorphized copies of foo and give an error. G++ and Clang both catch this today. Does Rust? If so, how can it do it without comparing definitions?
1
u/eddyb Jul 22 '18
How does anything check for equivalence when all you have is machine code? I'm genuinely curious, everyone I asked said no in-depth checks actually happen.
As for your hypothetical example, you could try it out. You should get errors when trying to use A even without B ever existing because the C your A was compiled against is nowhere to be found (IIRC we hash everything in the crate so changing it at all and recompiling it will not result in a compatible artifact, despite having the same name).
So in a sense Rust does have link-time errors but they amount to you replaced one of the dependencies (and didn't recompile dependents), you can't get them by compiling each crate once.
And it's checking one integer value, not thousands/millions of queries in the history of each crate.
1
u/tending Jul 22 '18
Apparently gold complains if the type or size is different, and address sanitizer has a more detailed mode: https://github.com/google/sanitizers/wiki/AddressSanitizerOneDefinitionRuleViolation
I don't see why just having machine code is a problem though. If the machine code isn't identical for a given symbol, it's a violation. I think gold only checks the size and type for better performance. Inline functions could be tricky -- I guess you'd always have to emit a non-inline version for comparing, but I imagine that's usually done anyway?
3
u/Shnatsel Jul 19 '18
Could anyone link me some reading on the use cases for CTFE and why const propagation alone is insufficient for them?
2
u/eddyb Jul 20 '18
Anything where the constant is used in the typesystem, e.g.
[u8; size_of::<String>()](try it, it works today!).1
u/Shnatsel Jul 20 '18
HELL YEAH AT LAST
I can dehardcode some magic bitfield-related constants now! Yay!
0
Jul 19 '18
[deleted]
4
u/ralfj miri Jul 19 '18
It was my understanding (but I am just relying information here) that x87 is still used on 32bit platforms, where you cannot always rely on SSE and friends being available.
E.g. sign(sqrt(-1.0)) is different on ARM and x86, both are standards compliant.
Oh, interesting. That's certainly not helping, either. (Not sure what CTFE would do.)
4
Jul 19 '18
SSE was introduced in the Pentium III and Athlon XP. Anything older is pretty much completely irrelevant for modern software. A 32-bit x86 platform these days is way more likely to be "someone accidentally installed 32-bit windows on a 64-bit box" than a Pentium II.
1
u/ralfj miri Jul 20 '18
Fair enough, but can we guarantee that LLVM will never use x87? (I also don't know what the default architecture for Rust in 32bit is, i.e, which extensions it will assume.)
12
u/sociopath_in_me Jul 19 '18
I don't understand the reasoning why CTFE cannot read files. The compiler can already include arbitrary files compile time with the include macros. What's the difference? I think this limitation is just way too forced and does not really make sense.