Assert your Go code is inlined and bounds-check eliminated

gcassert

gcassert is a program for making assertions about compiler decisions in Golang programs, via inline comment directives like //gcassert:inline.

Example

Given a file foo.go:

package foo

func addOne(i int) int {
    return i+1
}

//gcassert:inline
func addTwo(i int) int {
    return i+1
}

func a(ints []int) int {
    var sum int
    for i := range ints {
        //gcassert:bce,inline
        sum += addOne(ints[i])

        sum += addTwo(ints[i]) //gcassert:bce

        sum += ints[i] //gcassert:bce
    }
    return sum
}

The inline //gcassert directive will cause gcassert to fail if the line sum += addOne(ints[i]) is either not inlined or contains bounds checks.

A //gcassert:inline directive on a function will cause gcassert to fail if any of the callers of that function do not get inlined.

//gcassert comments expect a comma-separated list of directives after //gcassert:. They can be included above the line in question or after, as an inline comment.

Installation

go get github.com/jordanlewis/gcassert/cmd/gcassert

Usage

Run gcassert on packages containing gcassert directives, like this:

gcassert ./package/path

The program will output all lines that had a gcassert directive that wasn't respected by the compiler.

For example, running on the testdata directory in this library will produce the following output:

$ gcassert ./testdata
testdata/bce.go:8:	fmt.Println(ints[5]): Found IsInBounds
testdata/bce.go:16:	sum += notInlinable(ints[i]): call was not inlined
testdata/inline.go:22:	sum += notInlinable(i): call was not inlined

Inspecting each of the listed lines will show a //gcassert directive that wasn't upheld when running the compiler on the package.

Directives

//gcassert:inline

The inline directive on a CallExpr asserts that the following statement contains a function that is inlined by the compiler. If the function does not get inlined, gcassert will fail.

The inline directive on a FuncDecl asserts that every caller of that function is actually inlined by the compiler

//gcassert:bce

The bce directive asserts that the following statement contains a slice index that has no necessary bounds checks. If the compiler adds bounds checks, gcassert will fail.

Owner
Jordan Lewis
Working on databases at @cockroachdb. https://twitch.tv/large__data__bank
Jordan Lewis
Comments
  • False negative for method calls involving inlinable selector expressions

    False negative for method calls involving inlinable selector expressions

    Input:

    package pkg
    
    func foo() {
    	Gen().Layout()
    }
    
    func Gen() S {
    	return S{}
    }
    
    //gcassert:inline
    func (s S) Layout() {
    	select {}
    }
    
    type S struct{}
    

    go build -gcflags=-m:

    $ go build -gcflags=-m foo.go
    # command-line-arguments
    ./foo.go:7:6: can inline Gen
    ./foo.go:3:6: can inline foo
    ./foo.go:4:5: inlining call to Gen
    

    Output of gcassert@latest:

    None.

    This works fine for var s S; s.Layout(). This also works fine if Gen cannot be inlined.

  • allow a space between the double slash and gcassert:

    allow a space between the double slash and gcassert:

    Previously, only the comments of the form //gcassert: would match the regex pattern. This commit eases up the matching to also work for // gcassert: (note the space after the double slash). We already have a few cases like that in CockroachDB repo (even introduced by the author of the gcassert himself :D ), so it makes sense to relax the matching a bit.

  • go.mod: bump `x/tools`

    go.mod: bump `x/tools`

    On Go 1.18, gcassert fails with e.g.:

    internal error: package "github.com/cockroachdb/cockroach/pkg/base" without types was imported from "github.com/cockroachdb/cockroach/pkg/storage"
    

    Upgrading x/tools fixes this.

    Thanks for gcassert btw, it's really neat!

  • gcassert doesn't notice when compilation fails

    gcassert doesn't notice when compilation fails

    gcassert doesn't notice when compilation fails and subsequently considers all assertions as having failed. This can be quite confusing.

    $ go build foo.go
    # command-line-arguments
    ./foo.go:8:9: undefined: potato
    $ go run github.com/jordanlewis/gcassert/cmd/gcassert@latest foo.go
    foo.go:3:	foo(): call was not inlined
    exit status 1
    
  • [dnm] demonstrate that dead code hits assertions

    [dnm] demonstrate that dead code hits assertions

    In https://github.com/cockroachdb/apd/pull/103, I'm looking into hooking up gcassert to CI. It's pretty magical to be able to add these assertions in and protect against regressions in carefully tuned code. I'd like to start using it in more places.

    Unfortunately, I'm running into the problem that platform-specific conditional logic that leads to compile-time dead code can cause assertion failures if one of these assertions lands in the dead code. I'm not quite sure what to do about that, so I figured I'd open up a PR to demonstrate the problem.

VHDL Simulator, code VHDL in your browser! 🤖
VHDL Simulator, code VHDL in your browser! 🤖

VHDL Runner ?? VHDL Simulator, code VHDL in your browser! ?? Usage Go to https://vhdl.mateuszwozniak.com/ Next, type some VHDL code, with appropriate

Feb 6, 2022
Simple boilerplate code to get started with building and deploying a serverless CRUD API

Simple boilerplate code to get started with building and deploying a serverless CRUD API with Go, MongoDB and Netlify

Jan 20, 2022
An open source, online coding platform that offers code practice and tutoring in 50 different programming languages
An open source, online coding platform that offers code practice and tutoring in 50 different programming languages

Exercism Golang En este repositorio voy a subir los ejercicios de la plataforma

Jan 7, 2022
Markdown version of Reverse Engineering the source code of the BioNTech/Pfizer SARS-CoV-2 Vaccine

The big BNT162b2 archive All vaccine data here is sourced from this World Health Organization document. This describes the RNA contents of the BNT162b

Dec 2, 2022
Go-Notebook is inspired by Jupyter Project (link) in order to document Golang code.
Go-Notebook is inspired by Jupyter Project (link) in order to document Golang code.

Go-Notebook Go-Notebook is an app that was developed using go-echo-live-view framework, developed also by us. GitHub repository is here. For this proj

Jan 9, 2023
Example code for my Cadence Intro Workshop

Temporal Intro Workshop This repository contains example code for my Temporal Intro Workshop. Prerequisites Git, Make, etc. Make sure you have the lat

Dec 18, 2022
Sample code for my Go Deadlocks talk

Go Deadlocks Talk This is sample code for my Go Deadlocks talk. Simple simple - a super simple deadlock simple2 - the same but with an extra Go deadlo

Jul 30, 2022
Test-Driven Development code kata in Golang

Bowling Kata Golang Test-Driven Development code kata in Golang Bowling Rules The game consists of 10 frames. In each frame the player has two rolls t

Jan 5, 2022
A series of small code snipppets & exercises to learn Go.

Learning-Go A series of small code snipppets & exercises to learn Go. WARNING: During this excercise you will be learning along with me, I am not your

Dec 18, 2021
Source code of the 100 Go Mistakes book

100 Go Mistakes and How to Avoid Them Source code of 100 Go Mistakes and How to Avoid Them. Table of Contents Chapter 1 - Introduction Chapter 2 - Cod

Dec 30, 2022
Code solution for question against internship application at Eluvio

Eluvio Problem Statement Imagine you have a program that needs to look up information about items using their item ID, often in large batches. Unfortu

Feb 5, 2022
Mastering-go-exercises - Some code examples from Mihalis Tsoukalos book titled Mastering GO

Mastering go exercises This is a training repository. Here are some code example

Feb 16, 2022
Cook amazing genetic parts using our cookbook. Recipes and synthetic biology tools to take your breath away.

friendzymes-cookbook Friendly tools for a friendly community. A collection of tutorials and genetic tools for synthetic biology. This cookbook is a su

Aug 19, 2022
💯 Materials to help you rock your next coding interview

Tech Interview Handbook Credits: Illustration by @leftaligned Read on the website Black Lives Matter. Support the Equal Justice Initiative What is thi

Jan 4, 2023
Publish posts to your personal facebook profile.

FB Post Publish posts to your personal facebook profile. Usage Get list of codes for 2FA. then copy them and paste them in txt/codes.txt use the comma

Jan 13, 2022
Easily kick-start your python project with very opinionated best practices.

Pyproject Easily kickstart your Python project with very opionionated best practices. Manage your project using poetry https://python-poetry.org/ Add

Jan 24, 2022
Tool (in Go!) to compare and diff container and host environments. Dinosaur fun!

Compe compare environments and other things between containers, and host ??️ This is a simple tool to compare environments and other features of conta

Sep 24, 2022
Go Cheat Sheet - An overview of Go syntax and features.

Go Cheat Sheet - An overview of Go syntax and features.

Dec 31, 2022
An online book focusing on Go syntax/semantics and runtime related things

Go 101 is a book focusing on Go syntax/semantics and all kinds of runtime related things. It tries to help gophers gain a deep and thorough understanding of Go. This book also collects many details of Go and in Go programming. The book is expected to be helpful for both beginner and experienced Go programmers.

Dec 29, 2022