Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/19eae39185c007c3c64a to your computer and use it in GitHub Desktop.
Save rightfold/19eae39185c007c3c64a to your computer and use it in GitHub Desktop.
declare void @llvm.gcroot(i8** %ptrloc, i8* %metadata)
declare void @styx_alloc(i8**, i64)
declare void @styx_list_objects()
define i32 @main() gc "shadow-stack" {
entry:
%int_size_ptr = getelementptr { i8*, i32 }* null, i32 1
%int_size = ptrtoint { i8*, i32 }* %int_size_ptr to i64
%x = alloca { i8*, i32 }*
%xptr = bitcast { i8*, i32 }** %x to i8**
call void @llvm.gcroot(i8** %xptr, i8* null)
call void @styx_alloc(i8** %xptr, i64 %int_size)
%y = alloca { i8*, i32 }*
%yptr = bitcast { i8*, i32 }** %y to i8**
call void @llvm.gcroot(i8** %yptr, i8* null)
call void @styx_alloc(i8** %yptr, i64 %int_size)
call void @styx_list_objects()
ret i32 0
}
#include <cstdint>
#include <iostream>
extern "C" void styx_alloc(void** target, std::uint64_t size) {
*target = operator new(size);
}
struct FrameMap {
std::int32_t numRoots;
std::int32_t numMeta;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wzero-length-array"
const void* meta[0];
#pragma clang diagnostic pop
};
struct StackEntry {
StackEntry* next;
const FrameMap* map;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wzero-length-array"
void* roots[0];
#pragma clang diagnostic pop
};
extern "C" StackEntry* llvm_gc_root_chain;
extern "C" void styx_list_objects() {
for (auto entry = llvm_gc_root_chain; entry != nullptr;
entry = entry->next) {
for (auto i = 0; i < entry->map->numRoots; ++i) {
std::cout << entry->roots[i] << '\n';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment