Operating Systems 2014F: Assignment 4: Difference between revisions

From Soma-notes
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
Please submit the answers to the following questions via CULearn by midnight on Wednesday, October 1, 2014.  There 10 points in 4 questions.
Please submit the answers to the following questions via CULearn by midnight on '''Saturday, October 4, 2014''' (extended from October 1st).  There 10 points in 4 questions.


Submit your answers as a single text file named "<username>-comp3000-assign4.txt" (where username is your MyCarletonOne username).  The first four lines of this file should be "COMP 3000 Assignment 4", your name, student number, and the date of submission.  You may wish to format your answers in [http://en.wikipedia.org/wiki/Markdown Markdown] to improve their appearance.
Submit your answers as a single text file named "<username>-comp3000-assign4.txt" (where username is your MyCarletonOne username).  The first four lines of this file should be "COMP 3000 Assignment 4", your name, student number, and the date of submission.  You may wish to format your answers in [http://en.wikipedia.org/wiki/Markdown Markdown] to improve their appearance.
Line 12: Line 12:
# [3] Write a program "syscall-hello-pid.c" that is based on syscall-hello.c but outputs "Hello, I am process 417!" (assuming the PID of the process running it is 417).  Note that you will have to get the current process ID, convert it to a string, and combine it with the "Hello" string.  USE NO LIBRARY FUNCTIONS except syscall().
# [3] Write a program "syscall-hello-pid.c" that is based on syscall-hello.c but outputs "Hello, I am process 417!" (assuming the PID of the process running it is 417).  Note that you will have to get the current process ID, convert it to a string, and combine it with the "Hello" string.  USE NO LIBRARY FUNCTIONS except syscall().
# [4] Add a comment to every line of the assembly code version of your syscall-hello-pid.c code, as produced by <tt>gcc -s</tt>, explaining the purpose of each line.  Explain with reference to the C code, not the assembly code.  For example, a change in the stack pointer should be notated as "allocate space for local variables x and pid" not "changed the stack pointer".  You may want to add in flags such as "-O2" and "-g" to get easier to interpret assembly code.  '''You may ignore/delete all lines that start with a period,''' as those do not generate code directly.  If you had trouble with the previous question, you may instead comment the assembly from syscall-hello.c, but at the loss of one point (out of 10).
# [4] Add a comment to every line of the assembly code version of your syscall-hello-pid.c code, as produced by <tt>gcc -s</tt>, explaining the purpose of each line.  Explain with reference to the C code, not the assembly code.  For example, a change in the stack pointer should be notated as "allocate space for local variables x and pid" not "changed the stack pointer".  You may want to add in flags such as "-O2" and "-g" to get easier to interpret assembly code.  '''You may ignore/delete all lines that start with a period,''' as those do not generate code directly.  If you had trouble with the previous question, you may instead comment the assembly from syscall-hello.c, but at the loss of one point (out of 10).
==Solutions==
# Library calls (as reported by ltrace) are function calls that are made to dynamically-linked libraries.  Functions whose addresses have been resolved at compile time (statically linked) are not reported by ltrace.
# Note that the following assumes that the no optimization has been done.  With optimization, fewer instructions are made to invoke functions, up to and including inlining functions so that the code for the function is effectively "inserted" into the caller.  Optimization may also change the opcodes that are used and may pass and return values in registers.  For more information, see [http://www.cs.dartmouth.edu/~sergey/cs108/tiny-guide-to-x86-assembly.pdf The Tiny Guide to x86 Assembly].
## The caller saves any "caller save" registers it wishes to using <tt>push</tt>
## The caller pushes the function arguments onto the stack (right to left order normally) using <tt>push</tt> or other instructions along with space for the return value, if necessary.
## The caller invokes the function (using <tt>call</tt>)
## The callee saves the base pointer %ebp and copies the stack pointer %esp to %ebp (using <tt>push</tt> and <tt>mov</tt>).  Note %ebp is used to access local variables and arguments.
## The callee allocates spaces on the stack for local variables using <tt>sub %esp</tt>.
## When the callee finishes, it puts the return value in %eax, removes local variables from the stack then executes <tt>ret</tt>.
## The caller removes the arguments from the stack, restores its registers (using <tt>pop</tt>) as necessary, and uses the return value in %eax as necessary to continue the computation.
# See syscall-hello-pid.c below.
# See syscall-hello-pid.s below.
===syscall-hello-pid.c===
<source lang="c">
/* syscall-hello-pid.c */
/* this works in GCC */
typedef __SIZE_TYPE__ size_t;
/* First, the helper functions that we can avoid writing! */
/* strncpy() from:
  http://code.metager.de/source/xref/OpenBSD/src/sys/lib/libsa/strncpy.c
* Copyright (c) 1996 Michael Shalayeff
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
char *
strncpy(char *s1, const char *s2, size_t n)
{
char *p = s1;
while (n-- && (*s1++ = *s2++) != '\0')
;
return p;
}
/* strlen() from
  http://code.metager.de/source/xref/OpenBSD/src/sys/lib/libsa/strlen.c
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
size_t strlen(const char *str)
{
const char *s;
       
for (s = str; *s; ++s)
;
return (s - str);
}
/* Now our code */
#include <unistd.h>
#include <sys/syscall.h>
char start[] = "Hello I am process ";
char end[] = "!\n";
char all[100];  /* leave lots of space */
int main(int argc, char *argv[]) {
        int result, pid, i, j, digit;
        size_t len;
        char pidstr_rev[12], pidstr[12];  /* big enough for a 32-bit PID */
        pid = syscall(SYS_getpid);
        i = 0;
        for (i = 0;  pid > 0; i++) {
                digit = pid % 10;
                pid = (pid - digit) / 10;
                pidstr_rev[i] = (char) digit + '0';
        }
        pidstr_rev[i] = '\0';
        for (j = 0; i > 0; j++,i--) {
                pidstr[j] = pidstr_rev[i-1];
        }
        pidstr[j] = '\0';
        /* I could check whether the length of all the strings fit in all,
          but given the way we built pidstr we should be fine. */
        strncpy(all, start, strlen(start));
        len = strlen(start);
        strncpy(all + len, pidstr, strlen(pidstr));
        len += strlen(pidstr);
        strncpy(all + len, end, strlen(end));
        len += strlen(end);
        all[len] = '\0';
        /* "man 2 write" to see arguments to write syscall */
        result = syscall(SYS_write, 1, all, strlen(all));
 
        return (int) result;
}
</source>
===syscall-hello-pid.s===
<source lang="asm">
</source>

Latest revision as of 00:07, 17 October 2014

Please submit the answers to the following questions via CULearn by midnight on Saturday, October 4, 2014 (extended from October 1st). There 10 points in 4 questions.

Submit your answers as a single text file named "<username>-comp3000-assign4.txt" (where username is your MyCarletonOne username). The first four lines of this file should be "COMP 3000 Assignment 4", your name, student number, and the date of submission. You may wish to format your answers in Markdown to improve their appearance.

No other formats will be accepted. Submitting in another format will likely result in your assignment not being graded and you receiving no marks for this assignment. In particular do not submit an MS Word or OpenOffice file as your answers document!

Don't forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources. You do not need to list help from the instructor, TA, or information found in the textbook.

Questions

  1. [1] What is the difference between function calls and library calls (as reported by ltrace)?
  2. [2] In assembly, how does a program make a function call (using C semantics)? Describe the process and state the specific instructions used in x86 assembly code.
  3. [3] Write a program "syscall-hello-pid.c" that is based on syscall-hello.c but outputs "Hello, I am process 417!" (assuming the PID of the process running it is 417). Note that you will have to get the current process ID, convert it to a string, and combine it with the "Hello" string. USE NO LIBRARY FUNCTIONS except syscall().
  4. [4] Add a comment to every line of the assembly code version of your syscall-hello-pid.c code, as produced by gcc -s, explaining the purpose of each line. Explain with reference to the C code, not the assembly code. For example, a change in the stack pointer should be notated as "allocate space for local variables x and pid" not "changed the stack pointer". You may want to add in flags such as "-O2" and "-g" to get easier to interpret assembly code. You may ignore/delete all lines that start with a period, as those do not generate code directly. If you had trouble with the previous question, you may instead comment the assembly from syscall-hello.c, but at the loss of one point (out of 10).

Solutions

  1. Library calls (as reported by ltrace) are function calls that are made to dynamically-linked libraries. Functions whose addresses have been resolved at compile time (statically linked) are not reported by ltrace.
  2. Note that the following assumes that the no optimization has been done. With optimization, fewer instructions are made to invoke functions, up to and including inlining functions so that the code for the function is effectively "inserted" into the caller. Optimization may also change the opcodes that are used and may pass and return values in registers. For more information, see The Tiny Guide to x86 Assembly.
    1. The caller saves any "caller save" registers it wishes to using push
    2. The caller pushes the function arguments onto the stack (right to left order normally) using push or other instructions along with space for the return value, if necessary.
    3. The caller invokes the function (using call)
    4. The callee saves the base pointer %ebp and copies the stack pointer %esp to %ebp (using push and mov). Note %ebp is used to access local variables and arguments.
    5. The callee allocates spaces on the stack for local variables using sub %esp.
    6. When the callee finishes, it puts the return value in %eax, removes local variables from the stack then executes ret.
    7. The caller removes the arguments from the stack, restores its registers (using pop) as necessary, and uses the return value in %eax as necessary to continue the computation.
  3. See syscall-hello-pid.c below.
  4. See syscall-hello-pid.s below.

syscall-hello-pid.c

/* syscall-hello-pid.c */

/* this works in GCC */
typedef __SIZE_TYPE__ size_t;

/* First, the helper functions that we can avoid writing! */

/* strncpy() from:

   http://code.metager.de/source/xref/OpenBSD/src/sys/lib/libsa/strncpy.c

 * Copyright (c) 1996 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

char *
strncpy(char *s1, const char *s2, size_t n)
{
	char *p = s1;

	while (n-- && (*s1++ = *s2++) != '\0')
		;
	return p;
}


/* strlen() from
   http://code.metager.de/source/xref/OpenBSD/src/sys/lib/libsa/strlen.c

 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

size_t strlen(const char *str)
{
	const char *s;
        
	for (s = str; *s; ++s)
		;
	return (s - str);
}

/* Now our code */


#include <unistd.h>
#include <sys/syscall.h>

char start[] = "Hello I am process ";
char end[] = "!\n";
char all[100];  /* leave lots of space */

int main(int argc, char *argv[]) {
        int result, pid, i, j, digit;
        size_t len;
        char pidstr_rev[12], pidstr[12];  /* big enough for a 32-bit PID */

        pid = syscall(SYS_getpid);

        i = 0;
        for (i = 0;  pid > 0; i++) {
                digit = pid % 10;
                pid = (pid - digit) / 10;
                pidstr_rev[i] = (char) digit + '0';
        }

        pidstr_rev[i] = '\0';

        for (j = 0; i > 0; j++,i--) {
                pidstr[j] = pidstr_rev[i-1];
        }
        pidstr[j] = '\0';

        /* I could check whether the length of all the strings fit in all,
           but given the way we built pidstr we should be fine. */

        strncpy(all, start, strlen(start));
        len = strlen(start);

        strncpy(all + len, pidstr, strlen(pidstr));
        len += strlen(pidstr);

        strncpy(all + len, end, strlen(end));
        len += strlen(end);

        all[len] = '\0';

        /* "man 2 write" to see arguments to write syscall */
        result = syscall(SYS_write, 1, all, strlen(all));
  
        return (int) result;
}

syscall-hello-pid.s