Skip to content

Commit fbfa67c

Browse files
committed
Add sanity checks on locks for debugging.
1 parent 1b1180c commit fbfa67c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

runtime/process-posix.c

+45
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@
44
#include <spawn.h>
55
#include "process.h"
66

7+
//============================================================
8+
//=================== Lock Assumption ========================
9+
//============================================================
10+
11+
volatile int locked = 0;
12+
13+
void assert_lock (char* ctxt) {
14+
if(!locked){
15+
printf("ERROR: Lock is assumed to be obtained but isn't. (%s)\n", ctxt);
16+
exit(-1);
17+
}
18+
}
19+
20+
void obtain_lock (char* ctxt) {
21+
if(locked){
22+
printf("ERROR: Cannot obtain lock. Lock is already assumed. (%s)\n", ctxt);
23+
exit(-1);
24+
}
25+
locked = 1;
26+
}
27+
28+
void release_lock (char* ctxt) {
29+
if(!locked){
30+
printf("ERROR: Cannot release lock. Lock is not assumed. (%s)\n", ctxt);
31+
exit(-1);
32+
}
33+
locked = 0;
34+
}
35+
736
//============================================================
837
//================= ChildProcess Registration ================
938
//============================================================
@@ -38,6 +67,7 @@ volatile ChildProcessList * volatile child_processes = NULL;
3867
// Add a new ChildProcess to the global 'child_processes' list.
3968
// Precondition: SIGCHLD is blocked
4069
void add_child_process (ChildProcess* child) {
70+
assert_lock("add_child_process");
4171
ChildProcessList * new_node = (ChildProcessList*)malloc(sizeof(ChildProcessList));
4272
new_node->proc = child;
4373
new_node->next = child_processes;
@@ -47,6 +77,7 @@ void add_child_process (ChildProcess* child) {
4777
// Return the ChildProcess with the given process id.
4878
// Returns NULL if there is none.
4979
static ChildProcess* get_child_process (pid_t pid){
80+
assert_lock("get_child_process");
5081
volatile ChildProcessList * volatile curr = child_processes;
5182
while(curr != NULL && curr->proc->pid != pid)
5283
curr = curr->next;
@@ -58,6 +89,7 @@ static ChildProcess* get_child_process (pid_t pid){
5889
// 'child_processes' list if it exists in the list.
5990
// Precondition: SIGCHLD is blocked
6091
static void remove_child_process (pid_t pid) {
92+
assert_lock("remove_child_process");
6193

6294
// Find the ChildProcess with matching pid.
6395
// After this loop, either:
@@ -92,6 +124,7 @@ static void register_child_process (
92124
FILE* fout,
93125
FILE* ferr,
94126
ProcessStatus** status) {
127+
assert_lock("register_child_process");
95128

96129
// Initialize and save ProcessStatus struct.
97130
ProcessStatus* st = (ProcessStatus*)malloc(sizeof(ProcessStatus));
@@ -125,6 +158,7 @@ static bool is_dead_status (stz_int status_code) {
125158
// with the given process id.
126159
// Precondition: SIGCHLD is blocked
127160
static void set_child_status (pid_t pid, stz_int status_code) {
161+
assert_lock("set_child_status");
128162
ChildProcess* proc = get_child_process(pid);
129163
if(proc != NULL)
130164
*(proc->status) = status_code;
@@ -135,6 +169,8 @@ static void set_child_status (pid_t pid, stz_int status_code) {
135169
// metadata struct.
136170
// Precondition: SIGCHLD is blocked
137171
static void update_child_status (pid_t pid) {
172+
assert_lock("update_child_status");
173+
138174
//Retrieve the status of the given process.
139175
int status;
140176
int ret_pid = waitpid(pid, &status, WNOHANG | WUNTRACED | WCONTINUED);
@@ -151,6 +187,8 @@ static void update_child_status (pid_t pid) {
151187
// Update the current status of all registered child processes.
152188
// Precondition: SIGCHLD is blocked
153189
static void update_all_child_statuses () {
190+
assert_lock("update_all_child_statuses");
191+
154192
volatile ChildProcessList * volatile curr = child_processes;
155193
while(curr != NULL) {
156194
//NOTE: curr->next is retrieved preemptively because
@@ -176,6 +214,8 @@ struct sigaction old_sigchild_action;
176214
// be executed if it is already in the middle of executing. SIGCHILD
177215
// must be blocked.
178216
void autoreaping_sigchld_handler(int sig){
217+
obtain_lock("autoreaping_sigchld_handler");
218+
179219
//Update the statuses of all registered child processes.
180220
update_all_child_statuses();
181221

@@ -189,6 +229,8 @@ void autoreaping_sigchld_handler(int sig){
189229
old_sigchild_action.sa_handler != SIG_IGN){
190230
old_sigchild_action.sa_handler(sig);
191231
}
232+
233+
release_lock("autoreaping_sigchld_handler");
192234
}
193235

194236
// This installs the autoreaping signal handler.
@@ -215,6 +257,8 @@ void install_autoreaping_sigchld_handler () {
215257
//Blocks the SIGCHILD signal by updating the signal mask.
216258
//Returns the previous signal mask.
217259
static sigset_t block_sigchild () {
260+
obtain_lock("block_sigchild");
261+
218262
//Create signal mask containing only SIGCHLD.
219263
sigset_t sigchld_mask;
220264
sigemptyset(&sigchld_mask);
@@ -250,6 +294,7 @@ static void suspend_until_sigchild () {
250294
static void restore_signal_mask (sigset_t* old_mask) {
251295
if(sigprocmask(SIG_SETMASK, old_mask, NULL))
252296
exit_with_error();
297+
release_lock("restore_signal_mask");
253298
}
254299

255300
//============================================================

scripts/make.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ mkdir -p "$PKGDIR"
8686

8787
if [[ "$COMPILE" == 1 ]]; then
8888
echo "Compiling $DPLATFORM Stanza Pkgs"
89-
"$STANZA" build-stanza.proj stz/driver $PKGFILES -pkg "$PKGDIR" -platform $PLATFORM
89+
"$STANZA" build-stanza.proj stz/driver $PKGFILES -pkg "$PKGDIR" -platform $PLATFORM -build-from-source
9090

9191
echo "Compiling $DPLATFORM Stanza Optimized Pkgs"
92-
"$STANZA" build-stanza.proj stz/driver $PKGFILES -pkg "$PKGDIR" -optimize -platform $PLATFORM
92+
"$STANZA" build-stanza.proj stz/driver $PKGFILES -pkg "$PKGDIR" -optimize -platform $PLATFORM -build-from-source
9393

9494
echo "Compiling $DPLATFORM Stanza Executable"
9595
"$STANZA" build-stanza.proj stz/driver -pkg "$PKGDIR" -s "$STANZA_S" -optimize -platform $PLATFORM

0 commit comments

Comments
 (0)