Midterm Review. What makes a file executable? Dr. Jack Lange 2/10/16. In UNIX: Everything is a file

Similar documents
CS 449 Lecture 3. Variables in memory. Dr. Jack Lange. int x = 4;

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture 03 Bits, Bytes and Data Types

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Heap Arrays. Steven R. Bagley

ECE551 Midterm Version 1

CSCI 171 Chapter Outlines

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

File I/O. Preprocessor Macros

Memory Management. CS449 Fall 2017

ECE551 Midterm Version 1

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

211: Computer Architecture Summer 2016

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

ECE551 Midterm Version 2

CS 261 Fall Mike Lam, Professor. Structs and I/O

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Processes. Johan Montelius KTH

Dynamic memory allocation

Kurt Schmidt. October 30, 2018

A process. the stack

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

CS240: Programming in C

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Standard File Pointers

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Lectures 5-6: Introduction to C

CS201 Lecture 2 GDB, The C Library

C Programming Review CSC 4320/6320

Lecture 05 Pointers ctd..

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

ECE 551D Spring 2018 Midterm Exam

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Memory and C/C++ modules

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Lecture 4: Build Systems, Tar, Character Strings

File and Console I/O. CS449 Spring 2016

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

CS1003: Intro to CS, Summer 2008

Course organization. Course introduction ( Week 1)

File IO and command line input CSE 2451

211: Computer Architecture Summer 2016

Programming refresher and intro to C programming

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

25.2 Opening and Closing a File

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

C mini reference. 5 Binary numbers 12

C for C++ Programmers

DAY 3. CS3600, Northeastern University. Alan Mislove

POINTER AND ARRAY SUNU WIBIRAMA

ECE 551D Spring 2018 Midterm Exam

Lectures 5-6: Introduction to C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pointers and File Handling

CS201 - Introduction to Programming Glossary By

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

File Access. FILE * fopen(const char *name, const char * mode);

Getting Started. Project 1

Computational Methods of Scientific Programming Fall 2007

PROGRAMMAZIONE I A.A. 2017/2018

C programming basics T3-1 -

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

CSE 12 Spring 2016 Week One, Lecture Two

Short Notes of CS201

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

ANITA S SUPER AWESOME RECITATION SLIDES

High Performance Programming Programming in C part 1

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Reminder: compiling & linking

Binghamton University. CS-220 Spring Includes & Streams

CSE 303 Midterm Exam

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Computer Programming Unit v

UNIX input and output

CS 61c: Great Ideas in Computer Architecture

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS 241 Data Organization Pointers and Arrays

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Process Management! Goals of this Lecture!

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Basic OS Progamming Abstrac7ons

Arrays and Memory Management

Computer Architecture and Assembly Language. Practical Session 5

Contents. A Review of C language. Visual C Visual C++ 6.0

LSN 3 C Concepts for OS Programming

Basic OS Progamming Abstrac2ons

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Input/Output and the Operating Systems

C-Refresher: Session 10 Disk IO

Transcription:

Midterm Review Dr. Jack Lange What makes a file executable? In UNIX: Everything is a file For a file to execute it needs to be marked as special Someone needs to grant it permission to execute Every file has a set of permissions that define who can do what with it File permissions Set of bits that flag specific permissions for certain users <type (1 flag)><user (3 flags)><group (3 flags)><everyone (3 flags)> E.g. drwxr- xr- x this is a directory that the owner can modify, but everyone else can only read Type./- = regular file d = directory r = reads allowed w = writes allowed x = execu[on allowed, or directory can be accessed 1

What is memory? Memory is a giant array of bytes (8 bits) Each byte is referenced via an index/offset The base index/offset always starts at 0 The index of a byte is its address Byte with address 9 == memory[9] Hardware interacts with memory in the same way How big is the array? Determined by the machines address size. Not the amount of memory installed in the system Variables Variables name a region of memory, not a single byte loca[on Variables have types other than raw bytes char, int, short, float, etc A type defines what the value of a variable means, and what value it holds Types can take up more than one byte More than a single entry in the memory array The number of bytes required to represent a variable type is chosen by the compiler There are some loose rules, but this is a source of pain int sizeof(); Built in compiler func[on that returns the size of a variable s type 2

Standard primi[ve types (x86_64) char 1 byte -128 to 127 unsigned char 1 byte 0 to 255 short 2 bytes -32768 to 32767 unsigned short 2 bytes 0 to 65535 int 4 bytes -2147483648 to 2147483647 unsigned int 4 bytes 0 to 4294967295 long 8 bytes -9223372036854775808 to 9223372036854775807 unsigned long 8 bytes 0 to 18446744073709551615 float 4 bytes 1.175494e-38 to 3.402823e+38 double 8 bytes 2.225074e-308 to 1.797693e+308 pointer 8 bytes Important: These sizes are not always true!!! It depends on the compiler and underlying architecture Type encodings Remember: C interacts directly with memory So all data is stored directly as raw bits (not objects) char y = 10; ==>> 1 byte of value 0001010 Signed vs unsigned Most primi[ve types can be signed (+/- ) or unsigned (+) Unsigned: Raw binary value Signed: 2 s complement Flip the bits and add 1 1 in the highest order bit loca[on means value is nega[ve 3

Geong Input for a C program 1 st mechanism: Command line arguments List of words you typed into command line Available as an array of strings named argv char * argv[] argv[0] is always the name of the executable you are running Example: cmdargs.c Interac[ve Input 2 nd Mechanism: Console Input Example: hello- 1.c scanf() int scanf(const char *format,...); Waits for input from the console and parses it According to a format string that specifies how to parse input Copies resul[ng values into memory loca[ons Uses loca[on of variables in memory (& operator) Returns the number of successful matches Used to detect errors (0 == No input parsed) 4

Interac[ve Input - Streams 3 rd mechanism: FIFO Streams FIFO = First In, First Out (in order processing) Console streams stdin standard input stdout standard output stderr standard error Other streams Files, Devices, Network Sockets We ll get to block interfaces later Easy File I/O The UI shell can be used for file I/O Files and streams are the same in C Unix: Everything is a file so we can mix and match easily Shell Redirec[on Redirect console streams 1. Redirect stdout to a file ( > and >> ) ls > files.out - - creates new file files.out ls >> all_files.out - - appends to file all_files.out 2. Read stdin from a file ( < ) wc < files.out 3. Pipe stdout of one program to stdin of another ( ) ls -l sort ls grep.c wc 5

File I/O in programs Command line and streams handled by system Files you have to manage on your own FILE * file = fopen(name, <mode>); On error NULL is returned void fclose(file * file) Text files Once open, a text file CAN be treated like a console fprintf, fscanf variants that can work on files ASCII Files ASCII: Encoding that maps bytes to text characters Translates characters from human readable to machine readable 7 is a human readable string representa[on of the number 7 7 is a raw binary number stored in memory Examples FILE * file; int i = 7 file = fopen( foo, r+ ); fprintf(file, "%d\n", i); Memory: 0x00000007 File: 0x37, 0x0a FILE * file; int i = -7; file = fopen( foo, r+ ); fprintf(file, "%d\n", i); Memory: 0xfffffff9 File: 0x2d, 0x37, 0x0a 6

ASCII Files ASCII: Encoding that maps bytes to text characters Translates characters from human readable to machine readable 7 is a human readable string representa[on of the number 7 7 is a raw binary number stored in memory Examples FILE * file; int i = 7 file = fopen( foo, r+ ); fprintf(file, "%d\n", i); Memory: 0x00000007 File: 0x37, 0x0a FILE * file; int i = -7; file = fopen( foo, r+ ); fprintf(file, "%d\n", i); Memory: 0xfffffff9 File: 0x2d, 0x37, 0x0a Pointers A pointers is an alias, a renaming of a variable Similar to a file shortcut/alias on a desktop int * pi; -> * indicates a pointer A pointer includes the type of data it is poin[ng to In this case an int Can be any C data type and a special void type 7

int x = 4; int * y = &x; int z = *y; printf( z = %d\n, z); è z = 4 Pointers Examples Examples: dereference.c GeMng around pass by value ptrswap.c Swapping two numbers in another funcpon dangerous.c Be very careful about dereferencing This is where a LOT of bugs happen Pointers are very powerful, but can be very dangerous Arrays Arrays are a natural data type in C Memory is an array Just a sequence of values back to back in memory Raw values of a single data type This means that arrays cannot change size Arrays must be ini[alized with the correct size Ini[aliza[on int a[] = {1, 2, 3, 4, 5, 6}; int c[6]; Accessing arrays int x = c[2]; The compiler knows where in memory c starts so it just goes to the correct offset in memory 8

Arrays and pointer arguments int * x; and int x[]; are the same thing! int foo(int * x) == int foo(int x[]); x[1] is a base/offset calcula[on Address of array x + offset of index 1 Offsets C is smart about offset calcula[ons Scales offset by the size of the type Strings What are strings in C? Already saw them defined as (char *) s. so they are just arrays of characters (bytes) with a NULL terminator ( \0 ) at the end String format Valid strings must be NULL terminated Assumed by the standard library string func[ons Many func[ons that look like: str*( ); Syntax: c is a string (2 bytes) c is a single character (1 byte) 9

Reading a string safely You MUST ensure that you only read a certain length Limit the input you accept to the size of the target buffer char * fgets(char * s, int size, FILE * stream); Reads a line of text from the console. Returns azer a newline, UNLESS the string exceeds size bytes int sscanf(char * str, char * fmt, ); Same func[onality as scanf, but reads input from string str and not the console Together fgets and sscanf can safely parse input from the console First, read a line into a char array with fgets, then parse it with sscanf Be{er string func[ons Earlier func[ons were VERY DANGEROUS Did not have any bounds restric[ons Be{er to use explicit bounds versions size_t strnlen(char * s, size_t maxlen); char * strncpy(char * dst, char * src, size_t n); int strncmp(char * s1, char * s2, size_t n); char * strncat(char * s1, char * s2, size_t n); All(?) str*() func[ons have strn*() versions But you s[ll need to be careful strn*() func[ons do not guarantee the output will be NULL terminated 10

Be{er string func[ons Earlier func[ons were VERY DANGEROUS Did not have any bounds restric[ons Be{er to use explicit bounds versions size_t strnlen(char * s, size_t maxlen); char * strncpy(char * dst, char * src, size_t n); int strncmp(char * s1, char * s2, size_t n); char * strncat(char * s1, char * s2, size_t n); All(?) str*() func[ons have strn*() versions But you s[ll need to be careful strn*() func[ons do not guarantee the output will be NULL terminated Alloca[ng stack memory Alloca[ng stack space means increasing the size of the stack Add more memory to the end of the stack DOES NOT INITIALIZE THE CONTENTS The Stack Pointer (esp) A special register that contains a memory address The keyword is Pointer Marks the current end of the stack Address of the last byte of data on the stack Memory is allocated by adding/subtrac[ng from esp Example: dangerous2.c 11

Dynamic Memory Some[mes the stack is not good enough You want to share data across func[ons You want data to be persistent across func[on calls The data size is too large for the stack You don t know how much memory you will need at compile [me E.g. reading a file into memory (how big is the file?) A new way to get memory Like new/delete from Java Allocates memory from the HEAP void * malloc(size_t size); Allocates size bytes of memory and returns the address void free(void * ptr); Frees the memory allocated at the address ptr The Heap A region of memory that is managed by the programmer Programmer requests X bytes of memory Heap memory is not scoped Memory is reserved un[l its released/free() d Heap memory has no name int x; - - > compiler names 4 bytes of memory When x goes out of scope its memory is automa[cally released (How?) The compiler does not name a heap alloca[on Not automa[cally released when it goes out of scope 12

Accessing heap memory Compiler does not associate a variable with heap alloca[ons Q: How do we access it? A: Pointers Review: what are pointers? A variable that stores an address Pointer variables are scoped Compiler manages the memory for the pointer variable Pointers are used as handles for heap memory realloc and calloc void * realloc(void *ptr, size_t size); Similar to malloc, but changes the size of an already allocated buffer First argument is a pointer to the buffer to change realloc(null, 5); == malloc(5); char * x = malloc(5); x = realloc(x, 10); is the same as x = malloc(10); void * calloc(size_t nmemb, size_t size); Allocates an array of nmemb elements of size size INITIALIZES BUFFER MEMORY TO \0 I prefer to use this over malloc char * x = calloc(5, sizeof(char)); is the same as x = malloc(5 * sizeof(char)); memset(x, 0, 5 * sizeof(char)); 13

Pointer arguments Used to pass values as well as return values from a func[on Some[mes both at the same [me Another use of ** is to return a pointer from a func[on ssize_t getline(char ** linep, size_t * linecapp, FILE * stream); Similar to fgets, but can dynamically allocate memory from the heap { } { } size_t len = 0; char * str = NULL; len = getline(&str, NULL, stdin); size_t len = 5; char * str = malloc(5); len = getline(&str, &len, stdin); Searching (2) char * strnstr(char * s1, char * s2, size_t n); Similar to strchr but searches for a string (s2) Also has bounds limit via (n) Returns loca[on of first occurrence of string s2 in s1 char * strtok(char * str, const char * delim); Returns the next string azer a token delim is a list of tokens, as a string Modifies the contents of str char * strsep(char ** stringp, char * delim); Replacement for strtok, but not as portable Modifies value of stringp (input and output argument) These funcpons are very complex, with lots of rules for return values and edge cases Use with caupon 14

Structs C structs are like classes in Java Declares a new type struct foo; means there is a type struct foo Java classes encapsulates code + data C structs include only data No fancy OOP techniques (inheritance, subclasses, etc) But in C : code == data (more on this later) Typedef typedef <original type> <new type>; typedef unsigned int uint32_t; typedef struct foo foo_t; Assigning values to structs structs are like any over data type Automa[cally or dynamically allocated Stack Alloca[on: Fields accessed with. int main() { struct foo my_foo; memset(&my_foo, 0, sizeof(struct foo)); } my_foo.x = 4; my_foo.y = 10; my_foo.name = George ; 15

Assigning values to structs structs are like any over data type Automa[cally or dynamically allocated Heap Alloca[on: Fields accessed with - > int main() { struct foo * my_foo = calloc(1, sizeof(struct foo)); my_foo->x = 4; my_foo->name = George ; (*my_foo).y = 10; } free(foo); Structs in memory What do structs look like in memory? Raw values of member variables struct foo { int x; int y; char * name int main() { struct foo struct foo my_foo_1; my_foo_2; }; my_foo_1 = my_foo_2; } Copying Structs C provides shallow copies of struct values memcpy(&my_foo_1, &my_foo_2, sizeof(struct foo)); Copies raw bytes of struct data We know the how many bytes via sizeof(); 16

More complex data structures KEY POINT: structs can contain pointers to the same struct type struct list_node { int x; struct list_node * next; } This is not self referen[al Rather it points to another variable structure elsewhere in memory This allows us to construct collec[ons lists, trees, graphs, etc Preprocessing Compiling C programs is a mul[stage opera[on Before code is actually compiled it is pre- processed C Pre- Processor Basically just a simple syntax for text manipula[on Produces the final text that is used by the compiler We ve seen an example already #include <stdio.h> Replace the line with the raw contents of the specified file 17

#define Example Usually used for commonly used constants E.g. buffers usually have a standard size Allows changing the constant in one loca[on Don t need to track down every usage Avoids a lot of bugs #define BUF_SIZE 100 int main() { char buf[buf_size]; memset(buf, 0, BUF_SIZE); fgets(buf, BUF_SIZE, stdin); } Condi[onal Macros Enable/Disable blocks of code before compila[on #ifdef MACRO #endif AND #ifndef MACRO #endif #ifdef Useful for turning on/off debugging features Useful for compile [me configura[on Example: #ifdef ENABLE_DEBUGGING printf(lots of state values); #endif > gcc DENABLE_DEBUGGING test.c o test 18

Func[on Macros Text replacement that understands arguments Looks like a func[on defini[on Only matches text that looks like a func[on : () #define FOO(x) (x++) #define MAX(x, y) (x > y)? x : y; Only text replacement The func[on body can be ANY text Syntax is not checked un[l actual compila[on Can be extremely confusing and convoluted BEWARE COMPLEX MACROS Typecas[ng C provides basic type checking If you try int x = foo ; the compiler will catch it However, you can easily override this Example: typecase1.c char * str = foo ; int x = *(int *)str; The universal type : void * (Example: typecast2.c) A raw memory address, can point to anything Any pointer can be implicitly cast to a void * int foo(void * x); void * ptr = NULL; char * str = hello ; int x = 4; foo(ptr); foo(str); foo(&x); // This is OK // This is also OK 19

Func[on Pointers Every part of the program is stored in memory Code, data, everything C allows you direct access to memory So we can access the code itself Or, we can execute data as code FuncPon pointers: FuncEons as a data type We can assign a func[on to a variable Just a memory address where the code is located Then call the variable Force the CPU to jump to that loca[on in memory Func[on Pointer Example int bar(int x, int y) {return x + y); int (*foo)(int x, int y); foo = bar; foo = &bar; // Same thing foo(1, 2); Looks similar to regular pointers But there is a subtle difference int * x; // x is a pointer to an int int (*x)(); /* x is a pointer to a function * that returns an int */ 20