Skip to content

Trust Boundary

We mentioned previously that the C-FFI can be used to break pretty much every guarantee that Pony makes. This is because, once you’ve called into C, you are executing arbitrary machine code that can stomp memory addresses, write to anything, and generally be pretty badly behaved.

Trust boundaries

When we talk about trust, we don’t mean things you trust because you think they are perfect. Instead, we mean things you have to trust in order to get things done, even though you know they are imperfect.

In Pony, when you use the C-FFI, you are basically declaring that you trust the C code that’s being executed. That’s fine, because you may need it to get work done. But what about trusting someone else’s code to use the C-FFI? You may need to, but you definitely want to know that it’s happening.

Safe modules

The normal way to handle that is to be sure you’re using just the code you need to use in your program. Pretty simple! Don’t use some random package from the internet without looking at the code and making sure it doesn’t do nasty FFI stuff.

But we can do better than that.

In Pony, you can optionally declare a set of safe modules on the ponyc command line. For example, on a POSIX system:

ponyc --safe=files:net:process my_project

With this command, files, net and process may make C-FFI calls. The compiler also includes builtin in the safe list automatically. A C-FFI call in any other module is a compile-time error.

What code have you trusted when you list files? The compiler resolves each name on the safe list to a directory. For a name like files, it first checks for a directory of that name where you ran ponyc, then checks the compiler’s search paths. A module may use the C-FFI only if its directory matches one on the resolved list. When you review which code may call C, check which directories your build selects. See How the compiler finds a module for how a use in a source file is resolved.