Skip to content

Commit b0f00cf

Browse files
author
Garrett Wang
committed
Add flag to switch between order or non-order
1 parent 7e3e1c9 commit b0f00cf

File tree

12 files changed

+286
-25
lines changed

12 files changed

+286
-25
lines changed

example/test.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
#include <assert.h>
1+
#include <pthread.h>
22
#include <stdio.h>
33
#include <iostream>
44

55
#include "color_log.hh"
66
#include "libgthread.hh"
77
#include "log.h"
88

9-
#define THREAD_NUM 50
10-
#define SECONDARY_THREAD_NUM 50
9+
#define THREAD_NUM 20
10+
#define SECONDARY_THREAD_NUM 30
1111

12-
#define A_SIZE 1000
13-
#define B_SIZE 1500
12+
#define A_SIZE 100
13+
#define B_SIZE 150
1414

15-
#define DOUBLE
15+
//#define DOUBLE
1616

1717
// Forward declaration
1818
void *fn1(void *);
@@ -48,12 +48,12 @@ void *fn1(void *arg) {
4848
b->c = b->c + 1;
4949

5050
#if defined(DOUBLE)
51-
gthread_t threads[SECONDARY_THREAD_NUM];
51+
pthread_t threads[SECONDARY_THREAD_NUM];
5252
for (int i = 0; i < SECONDARY_THREAD_NUM; i++) {
53-
GThread::Create(&threads[i], fn2, b);
53+
pthread_create(&threads[i], NULL, fn2, b);
5454
}
5555
for (int i = 0; i < SECONDARY_THREAD_NUM; i++) {
56-
GThread::Join(threads[i]);
56+
pthread_join(threads[i], NULL);
5757
}
5858
#endif
5959

@@ -93,15 +93,15 @@ void print_blob(blob_t *b) {
9393
}
9494

9595
int main() {
96-
gthread_t threads[THREAD_NUM];
96+
pthread_t threads[THREAD_NUM];
9797
blob_t *b = (blob_t *)malloc(sizeof(blob_t));
9898
memset(b, 0, sizeof(blob_t));
9999

100100
for (int i = 0; i < THREAD_NUM; i++) {
101-
GThread::Create(&threads[i], fn1, b);
101+
pthread_create(&threads[i], NULL, fn1, b);
102102
}
103103
for (int i = 0; i < THREAD_NUM; i++) {
104-
GThread::Join(threads[i]);
104+
pthread_join(threads[i], NULL);
105105
}
106106

107107
verify(b);

partC.cc

+246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#include <math.h>
2+
#include <openssl/md5.h>
3+
#include <pthread.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <unistd.h>
10+
11+
#include "libgthread.hh"
12+
13+
#define NUM_THREAD 4
14+
#define MAX_USERNAME_LENGTH 24
15+
#define PASSWORD_LENGTH 6
16+
#define TOTAL_PASSWORDS (pow(26.0, PASSWORD_LENGTH))
17+
18+
// Use this struct to pass arguments to our threads
19+
typedef struct thread_args {
20+
int arg;
21+
} thread_args_t;
22+
23+
// use this struct to receive results from our threads
24+
typedef struct thread_result {
25+
int result;
26+
} thread_result_t;
27+
28+
typedef struct password_entry {
29+
char username[MAX_USERNAME_LENGTH + 1];
30+
uint8_t password_md5[MD5_DIGEST_LENGTH + 1];
31+
bool cracked;
32+
struct password_entry *next;
33+
} password_entry_t;
34+
35+
void crack_passwords(char *plaintext);
36+
void generate_all_possibilities(size_t number);
37+
int md5_string_to_bytes(const char *md5_string, uint8_t *bytes);
38+
password_entry_t *read_password_file(const char *filename);
39+
void print_md5_bytes(const uint8_t *bytes);
40+
41+
password_entry_t *passwords;
42+
43+
// pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
44+
45+
/**
46+
* This is our thread function. When we call pthread_create with this as an
47+
* argument
48+
* a new thread is created to run this thread in parallel with the program's
49+
* main
50+
* thread. When passing parameters to thread functions or accepting return
51+
* values we
52+
* have to jump through a few hoops because POSIX threads can only take and
53+
* return
54+
* a void*.
55+
*/
56+
void *thread_fn(void *void_args) {
57+
// Case the args pointer to the appropriate type and print our argument
58+
thread_args_t *args = (thread_args_t *)void_args;
59+
generate_all_possibilities(args->arg);
60+
// Return the pointer to allocated memory to our parent thread.
61+
return NULL;
62+
}
63+
64+
int main(int argc, char **argv) {
65+
if (argc != 2) {
66+
fprintf(stderr, "Usage: %s <path to password directory file>\n", argv[0]);
67+
exit(1);
68+
}
69+
70+
// Read in the password file
71+
passwords = read_password_file(argv[1]);
72+
73+
// Initilization
74+
password_entry_t *current = passwords;
75+
while (current != NULL) {
76+
current->cracked = false;
77+
current = current->next;
78+
}
79+
// You'll have to move over any code from partB that you would like to use.
80+
// Here's a quick little thread demo.
81+
pthread_t threads[NUM_THREAD];
82+
83+
// Make two structs so we can pass arguments to our threads
84+
thread_args_t thread_args[NUM_THREAD];
85+
86+
// Create threads
87+
for (size_t i = 0; i < NUM_THREAD; i++) {
88+
thread_args[i].arg = i * TOTAL_PASSWORDS / 4;
89+
if (pthread_create(&threads[i], NULL, thread_fn, &thread_args[i]) != 0) {
90+
perror("Error creating thread 1");
91+
exit(2);
92+
}
93+
}
94+
95+
// Make pointers to the thread result structs that our threads will write into
96+
// thread_result_t *thread_result[NUM_THREAD];
97+
98+
// Wait threads to join
99+
for (size_t i = 0; i < NUM_THREAD; i++) {
100+
if (pthread_join(threads[i], NULL) != 0) {
101+
perror("Error joining with thread 1");
102+
exit(2);
103+
}
104+
}
105+
106+
// // Show the results
107+
// printf("Thread 1 returned %d\n", thread1_result->result);
108+
// printf("Thread 2 retunred %d\n", thread2_result->result);
109+
//
110+
// // Free the result structs, which were originally allocated in our threads
111+
// free(thread1_result);
112+
// free(thread2_result);
113+
}
114+
115+
void generate_all_possibilities(size_t number) {
116+
int cur_digit = PASSWORD_LENGTH - 1;
117+
size_t len = TOTAL_PASSWORDS / 4;
118+
char guess[7] = "aaaaaa";
119+
size_t end = number + len;
120+
for (size_t i = number; i < end; i++) {
121+
size_t num = i;
122+
while (num > 0) {
123+
guess[cur_digit--] = num % 26 + 'a';
124+
num /= 26;
125+
}
126+
cur_digit = PASSWORD_LENGTH - 1;
127+
128+
crack_passwords(guess);
129+
}
130+
}
131+
132+
void crack_passwords(char *plaintext) {
133+
uint8_t password_hash[MD5_DIGEST_LENGTH];
134+
MD5((unsigned char *)plaintext, strlen(plaintext), password_hash);
135+
136+
// Check if the two hashes are equal
137+
password_entry_t *current = passwords;
138+
bool all_true = true;
139+
while (current != NULL) {
140+
if (!current->cracked) { // Has not been cracked yet
141+
if (memcmp(current->password_md5, password_hash, MD5_DIGEST_LENGTH) ==
142+
0) {
143+
printf("%s ", current->username);
144+
printf("%s\n", plaintext);
145+
// pthread_mutex_lock(&m);
146+
current->cracked = true;
147+
// pthread_mutex_unlock(&m);
148+
} else {
149+
all_true = false;
150+
}
151+
}
152+
current = current->next;
153+
}
154+
if (all_true) {
155+
printf("Rollback count= %zu\n", *Gstm::rollback_count_);
156+
_exit(0);
157+
}
158+
}
159+
160+
/**
161+
* Read a file of username and MD5 passwords. Return a linked list
162+
* of entries.
163+
* \param filename The path to the password file
164+
* \returns A pointer to the first node in the password list
165+
*/
166+
password_entry_t *read_password_file(const char *filename) {
167+
// Open the password file
168+
FILE *password_file = fopen(filename, "r");
169+
if (password_file == NULL) {
170+
perror("opening password file");
171+
exit(2);
172+
}
173+
174+
// Keep track of the current list
175+
password_entry_t *list = NULL;
176+
177+
// Read until we hit the end of the file
178+
while (!feof(password_file)) {
179+
// Make space for a new node
180+
password_entry_t *newnode =
181+
(password_entry_t *)malloc(sizeof(password_entry_t));
182+
183+
// Make space to hold the MD5 string
184+
char md5_string[MD5_DIGEST_LENGTH * 2 + 1];
185+
186+
// Try to read. The space in the format string is required to eat the
187+
// newline
188+
if (fscanf(password_file, "%s %s ", newnode->username, md5_string) != 2) {
189+
fprintf(stderr, "Error reading password file: malformed line\n");
190+
exit(2);
191+
}
192+
193+
// Convert the MD5 string to MD5 bytes in our new node
194+
if (md5_string_to_bytes(md5_string, newnode->password_md5) != 0) {
195+
fprintf(stderr, "Error reading MD5\n");
196+
exit(2);
197+
}
198+
199+
// Add the new node to the front of the list
200+
newnode->next = list;
201+
list = newnode;
202+
}
203+
204+
return list;
205+
}
206+
207+
/**
208+
* Convert a string representation of an MD5 hash to a sequence
209+
* of bytes. The input md5_string must be 32 characters long, and
210+
* the output buffer bytes must have room for MD5_DIGEST_LENGTH
211+
* bytes.
212+
*
213+
* \param md5_string The md5 string representation
214+
* \param bytes The destination buffer for the converted md5 hash
215+
* \returns 0 on success, -1 otherwise
216+
*/
217+
int md5_string_to_bytes(const char *md5_string, uint8_t *bytes) {
218+
// Check for a valid MD5 string
219+
if (strlen(md5_string) != 2 * MD5_DIGEST_LENGTH) return -1;
220+
221+
// Start our "cursor" at the start of the string
222+
const char *pos = md5_string;
223+
224+
// Loop until we've read enough bytes
225+
for (size_t i = 0; i < MD5_DIGEST_LENGTH; i++) {
226+
// Read one byte (two characters)
227+
int rc = sscanf(pos, "%2hhx", &bytes[i]);
228+
if (rc != 1) return -1;
229+
230+
// Move the "cursor" to the next hexadecimal byte
231+
pos += 2;
232+
}
233+
234+
return 0;
235+
}
236+
237+
/**
238+
* Print a byte array that holds an MD5 hash to standard output.
239+
*
240+
* \param bytes An array of bytes from an MD5 hash function
241+
*/
242+
void print_md5_bytes(const uint8_t *bytes) {
243+
for (size_t i = 0; i < MD5_DIGEST_LENGTH; i++) {
244+
printf("%02hhx", bytes[i]);
245+
}
246+
}

pthread/lock

-4.14 KB
Binary file not shown.

pthread/lock.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
#include "../src/log.h"
77

8-
#define THREAD_NUM 50
8+
#define THREAD_NUM 1000
99
#define SECONDARY_THREAD_NUM 50
1010

1111
#define A_SIZE 1000
1212
#define B_SIZE 1500
1313

14-
#define DOUBLE
14+
//#define DOUBLE
1515

1616
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1717

src/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ROOT = ..
22
TARGETS = gtest
3-
LIBS = pthread dl rt
3+
LIBS = pthread dl rt crypto
44

55
include $(ROOT)/common.mk

src/gstm.cc

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ void Gstm::WaitExited(pid_t predecessor) {
159159
// child
160160
if (ret != predecessor) {
161161
if (errno == ECHILD) {
162+
// Keep checking if the predecessor has exited in the case that
163+
// predecessor is not a child of current process
162164
while (kill(predecessor, 0) != -1)
163165
;
164166
if (errno == ESRCH) {

src/gtest

-8 Bytes
Binary file not shown.

src/gthread.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ void GThread::Create(gthread_t *t, void *(*start_routine)(void *), void *args) {
3939
} else {
4040
// Child process
4141
tid_ = getpid();
42-
// predecessor_ = 0;
42+
#if defined(NO_ORDER)
43+
predecessor_ = 0;
44+
#endif
4345

4446
AtomicBegin();
4547
// Execute thread function

src/test.cc src/lock.cc

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#include <pthread.h>
1+
#include <assert.h>
22
#include <stdio.h>
3+
#include <string.h>
34
#include <iostream>
45

5-
#include "color_log.hh"
66
#include "libgthread.hh"
77
#include "log.h"
88

9-
#define THREAD_NUM 20
10-
#define SECONDARY_THREAD_NUM 30
9+
#define THREAD_NUM 100
10+
#define SECONDARY_THREAD_NUM 50
1111

12-
#define A_SIZE 100
13-
#define B_SIZE 150
12+
#define A_SIZE 1000
13+
#define B_SIZE 1500
1414

1515
//#define DOUBLE
1616

@@ -104,7 +104,6 @@ int main() {
104104
pthread_join(threads[i], NULL);
105105
}
106106

107-
verify(b);
107+
// verify(b);
108108
std::cout << "c = " << b->c << std::endl;
109-
std::cout << "Rollback count = " << *Gstm::rollback_count_ << std::endl;
110109
}

src/passwords.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
curtsinger 84845701f95a308690645685ac1c6667
2+
kington 78e5ed5448a27a1d93a7c533e4421606
3+
klinge fca64b2fe3da88eef2ddddde2c4c96ff
4+
osera 9aa50c2bd8eed41ca562e19f0959e901
5+
rebelsky 7f50423c6c59ea2e3f5e783ceb714447
6+
stone b1099df5824f29f4cc06673b1ddf44e4
7+
vostinar 1fb1a060534164a18a99494122825190
8+
walker 27f5e15b6af3223f1176293cd015771d
9+
weinman 932d6f0f86ba8eba9492bf3c347a3eba
10+
wolz acae273a5a5c88b46b36d65a25f5f435

src/shm_object

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)