├── .gitignore ├── Computer Systems - A Programmer's Perspective (2nd).pdf ├── LICENSE ├── README.md ├── bomb24 ├── bomb ├── bomb.c ├── defuser.txt ├── output ├── test └── test.c ├── lab0 ├── arrays.c ├── lab0.pdf ├── lab1.pdf ├── lab2.pdf ├── lab3.pdf ├── lab4.pdf ├── lab5.pdf └── my_lab0.c ├── lab1.tar ├── lab1 ├── Makefile ├── README ├── bits.c ├── bits.h ├── btest ├── btest.c ├── btest.h ├── decl.c ├── dlc ├── driver.pl ├── fshow ├── fshow.c ├── ishow ├── ishow.c ├── pointer.c ├── pointer.h ├── ptest ├── ptest.c └── tests.c ├── lab2-bomb.tar ├── lab3.tar ├── lab3 ├── .output.S.swp ├── Makefile ├── back_up.txt ├── bufbomb ├── bufbomb.c ├── exploit_for_level0.byte ├── exploit_for_level0.txt ├── exploit_for_level1.byte ├── exploit_for_level1.txt ├── exploit_for_level2.S ├── exploit_for_level2.byte ├── exploit_for_level2.txt ├── exploit_for_level3.S ├── exploit_for_level3.byte ├── exploit_for_level3.txt ├── makecookie ├── output.S └── sendstring ├── lab4.tar.gz ├── lab4 ├── Makefile ├── Optional_extra_credit.c ├── cache-test ├── cache-test-skel.c ├── cacheExperiment.c ├── cacheExperiment.class ├── cacheExperiment.java ├── cacheExperimentInteger.class ├── cacheExperimentInteger.java ├── mystery-cache.h └── run.pl ├── lab5.tar.gz └── lab5 ├── Makefile ├── README ├── clock.c ├── clock.h ├── config.h ├── fcyc.c ├── fcyc.h ├── fsecs.c ├── fsecs.h ├── ftimer.c ├── ftimer.h ├── mdriver-realloc.c ├── mdriver.c ├── memlib.c ├── memlib.h ├── mm.c ├── mm.h └── traces ├── amptjp-bal.rep ├── binary-bal.rep ├── binary2-bal.rep ├── cccp-bal.rep ├── coalescing-bal.rep ├── cp-decl-bal.rep ├── expr-bal.rep ├── random-bal.rep ├── random2-bal.rep ├── realloc-bal.rep ├── realloc2-bal.rep ├── short1-bal.rep └── short2-bal.rep /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | -------------------------------------------------------------------------------- /Computer Systems - A Programmer's Perspective (2nd).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/Computer Systems - A Programmer's Perspective (2nd).pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CSAPP 2 | ===== 3 | 4 | Aha! 5 | 6 | Here is my solutions for labs of CS:APP. 7 | It's so fantastic to solve this problems. This labs will help us 8 | to understand this great book deeply. 9 | 10 | If you have puzzle with there problems and find something wrong 11 | with my solutions, you could touch me by e-mail: jasonleaster@gmail.com 12 | 13 | It will be my pleasure, if you would like to communicate with me 14 | and exchange our ideas about this labs : ) 15 | 16 | Yours, EOF 17 | -------------------------------------------------------------------------------- /bomb24/bomb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/bomb24/bomb -------------------------------------------------------------------------------- /bomb24/bomb.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Dr. Evil's Insidious Bomb, Version 1.0 3 | * Copyright 2002, Dr. Evil Incorporated. All rights reserved. 4 | * 5 | * LICENSE: 6 | * 7 | * Dr. Evil Incorporated (the PERPETRATOR) hereby grants you (the 8 | * VICTIM) explicit permission to use this bomb (the BOMB). This is a 9 | * time limited license, which expires on the death of the VICTIM. 10 | * The PERPETRATOR takes no responsibility for damage, frustration, 11 | * insanity, bug-eyes, carpal-tunnel syndrome, loss of sleep, or other 12 | * harm to the VICTIM. Unless the PERPETRATOR wants to take credit, 13 | * that is. The VICTIM may not distribute this bomb source code to 14 | * any enemies of the PERPETRATOR. No VICTIM may debug, 15 | * reverse-engineer, run "strings" on, decompile, decrypt, or use any 16 | * other technique to gain knowledge of and defuse the BOMB. BOMB 17 | * proof clothing may not be worn when handling this program. The 18 | * PERPETRATOR will not apologize for the PERPETRATOR's poor sense of 19 | * humor. This license is null and void where the BOMB is prohibited 20 | * by law. 21 | ***************************************************************************/ 22 | 23 | #include 24 | #include 25 | #include "support.h" 26 | #include "phases.h" 27 | 28 | /* 29 | * Note to self: Remember to erase this file so my victims will have no 30 | * idea what is going on, and so they will all blow up in a 31 | * spectaculary fiendish explosion. -- Dr. Evil 32 | */ 33 | 34 | FILE *infile; 35 | 36 | int main(int argc, char *argv[]) 37 | { 38 | char *input; 39 | 40 | /* Note to self: remember to port this bomb to Windows and put a 41 | * fantastic GUI on it. */ 42 | 43 | /* When run with no arguments, the bomb reads its input lines 44 | * from standard input. */ 45 | if (argc == 1) { 46 | infile = stdin; 47 | } 48 | 49 | /* When run with one argument , the bomb reads from 50 | * until EOF, and then switches to standard input. Thus, as you 51 | * defuse each phase, you can add its defusing string to and 52 | * avoid having to retype it. */ 53 | else if (argc == 2) { 54 | if (!(infile = fopen(argv[1], "r"))) { 55 | printf("%s: Error: Couldn't open %s\n", argv[0], argv[1]); 56 | exit(8); 57 | } 58 | } 59 | 60 | /* You can't call the bomb with more than 1 command line argument. */ 61 | else { 62 | printf("Usage: %s []\n", argv[0]); 63 | exit(8); 64 | } 65 | 66 | /* Do all sorts of secret stuff that makes the bomb harder to defuse. */ 67 | initialize_bomb(); 68 | 69 | printf("Welcome to my fiendish little bomb. You have 6 phases with\n"); 70 | printf("which to blow yourself up. Have a nice day!\n"); 71 | 72 | /* Hmm... Six phases must be more secure than one phase! */ 73 | input = read_line(); /* Get input */ 74 | phase_1(input); /* Run the phase */ 75 | phase_defused(); /* Drat! They figured it out! 76 | * Let me know how they did it. */ 77 | printf("Phase 1 defused. How about the next one?\n"); 78 | 79 | /* The second phase is harder. No one will ever figure out 80 | * how to defuse this... */ 81 | input = read_line(); 82 | phase_2(input); 83 | phase_defused(); 84 | printf("That's number 2. Keep going!\n"); 85 | 86 | /* I guess this is too easy so far. Some more complex code will 87 | * confuse people. */ 88 | input = read_line(); 89 | phase_3(input); 90 | phase_defused(); 91 | printf("Halfway there!\n"); 92 | 93 | /* Oh yeah? Well, how good is your math? Try on this saucy problem! */ 94 | input = read_line(); 95 | phase_4(input); 96 | phase_defused(); 97 | printf("So you got that one. Try this one.\n"); 98 | 99 | /* Round and 'round in memory we go, where we stop, the bomb blows! */ 100 | input = read_line(); 101 | phase_5(input); 102 | phase_defused(); 103 | printf("Congratulations! You've (mostly) defused the bomb!\n"); 104 | printf("Hit Control-C to escape phase 6 (for free!), but if you want to\n"); 105 | printf("try phase 6 for extra credit, you can continue. Just beware!\n"); 106 | 107 | /* Oh, so you want more danger? Let's make this one extra hard. */ 108 | input = read_line(); 109 | phase_6(input); 110 | phase_defused(); 111 | 112 | /* Wow, they got it! But isn't something... missing? Perhaps 113 | * something they overlooked? Mua ha ha ha ha! */ 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /bomb24/defuser.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/bomb24/defuser.txt -------------------------------------------------------------------------------- /bomb24/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/bomb24/test -------------------------------------------------------------------------------- /bomb24/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ans = 1; 6 | int tmp = 1; 7 | 8 | for(tmp = 1;ans != 0x375f00;tmp++) 9 | { 10 | ans *= tmp; 11 | } 12 | 13 | printf("%d \n",tmp-1); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /lab0/arrays.c: -------------------------------------------------------------------------------- 1 | /* 2 | CSE 351 Lab 0 3 | Lecture 2 and the first section meeting will help you 4 | if none of this makes sense yet. 5 | */ 6 | 7 | 8 | // These #includes tell the compiler to include the named 9 | // header files, similar to imports in Java. The code for 10 | // these is generally located under /usr/include/, such 11 | // as /usr/include/assert.h. assert.h contains the 12 | // declaration of the assert() function, stdio.h contains 13 | // the declaration of the printf() function, and stdlib.h 14 | // contains the declaration of the malloc() and free() 15 | // functions, all of which are used in the code below. 16 | #include 17 | #include 18 | #include 19 | 20 | // Fill the given array with values. Note that C doesn't 21 | // keep track of the length of arrays, so we have to 22 | // specify it as an explicit parameter here, rather than 23 | // looking it up from the array as in Java. 24 | // Additionally, note that the type of the array parameter 25 | // here is int*, a pointer to an int. We'll learn more 26 | // about why int* is used here, but for now it is enough 27 | // to understand that array is an array of ints. 28 | void fillArray(int* array, int len) { 29 | printf("Filling an array at address %p with %d " 30 | "values\n", array, len); 31 | for (int i = 0; i < len; ++i) { 32 | array[i] = i * 3 + 2; 33 | // assert() verifies that the given condition is true 34 | // and exits the program otherwise. This is just a 35 | // "sanity check" to make sure that the line of code 36 | // above is doing what we intend. 37 | assert(array[i] == i * 3 + 2); 38 | } 39 | printf("Done!\n"); 40 | } 41 | 42 | // Structs are blocks of memory composed of smaller parts, 43 | // each of which has a name and is called a field. The 44 | // following struct definition has four int fields named 45 | // a, b, c, and d. 46 | // In this case, we use typedef to give structs of this 47 | // type a name, FourInts, which can be used like we use 48 | // other types such as int or char. 49 | typedef struct { 50 | int a, b, c, d; 51 | } FourInts; 52 | 53 | // main() is the entry point of the program. It has two 54 | // parameters: argc is the number of arguments that were 55 | // passed on the command line; argv is an array of those 56 | // arguments as strings. (Strings in C are arrays of 57 | // chars.) 58 | int main(int argc, char* argv[]) { 59 | // Create a new array capable of storing 10 elements 60 | // and fill it with values using the function declared 61 | // above. Arrays declared in this manner are allocated on 62 | // the stack, and must generally have a size (10, here) 63 | // that is a constant (i.e., the size is known when 64 | // writing the program, not computed when running it). 65 | int array[10]; 66 | // This is a block of memory big enough to store 10 67 | // ints. The name "array" here actually refers to the 68 | // address of this block of memory. 69 | // array[0] is the first int in this block of 70 | // memory, array[1] is the second, and so on. C does 71 | // not track or check array lengths, so it is up to us 72 | // to know how many elements the array contains. 73 | // 74 | // TODO(1): What happens if the second argument is set 75 | // to 11 instead? How about 100? 1000? Make sure to set 76 | // the second argument back to 10 when you are done 77 | // testing. 78 | // Answer: 79 | fillArray(array, 10); 80 | 81 | int value; 82 | // In C, we can take the address of something using the 83 | // & operator. &value is of the type int*, meaning that 84 | // it is a pointer to an integer (as it stores the 85 | // address in memory of where the actual int is located). 86 | // 87 | // TODO(2): We can actually use the address of the value 88 | // declared here as if it were an array of a single 89 | // element; why is this possible? 90 | // Answer: 91 | fillArray(&value, 1); 92 | // fillArray should set value to 0 * 3 + 2 = 2. 93 | assert(value == 2); 94 | 95 | // The following creates an instance of FourInts on the 96 | // stack. FourInts is really just an array of four ints, 97 | // although we can refer to the ints stored in it by 98 | // name as well. 99 | FourInts four_ints; 100 | // Set the first int to have a value of 0 and verify 101 | // that the value changed. 102 | four_ints.a = 0; 103 | assert(four_ints.a == 0); 104 | 105 | // Depending on whether or not you like to live 106 | // dangerously, the following is either exciting or 107 | // terrifying. Though &four_ints is of type FourInts* 108 | // (as in a pointer to a FourInts struct), we can 109 | // use a cast to pretend that it is actually an array 110 | // of integers instead. It's all just memory after all. 111 | // The "(int*)" tells the C compiler that we want to treat 112 | // that address "&four_ints" as an address to an int (in 113 | // this case the start of an array) rather than a 114 | // FourInts struct. 115 | fillArray((int*) &four_ints, 4); 116 | // We can confirm that fillArray updated the values 117 | // in the FourInts struct: 118 | assert(four_ints.a == 2); 119 | assert(four_ints.b == 5); 120 | assert(four_ints.c == 8); 121 | assert(four_ints.d == 11); 122 | 123 | // In the case that the size of an array is not known 124 | // until runtime, the malloc() function can be used to 125 | // allocate memory dynamically. Memory that is 126 | // allocated dynamically is stored on the heap, which 127 | // is separate from the stack. We'll talk about all these 128 | // regions of memory later in the course. C is unlike Java, 129 | // however, in that dynamically-allocated memory must 130 | // be freed explicitly when the program is done using 131 | // it via the free() function. malloc() takes a single 132 | // argument, which is the number of bytes to allocate, 133 | // and returns the address of a fresh memory object 134 | // whose size is the given argument. 135 | // sizeof(int) gives the size of an int in bytes 136 | // (which is four), so sizeof(int) * 5 is 20. 137 | int* heap_array = (int*) malloc(sizeof(int) * 5); 138 | fillArray(heap_array, 5); 139 | // Now that we have finished with the heap-allocated 140 | // array, free() the memory associated with it. 141 | // 142 | // TODO(3): What happens if we remove the free() 143 | // statement below? Try running "valgrind ./arrays" 144 | // after compiling the program both with and without 145 | // it. valgrind is a tool for analyzing how programs 146 | // use memory, which is often invaluable for C and 147 | // C++ programming. 148 | // Answer: 149 | free(heap_array); 150 | 151 | // TODO(4): Now it's your turn to write some code. 152 | // Using malloc(), allocate a FourInts struct 153 | // dynamically (on the heap) and use fillArray to 154 | // populate it with values. The sizeof function can 155 | // be used on any data type. Make sure to free the 156 | // memory when you are done, and use the valgrind 157 | // tool mentioned above to check that there aren't 158 | // any errors. As a "sanity check," add four assert 159 | // statements to verify that the a, b, c, and d 160 | // fields of the FourInts struct are set to what 161 | // you would expect. (Hint, since you will have a 162 | // pointer to a FourInts struct you will need to 163 | // use the -> operator to access fields of a 164 | // FourInts* variable instead of the . operator 165 | // we used on the FourInts above. ptr->a is 166 | // equivalent to (*ptr).a . Note the difference 167 | // between FourInts and FourInts*.) 168 | return 0; 169 | } 170 | -------------------------------------------------------------------------------- /lab0/lab0.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab0/lab0.pdf -------------------------------------------------------------------------------- /lab0/lab1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab0/lab1.pdf -------------------------------------------------------------------------------- /lab0/lab2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab0/lab2.pdf -------------------------------------------------------------------------------- /lab0/lab3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab0/lab3.pdf -------------------------------------------------------------------------------- /lab0/lab4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab0/lab4.pdf -------------------------------------------------------------------------------- /lab0/lab5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab0/lab5.pdf -------------------------------------------------------------------------------- /lab0/my_lab0.c: -------------------------------------------------------------------------------- 1 | /* 2 | CSE 351 Lab 0 3 | Lecture 2 and the first section meeting will help you 4 | if none of this makes sense yet. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | void fillArray(int* array, int len) { 12 | printf("Filling an array at address %p with %d " 13 | "values\n", array, len); 14 | for (int i = 0; i < len; ++i) { 15 | array[i] = i * 3 + 2; 16 | assert(array[i] == i * 3 + 2); 17 | } 18 | printf("Done!\n"); 19 | } 20 | 21 | typedef struct { 22 | int a, b, c, d; 23 | } FourInts; 24 | 25 | int main(int argc, char* argv[]) { 26 | 27 | int array[10]; 28 | 29 | // TODO(1): What happens if the second argument is set 30 | // to 11 instead? How about 100? 1000? Make sure to set 31 | // the second argument back to 10 when you are done 32 | // testing. 33 | 34 | /* Answer by Jason Leaster: 35 | This problem's purpose is to guide 36 | you to understand what is "stack". You may have to 37 | understand code on running-time at level of assembly. 38 | Here is my notes, 39 | ------------------------------------------------------- 40 | http://blog.csdn.net/cinmyheart/article/details/24483461 41 | 42 | http://blog.csdn.net/cinmyheart/article/details/39142471 43 | ------------------------------------------------------- 44 | finish it and it will help you to get a background to 45 | understand this problem. 46 | 47 | If you set the second argument to "11" instead, the 48 | program work well in 64-bits Ubuntu. But if you set 49 | the second parameter bigger than 12 (like 13,18,1000), 50 | you would destroy the %rbp register's value in this 51 | program and you will see "core dump" when you run it. 52 | 53 | */ 54 | fillArray(array, 10); 55 | 56 | int value; 57 | // 58 | // TODO(2): We can actually use the address of the value 59 | // declared here as if it were an array of a single 60 | // element; why is this possible? 61 | 62 | /* Answer by Jason Leaster: 63 | 64 | For this problem, what I want to 65 | say is that "God save me, it just a feature of C". 66 | */ 67 | fillArray(&value, 1); 68 | assert(value == 2); 69 | 70 | FourInts four_ints; 71 | four_ints.a = 0; 72 | assert(four_ints.a == 0); 73 | 74 | fillArray((int*) &four_ints, 4); 75 | assert(four_ints.a == 2); 76 | assert(four_ints.b == 5); 77 | assert(four_ints.c == 8); 78 | assert(four_ints.d == 11); 79 | 80 | int* heap_array = (int*) malloc(sizeof(int) * 5); 81 | fillArray(heap_array, 5); 82 | 83 | // TODO(3): What happens if we remove the free() 84 | // statement below? Try running "valgrind ./arrays" 85 | // after compiling the program both with and without 86 | // it. valgrind is a tool for analyzing how programs 87 | // use memory, which is often invaluable for C and 88 | // C++ programming. 89 | 90 | /* Answer by Jason Leaster: 91 | I will show a figure which is 92 | in my blog to describe what would happen, if 93 | we remove the free() statement below. 94 | */ 95 | free(heap_array); 96 | 97 | // TODO(4): Now it's your turn to write some code. 98 | // Using malloc(), allocate a FourInts struct 99 | // dynamically (on the heap) and use fillArray to 100 | // populate it with values. The sizeof function can 101 | // be used on any data type. Make sure to free the 102 | // memory when you are done, and use the valgrind 103 | // tool mentioned above to check that there aren't 104 | // any errors. As a "sanity check," add four assert 105 | // statements to verify that the a, b, c, and d 106 | // fields of the FourInts struct are set to what 107 | // you would expect. (Hint, since you will have a 108 | // pointer to a FourInts struct you will need to 109 | // use the -> operator to access fields of a 110 | // FourInts* variable instead of the . operator 111 | // we used on the FourInts above. ptr->a is 112 | // equivalent to (*ptr).a . Note the difference 113 | // between FourInts and FourInts*.) 114 | 115 | /* 116 | Answer for TODO(4) by jasonleaster: 117 | 118 | It is too easy for a C programmer. 119 | I have to say sorry about someone who can't 120 | use malloc() and free() correctly. 121 | 122 | I don't want to demo for someone how to use 123 | free() and malloc(). 124 | 125 | Nice trip. :) 126 | */ 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /lab1.tar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/CSAPP/c593c50d1304678b71e98e1f4b3412ee595bbcd0/lab1.tar -------------------------------------------------------------------------------- /lab1/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile that builds btest and other helper programs for the CS:APP data lab 3 | # 4 | CC = gcc 5 | CFLAGS = -O -Wall -m64 -g 6 | LIBS = -lm 7 | 8 | all: btest fshow ishow ptest 9 | 10 | btest: btest.c bits.c decl.c tests.c btest.h bits.h 11 | $(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c 12 | 13 | fshow: fshow.c 14 | $(CC) $(CFLAGS) -o fshow fshow.c 15 | 16 | ishow: ishow.c 17 | $(CC) $(CFLAGS) -o ishow ishow.c 18 | 19 | ptest: ptest.c pointer.c 20 | $(CC) $(CFLAGS) -Wno-unused-variable -o ptest ptest.c pointer.c 21 | 22 | # Forces a recompile. Used by the driver program. 23 | btestexplicit: 24 | $(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c 25 | 26 | clean: 27 | rm -f *.o btest fshow ishow ptest *~ 28 | 29 | test: btest ptest 30 | ./btest 31 | ./ptest 32 | 33 | -------------------------------------------------------------------------------- /lab1/README: -------------------------------------------------------------------------------- 1 | *********************** 2 | The CS:APP Data Lab 3 | Directions to Students 4 | *********************** 5 | 6 | Your goal is to modify your copy of bits.c so that it passes all the 7 | tests in btest without violating any of the coding guidelines. 8 | 9 | 10 | ********* 11 | 0. Files: 12 | ********* 13 | 14 | Makefile - Makes btest, fshow, and ishow 15 | README - This file 16 | bits.c - The file you will be modifying and handing in 17 | bits.h - Header file 18 | btest.c - The main btest program 19 | btest.h - Used to build btest 20 | decl.c - Used to build btest 21 | tests.c - Used to build btest 22 | tests-header.c- Used to build btest 23 | dlc* - Rule checking compiler binary (data lab compiler) 24 | driver.pl* - Driver program that uses btest and dlc to autograde bits.c 25 | fshow.c - Utility for examining floating-point representations 26 | ishow.c - Utility for examining integer representations 27 | 28 | *********************************************************** 29 | 1. Modifying bits.c and checking it for compliance with dlc 30 | *********************************************************** 31 | 32 | IMPORTANT: Carefully read the instructions in the bits.c file before 33 | you start. These give the coding rules that you will need to follow if 34 | you want full credit. 35 | 36 | Use the dlc compiler (./dlc) to automatically check your version of 37 | bits.c for compliance with the coding guidelines: 38 | 39 | unix> ./dlc bits.c 40 | 41 | dlc returns silently if there are no problems with your code. 42 | Otherwise it prints messages that flag any problems. Running dlc with 43 | the -e switch: 44 | 45 | unix> ./dlc -e bits.c 46 | 47 | causes dlc to print counts of the number of operators used by each function. 48 | 49 | Once you have a legal solution, you can test it for correctness using 50 | the ./btest program. 51 | 52 | ********************* 53 | 2. Testing with btest 54 | ********************* 55 | 56 | The Makefile in this directory compiles your version of bits.c with 57 | additional code to create a program (or test harness) named btest. 58 | 59 | To compile and run the btest program, type: 60 | 61 | unix> make btest 62 | unix> ./btest [optional cmd line args] 63 | 64 | You will need to recompile btest each time you change your bits.c 65 | program. When moving from one platform to another, you will want to 66 | get rid of the old version of btest and generate a new one. Use the 67 | commands: 68 | 69 | unix> make clean 70 | unix> make btest 71 | 72 | Btest tests your code for correctness by running millions of test 73 | cases on each function. It tests wide swaths around well known corner 74 | cases such as Tmin and zero for integer puzzles, and zero, inf, and 75 | the boundary between denormalized and normalized numbers for floating 76 | point puzzles. When btest detects an error in one of your functions, 77 | it prints out the test that failed, the incorrect result, and the 78 | expected result, and then terminates the testing for that function. 79 | 80 | Here are the command line options for btest: 81 | 82 | unix> ./btest -h 83 | Usage: ./btest [-hg] [-r ] [-f [-1|-2|-3 ]*] [-T