# cs6332: Lab 4 supplementary guide
## Clarification
### 1. IA32 Decoder code
Correct/functioning implementation of *ia32Decode()*, which was to be done fro Unit4-3.
```c=
#define IS(i, f) ((ia32DecodeTable[i] & f) == f)
void ia32Decode(uint8_t *ptr, IA32Instr *instr) {
// remember the original ptr location
uint8_t *orig = ptr;
// skip past prefix bytes
while (IS(*ptr, IA32_PREFIX)) ptr++;
// read opcode
instr->opcode = *ptr++;
// handle 2-byte opcodes
if (IS(instr->opcode, IA32_2BYTE)) {
instr->opcode = *ptr++ | 0x100;
}
// parse mod/rm byte for number of things to read
if (IS(instr->opcode, IA32_MODRM)) {
instr->modRM = *ptr++;
uint8_t mod = instr->modRM >> 6;
uint8_t rm = instr->modRM & 0b111;
if (rm == 0b100 && mod != 3) ptr++;
if (mod == 1) ptr++;
else if (mod == 2) ptr += 4;
else if (rm == 0b101 && mod == 0) ptr += 4;
}
// handle 8-bit immediates
if (IS(instr->opcode, IA32_IMM8)) {
instr->imm = *ptr++;
}
// handle 32-bit immediates
if (IS(instr->opcode, IA32_IMM32)) {
instr->imm = *(uint32_t*)ptr;
ptr += 4;
}
// check how far we read to calculate length
instr->len = ptr - orig;
}
```
### 2. Sample callout pseudo code (jmpCallout)
The following is *the pseudocode example* for one of the four types of control-flow instructions that your system must trap and emulate. While the underlying design principles are the same across the other callout routines, you need to take extra care to ensure that your binary translator seamlessly supports provided test functions.
```c=
void handleJmpCallout(SaveRegs regs) {
// 1. Clean up/Restore last patch (where we called out from.)
unpatch(last_patch_address);
// 2. Parse the instruction that is about to run.
IA32Instr current_instruction;
memset(¤t_instruction, 0, sizeof(current_instruction));
ia32Decode(last_patch_address, ¤t_instruction);
// 3. Identify the start of the next basic block
// This line works for relative jumps (opcodes EB and E9)
// - https://www.felixcloutier.com/x86/jmp
// You may need to consider other kinds of jumps for the bonus in libc.
void *jump_target = last_patch_address + current_instruction.len + current_instruction.imm;
// 4. Walk to next control flow instruction (here is where you would track basic blocks)
IA32Instr walk_instruction;
void *walk_address = jump_target;
do{
memset(&walk_instruction, 0, sizeof(walk_instruction));
ia32Decode(walk_address, &walk_instruction);
walk_address += walk_instruction.len;
} while((ia32DecodeTable[i.walk_instruction.opcode] & IA32_CFLOW) != IA32_CFLOW);
// 5. Take one step back to point walk_address at walk_instruction
walk_address -= walk_instruction.len;
// *Postcondition*: walk_instruction and walk_address point to the next control flow instruction.
// 6atch the next control flow instruction.
last_patch_address = walk_address;
patch_block(last_patch_address);
regs.pc = jump_target;
}
```
## 3. Intricate issues overwriting handler routines that follows `ret` instruction.
Sometimes overwriting the bytes that come after the last `ret` of `user_prog` will trample on some nearby function that could become important. This is *usually* not an issue, but if it happens to you, the easiest way to fix it for sure it to add `asm volatile ("nop;nop;nop;nop;nop;");` to the end of the user program definitions in `fib.c`, `isprime.c`, etc. This will force the compiler to put some extra junk bytes at the end of `user_prog`, which ensures that the trampled bytes will be unused.
For instance, you can have a look at the [patch](https://gitlab.syssec.org/kjee/unit4-binary-translator/-/commit/1267e08da52af773af4d4b1e7567fee221ff1ed3) to see how this corner-case can be avoided.
```diff=
Author: Kangkook Jee <
[email protected]>
Date: Sun Nov 16 08:13:04 2025 -0600
dummy after return.
diff --git a/fib.c b/fib.c
index 29ebc02..c527ee8 100644
--- a/fib.c
+++ b/fib.c
@@ -22,3 +22,9 @@ uint64_t fib(uint32_t i) {
return fib(i - 1) + fib(i - 2);
}
+
+void dummy() {
+ asm volatile (
+ ".byte 0x90, 0x90, 0x90, 0x90, 0x90\n"
+ );
+}
diff --git a/lab4.c b/lab4.c
index 50681d0..1a72dac 100644
--- a/lab4.c
+++ b/lab4.c
@@ -19,6 +19,7 @@ extern uint32_t ia32DecodeTable[]; /* see below */
/* instrumentation target */
extern int user_prog(void *a);
+extern int dummy();
void StartProfiling(void *func);
(END)
```
## 4. GlueCode handling
JccCallout and JmpCallout expect your next address to be in `regs.pc`
CallCallout expects your next address to be in `callTarget`
RetCallout expects your next address to be in `regs.RetPC`
<!--
5. Expected output formats
## Bonus points
For bof0.c and rop0.c involves
-->
###### tags: `cs6332`,`lab4`,`unit4`