Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.

Slides:



Advertisements
Similar presentations
Algorithms and data structures
Advertisements

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
User-Level Memory Management in Linux Programming
Templates and the STL Bryce Boe 2012/09/10 CS32, Summer 2012 B.
Informática II Prof. Dr. Gustavo Patiño MJ
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Run-Time Storage Organization
C and Data Structures Baojian Hua
Run-time Environment and Program Organization
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Unions and Memory layout.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
Analyzing C/C++ Vulnerabilities -- Mike Gerschefske.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Object Lifetime and Pointers
Processes and threads.
CS/COE 0447 (term 2181) Jarrett Billingsley
Data in Memory variables have multiple attributes symbolic name
CSE 374 Programming Concepts & Tools
Process Memory COMP 40: Machine Structure and
Machine-Level Programming V: Unions and Memory layout
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
CSE 374 Programming Concepts & Tools
Run-time organization
Procedures (Functions)
C Basics.
Lecture 6 C++ Programming
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Assembly Language Programming I: Introduction
Chapter 15 Pointers, Dynamic Data, and Reference Types
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Pointers, Dynamic Data, and Reference Types
C Programming APP3o.
Dynamic Memory Allocation
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Format String.
Understanding Program Address Space
Dynamic Memory A whole heap of fun….
Comp Org & Assembly Lang
C (and C++) Pointers April 4, 2019.
Comp Org & Assembly Lang
Dynamic Memory A whole heap of fun….
Copyright © 2013 Elsevier Inc. All rights reserved.
Computer Architecture and System Programming Laboratory
Understanding and Preventing Buffer Overflow Attacks in Unix
COMP755 Advanced Operating Systems
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B

Overview Review of Variable Segment Locations Variable types and storage

Review The program’s code is stored in the text segment (it is read-only) The value(s) of initialized global and static variables are stored in the data segment The value(s) of uninitialized global and static variables are stored in the bss segment The value(s) of local variables are stored on the stack The value(s) of dynamically allocated variables are stored on the heap

Sections of an executable file Segments:

Where is all the data stored? int a1[] = {5, 6, 7, 8, 9}; char msg[] = “hello world”; int main { static int call_count = 0; int i; return 0; }

Where is all the data stored? int a1[5]; char *msg; int main { int blah[16]; string *tmp = new string(“some message”); return 0; }

Where is all the data stored? int a1[5]; int main { Point p; return 0; } class Point { private: int a; int b; string *name; };

How do initialized local arrays work? void main2(int count) { if (count <= 0) return; int array[] = {0, 1, 2, 3, 4}; main2(count – 1); } int main() { main2(3); return 0; }

Function’s Activation Record Stores: – Return value – Previous AR’s ebp (base pointer) – Function parameters – Function local variables 1 activation record per function call (allows for recursion)

Why is mixing data and control on the stack not the best idea? Data – Variable values Control – Return value – Previous EBP Buffer overflow example

Variables and objects in memory Variables and data objects are data containers with names The value of the variable is the code stored in the container To evaluate a variable is to fetch the code from the container and interpret it properly To store a value in a variable is to code the value and store the code in the container The size of a variable is the size of its container 'A''A' (short big endian)

Variable Types and Storage

Overflow is when a data code is larger than the size of its container e.g., char i; // just 1 byte int *p = (int*)&i; // legal *p = ; // result if " big endian " storage: If whole space (X) belongs to this program: – Seems OK if X does not contain important data for rest of the program ’ s execution – Bad results or crash if important data are overwritten If all or part of X belongs to another process, the program is terminated by the OS for a memory access violation (i.e., segmentation fault)

More about overflow Previous slide showed example of " right overflow " – result truncated (also warning) Compilers handle " left overflow " by truncating too (usually without any warning) – Easily happens: unsigned char i = 255; i++; // What is the result of this increment? …

Placement & padding – word Compiler places data at word boundaries – e.g., word = 4 bytes Imagine: struct { char a; int b; } x; Classes too Compilers do it this way Not like this!

Pointers are data containers too As its value is a memory address, we say it " points " to a place in memory It points at just 1 byte, so it must " know " what data type starts at that address – How many bytes? – How to interpret the bits? Question: What is stored in the 4 bytes at addresses in the diagram at right? – Continued next slide

What is ? Could be four chars: ‘ A ’, ‘ B ’, ‘ C ’, ‘ D ’ Or it could be two shorts: 16961, – All numerical values shown here are for a " little endian " machine (more about endian next slide) Maybe it ’ s a long or an int: It could be a floating point number too:

Beware: two different byte orders Matters to actual value of anything but chars Say: short int x = 1; On a big endian machine it looks like this: – Some Macs, JVM, TCP/IP " Network Byte Order " On a little endian machine it looks like this: – Intel, most communication hardware