Skip to content

Commit

Permalink
Initial section material for 2022.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 7, 2022
0 parents commit 5a54372
Show file tree
Hide file tree
Showing 7 changed files with 653 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#*#
*.aux
*.backup
*.core
*.dSYM
*.dvi
*.gcda
*.gch
*.log
*.noopt
*.o
*.optimized
*.pch
*.unsafe
*.xcodeproj
*~
.DS_Store
.cproject
.deps
.gitcheckout
.goutputstream-*
.hg
.project
.svn
.vscode
a.out
core*
cmake-build-*
cscope.out
pset.tgz
pset[1-9].tgz
pset[1-9]grade.tgz
strace.out
tags
tags.*
typescript
vgcore*
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CS 61 Sections
==============

This repository contains section material for Harvard’s CS 61 class,
Systems Programming and Machine Organization.

For more information, see the course site:
https://cs61.seas.harvard.edu/

Follow the section discussion using the pages listed under “Sections”
at https://cs61.seas.harvard.edu/
184 changes: 184 additions & 0 deletions common/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# compiler flags
CFLAGS := -std=gnu11 -Wall -Wextra -Wshadow -g $(DEFS) $(CFLAGS)
CXXFLAGS := -std=gnu++2a -Wall -Wextra -Wshadow -g $(DEFS) $(CXXFLAGS)

O ?= -O3
ifeq ($(filter 0 1 2 3 s z g fast,$(O)),$(strip $(O)))
override O := -O$(O)
endif

PTHREAD ?= 0
ifeq ($(PTHREAD),1)
CFLAGS += -pthread
CXXFLAGS += -pthread
WANT_TSAN ?= 1
endif

PIE ?= 1
ifeq ($(PIE),0)
LDFLAGS += -no-pie
endif

# compiler variant
ifeq ($(COMPILER),clang)
ifeq ($(origin CC),default)
ifeq ($(shell if clang --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi),1)
CC = clang
endif
endif
ifeq ($(origin CXX),default)
ifeq ($(shell if clang++ --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi),1)
CXX = clang++
endif
endif
endif
ifeq ($(COMPILER),gcc)
ifeq ($(origin CC),default)
ifeq ($(shell if gcc --version 2>&1 | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
CC = gcc
endif
endif
ifeq ($(origin CXX),default)
ifeq ($(shell if g++ --version 2>&1 | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
CXX = g++
endif
endif
endif

ISCLANG := $(shell if $(CC) --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi)
ifeq ($(ISCLANG),1)
BADCXXFLAGS ?= -fno-if-conversion -fno-if-conversion2
endif

ifeq ($(NEED_CXX_GCC),1)
GXX_ISCLANG := $(shell if g++ --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi)
ifeq ($(GXX_ISCLANG),1)
ifeq ($(shell if g++-12 --version 2>/dev/null | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
CXX_GCC = g++-12
else ifeq ($(shell if g++-11 --version 2>/dev/null | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
CXX_GCC = g++-11
else ifeq ($(shell if g++-10 --version 2>/dev/null | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
CXX_GCC = g++-10
else
CXX_GCC = false
endif
else
CXX_GCC = g++
endif
endif

# sanitizer arguments
ifndef SAN
SAN := $(SANITIZE)
endif
ifeq ($(SAN),1)
ifndef ASAN
ASAN := $(if $(strip $(shell $(CC) -v 2>&1 | grep 'build=aarch.*target=x86')),,1)
endif
endif
ifndef TSAN
ifeq ($(WANT_TSAN),1)
TSAN := $(SAN)
endif
endif

check_for_sanitizer = $(if $(strip $(shell $(CC) -fsanitize=$(1) -x c -E /dev/null 2>&1 | grep sanitize=)),$(info ** WARNING: The `$(CC)` compiler does not support `-fsanitize=$(1)`.),1)
ifeq ($(TSAN),1)
ifeq ($(call check_for_sanitizer,thread),1)
CFLAGS += -fsanitize=thread
CXXFLAGS += -fsanitize=thread
endif
else
ifeq ($(or $(ASAN),$(LSAN),$(LEAKSAN)),1)
ifeq ($(call check_for_sanitizer,address),1)
CFLAGS += -fsanitize=address
CXXFLAGS += -fsanitize=address
endif
endif
ifeq ($(or $(LSAN),$(LEAKSAN)),1)
ifeq ($(call check_for_sanitizer,leak),1)
CFLAGS += -fsanitize=leak
CXXFLAGS += -fsanitize=leak
endif
endif
endif
ifeq ($(or $(UBSAN),$(SAN)),1)
ifeq ($(call check_for_sanitizer,undefined),1)
CFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
CXXFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
endif
endif

# profiling
ifeq ($(or $(PROFILE),$(PG)),1)
CFLAGS += -pg
CXXFLAGS += -pg
endif

# these rules ensure dependencies are created
DEPCFLAGS = -MD -MF $(DEPSDIR)/$(patsubst %.o,%,$(@F)).d -MP
DEPSDIR := .deps
BUILDSTAMP := $(DEPSDIR)/rebuildstamp
DEPFILES := $(wildcard $(DEPSDIR)/*.d)
ifneq ($(DEPFILES),)
include $(DEPFILES)
endif

# when the C compiler or optimization flags change, rebuild all objects
ifneq ($(strip $(DEP_CC)),$(strip $(CC) $(CPPFLAGS) $(CFLAGS) $(O)))
DEP_CC := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CC:=$(CC) $(CPPFLAGS) $(CFLAGS) $(O)" >$(DEPSDIR)/_cc.d)
endif
ifneq ($(strip $(DEP_CXX)),$(strip $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(LDFLAGS)))
DEP_CXX := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CXX:=$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(LDFLAGS)" >$(DEPSDIR)/_cxx.d)
endif


V = 0
ifeq ($(V),1)
run = $(1) $(3)
xrun = /bin/echo "$(1) $(3)" && $(1) $(3)
else
run = @$(if $(2),/bin/echo " $(2) $(3)" &&,) $(1) $(3)
xrun = $(if $(2),/bin/echo " $(2) $(3)" &&,) $(1) $(3)
endif
runquiet = @$(1) $(3)

CXX_LINK_PREREQUISITES = $(CXX) $(CXXFLAGS) $(LDFLAGS) $(O) -o $@ $^

CLEANASM = 1
ifeq ($(CLEANASM),1)
cleanasm = perl -ni -e '$$badsection = !!/\.note\.gnu/ if /^\s+\.section/; print if !/^(?:\# BB|\s+\.cfi|\s+\.p2align|\s+\.loc|\.LF[BE]|\.LVL|\s+\# =>This|\s+\# kill)/ && !$$badsection' $(1)
else
cleanasm = :
endif

DEFAULT_ASM_CXXFLAGS ?= $(O)
flagged_compile = @ARGS=$$(grep '^//!' $< | sed 's/.*!//$(patsubst %,;s/ % */ /,$(BADCXXFLAGS));s/^ | $$//'); \
if test -z "$$ARGS"; then ARGS="$(DEFAULT_ASM_CXXFLAGS)"; fi; \
$(call xrun,$(CXX) $(3) $$ARGS -o $(2) $(1),COMPILE $$ARGS $(1) -o $(2))
flagged_compile_S = $(call flagged_compile,$(1),$(2),$(filter-out -g,$(3) -S)) && { $(call cleanasm,$(2)); }
flagged_compile_c = $(call flagged_compile,$(1),$(2),$(3) -c)


PERCENT := %

# cancel implicit rules we don't want
%: %.c
%.o: %.c
%: %.cc
%.o: %.cc
%: %.o
%.o: %.s

$(BUILDSTAMP):
@mkdir -p $(@D)
@echo >$@

always:
@:

clean-hook:
@:

.PHONY: always clean-hook
.PRECIOUS: %.o
122 changes: 122 additions & 0 deletions cs61-run-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#! /bin/bash

maindir=""
destdir=cs61-sections

fresh=
verbose=false
arch="`arch`"
tag=
platform=
while test "$#" -ne 0; do
if test "$1" = "-f" -o "$1" = "--fresh"; then
fresh=1
shift
elif test "$1" = "-V" -o "$1" = "--verbose"; then
verbose=true
shift
elif test "$1" = "-a" -o "$1" = "--arm" -o "$1" = "--arm64"; then
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
platform=linux/arm64
shift
else
echo "\`cs61-run-docker --arm\` only works on ARM64 hosts" 1>&2
exit 1
fi
elif test "$1" = "-x" -o "$1" = "--x86-64" -o "$1" = "--x86_64" -o "$1" = "--amd64"; then
platform=linux/amd64
shift
else
armtext=
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
armtext=" [-a|--arm] [-x|--x86-64]"
fi
echo "Usage: cs61-run-docker [-f|--fresh]$armtext [-V|--verbose]" 1>&2
exit 1
fi
done
if test -z "$platform" -a \( "$arch" = "arm64" -o "$arch" = "aarch64" \); then
platform=linux/arm64
elif test -z "$platform"; then
platform=linux/amd64
fi
if test -z "$tag" -a "$platform" = linux/arm64; then
tag=cs61:arm64
elif test -z "$tag"; then
tag=cs61:latest
fi


vexec () {
if $verbose; then
echo "$@"
fi
exec "$@"
}

if stat --format %i / >/dev/null 2>&1; then
statformatarg="--format"
else
statformatarg="-f"
fi
myfileid=`stat $statformatarg %d:%i "${BASH_SOURCE[0]}" 2>/dev/null`

dir="`pwd`"
subdir=""
while test "$dir" != / -a "$dir" != ""; do
thisfileid=`stat $statformatarg %d:%i "$dir"/cs61-run-docker 2>/dev/null`
if test -n "$thisfileid" -a "$thisfileid" = "$myfileid"; then
maindir="$dir"
break
fi
subdir="/`basename "$dir"`$subdir"
dir="`dirname "$dir"`"
done

if test -z "$maindir" && expr "${BASH_SOURCE[0]}" : / >/dev/null 2>&1; then
maindir="`dirname "${BASH_SOURCE[0]}"`"
subdir=""
fi

ssharg=
sshenvarg=
if test -n "$SSH_AUTH_SOCK" -a "`uname`" = Darwin; then
ssharg=" --mount type=bind,src=/run/host-services/ssh-auth.sock,target=/run/host-services/ssh-auth.sock"
sshenvarg=" -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
fi

if test -n "$maindir" -a -z "$fresh"; then
existing_image="`docker ps -f status=running -f ancestor=$tag -f volume=/host_mnt"$maindir" --no-trunc --format "{{.CreatedAt}},{{.ID}}" | sort -r | head -n 1`"
if test -n "$existing_image"; then
created_at="`echo $existing_image | sed 's/,.*//'`"
image="`echo $existing_image | sed 's/^.*,//'`"
image12="`echo $image | head -c 12`"
echo "* Using running container $image12, created $created_at" 1>&2
echo "- To start a new container, exit then \`cs61-run-docker -f\`" 1>&2
echo "- To kill this container, exit then \`docker kill $image12\`" 1>&2
vexec docker exec -it$sshenvarg $image /bin/bash
fi
fi

netarg=
if test `uname` = Darwin; then
if ! netstat -n -a -p tcp | grep '\.6169[ ].*LISTEN' >/dev/null; then
netarg="$netarg "'--expose=6169/tcp -p 6169:6169/tcp'
fi
if ! netstat -n -a -p tcp | grep '\.12949[ ].*LISTEN' >/dev/null; then
netarg="$netarg "'--expose=12949/tcp -p 12949:12949/tcp'
fi
elif test -x /bin/netstat; then
if ! netstat -n -a -p tcp | grep '\.6169[ ].*LISTEN' >/dev/null; then
netarg="$netarg "'--expose=6169/tcp -p 6169:6169/tcp'
fi
if ! netstat -n -l -t | grep ':12949[ ]' >/dev/null; then
netarg="$netarg "'--expose=12949/tcp -p 12949:12949/tcp'
fi
fi

if test -n "$maindir"; then
vexec docker run -it --platform $platform --rm --privileged --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined -v "$maindir":/home/cs61-user/$destdir$ssharg -w "/home/cs61-user/$destdir$subdir" $netarg$sshenvarg $tag
else
vexec docker run -it --platform $platform --rm --privileged --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined $netarg$sshenvarg $tag
fi
Loading

0 comments on commit 5a54372

Please sign in to comment.