Using unsafe.Pointer to explore Linux system calls

By Matt Layher

Elevator Pitch

Package unsafe is one of the most powerful tools in the Go standard library, but it has some sharp edges. During this talk, you will learn techniques to make effective use of package unsafe, and real world examples with Linux system calls will be used to demonstrate its true power and flexibility.

Description

While building a new Linux sockets package for Go, I came across an interesting system call: ioctl(2). What’s an “ioctl” anyway? ioctl is a Linux system call that can be used to configure kernel objects, but it require passing blobs of arbitrary memory from userspace to the kernel in order to do so. Typically, the Go type system would prevent us from doing such an unusual and potentially dangerous operation, but we have an escape hatch: package unsafe.

Package unsafe is one of the most powerful and misunderstood pieces of the Go standard library. It’s a useful tool with an incredibly sharp edge, but if you follow a few guidelines, you can achieve a level of flexibility that is otherwise impossible in normal Go programs. In particular, unsafe can be used to:

  • cast memory between arbitrary types via the unsafe.Pointer type, stepping around Go’s type system
  • improve performance in specific circumstances
  • pass Go equivalents of C structures in Linux system calls to configure the kernel

This talk will demonstrate the necessary techniques for safe and productive usage of package unsafe. Real world examples will be used to explain exactly when, where, and why the use of unsafe is necessary to solve a given problem. The audience will learn:

  • the unique operations that can be performed on unsafe.Pointer, but not on other Go types
  • the mechanics of performing unsafe.Pointer conversions between various types
  • how to implement effective safeguards in situations where use of unsafe is unavoidable
  • how to pass data using unsafe.Pointer to the Linux kernel to perform system calls

During this talk, I will share my journey into the fascinating world of unsafe, and I will show you how to build Go applications that can make the Linux kernel bend to your will. At the presentation’s conclusion, audience members will walk away with the knowledge and confidence necessary to make effective use of package unsafe in their applications.

Notes

This talk is intended for intermediate-to-advanced Go users who are interested in learning when, where, and why package unsafe can be necessary to solve a problem outside the confines of Go’s type system.

Although the package name “unsafe” is meant to convey a warning to its users, it can sometimes be necessary to make use of such an escape hatch in unusual Go applications. I intend to cater to audience members who are curious about the use of unsafe, but would like to learn useful patterns and best practices for doing so. I plan to tell the story of the lessons I learned while building the aforementioned Linux socket package, in order to empower the audience members to explore their own use cases for package unsafe.

I’ve been working in Go for the past 5 years, and have a great deal of experience working with unsafe and system calls both at work and in my open source projects. I have previously blogged about the topic, and I believe I can effectively convey many of the important concepts in a conference presentation format. I believe strongly in taking complex and nuanced concepts, breaking them into small pieces, and composing those pieces together in a way that is palatable for an audience with a variety of levels of experience in Go. I have previously spoken at several software conferences related to Go and other projects, and would absolutely love an opportunity to share my knowledge with the Go community at [conference].

Thank you very much for your time and for your consideration.