Skip to content

Instantly share code, notes, and snippets.

@abcarroll
Created September 15, 2020 20:06
Show Gist options
  • Save abcarroll/06c801e0e9fd7f69c29502d22a46483a to your computer and use it in GitHub Desktop.
Save abcarroll/06c801e0e9fd7f69c29502d22a46483a to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
//void _dump_debug_trace() {}
//void _dump_string(void *pointerToString) {}
// This will work for GCC and clang for x64
// #define GET_STACK_POINTER(NAME) uintptr_t NAME; asm("mov %%rsp, %0": "=r" ( NAME ));
#define FRAME_SIZE 4096
#define dump_frame \
dump("Current Stack Frame:", __builtin_frame_address(0), FRAME_SIZE); \
dump("Current Stack Frame:", __builtin_frame_address(1), FRAME_SIZE);
void dump(char *desc, void *pointerToValue, int len) {
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char *)pointerToValue;
// Output description if given.
if (desc != NULL)
printf("%s:\n", desc);
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\n", buff);
// Output the offset.
printf(" %04x ", i);
}
// Now the hex code for the specific character.
printf(" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
buff[i % 16] = '.';
} else {
buff[i % 16] = pc[i];
}
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}
// And print the final ASCII bit.
printf(" %s\n", buff);
}
#include <stdio.h>
#include "dump.c"
void
random_stack_increase(int depth)
{
if(depth < 3) {
random_stack_increase(depth++);
} else {
dump_frame; // dump.c
return;
}
}
void
main(void)
{
int *a = malloc(100);
int *b = malloc(200);
int z = a < b;
printf("%i", z);
char f[3] = "abc";
dump("abc", &f, 5);
char *g = malloc(1024);
#if __GNUC__
printf("Hello world!\n");
#else
printf("You suck\n");
#endif
}
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@.str = private unnamed_addr constant [5 x i8] c"%s:\0A\00", align 1
@.str.1 = private unnamed_addr constant [6 x i8] c" %s\0A\00", align 1
@.str.2 = private unnamed_addr constant [8 x i8] c" %04x \00", align 1
@.str.3 = private unnamed_addr constant [6 x i8] c" %02x\00", align 1
@.str.4 = private unnamed_addr constant [4 x i8] c" \00", align 1
@.str.5 = private unnamed_addr constant [21 x i8] c"Current Stack Frame:\00", align 1
@.str.6 = private unnamed_addr constant [3 x i8] c"%i\00", align 1
@__const.main.f = private unnamed_addr constant [3 x i8] c"abc", align 1
@.str.7 = private unnamed_addr constant [4 x i8] c"abc\00", align 1
@.str.8 = private unnamed_addr constant [14 x i8] c"Hello world!\0A\00", align 1
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @dump(i8* %0, i8* %1, i32 %2) #0 {
%4 = alloca i8*, align 8
%5 = alloca i8*, align 8
%6 = alloca i32, align 4
%7 = alloca i32, align 4
%8 = alloca [17 x i8], align 16
%9 = alloca i8*, align 8
store i8* %0, i8** %4, align 8
store i8* %1, i8** %5, align 8
store i32 %2, i32* %6, align 4
%10 = load i8*, i8** %5, align 8
store i8* %10, i8** %9, align 8
%11 = load i8*, i8** %4, align 8
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %4, align 8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %3
store i32 0, i32* %7, align 4
br label %17
17: ; preds = %78, %16
%18 = load i32, i32* %7, align 4
%19 = load i32, i32* %6, align 4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %81
21: ; preds = %17
%22 = load i32, i32* %7, align 4
%23 = srem i32 %22, 16
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %21
%26 = load i32, i32* %7, align 4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %7, align 4
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %21
%35 = load i8*, i8** %9, align 8
%36 = load i32, i32* %7, align 4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38, align 1
%40 = zext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i8*, i8** %9, align 8
%43 = load i32, i32* %7, align 4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45, align 1
%47 = zext i8 %46 to i32
%48 = icmp slt i32 %47, 32
br i1 %48, label %57, label %49
49: ; preds = %34
%50 = load i8*, i8** %9, align 8
%51 = load i32, i32* %7, align 4
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53, align 1
%55 = zext i8 %54 to i32
%56 = icmp sgt i32 %55, 126
br i1 %56, label %57, label %62
57: ; preds = %49, %34
%58 = load i32, i32* %7, align 4
%59 = srem i32 %58, 16
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 %60
store i8 46, i8* %61, align 1
br label %72
62: ; preds = %49
%63 = load i8*, i8** %9, align 8
%64 = load i32, i32* %7, align 4
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8, i8* %66, align 1
%68 = load i32, i32* %7, align 4
%69 = srem i32 %68, 16
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 %70
store i8 %67, i8* %71, align 1
br label %72
72: ; preds = %62, %57
%73 = load i32, i32* %7, align 4
%74 = srem i32 %73, 16
%75 = add nsw i32 %74, 1
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 %76
store i8 0, i8* %77, align 1
br label %78
78: ; preds = %72
%79 = load i32, i32* %7, align 4
%80 = add nsw i32 %79, 1
store i32 %80, i32* %7, align 4
br label %17
81: ; preds = %17
br label %82
82: ; preds = %86, %81
%83 = load i32, i32* %7, align 4
%84 = srem i32 %83, 16
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %82
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%88 = load i32, i32* %7, align 4
%89 = add nsw i32 %88, 1
store i32 %89, i32* %7, align 4
br label %82
90: ; preds = %82
%91 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 0
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %91)
ret void
}
declare dso_local i32 @printf(i8*, ...) #1
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @random_stack_increase(i32 %0) #0 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = icmp slt i32 %3, 3
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* %2, align 4
%7 = add nsw i32 %6, 1
store i32 %7, i32* %2, align 4
call void @random_stack_increase(i32 %6)
br label %11
8: ; preds = %1
%9 = call i8* @llvm.frameaddress.p0i8(i32 0)
call void @dump(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %9, i32 4096)
%10 = call i8* @llvm.frameaddress.p0i8(i32 1)
call void @dump(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %10, i32 4096)
br label %11
11: ; preds = %8, %5
ret void
}
; Function Attrs: nounwind readnone
declare i8* @llvm.frameaddress.p0i8(i32 immarg) #2
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @main() #0 {
%1 = alloca i32*, align 8
%2 = alloca i32*, align 8
%3 = alloca i32, align 4
%4 = alloca [3 x i8], align 1
%5 = alloca i8*, align 8
%6 = call noalias i8* @malloc(i64 100) #5
%7 = bitcast i8* %6 to i32*
store i32* %7, i32** %1, align 8
%8 = call noalias i8* @malloc(i64 200) #5
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %2, align 8
%10 = load i32*, i32** %1, align 8
%11 = load i32*, i32** %2, align 8
%12 = icmp ult i32* %10, %11
%13 = zext i1 %12 to i32
store i32 %13, i32* %3, align 4
%14 = load i32, i32* %3, align 4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %14)
%16 = bitcast [3 x i8]* %4 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %16, i8* align 1 getelementptr inbounds ([3 x i8], [3 x i8]* @__const.main.f, i32 0, i32 0), i64 3, i1 false)
%17 = bitcast [3 x i8]* %4 to i8*
call void @dump(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %17, i32 5)
%18 = call noalias i8* @malloc(i64 1024) #5
store i8* %18, i8** %5, align 8
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
ret void
}
; Function Attrs: nounwind
declare dso_local noalias i8* @malloc(i64) #3
; Function Attrs: argmemonly nounwind willreturn
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #4
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nounwind readnone }
attributes #3 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #4 = { argmemonly nounwind willreturn }
attributes #5 = { nounwind }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"Debian clang version 12.0.0-++20200909020009+d4b88ac1658-1~exp1~20200909120636.3474"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment