llvm

LLVM笔记(6) - CompilerRT之safestack

落花浮王杯 提交于 2019-12-06 13:15:59
好久没更新博客了, 最近调研安全编译选项(各类sanitizer), 抽空做个笔记. 本来想系统的分析一下compiler-rt代码, 但是最近实在太懒了, 所以先介绍最简单的安全栈safestack, 之后有空再补上compiler-rt框架以及其它sanitizer工具. 什么是safestack safestack是Code Pointer Integrity (CPI) Project的部分实现. CPI(代码指针完整性)是为了阻止控制流劫持攻击而提出的一种通过保证代码指针安全性的设计, 关于CPI的具体内容可以参考论文https://dslab.epfl.ch/pubs/cpi.pdf或官网(https://dslab.epfl.ch/proj/cpi/). safestack是CPI的一个组件, 但也可以单独使用(用于防止基于栈的控制流攻击), 它通过将栈分为两个独立区域, safe stack(用于存储函数返回地址, 寄存器spill, 保证安全访问的局部变量)和unsafe stack(其它存储在栈上的内容)来保证即使栈空间溢出也不会影响到程序流执行(链接地址不会被覆写). 一个基于栈攻击的例子 目前CPI并没有完整的实现, 其preview版本可以通过https://dslab.epfl.ch/proj/cpi/levee-early-preview-0.2

How to generate metadata for LLVM IR?

做~自己de王妃 提交于 2019-12-06 12:51:54
I am trying to generate a metadata for the LLVM IR i have generated. I want to generate a metadata of the form : !nvvm.annotations = !{!0} !0 = metadata !{void ()* @foo, metadata !"kernel", i32 1} Where foo is a function in my LLVM IR. Right now I am only able to generate a metadata of the form: !nvvm.annotations = !{!0} !0 = !{!"kernel"} I used the following code for the above metadata generation. char metaDataArgument[512]; sprintf(metaDataArgument, "%s", pipelineKernelName); llvm::NamedMDNode *nvvmMetadataNode = LLVMModule->getOrInsertNamedMetadata("nvvm.annotations"); llvm::MDNode

llvm pass: How to insert a variable using existing variable value

蓝咒 提交于 2019-12-06 11:56:14
问题 I defined int a = 5 ; in the source code, and I transform the source to LLVM IR: %a = alloca i32, align 4 store i32 5, i32* %a, align 4 I want to insert int b = a; by writing a pass. I compile int a=5; int b=a into LLVM IR, it load "a" first, then store it. I also checked the doxygen, in which the LoadInst is LoadInst (Value *Ptr, const Twine &NameStr, Instruction *InsertBefore) Still, I don't know how to get the Value of "a". How to get a variable value? 回答1: In LLVM IR the sequence int a =

How to intercept LLVM lli tool input?

久未见 提交于 2019-12-06 11:54:53
I'd like to use LLVM lli tool as static library (rename main() to lli() and export it in libLLi.a) - to create rich UI for it. How can i modify it (or use without modifications) in order to intercept stdin? Assume i know how to generate LLVM assembly file (using clang -S -emit-llvm .. -o output.ll ) and how to execute it using lli tool ( lli output.ll ). Common use case: Source code of simple app to be interpreted by lli: #include <iostream> using namespace std; int main() { char name[128]; cout << "type your name: "; cin.getline(name, sizeof(name)); cout << "hi, " << name << endl; return 0; }

Convert std::string to llvm::MemoryBuffer

僤鯓⒐⒋嵵緔 提交于 2019-12-06 10:47:01
问题 I am looking to create an LLVM Module from existing LLVM IR code. The two methods I have found are the following: ParseIRFile - This accepts a file name and generates a module ParseIR - This accepts MemoryBuffer and generates a module I want to create a Module when the LLVM IR is already read to a string as an std::string or const char * . Is there a way to convert an IR string to llvm::MemoryBuffer ? 回答1: I figured this out with the help of a colleague. This is how you would do it: std:

Using the LLVM linker to produce C code

半世苍凉 提交于 2019-12-06 10:15:31
问题 I have tried to generate C code from C++ code compiled by llvm-g++ , using the following commands: llvm-g++ -emit-llvm -c ./example.cpp -o example.o llc -march=c example.o I tried these commands on a machine running Ubuntu (Linux 3.16.0-45-generic) . However instead of writing C code to standard output, the LLVM static linker reports that the compiled file is invalid: error: expected top-level entity . How can I generate C code using the LLVM linker? 回答1: Unfortunately emitting LLVM bitcode

Linker error while building clang using Makefile with a checker

半城伤御伤魂 提交于 2019-12-06 09:57:10
问题 I'm facing a problem during the linking phase. What am I trying to do? I'm trying to add a checker to clang. I built LLVM and libc++ from the source code (Yes, some test failed during libcxx testing after build). To identify the libc++ library, I added the following code to the Clang's root directory Makefile CXX.Flags += -stdlib=libc++ CXX.Flags += -std=c++11 CXX.Flags += -nostdinc++ CXX.Flags += -I/path/to/my/own/c++library/libcxx/include LD.Flags += -L/path/to/my/own/c++library/libcxx/lib

Proper way of compiling OpenCL applications and using available compiler options

心已入冬 提交于 2019-12-06 09:35:06
I am a newbie in OpenCL stuffs. Whats is the best way to compiler an OpenCL project ? Using a supported compiler ( GCC or Clang ): When we use a compiler like gcc or clang , how do we control these options? Are they have to be set inside the source code, or, likewise the normal compilation flow we can pass them on the command line. Looking at the Khornos-Manual-1.2 , there are a few options provided for cl_int clBuildProgram for optimizations. : gcc|clang -O3 -I<INCLUDES> OpenCL_app.c -framework OpenCL OPTION -lm Actually, I Tried this and received an error : gcc: error: unrecognized command

How to tell clang that my LLVM Target should use 16-bit 'int'?

笑着哭i 提交于 2019-12-06 08:49:12
For my PIC Backend, I want 'int' to be 16 bits. How can I / my target tell clang what should be the size of 'int'? Defining 16-bit registers only seems not sufficient. Currently "clang -O2 -emit-llvm -target pic" converts int foo(int a, int b) { return a + b; } to this IR code, using 32-bit integers: ; ModuleID = '../test/sum.c' source_filename = "../test/sum.c" target datalayout = "e-m:e-p:16:16-i16:16-a:0:16-n16-S16" target triple = "pic" ; Function Attrs: norecurse nounwind readnone define i32 @foo(i32 %a, i32 %b) local_unnamed_addr #0 { entry: %add = add nsw i32 %b, %a ret i32 %add }

LLVM Error : External function could not be resolved

99封情书 提交于 2019-12-06 07:47:15
I am reading the LLVM's Kaleidoscope tutorial ( http://llvm.org/docs/tutorial/index.html ). I wanted to compile and test the language. After some compiler's errors (EngineBuilder and Module's constructor, linking libs...), the example program was built. Then, I tried the language. I got a few problems with InitializeNativeTargets, DataLayoutPass... But I managed to correct them. Howewer, I don't manage to resolve one error. When I write extern printd(x); printd(5); , the program doesn't work : "LLVM ERROR : Program used external function 'printd' which could not be resolved". I looked for the