Skip to content

Commit 926fd9e

Browse files
committed
Misc improvements
1 parent cbf83ad commit 926fd9e

File tree

7 files changed

+25
-13
lines changed

7 files changed

+25
-13
lines changed

examples/programs/fib_c_nostdlib/fib_c_nostdlib.c

+16-8
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ int sys_write(unsigned fd, const char *buf, unsigned count)
2626
return ret;
2727
}
2828

29-
void to_hex(char *buf, uint64_t num)
29+
int to_hex(char *buf, uint64_t num)
3030
{
3131
int i = 0;
3232

3333
if (num == 0)
3434
{
3535
buf[i] = '0';
36+
i++;
3637
}
3738
else
3839
{
39-
char stack[16] = {0};
40+
char stack[32] = {0};
4041
int stack_i = 0;
4142

4243
while (num > 0)
@@ -63,11 +64,13 @@ void to_hex(char *buf, uint64_t num)
6364
i++;
6465
}
6566
}
66-
buf[i++] = '\n';
67+
buf[i] = '\n';
68+
i++;
6769
buf[i] = '\0';
70+
71+
return i;
6872
}
6973

70-
// Print first 75 Fibonacci numbers
7174
void _start()
7275
{
7376
uint64_t a = 0;
@@ -77,15 +80,20 @@ void _start()
7780

7881
char buf[128];
7982

80-
while (counter < 75)
83+
while (counter < 25)
8184
{
82-
int64_t c = a + b;
85+
int64_t c;
86+
if (__builtin_add_overflow(a, b, &c))
87+
{
88+
sys_write(2, "Overflow\n", 9);
89+
sys_exit(1);
90+
}
8391
a = b;
8492
b = c;
8593
counter++;
8694

87-
to_hex(buf, a);
88-
sys_write(1, buf, 128);
95+
int len = to_hex(buf, a);
96+
sys_write(1, buf, len);
8997
}
9098

9199
sys_exit(0);

src/elf/elf.rs

+2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ mod tests {
354354

355355
// test_binary![exit_c; "../../testdata/exit_c.bin"; ""; 5];
356356

357+
// test_binary![fib_c_nostdlib; "../../testdata/fib_c_nostdlib.bin"; "1\n1\n2\n3\n5\n8\nd\n15\n22\n37\n59\n90\ne9\n179\n262\n3db\n63d\na18\n1055\n1a6d\n2ac2\n452f\n6ff1\nb520\n12511"; 0];
358+
357359
test_async![binary_without_symbols; async {
358360
let bin = Axecutor::from_binary(include_bytes!("../../testdata/exit_c_no_symbols.bin")).expect("Failed to parse binary");
359361
// Should only include the _start symbol

src/helpers/stack.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: Implement printing the current stack

src/helpers/trace.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) enum TraceVariant {
2020
Call,
2121
Return,
2222
Jump,
23+
// TODO: Add syscall
2324
}
2425

2526
// This is for the full tracing functionality, not to be confused with call_stack
@@ -94,7 +95,7 @@ impl Axecutor {
9495
}
9596
};
9697
let instruction_symbol = if entry.instr_ip == 0 {
97-
"<emulator_start>".to_string()
98+
"<emulation_start>".to_string()
9899
} else {
99100
match self.symbol_table.get(&entry.instr_ip) {
100101
Some(sym) => format!("{}@{:#x}", sym, entry.instr_ip),
@@ -155,7 +156,7 @@ mod tests {
155156

156157
debug_log!("Trace:\n{}", trace);
157158

158-
assert_eq!(trace, r#"<emulator_start>: entrypoint => _start@0x401000
159+
assert_eq!(trace, r#"<emulation_start>: entrypoint => _start@0x401000
159160
_start@0x401000: call 0000000000401015h => first_level@0x401015
160161
first_level@0x401015: jmp short 000000000040101Eh => 0x40101e
161162
0x40101e: call 0000000000401031h => second_level@0x401031
@@ -210,7 +211,7 @@ mod tests {
210211

211212
debug_log!("Trace:\n{}", trace);
212213

213-
assert_eq!(trace, format!(r#"<emulator_start>: entrypoint => _start@0x40101a
214+
assert_eq!(trace, format!(r#"<emulation_start>: entrypoint => _start@0x40101a
214215
0x401034: jmp short 000000000040103Eh => 0x40103e
215216
0x401042: jle short 0000000000401036h => 0x401036 ({} times)
216217
0x401049: call 0000000000401000h => sys_exit@0x401000

src/state/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl MemoryArea {
121121
s.push_str(&format!(
122122
"{} string: {:?},\n",
123123
" ".repeat(i * 4),
124-
String::from_utf8_lossy(&self.data)
124+
String::from_utf8_lossy(&self.data[..self.data.len() - 1])
125125
));
126126
}
127127

t.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ def is_new(flags_set, flags_not_set, input_flags):
992992
def imap_func(input: tuple[int, Input]):
993993
return TestCase.learn_single_flags(input[0], assembled, instruction, input[1], tmpdir)
994994

995-
with Pool(os.cpu_count() * 4) as p:
995+
with Pool(os.cpu_count() * 8) as p:
996996
temp_results = list(
997997
tqdm(
998998
p.imap(imap_func, enumerate(input_args)),

testdata/fib_c_nostdlib.bin

14.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)