{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


SampleFinalCS3214S10 - CS 3214 Sample Final Exam(Spring...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 3214 Sample Final Exam (Spring 2010) 1/12 Sample Final Exam (Spring 2010) Solutions are shown in this style. This exam was given Spring 2010. 1. Linking and Loading (12 pts) The following questions are related to linking and loading in a C/Unix environment. a) (4 pts) A coding style rule that is used in many C-based projects is that variables that are used only within one compilation unit be declared static. Sketch briefly how you could create a script as part of the build process that would check if programmers followed this rule! Use nm or a similar tool and verify that for every defined global symbol (recognizable with a capital letter such as ‘T’, ‘C’, or ‘D’ in nm), there exists at least one undefined (‘U’) reference in another .o file. Some answers suggested a source code analysis approach by looking for ‘extern’ declarations. While possible, this is more difficult because simply declaring a variable or function extern does not produce an undefined reference unless the variable or function is actually used. A common mistake was to propose an algorithm that would check that static variables with the same name aren’t used outside the current compilation unit. This is wrong: static variables are local to a compilation unit, so using a static variable with the same name in different files is entirely ok (and occurs quite often). b) (4 pts) Consider the following two .c files: // inck.c extern int k; void inc_k() { k++; } // main.c extern void inc_k(); int main() { inc_k(); return 0; } What error message would the command gcc inck.c main.c -o main produce if one attempted to compile and link this program? $ gcc -Wall inck.c main.c -o main /tmp/cc4HL3gU.o: In function `inc_k': inck.c:(.text+0x4): undefined reference to `k' inck.c:(.text+0xc): undefined reference to `k'
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS 3214 Sample Final Exam (Spring 2010) 2/12 collect2: ld returned 1 exit status The symbol ‘k’ is referenced in inck.o, but never defined. ‘extern’ declares, but does not define a symbol. c) (4 pts) Address space randomization is a defensive technique used to increase an attacker’s difficulty of succeeding with overflow attacks that require a priori knowledge of where a program’s data is located in its virtual address space. For instance, the location of the stack and the start of the heap are randomized on modern systems, so that they vary between different runs of a program. Could the location of global variables be similarly randomized? State your assumptions if necessary! No or yes, depending on your assumptions. Assuming the way executables are customarily built, the answer is no because the linker determines the location of all global variables at link time, then places the computed addresses directly into the executable. This could be avoided by building position-independent executables that use indirection via a global offset table for each access to global variables, just like shared libraries do. Note that shared libraries can access global variables used in their implementation, even though they may be located at different virtual addresses in the processes that use this shared library.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.