Skip to content

Commit 8301bc3

Browse files
authored
Merge pull request #1680 from luoliwoshang/wip/fix-32bit-slice-index-regression
fix: normalize 32-bit slice bounds
2 parents 6df3f31 + cac15cc commit 8301bc3

6 files changed

Lines changed: 99 additions & 50 deletions

File tree

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
11
#!/bin/bash
2-
# ESP serial targets smoke test (build + emulator run only).
2+
# ESP serial targets smoke test (emulator run only).
33
set -euo pipefail
44

55
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
66
TEMP_DIR="$SCRIPT_DIR/.test_tmp_$$"
7-
TEST_GO="$TEMP_DIR/main.go"
87

9-
ESP32C3_PREFIX="esp32c3_smoke"
10-
ESP32_PREFIX="esp32_smoke"
8+
CASE_ROOT="$SCRIPT_DIR/testdata/esp32-serial"
119

1210
cleanup() {
1311
rm -rf "$TEMP_DIR"
1412
}
1513
trap cleanup EXIT
1614

17-
build_target() {
18-
local target="$1"
19-
local prefix="$2"
20-
local label="$3"
21-
22-
echo "==> Building for $label target ($target): ELF + BIN..."
23-
llgo build -target="$target" -o "$prefix" -obin "$TEST_GO"
24-
25-
if [ ! -f "${prefix}.elf" ]; then
26-
echo "✗ FAIL: Build failed, ${prefix}.elf not found"
27-
exit 1
28-
fi
29-
30-
if [ ! -f "${prefix}.bin" ]; then
31-
echo "✗ FAIL: Build failed, ${prefix}.bin not found"
32-
exit 1
33-
fi
34-
35-
echo "✓ PASS: $label build artifacts generated"
36-
}
37-
3815
run_emulator_smoke() {
3916
local target="$1"
4017
local label="$2"
41-
local expected="$3"
18+
local case_dir="$3"
19+
local expected_file="$4"
4220

4321
echo ""
4422
echo "=== Smoke: $label emulator output ==="
@@ -47,7 +25,7 @@ run_emulator_smoke() {
4725
run_out_file=$(mktemp "${TEMP_DIR}/run_${target}.XXXX.log")
4826

4927
set +e
50-
llgo run -a -target="$target" -emulator . 2>&1 | tee "$run_out_file"
28+
llgo run -a -target="$target" -emulator "$case_dir" 2>&1 | tee "$run_out_file"
5129
local run_rc=${PIPESTATUS[0]}
5230
set -e
5331

@@ -59,46 +37,86 @@ run_emulator_smoke() {
5937
echo "[WARN] $label emulator exited with code $run_rc; validating output tail instead"
6038
fi
6139

62-
local last_line
63-
last_line=$(printf "%s\n" "$run_out" | tr -d '\r' | awk 'NF{line=$0} END{print line}')
40+
local normalized_out
41+
normalized_out=$(printf "%s\n" "$run_out" | tr -d '\r')
6442

65-
if [ "$last_line" = "$expected" ]; then
66-
echo "✓ PASS: $label output ends with $expected"
43+
local expected_tail
44+
expected_tail=$(cat "$expected_file")
45+
46+
local normalized_expected
47+
normalized_expected=$(printf "%s" "$expected_tail" | tr -d '\r')
48+
49+
local n
50+
n=$(printf "%s\n" "$normalized_expected" | awk 'END{print NR}')
51+
if [ -z "$n" ] || [ "$n" -le 0 ]; then
52+
echo "✗ FAIL: invalid expected tail for $label"
53+
exit 1
54+
fi
55+
56+
local actual_tail
57+
actual_tail=$(printf "%s\n" "$normalized_out" | awk 'NF{print}' | tail -n "$n")
58+
59+
if [ "$actual_tail" = "$normalized_expected" ]; then
60+
echo "✓ PASS: $label output tail (last $n line(s)) matched"
6761
else
6862
echo "✗ FAIL: $label output mismatch"
69-
echo "Last line: $last_line"
63+
echo "Expected tail (last $n line(s)):"
64+
printf "%s\n" "$normalized_expected"
65+
echo "Actual tail:"
66+
printf "%s\n" "$actual_tail"
7067
echo ""
7168
echo "Full output:"
7269
echo "$run_out"
7370
exit 1
7471
fi
7572
}
7673

77-
mkdir -p "$TEMP_DIR"
74+
run_case() {
75+
local case_dir="$1"
76+
local case_name
77+
case_name="$(basename "$case_dir")"
7878

79-
echo "==> Creating minimal test program..."
80-
cat > "$TEST_GO" << 'EOGO'
81-
package main
82-
83-
import "github.com/goplus/lib/c"
79+
local expected_file="$case_dir/expect.txt"
80+
if [ ! -f "$case_dir/main.go" ]; then
81+
echo "✗ FAIL: missing testcase source: $case_dir/main.go"
82+
exit 1
83+
fi
8484

85-
func main() {
86-
c.Printf(c.Str("Hello World\n"))
85+
run_emulator_smoke "esp32c3-basic" "ESP32-C3 [$case_name]" "$case_dir" "$expected_file"
86+
run_emulator_smoke "esp32" "ESP32 [$case_name]" "$case_dir" "$expected_file"
8787
}
88-
EOGO
8988

90-
cd "$TEMP_DIR"
89+
run_all_cases() {
90+
local found=0
91+
local case_dir
92+
exec 3< <(find "$CASE_ROOT" -mindepth 1 -maxdepth 1 -type d | sort)
93+
while IFS= read -r case_dir <&3; do
94+
if [ -f "$case_dir/main.go" ] && [ -f "$case_dir/expect.txt" ]; then
95+
found=1
96+
run_case "$case_dir"
97+
fi
98+
done
99+
exec 3<&-
100+
101+
if [ "$found" -eq 0 ]; then
102+
echo "✗ FAIL: no testcase found under $CASE_ROOT (need main.go + expect.txt)"
103+
exit 1
104+
fi
105+
}
91106

92-
echo ""
93-
echo "=== ESP Serial Smoke Tests: Build + Emulator Run ==="
107+
mkdir -p "$TEMP_DIR"
108+
if [ ! -d "$CASE_ROOT" ]; then
109+
echo "✗ FAIL: testcase root not found: $CASE_ROOT"
110+
exit 1
111+
fi
94112

95-
build_target "esp32c3" "$ESP32C3_PREFIX" "ESP32-C3"
96-
run_emulator_smoke "esp32c3-basic" "ESP32-C3" "Hello World"
113+
cd "$SCRIPT_DIR"
97114

98-
build_target "esp32" "$ESP32_PREFIX" "ESP32"
99-
run_emulator_smoke "esp32" "ESP32" "Hello World"
115+
echo ""
116+
echo "=== ESP Serial Smoke Tests: Emulator Run ==="
117+
run_all_cases
100118

101119
echo ""
102120
echo "=== Smoke Tests Passed ==="
103-
echo "✓ ESP32-C3 build + emulator run passed"
104-
echo "ESP32 build + emulator run passed"
121+
echo "✓ ESP32-C3 and ESP32 emulator smoke passed for all serial testcases"
122+
echo "Cases are discovered only from testdata/esp32-serial (main.go + expect.txt)"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/goplus/lib/c"
4+
5+
func main() {
6+
c.Printf(c.Str("Hello World\n"))
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slice64 ok
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "github.com/goplus/lib/c"
4+
5+
func main() {
6+
s := "hello"
7+
var idx int64 = 1
8+
tail := s[idx:]
9+
if len(tail) == 4 && tail[0] == 'e' && tail[1] == 'l' && tail[2] == 'l' && tail[3] == 'o' {
10+
c.Printf(c.Str("slice64 ok\n"))
11+
} else {
12+
c.Printf(c.Str("slice64 bad\n"))
13+
}
14+
}

ssa/datastruct.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) {
341341
var lowIsNil = low.IsNil()
342342
if lowIsNil {
343343
low = prog.IntVal(0, prog.Int())
344+
} else {
345+
low = b.fitIntSize(low)
346+
}
347+
if !high.IsNil() {
348+
high = b.fitIntSize(high)
349+
}
350+
if !max.IsNil() {
351+
max = b.fitIntSize(max)
344352
}
345353
switch t := x.raw.Type.Underlying().(type) {
346354
case *types.Basic:

0 commit comments

Comments
 (0)