Read-Only eXecutable (ROX) #
OS가 수정되는 파일을 실행하려고 시도하면 어떻게 될까? 많은 OS에서는 디스크에서 변경 중인 코드를 프로세스가 실행하려고 할 경우, 예측할 수 없는 결과가 발생할 수 있기 때문에 실행 파일로 사용 중인 파일에 대한 쓰기를 거부Denying writes to executable하는 코드를 사용한다.
file_deny_write(): 열려 있는 파일에 대한 쓰기를 차단함file_allow_write(): 해당 파일을 호출하면 쓰기가 다시 활성화됨
파일을 닫으면 쓰기가 다시 활성화되기 때문에, 프로세스의 실행 파일에 대한 쓰기를 차단하려면 프로세스가 실행 중인 동안 해당 파일을 열어둬야 한다.
static bool load()- 프로그램이 open될 때
file_deny_write()호출 struct thread에 실행 중인 파일 구조체 추가하기
- 프로그램이 open될 때
void process_exit()- 현재 프로세스가 실행 중인 파일을 닫도록 수정하기
load()에 다음 코드를 추가했다. (struct thread에struct file *running_file필드를 추가했다.)
/* Open executable file. */
file = filesys_open (file_name);
if (file == NULL) {
printf ("load: %s: open failed\n", file_name);
goto done;
}
file_deny_write(file); // ← 추가
t->running_file = file; // ← 추가
process_exit()에서file_close()를 사용해 파일을 닫고 있는데, 이 함수 안에file_allow_write()가 포함되어 있기 때문에 명시적으로 write를 allow해줄 필요는 없다.
/* Close all file and deallocate the FDT */
for (int fd = 2; fd < 64; fd++) {
if (curr->fdt[fd] != NULL) {
file_close(curr->fdt[fd]); // Closing file allows writing file
curr->fdt[fd] = NULL;
}
}
open()에 다음 코드를 추가했다.
lock_acquire(&filesys_lock);
struct file *file = filesys_open(filename);
lock_release(&filesys_lock);
if (file == NULL) return -1;
file_deny_write(file); // ← 추가
thread_current()->running_file = file; // ← 추가
write()에 다음 코드를 추가했다.
if (curr->running_file == file) return 0; // ← 추가
lock_acquire(&filesys_lock);
off_t res = file_write(file, buffer, size);
lock_release(&filesys_lock);
close()에 다음 코드를 추가했다.
if (curr->running_file == file) curr->running_file = NULL; // ← 추가
lock_acquire(&filesys_lock);
file_close(file);
lock_release(&filesys_lock);
이 시점에서 테스트를 진행했을 때 rox-simple은 pass했지만, 나머지 rox 테스트들은 FAIL되었다.
'Krafton Jungle > 5. PintOS' 카테고리의 다른 글
| [PintOS 4주차] Day 0-1 (0) | 2025.05.28 |
|---|---|
| [PintOS 3주차] Day 5-6 (0) | 2025.05.26 |
| [PintOS 3주차] Day 3 (0) | 2025.05.24 |
| [PintOS 3주차] Day 1-2 (2) | 2025.05.23 |
| [PintOS 2주차] Day 8 (0) | 2025.05.22 |