Skip to content

Instantly share code, notes, and snippets.

@andermoran
Created August 22, 2019 23:24
Show Gist options
  • Select an option

  • Save andermoran/aa219e345c806ccc4913cde0e2d7110a to your computer and use it in GitHub Desktop.

Select an option

Save andermoran/aa219e345c806ccc4913cde0e2d7110a to your computer and use it in GitHub Desktop.

Revisions

  1. andermoran created this gist Aug 22, 2019.
    24 changes: 24 additions & 0 deletions funkyClang.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // https://twitter.com/zneakr/status/1164651753993715712
    /* So @zneakr tweeted about this weird behavior and I decided to tinker with his example. In order to optimize, clang
    assigns fun_ptr to leak_all_my_secrets no matter what. This leads to "I have 9 toes" being printed no matter the
    result of the if statement. Super weird behavior from clang and I just wanted to make a note of it :)
    To reproduce this result:
    clang funkyClang.c -O1 -o funkyClang; ./funkyClang
    */

    #include <stdlib.h>
    #include <stdio.h>

    static void (*fun_ptr)(void);

    void leak_all_my_secrets () {
    printf("I have 9 toes\n");
    }

    int main() {
    if ((random() % 1000000) == 12321) {
    fun_ptr = leak_all_my_secrets;
    }
    fun_ptr();
    }