Fil-C: Garbage In, Memory Safety Out
Comments
Transcript
Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.
[applause] Thanks, Isaac. Um,
yeah. So, let's talk about Phil C. Um, so what what what is what is Phil C? Um,
it's a totally memory safe implementation of C and C++. What I mean by that is if you take a C or C++
program, compile it with my compiler, then the resulting program is strictly safer than if you had rewritten it in any other memory safe language. So that's the memory safety definition. I'll go into exactly what I mean by memory safety here in a little bit. Um, so you can get a more formal take on what I mean by that. Um, and as I'll uh
hopefully convince you in this talk, it is fanatically compatible with C and C++. Uh, so tons and tons of C and C++
code work in Phil C out of the box or with just minimal uh, changes. I think
roughly half of the software packages I've ported to Phil C just work out of the box. I just point them at my compiler and it works. Um, uh, and, uh,
one of the things that's the catch is it's a personal project. Uh, so there's some gotchas. Uh, one of them is that it only works on Linux x8664. Uh, but but hey, I mean, I'm working on it in my spare time. Uh, so, uh, if
that's the only thing you don't like about Phil C, then like send patches. Um, so the agenda for this talk is I first want to define memory safety. I think, uh, different people define it in a different way. Uh, and that's fine. I'm going to give you my definition so we're on the same page. Then I'm going to review the prior art. Uh there's been a lot of attempts to make uh C memory safe. Um and uh none of
them really worked out. Uh uh I think of all of them, Phil C has the most users. Um uh so I want to review h how the prior art looks. Um then I'm going to tell you the key idea behind Philc uh as well as then going into the details of the compiler and the runtime. And uh at some point uh throughout this talk I promise there will be a demo. Um so what is memory safety? Um uh I
kind of like this definition. In a memory safe programming language, memory logic bugs don't give the attacker control over all of memory. Um
so what's that all about? Uh so examples of how the attacker might get control over all of memory. Uh let's say that you're uh using yolo C which is what I call the C with that's not the fill C. If you're using YOLO C um and you fail to check array bounds um and you use an index that's uh user controlled attacker controlled then the attacker could supply a very large index that essentially turns that array index access into an access of anywhere in memory that they like. So that's a case where the attacker suddenly gets to control all of memory. Uh, another example is type confusion. Um, if the attacker finds themselves in a position where they can store an integer of their liking into memory, knowing that the program will load that integer as a pointer and then use that pointer for dreferences, then by storing integers that are addresses, they can make the program access whatever memory they like. These are memory safety bugs. Um, these are the kinds of things we want to prevent. There are many other bugs that are kind of like in this vein.
Um so why why why is this bad? I want to
give you like a vivid uh taste of of why
uh it is bad. Um so uh in the 19
starting in the 1980s you started to see attacks that would use memory safety vulnerabilities to just write machine code into memory. Uh this is called shell code. Um you have a stack smasher where someone's got a buffer on the stack. You overwrite the buffer with machine code and the and a return address that points at that machine code. Now all of a sudden your program is just executing whatever machine code uh the attacker wanted. Um people realized this was bad. Uh so a bunch of mitigations against that landed including uh address space layout randomization and the no execute bit. Um but then attackers very quickly found a way around this. Turns out if you don't attack the problem at the source, namely controlling all of memory, attackers are still going to find a way to get full control over your program. The way that they do this, even if you prevent them from writing machine code into memory, is that they um overwrite the stack with what's called a rob chain, where they keep pointing return addresses at anywhere in your code that they like, stitching together different parts of your program to basically make your program do whatever they want. And it this like fairly straightforward to get to the point where the attacker can just make your computer do whatever they want it to do. Um so people realize this was bad. The story continues again. uh the more mitigations landed like CFI pointer authentication to try to prevent attackers from writing code addresses that that end up working. Um but but then uh all that attackers do is that they just overwrite sufficiently critical data to make your program do whatever they want. The point is if the attacker can over can access all of memory then you've already lost and there's no point in having any other mitigations. Um, so here's an example of a modern attack that doesn't involve overwriting machine code, doesn't involve a rock chain, doesn't involve overwriting any code pointers whatsoever. Um, so you've got a program.
Uh, somewhere in that program, there's a parser. Uh, somewhere else, maybe uh, for the purpose of this example, another thread. Uh, there's a thread that's going to call posix spawn, which is the system call on Unix to start another process. And it's going to have a command buffer that it fills in not with the user's data. It's maybe it's like it's filling it in with like whatever the program needs. So the user isn't supposed to meant to me isn't meant to be able to control the command buffer, but your parser has a bug uh and that bug allows the attacker to write anywhere in memory. So the attacker overwrites the command buffer. Now all of a sudden your computer program is doing not what you wanted, but whatever the attacker wanted. Um so this is an example of how modern uh exploits uh
defeat like basically all of those mitigations that I had on the previous slide. But it gets worse. There's a thing people call uh weird execution. Basically, if you look at like what your program is um and and zoom in uh to
what's going on in the millions of uh machine code instructions, what you're going to see is uh branches that depend on memory. That's like your program at a high level. Bunch of branches that depend on memory. millions of them uh both from your own code and from all your dependencies. So if the attacker can control all of memory, they can use all of your branches that read from memory to send your program on whatever journey they want and essentially stitch together the different sequences of of your program to write whatever program they want your computer to execute. So uh the goal
prevent the attacker from controlling all of memory. Um, we wanted we want to be in a situation where if you've got a bad array access, the worst thing that can happen is you're accessing the wrong index in that array. Or if you have a use after free, the worst thing that can happen is your program immediately crashes. That's the kind of thing we want, not that they control all of memory. So, uh, this is not a new idea.
Um, people have known that this is something that we should fix and see for a long time. Um, so, uh, I think the
earliest attempts were in the 90s. Um there's a bunch of them. Um so the the
first major one was called Safe C. Um
you can still download it. It doesn't actually support all of C. It doesn't support C++. Doesn't support threads. Um
I'm not sure how well it works. Uh there's modern approaches like sanitizers. Uh so most of these approaches aren't actually memory safe. Uh for example, in sanitizers, uh all that the sanitizer is checking is that you aren't accessing a deallocated uh region of memory. Um if you access any valid region of memory where there's some object that is currently live, not yet dead, it'll just let you do it, which is exactly what the attacker wants. When the attacker controls all of memory, they want to access the live memory. So a lot of these approaches don't actually give you memory safety. They're just good for catching bugs, or they maybe like make the attacker's job just a little bit harder. uh the remaining approaches like safe C um don't actually support the full C and C++ languages. Uh these approaches actually don't support C++ at all and when it comes to C they don't support threads. They don't support GCC and clang extensions. They don't support um pointer integer unions or actually any interesting unions in some cases. Um so they're not actually compatible. Uh so I think that's why a lot of these approaches either failed uh or were just have never been used as an actual production defense to make software more secure. Um well I guess there's mte and miie which is being used but that's a probabilistic defense. So the attacker has like a one in5 chance of uh bypassing the mitigation. So it's like I mean it's it's a better than nothing but it's only a probabilistic defense. So that only leaves cherry standing. Um and the problem with cherry is that uh it's hardware that you can't buy. Um uh so um
and and the problem with uh the pro the the problem with designing uh programming language technology around new hardware with special instructions is that the special hardware is going to trail commodity hardware in performance um by such an amount that um you know
I've got this cheap Dell laptop actually really like it but it was like the cheapest one they had. um running Philc on this laptop will give you better performance than running on the fastest cherry hardware you could ever buy. Um so the hard there's just nothing that you get out of having the special hardware and anyway you wouldn't be able to buy it so um I don't have to worry about it as a competitor. Um >> so what is the idea that started Phil C? Um well uh as Isaac uh said in the
introduction uh my job is that I am a pointyhaired boss. Um so uh I I I
approached this problem partly as a as a manager. Um so scoping the problem was really important to me. Um and then uh and then there is a trick. So a lot of it is I've been thinking about this for a long time and I came up with a really cool trick. So uh let's let's scope the problem together. Um so here are possible goals that you could have for uh a memory safe implementation of C. Um, now I actually decided that the
first thing that I noticed using my manager hat is this too many goals. Uh, if it because we haven't actually achieved even a subset of these goals yet. So the first stab needs to be something that achieves some of the goals. That's important because Philc might go down in history as merely a stepping stone to a better technology that will build on what I did in order to achieve the rest of these goals. But I'm just scoping it so that I can give y'all something meaningful um instead of just like an academic exercise. So the the goal that I eliminated outright is the goal of binary compatibility. What this means is that when you compile with Phil C, you can't dynamically link with things not compiled with Phil C. You can dynamically link Phil C with Phil C, but you can't dynamically inter intermix. Um so that's just like a non-goal. Um supporting that would be a lot of work that I just haven't done.
And then when I first started out, I said to myself, you know what? I don't care how fast it is. I don't care how slow it is. I just want to be memory safe and uh and and and compatible. The first version was 200 times slower. Um it's not 200 times slower anymore. It's like uh median four times slower. I think um uh uh Dr. Hips says six times slower for SQLite. There's some situations where it's actually really fast. Uh I benchmarked the Blake 3 implementation uh in Phil C and compared it to Blake 3 and Rust and Phil C was actually faster. Reason being that uh Phil C has better SIMD support. Um so uh huge range
anywhere from a little bit faster than Rust to six times slower. Uh but I'll in the demo I think we'll convince you that it's fast enough for like everyday use. Um okay. So what's the trick? because the trick is really about making this an economical change to LVM in the runtime that I could do in my spare time. Um, so
the the the key observation that I made at some point is I don't need to write a whole new compiler from scratch because something that I get from the way that clang and LLVM are architected is that the LLVMIR that is produced by clang but
right at the moment when it's produced before it's actually optimized and transformed by the LLVM pipeline is remarkably well behaved in how it uses pointers. It uses pointers in a way where the original intent of the programmer is largely preserved. Namely, if the programmer had uh a field in memory that is pointer type or reference type in C++, that gets turned into a ptr type in LVMIR. If the programmer had a field in memory that's not a pointer, not a reference, it would use some other LVM type. So, by reading the LVMIR, I can I I know where the pointers are. Um, as soon as I run any LLVM optimization, that stops being true. But right at that moment, I know I know where the pointers are.
So the trick is that I instrument all of the LVM ptr operations with invisible
capabilities or invisic caps for short. Uh, and so that each pointer carries bounds and all pointer operations preserve the bounds and check the bounds. Um the bounds means that what this means is that you as the user of LVMIR or C will
perceive the pointer as just being its raw pointer value. It'll be whatever combination of bits you put in there. Um but somewhere for each pointer that you that you see in your program somewhere out there there will be a lower bound and upper bound and some bits that determine what you can do with the pointer. Um, but where do we store the bounds? This is the important thing because if we stored the bounds somewhere where you have access to as a Philc programmer, then you could just corrupt the bounds and then the attacker gets control over all of memory. So the bounds have to be stored somewhere where you can't actually mess with them. Hence invisible capabilities. How does that work? All right. Um when you have a pointer that's in flight, meaning um it's in your local data flow, um then it's represented as
uh a 64-bit int val as I call it. Uh it's the pointer's integer value. It's the untrusted value, the value you see and that you control. Uh combined with a lower, this is the lower bounds. You don't get to see it. You don't even know it's there. It's just kind of flowing with your pointer in local data flow. Um and it's just local data flow. So forget about memory for a minute. This is when it's like in a register. It's in two registers instead of one. The lower
the lower bounds uh in addition to pointing at the lower bounds of that object uh at the lower bounds of that object to the left of those lower bounds is the capability header where you will find the upper bounds. So if you have the lower bounds you can find the upper bounds. Um and another word the ox word
that stores some extra stuff. Um, no capability ever produced by any allocation in Phil C ever has its lower and upper bounds around any of the red stuff. So you don't ever get to modify the lower bounds. You don't ever get to modify the upper bounds. Uh, and then um
if you store pointers into this allocation, then the auxiliary word ends up pointing at an auxiliary allocation that has the lower bounds for every pointer stored into that allocation. Let me walk you through that with an example. Uh because that's that is a little confusing. Uh it's confusing because this is like literally the trick, right? Um so let's say that you malic something. Uh that's what you're going to get. Uh the your int val is going to point at the bottom of this object. So is the lower. Um all allocations are zero initialized in phil C. There's no way to not get them to be init zero initialized. So you're going to have zeros in there. And then let's say uh we store uh the current most popular number with my kids uh 67 um
into uh into this object. Uh so because you're storing a primitive that's just really simple. We bounce check and we we let you do that store. But what if you store another allocation? Let's see that animation again just so yeah look at that. What a cool animation. Um, so what's happening is we're allocating this other object and then the int val for that pointer is stored in that blue area, the payload of the object that you get to see and its lower bounds is stored into the auxiliary allocation. And because we didn't have an auxiliary allocation already, we created one. Um, so this incurs a 2x space overhead for those objects that have any pointers, uh, does not incur space overhead for objects that don't have pointers. So if you have like a frame buffer um that's not going to incur the 2x overhead. If you have a string not going to incur the space overhead but if you have like I don't know a binary tree node then uh need twice the memory. Um
so C and C++ code uh sees 64-bit pointers. Uh my compiler makes you believe that you're on an x8664 machine because you are. So 64-bit pointers checks out. Size of void star is eight. uh you don't have to change your code to adapt different pointer sizes. Um and there's capability bounds flowing around with your pointers. Um it's just they're invisible to you. You can't modify them and so therefore my runtime gets to trust them. Um
so the trick behind fil lvmir instrumentation for the ptr type um using invisible capabilities that preserve the pointer size and prevent temp tampering with the bounce. That's how it works. So that's Phil C like in a nutshell. Uh now let me go into the details by telling you a little bit about the compiler and then after that telling you a little bit about the runtime. So the current compiler actually does optimizations. Um I've taken uh some
amount of LLVM's optimizations and um
made them work with the capability model and I run them before I run my my instrumentation. Um, the optimizations that I run include things like inlining, uh, constant propagation, basically all of the major LLVM optimizations that you would expect. Um, not all of them yet though. There's still more optimizations I could add. Um, but I add them once I determine that they're they're safe to add. Um, and the the the instrumentation is called the Pislanator because why not? It's a personal project. Uh, I get to name it. Um and that implements garbage in memory safety out semantics. Meaning that um whatever LLVMIR shows up
at Pislanator's doorstep, either Pisolinator will refuse to compile it by crashing the compiler uh or it will emit code that has all of the checks necessary to make sure that that LVMIR is obeying Phil C's laws of memory safety, which means capabilities to objects. Pointer can't write out of the bounds of the capability of the object it was allocated to point to, that kind of thing. Uh and then after that I run just a normal LVM optimizing pipeline. [snorts] Um so yeah every every every sideffecting operation is instrumented with runtime checks. Uh operations that can't be checked are just rejected. Um and there's no escape hatch. Um, this is very important because in in in some memory safe languages like Rust, there is an escape hatch unsafe and you're expected to use it. Phil C has no equivalent of that. Additionally, recall that um, Philc has no dynamic linking to YOLO land. Um, but it does have uh, fanatical compatibility. So what that means is whereas like if you're a Go user or a Rust user or a Zig user, if you need to use some C++ library, you're going to use the YOLO version of that library and so you're going to end up having a process that has memory unsafe stuff in it. But if you're a Phil C user, you're just going to take that library and compile it with Phil C. So your whole stack is going to be memory safe. Hence, Phil C, uh, weirdly, despite it being C, is the most memory safe memory safe programming language in existence today.
Um, kind of cool. Um, so features of the compiler, it is just a fork of Clang. Uh, 20.1.8 is the last rebase that I did. So it supports C23, C++ 23, almost all clang and GCC extensions. Full SIMD support. Um, it even supports inline assembly. Uh, if you have an inline assembly snippet, I run a static analysis over it to make sure that it's not doing side effects that would violate the Philc model. If it doesn't, then I allow it. Um so like a common
idiom in SIMD code like Blake 3 is that you use SIMD intrinsics for your actual math but use inline assembly for CPU detection uh using instructions like CPU ID X get BV maybe some other stuff. Um it that kind of inline assembly that does CPU ID is allowed by my compiler because it's not it doesn't perform harmful effects. Well I actually check to make sure it doesn't perform harmful effects. like you could do CPU ID and it clobberers the A, B, C, and D registers. If you fail to list that as in your clobber list, the Philc compiler will reject it. So, it's actually doing like the checking that your inline assembly is not completely busted. Um, and it's just clank. So, if you want to build something with it, you just point your build system at it. Um, and it just works. Um, and it is like really compatible. Um, so it's not that I'm like crashing your program on undefined behavior. Uh I I'm I like think undefined behavior is BS. Like I I use all the things that the spec says I'm not allowed to use when I'm a C programmer. Uh and I want that kind of code to be able to work in phil C. So Philc does not enforce strict aliasing.
It's integer overflows wrap. You can bit cast through unions. Uh you can race all you like the race. If you race on a pointer, it won't violate the memory safety. You'll just get a bad outcome. Um so like real CC code uh works in Phil C. it. It's not just the CC code that's written to conform to the spec because nobody writes CC code that conforms to the spec. Um, [clears throat] now let's talk about the runtime. Uh, the most controversial part of the runtime is that it uses a garbage collector. Um, so I want to talk about that a little bit. It's an interesting choice. Um, and then the rest of the runtime is meant to make it feel like a conventional uh Linux C or C++ runtime.
I use uh either the muscle or the gibbc
uh lib C's that are compiled to Phil C and the rest of the user land compiled with Phil C. So you get you get the experience that you would get if you were just like programming in C or C++ on Linux. So why garbage collector? Um
it gives me the most deterministic possible answer to what happens on use after free in Phil C. If you free an object and then you try to access it, it's a panic. Um, and it will be it will continue to be a panic forever. There's no amount of heap grooming or trying to allocate enough memory to to uh use up a quarantine or whatever that will make it not a panic. You'll it'll forever be a panic. There's no way use after free or keeping a dangling pointer around for as long as you like. No way that's ever going to cause any kind of thing other than a panic. Um uh and because Philc
has comprehensive memory safety and capabilities that are trusted, I'm able to implement an accurate garbage collector. It's not a conservative garbage collector. It's an accurate garbage collector like what you would find in a Java VM. Um and I'm a I'm a
garbage collection person by trade. Uh so I I wrote like a really good concurrent garbage collector that has uh no pauses whatsoever. Um uh so it's a it's an accurate garbage collector. It's parallel uh so it'll use multiple cores when it has to run. Um it's concurrent and on the fly meaning it never pauses the program. Um it's non-moving uh mainly so that you if you do a pointer to integer cast multiple times you always get the same answer. Uh and it has what I call turbo sweep which is a bit vector swar algorithm for sweeping. So the sweep is really really fast. In addition to running on multiple cores each loop of the each loop cycle of the sweeper is actually sweeping multiple objects.
Now, um, could you make a phil C without a GC? The answer is you absolutely could. Um, you would follow the Cherry route. Uh, Cherry has a no GC mode. It's how most people run it. The idea is that if you have a use after free, then that capability now points to the same region of memory that it pointed to before, but now there's just like a different object there. Um, and uh, most uh, security
folks that I've talked to don't believe that that uh, is exploitable so long as the thing that ends up in that memory region isn't the meta data that you want to keep trusted. So you'd have to do some allocator tricks to make sure that you, for example, never allocate the trusted metadata inside of regions that were reused from user data and vice versa. U, but you could totally make a Philc without a GC. I chose not to uh because I'm a GC guy. Uh if you don't like it, send patches. Um yeah, I I want to thank the ffmpeg guy on Twitter for inventing that slogan.
Such a great slogan. Um so, uh Phil C
uses what I call the sandwich architecture. So like if you're using muscle then the phil C runtime which is
lib pislo obviously uh is running on top of muscle to get like sis calls from muscle basically um and then and that's
compiled with just GCC and then you run
the memory safe muscle on top of that which is just the same version of muscle with some changes compiled on top of compiled with phil C running on top of that. Um and then the rest of the user land is compiled with phil C. Uh key thing is like here in yolo muscle when it does a sys call it's using inline assembly for the sys call. When user muscle does a sys call it's using um phil internal API to go through the phil runtime sys call filters. Um so even sis calls are memory safe and phily. Um yeah
that's right. Um, so like in Rust, if
you make a SIS call, you're doing an unsafe. If you pass it a pointer uh with a length that's too big or you did some bad pointer math, then you're just in you're you're just corrupting memory. And Phil C, if you do that, you get a panic. Um, so yeah, Phil C uh safer than Rust. Rust is the faster but less safe alternative to Phil C. Um
uh so yeah, Phil FC runtime features uh system call filtering threads atomics uh memory safe signal handling uh shared memory um so like for example if I were
to run a windowing system like Wayland that does shared memory for its frame buffers that just works in Philc um the capability meta data isn't shared just the data payload is shared so you can't hurl pointers across the process boundary which is good because if you could then that would unsafe. Uh long jump and set jump work in Phil C. They I had to do that because there's so much code out there that uses them for exception handling. And long jump and set jump are completely safe. If you use them incorrectly, you get a panic. Make context and swap context also work in Phil C. Uh again, if you use them unsafeely, you get a panic. Um so you can write your own fibers in Phil C.
That just works. Uh C++ exceptions work great in Phil C as do all other C++ features. The one C++ feature I haven't ported yet is co- routines just because nobody uses co- routines. Um except for uh uh the Nyx package manager now uses co- routines. So I'm going to have to implement co- routines so I can have nyx running on phil. Um but so I guess thank you Nyx for using an obscure feature that forces me to edit. Um and then you can you can also do ffi in phil. Um meaning like you can reflectively uh make up a function call in phil C. uh des use data to describe what arguments to pass to a function and call it. Um and all of these things are safe. You can't use them to escape uh the protections.
Um there are two main distributions. You can download binary distributions. You can download or you can build build them from source. What I call the pisfix slice which is just a local installation. You untar it into a local directory use it from there. Um you don't need to be root. Uh and it's muscle based. So you just get muscle. You get lib C++ lib C++ AI. It's uh if you want to just really quickly test out what Phil C's like, that's what you use. Uh and then the opt fill slice is my favorite way of installing it. Um uh it's not installed on this computer for a reason. I'll show you in a minute. Um uh that installs uh phil C libraries into optfill. You need to be root. It's gibb based and it includes something really cool which is a memory safe open SSH demon with full pam support opensl
uh se Linux um and keraros. All of those libraries compiled with Phil C totally memory safe and Phil C does support set comp filter sandboxes. So all of OpenSSH's existing security features continue to work in this model. They're just even better um because it's memory safe. Um so like the next time there's a memory safety bug like regression in Phil C, you want to be running this because regression would cause just a panic in Philc. It would not lead to arbitrary uh remote code execution. Uh, and then if you want to have a memory safe pseudo and a memory safe core utils that don't have any of the new bugs introduced by the rewrites, this is also what you want to use because I have the original pseudo and the original GNU core utils just compiled with Bill C as well as a whole bunch of other stuff.
Uh, so now let me show you the demo. Are you ready for the demo? [applause] >> This was the demo. [laughter] Um, uh, I am running Libé Office Impress compiled with Phil C++. Um, all of its dependencies are compiled with PC C and P C++. [applause] Um,
uh, yeah. Uh, yeah, a lot of libraries.
Um, so I don't know what are some what are some big ones? GTK3, ICU, a bunch of Xlibs, Cairo, GDK, LZMA. I I can't list
all of them. It's too many. Um, but they're all Oh, CPython. There's a CPython interpreter inside of Libre Office because of course there is um [laughter] >> Pango. Boost. Yeah, that's right. Boost is in there. Boost works great in Phil C. Um yeah. Uh and on top of that um I'm running on an OS where the entire user land is compiled with Phil C. So the only uh yeah um [applause]
so it's about 94 million lines of CN C++ code compiled through my tool chain and running uh evidently reliably right like it seems to work. Um so uh let me show you this is uh this is the the Libre Office uh and to keep it real I actually made this entire slideshow from scratch in this operating system. So this me slide deck was made memory safely. Um
uh so this is this is this is the most memory safe presentation ever. Um [laughter] uh yeah. Do I do I have more time or am I how am I doing on time? >> Uh yeah, you have more time. >> All right, great. So I'll show you more stuff. Uh so uh I'm just launching a Weston terminal. Font size actually. Oh yeah, should just do this. All right, cool. Font size equals 36. I think is what I want. Yeah, there we go. Um, so here is a Weston terminal. Uh, also memory safe obviously. Uh, so then let's launch a memory safe or not. CPP or sorry, I've typed the wrong file name there. Uh, whoops. Hello. CPP.
There we go. So, I'm using Emacs, the full thing with the ELISP interpreter and everything. Um, and it's running C mode here. uh doing all that pretty syntax highlighting for me. Um and uh yeah, it's a hello world program. So let's let's compile it with uh with Phil C. So on this on this system, even if you run like G++, if you run version on it, um it's really my compiler. Um so it's Clang version 20.1.8, Philc version 0.68.1. Um, so I can, you know, compile. Whoop. Hold on. So I can compile this program. Hello. CPP. Let's do it with optimizations because why not? Um,
doop doop. And then we run it. You can see it works. Works great. Uh, now let's look at a program that's not so great. uh maybe a program that doesn't out of bounds allocate 16 bytes and then store it at index uh 67 um uh so then um yeah g++- o ob
um so you know the compiler does give me
a warning completely unrelated to phil c because the compiler has some static analysis for this but it still allows the program to be compiled um and when I run uh you get a beautiful Phil C panic. Um so um Phil C is
[applause] so the the way to read this is it's pointer, lower, comma, upper. So it's telling you that the pointer is out of bounds of the upper bound. Um and then
uh it's telling you where it happened. um you get this pretty stack trace and then uh thwarted a futile attempt to
violate memory safety. That's the slogan. Um
uh let's do let's see what else I got some other programs in here. There's um
what's another cool one? Uh let's do let's do the UF one. UF C. All right. So really simple. Malo free and then try to store into the object. Um, so let's see how this is
still working out is okay cool. Uh, G++ UAF UAF. C. Um, oh, what's going on
here? Oh, I'm trying to compile with G++ instead of GCC. Okay, so whatever. I'll just use CC. It doesn't matter. All of them link to Phil C. Um, so, uh, I
noticed I compiled without - G. So then uh you get this somewhere instead of the the method name. So - g preserves debug info and so you get actual uh you know where it happened. Um and boy is it taking its sweet time to dump the core into my USB stick. Uh that's not Phil C being slow. That's the kernel not having having a bad time with my USB stick. Um
and then I don't know I'll show you one more example. Uh inline. Um, so this
inline assembly is completely safe. Can anyone guess why in the world you would want to write this inline assembly? Cuz you just add and see. So why would you want to do this? Can anybody anybody know?
>> Sorry, I can't can't hear you. >> No. >> No. Yes. Um so uh crypto people um know that
if you tell the compiler hey a plus b the compiler might uh try to insert speculative branches in there like oh maybe I can optimize this code under this case so I'm going to if what what this is and run take different path depending on what what the values are and if that value that you're doing the plus on is like a a secret key then all of a sudden the compiler has introduced a timing ch uh side channel. So, um,
crypto [clears throat] system writers uh, prefer to either write their crypto entirely in assembly or they write it in C using tricks like like what I'm doing right here, adding using inline assembly. Um, now this inline assembly is actually correct. It's saying add the constant one to uh, uh, operand zero.
Compiler gets to pick operand zero. We're telling the compiler that operand zero is plus R means it's a register and I'm going to read and write to it. Um
and and I'm going to clobber the the control bit because AD sets the control bit depending on the the outcome. So if I if I compile this with Phil C
inline inline C let's let's enable debug info just to make it so this this works but what if I what if I mess this up like um
one way I could mess this up is forget to say that I clobbered the control bit
that's not what I want to There we go. I want to do that and then I want to run it. Um uh now uh it's a
little weird that I'm catching this error at runtime, but again send patches. Uh but the compiler finds that hey look this you cannot handle this inline assembly because it sets the it it sets flags but CC uh clobber is missing. Um, and if you tried to modify this inline assembly in any other way that would make it unsafe, you would uh similarly find yourself in this situation where that inline assembly snippet will panic at runtime or or it could even be a compiler error if someone wanted to make it a compiler error. Um, yeah. Well, that's my demo.
Hope you liked it. [applause]
>> [applause] >> questions.
>> So, you mentioned that. By the way, thank you for the This was amazing. Uh, you mentioned mostly unmodified code.
>> Yeah. for I'm kind of curious what would what what kind of code did you have to rewrite to be able to compile it in Philc like level of triviality the categories of like things that you you had to deal with. >> Yeah. Um uh uh two ma so first of all uh
my patch to uh uh to the software that I'm running on still includes changes where I replace a lot of inline assembly with intrinsics that I no longer have to do. I could remove that patch. I've started incrementally removing that patch since adding that feature. Uh so the the the things that I've changed that remain that have to have to be changed are for example uh in both Ruby
uh in in Ruby Emacs and GIB. GIB is the
foundation of GTK which is what Libre Office was running on. Um they all use
um tagged pointers that they represent as integers. So tagged pointers are fine. You could totally have a void star and like add add whatever you want to it. Put some bits in the high part of the pointer. That's fine. Um, but if you store it into memory as an integer, that's an integer store and I don't store the corresponding capability. So when you load the pointer back out, you don't get the capability and then you get a phil C panic saying you're trying to access a pointer with a null capability. So there's a pretty substantial change to Gibb and to everybody who uses GIB. very mechanical change but it's it's like a decent number of lines where I change the type of gtype which is the gtype is the type in gibb that does this from u from from g integer to gp pointer um and
then that results in some places assume that it's an integer and then you have to change those places um
and then uh the other thing that happens is uh for example in libre office
uh they actually take pointers
uh convert them to integers print them into a string pass the string around and then parse the string and get the pointer out of it. Um and the reason why they do that is they have their own guey framework abstraction on top of whatever guey framework they use and they want to be able to have the menu as well as some other things but the menu is a great example. They want like when they get a call back from the menu, they want a pointer to their like object describing what to do. But what GTK does is it gives you a menu command string. So they encode the pointer into the string. Um, and Philc has an API uh to
help you with that. It's called the Zpointer table. Basically, you can ask the Zpointer table, hey, convert this to an integer, but keep it in the table. Um, and the table is garbage collected. So if that memory goes away, the table will automatically remove that entry. And then later when you parse the string and get the integer, you call zpointer table to ask it for the pointer and then you get the pointer with the capability. Um so and that doesn't really break the memory safety promise because this it's like there's a zpointer table in Libre Office exactly for this purpose. It's just for the pointers that are being used for the for the commands. Um uh so
those are the two classes of changes. Uh I measured the size of the change to Libre Office and uh most of it was like
there's some build artifacts that changed uh just because their build system dirty is their source directory. Um and if you include those it was 4,000 lines changed but I think about half of those 4,000 lines were the build artifacts and Libre Office is like many millions of lines of code. So the percent change in something like Libre Office is on the order of 0.1%. Uh, but there's a lot of software packages out there that require zero changes. Um, so it just depends on how you write your code.
This is amazing. I can already see like some kind of this integrated into my CI pipeline. Um, but one question I have is
you now catch errors at runtime. Is there something that you've been surprised by or like any weird bug





