CS 474: OOLE Lecture 1: Names and Bindings Instructor: Dr. Mark Grechanik University of Illinois at Chicago Names A name is a mnemonic character string that is used to represent some object • Names are alpha-numeric tokens (in many languages) also called identifiers • Names allow us to refer to variables, constants, operations and other programming entities using symbolic identifiers rather than low-level concepts Names allow programmers realize abstractions by associating names with code fragments • By hiding irrelevant details, abstraction reduces conceptual complexity • Programmers can focus on a manageable subset of the program text Department of Computer Science, the University of Illinois at Chicago 2 Binding Name Binding Thing Department of Computer Science, the University of Illinois at Chicago 3 Binding Time A binding is an association between a name and the thing it names Binding time is the time at which a binding is created, i.e., implementation decisions are made Department of Computer Science, the University of Illinois at Chicago 4 Different Binding Times Department of Computer Science, the University of Illinois at Chicago 5 Early and Late Binding Times Early binding times = greater efficiency • Compiler decides on the layout of declared variables in memory and generates efficient code to access these variables in programs • Addresses of variables cannot always be predicted, but their offsets can be defined Late binding times = greater flexibility • Interpreters analyze declarations every time the program executes or subroutines are called Department of Computer Science, the University of Illinois at Chicago 6 Binding Lifetime and Key Events Create bindings Create objects Reference The period of time between the creation and the destruction of a name-to-object binding is called the binding’s lifetime Deactivate Destroy objects bindings Destroy bindings Reactivate bindings Department of Computer Science, the University of Illinois at Chicago 7 Questions n Is the binding between the parameter name and the variable that was passed as a parameter to a subroutine has a longer lifetime than that of the variable itself? n Is it possible for a name-to-object binding to have a lifetime longer than that of the object? Department of Computer Science, the University of Illinois at Chicago 8 Object Lifetimes Static objects are given an absolute address that is retained throughout the program’s execution • Global variables are examples of static objects • Instructions, local static variables Stack objects are allocated in the LIFO order • Usually in conjuction with subroutine calls and returns Heap objects may be allocated and deallocated at any time • Requires more general and expensive storage management Department of Computer Science, the University of Illinois at Chicago 9 Local Variables n Local variables are created when their subroutines are called and destroyed when these subroutines return q q n Repeatedly calling a subroutine creates and destroys separate instances of its local variables What about recursion? Compile time constants are assigned values that are determined at compile time Department of Computer Science, the University of Illinois at Chicago 10 Program Memory Segmentation n Text q n Data q n Stores static variables that are filled solely with zero-valued data initially Heap q n Stores initialized global program variables BSS (Block Started by Symbol) q n It is a code segment that contains program instructions. EIP points to an instruction to be executed Stores program variables and can grow and shrink in size. It grows toward higher memory addresses Stack q LIFO temporary storage that’s stores context during subroutine calls. Push and Pop are two basic operations for the stack. It grows toward lower memory addresses 11 Memory Layout STACK Higher Address HEAP CODE Lower Address 12 Stack-Based Allocation n Each instance of a subroutine at run time has its own frame (activation record) q n Arguments and return values lie at the bottom of the frame q n Contains arguments, return values, local vars, etc Easier for a caller to access them A stack is maintained by a subroutine called calling sequence. It consists of q q Prologue (code executed at the beginning) Epilogue (code executed at the end) 13 Stack-Based Allocation sp fp Temporaries Subroutine D Subroutine B Subroutine A called from the main program Stack grows this way Subroutine C Lower Address Local variables Bookkeeping Return address Arguments and returns fp when C is running Higher Address 14 Stack-Based Allocation n Location of a stack frame cannot be predicted at compile time q n n n Because we do not know what other frames may already be on the stack The offset of objects within a frame can be determined statically Frame pointer (fp) points to some known location within the frame of the currently executing subroutine The stack pointer contains the address of the first unused location at the top of the stack 15 Stack Operations n n n n n n n Code is executed until function call F is encountered Push the arguments for F on the stack Call F and place its return address RET on the stack q RET is the address stored in the instruction pointer EIP at the time when F is called q It is the location at which to continue the execution when F is completed Execute the prolog code q EBP (fp) value is pushed onto the stack q Copy ESP to EBP Calculate the size of the space required to keep local variables Push local variables to the stack We are done! 16 Example void function(int a, int b) { int array[5]; } int main() { function(1, 2); } sp AAAAAAAAAA AAAAAAAAAA array AAAAAAAAAA AAAAAAAAAA EBP AAAAAAA RET b a 17 Program Exploit: Buffer Overflow Look at this C code example n 1. 2. 3. 4. 5. 6. 7. n int main( int argc, char *argv[] ) { char buffer[100]; int i; for( i = 0; i < 150; i++ ) { buffer[i] = ‘A’; } } What happens when we compile and run this program? 18 Heap-Based Allocations A heap is a region of storage in which subblocks are allocated and deallocated at arbitrary times Heaps are required for objects whose sizes may change as a results of different operations Department of Computer Science, the University of Illinois at Chicago 19 What Is the Value of sum? int j = 0, sum = 0; for(int i = 1; i < 100; i++){ j = j++*i; sum += j; } System.out.println(sum); Department of Computer Science, the University of Illinois at Chicago 20 Java Language Specification n n n n n Version Java SE X Edition q https://docs.oracle.com/en/java/javase/X q https://docs.oracle.com/javase/specs/jls/seX/html/index.html Section 15.14 https://docs.oracle.com/javase/specs/jls/seX/html/jls15.html#jls-15.14 At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored. Department of Computer Science, the University of Illinois at Chicago 21 What Is the Value of sum? int j = 0, sum = 0; for(int i = 1; i < 100; i++){ j = j++*i; sum += j; } System.out.println(sum); Department of Computer Science, the University of Illinois at Chicago 22 What About This Program? public class Null { public static void greet( ) { System.out.println("Hello world!"); } public static void main(String[ ] args) { ((Null) null).greet( ); } } Department of Computer Science, the University of Illinois at Chicago 23 null n 3.10.7 The Null Literal q q n n n n n The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters. A null literal is always of the null type (§4.1) There are two kinds of types in the Java programming language: primitive types(§4.2) and reference types (§4.3). There is also a special null type, the type of the expression null ( §3.10.7, §15.8.1),which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be assigned or cast to any reference type (§5.2, §5.3,§5.5). Department of Computer Science, the University of Illinois at Chicago 24 Null n n An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile time error occurs. Two identifiers are the same only if, after ignoring characters that are ignorable, the identifiers have the same Unicode character for each letter or digit. q n An ignorable character is a character for which the method Character.isIdentifierIgnorable(int) returns true. Identifiers that have the same external appearance may yet be different. Keywords are 51 character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8). Department of Computer Science, the University of Illinois at Chicago 25 JLS Again n n n n n n Section 8.4.3.2. static Methods A method that is declared static is called a class method. It is a compile-time error to use the name of a type parameter of any surrounding declaration in the header or body of a class method. A class method is always invoked without reference to a particular object. It is a compile-time error to attempt to reference the current object using the keyword this (§15.8.3) or the keyword super (§15.11.2). A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body. Department of Computer Science, the University of Illinois at Chicago 26 Reading Mandatory n q q n The Michael Scott book, chapter 3 The Scala book, chapters 1-3 Optional for undergrads, mandatory for graduate students q Max Schäfer, Torbjörn Ekman, Oege de Moor: Sound and extensible renaming for java. OOPSLA 2008: 277-294. Department of Computer Science, the University of Illinois at Chicago 27