LLVM Memory Review and Function Example

 
EXERCISE #11
 
1
 
LLVM MEMORY REVIEW
 
Write your name and answer the following on a piece of paper
 
Write an LLVM IR function that takes a pointer to an array of ints and returns the
element at index 2
 
ADMINISTRIVIA
AND
ANNOUNCEMENTS
 
 
LLVM CALLS
 
EECS 677: Software Security Evaluation
Drew Davidson
 
CLASS PROGRESS
 
WE’RE BASICALLY READY TO DO STATIC
ANALYSIS
 
4
 
5
 
LAST TIME: LLVM MEMORY
 
REVIEW: LAST LECTURE
 
D
ESCRIBED
 LLVM’
S
 C
ONCEPT
 
OF
 N
AMED
 M
EMORY
 
No mathematical relationship between distinct named
memory items
Guaranteed mathematical relation WITHIN named
memory items (as exploited by GEP)
 
D
ECLARING
 
MEMORY
 
Local memory:
 
%ptrL = alloca i32, align 4
Global memory:
 
@ptrG = global i32 2, align 4
(Global) constant: Guaranteed mathematical relation
 
@ptrC = constant i32 2, align 4
 
%ptrLarr = alloca [8 x i32], align 4
 
%ptrGstruct = global i32 {i32, i8}, align 4
 
@ptrCstructs = constant [2 x {i8, i32}]
        [{i8, i32} {i8 1, i32 2}, {i8, i32}{ i8 3, i32 4} ] ,
        align 4
 
6
 
LAST TIME: LLVM MEMORY
 
REVIEW: LAST LECTURE
 
D
ECLARING
 
MEMORY
 
Local memory:
 
%ptrL = alloca i32, align 4
Global memory:
 
@ptrG = global i32 2, align 4
(Global) constant: Guaranteed mathematical relation
 
@ptrC = constant i32 2, align 4
 
A
CCESSING
 
MEMORY
 
%ptrLarr = alloca [8 x i32], align 4
 
%ptrGstruct = global i32 {i32, i8}, align 4
 
@ptrCstructs = constant [2 x {i8, i32}]
        [{i8, i32} {i8 1, i32 2}, {i8, i32}{ i8 3, i32 4} ] ,
        align 4
 
Store scalar:
 
store i32 1, i32* %ptrL
Global memory:
 
%reg = load i32, i32* @ptrG
Load index of aggregate type
 
%
eltPtr = getelementptr [2 x {i8, i32}],
 
    [2 x {i8, i32}]* @ptrCstructs, i64 0, i64 0, i32 1
 
%res = load i32, i32* %ret
7
FOLLOW-UP WHAT IS THE #0 HERE?
REVIEW: LAST LECTURE
 
It’s an alias for an attribute list
 
LECTURE OUTLINE
 
A little more GEP intuition
Function calls
 
9
 
SOME TIME WITH GEP
 
LLVM BITCODE
 
10
 
GETELEMENTPTR
 
LLVM BITCODE
 
11
 
WRITING “INTERESTING” PROGRAMS
 
LLVM BITCODE
 
W
E
 
CAN
 
NOW
 
WRITE
 T
URING
 
COMPLETE
 
PROGRAMS
 
B
UT
 
THESE
 
PROGRAMS
 
ARE
 
VERY
 B
ORING
!
 
We need to interact with external functionality
12
LLVM CALLS
LLVM BITCODE
 
C
ONSTRAINED
 S
YNTAX
call         <callee sig>   <function name>         ( <argList> )
%res = call  i32(i32,i32)     @max     (i32 1, i32 2)
Somewhat surprisingly, does not (always) require the full function signature of the callee!
G
ENERAL
 
SYNTAX
 
call <return type> <function name>     ( <argList> )
 
%res = call  i32        @max     (i32 1, i32 2)
 
The general syntax IS required if a function has varargs
 
%len = call i32 (i8*, ...) @printf(i8* %strPtr, i32 123)
 
13
 
LLVM EXTERNAL CALLS
 
LLVM BITCODE
 
E
XAMPLE
 
%len = call i32 (i8*, ...) @printf(i8* %strPtr, i32 123)
 
14
 
LLVM ATOI
 
LLVM BITCODE
 
T
O
 
THE
 T
ERMINAL
!
 
15
 
LLVM PRINTF
 
LLVM BITCODE
 
T
O
 
THE
 T
ERMINAL
!
 
16
 
RUNNING THE LLVM TOOLS: CLANG
 
LLVM BITCODE
 
clang -S -emit-llvm foo.c -o foo.ll -disable-O0-optnone
 
17
 
RUNNING THE LLVM TOOLS: OPT
 
LLVM BITCODE
 
opt -dot-cfg foo.ll -disable-output
 
WRAP-UP
 
NEXT TIME
 
WRITING AN ANALYSIS
 
19
Slide Note
Embed
Share

In this exercise, an LLVM IR function is created to extract the element at index 2 from a pointer to an array of integers. The concept of named memory in LLVM is reviewed, distinguishing between local, global, and constant memory declarations. Additionally, memory access operations like store, load, and getelementptr are discussed.

  • LLVM
  • Memory Review
  • Function Example
  • Static Analysis

Uploaded on Aug 14, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. EXERCISE #11 LLVM MEMORY REVIEW Write your name and answer the following on a piece of paper Write an LLVM IR function that takes a pointer to an array of ints and returns the element at index 2 1

  2. LLVM CALLS EECS 677: Software Security Evaluation Drew Davidson

  3. 4 CLASS PROGRESS WE RE BASICALLY READY TO DO STATIC ANALYSIS

  4. 5 LAST TIME: LLVM MEMORY REVIEW: LAST LECTURE DESCRIBEDLLVM S CONCEPTOF NAMED MEMORY No mathematical relationship between distinct named memory items Guaranteed mathematical relation WITHIN named memory items (as exploited by GEP) DECLARINGMEMORY Local memory: %ptrL = alloca i32, align 4 Global memory: @ptrG = global i32 2, align 4 (Global) constant: Guaranteed mathematical relation @ptrC = constant i32 2, align 4 %ptrLarr = alloca [8 x i32], align 4 %ptrGstruct = global i32 {i32, i8}, align 4 @ptrCstructs = constant [2 x {i8, i32}] [{i8, i32} {i8 1, i32 2}, {i8, i32}{ i8 3, i32 4} ] , align 4

  5. 6 LAST TIME: LLVM MEMORY REVIEW: LAST LECTURE DECLARINGMEMORY Local memory: %ptrL = alloca i32, align 4 Global memory: @ptrG = global i32 2, align 4 (Global) constant: Guaranteed mathematical relation @ptrC = constant i32 2, align 4 %ptrLarr = alloca [8 x i32], align 4 %ptrGstruct = global i32 {i32, i8}, align 4 @ptrCstructs = constant [2 x {i8, i32}] [{i8, i32} {i8 1, i32 2}, {i8, i32}{ i8 3, i32 4} ] , align 4 ACCESSINGMEMORY Store scalar: store i32 1, i32* %ptrL Global memory: %reg = load i32, i32* @ptrG Load index of aggregate type %eltPtr = getelementptr [2 x {i8, i32}], [2 x {i8, i32}]* @ptrCstructs, i64 0, i64 0, i32 1 %res = load i32, i32* %ret

  6. 7 FOLLOW-UP WHAT IS THE #0 HERE? REVIEW: LAST LECTURE It s an alias for an attribute list

  7. LECTURE OUTLINE A little more GEP intuition Function calls

  8. 9 SOME TIME WITH GEP LLVM BITCODE

  9. 10 GETELEMENTPTR LLVM BITCODE

  10. 11 WRITING INTERESTING PROGRAMS LLVM BITCODE WECANNOWWRITE TURINGCOMPLETEPROGRAMS BUTTHESEPROGRAMSAREVERY BORING! We need to interact with external functionality

  11. 12 LLVM CALLS LLVM BITCODE GENERALSYNTAX call <callee sig> <function name> ( <argList> ) %res = call i32(i32,i32) @max (i32 1, i32 2) Somewhat surprisingly, does not (always) require the full function signature of the callee! CONSTRAINED SYNTAX call <return type> <function name> ( <argList> ) %res = call i32 @max (i32 1, i32 2) The general syntax IS required if a function has varargs %len = call i32 (i8*, ...) @printf(i8* %strPtr, i32 123)

  12. 13 LLVM EXTERNAL CALLS LLVM BITCODE EXAMPLE %len = call i32 (i8*, ...) @printf(i8* %strPtr, i32 123)

  13. 14 LLVM ATOI LLVM BITCODE TOTHE TERMINAL!

  14. 15 LLVM PRINTF LLVM BITCODE TOTHE TERMINAL!

  15. 16 RUNNING THE LLVM TOOLS: CLANG LLVM BITCODE clang -S -emit-llvm foo.c -o foo.ll -disable-O0-optnone

  16. 17 RUNNING THE LLVM TOOLS: OPT LLVM BITCODE opt -dot-cfg foo.ll -disable-output

  17. WRAP-UP

  18. 19 NEXT TIME WRITING AN ANALYSIS

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#