Skip to content

Commit dff5d3a

Browse files
committed
only match windowing needles that are C strings
1 parent ec6afcd commit dff5d3a

5 files changed

Lines changed: 52 additions & 5 deletions

File tree

‎configure_test.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func Test_ConfigureLinuxSDL(t *testing.T) {
454454
assert.EqualValues(t, dash.ArchArm64, c.LinuxInfo.Arch)
455455
byPath[c.Path] = c.LinuxInfo
456456
}
457-
require.Len(t, byPath, 6)
457+
require.Len(t, byPath, 8)
458458

459459
bundled := byPath["sdl2-bundled"]
460460
assert.EqualValues(t, "2", bundled.SDL)
@@ -478,6 +478,11 @@ func Test_ConfigureLinuxSDL(t *testing.T) {
478478
assert.EqualValues(t, "3", sdl3.SDL)
479479
assert.True(t, sdl3.SDLDynamicAPI)
480480

481+
sdl3Fixed := byPath["sdl3-bundled-fixed"]
482+
assert.EqualValues(t, "3", sdl3Fixed.SDL)
483+
assert.False(t, sdl3Fixed.SDLDynamicAPI)
484+
assert.EqualValues(t, []string{"gl"}, sdl3Fixed.Display)
485+
481486
shared := byPath["sdl2-shared"]
482487
assert.EqualValues(t, "2", shared.SDL)
483488
assert.False(t, shared.SDLBundled)
@@ -489,6 +494,11 @@ func Test_ConfigureLinuxSDL(t *testing.T) {
489494
assert.Empty(t, glfw.SDL)
490495
assert.EqualValues(t, []string{"gl", "glfw", "x11"}, glfw.Display)
491496

497+
packed := byPath["go-packed"]
498+
assert.Empty(t, packed.SDL)
499+
assert.False(t, packed.SDLBundled)
500+
assert.Empty(t, packed.Display)
501+
492502
// without the flag none of it is read
493503
v, err = dash.Configure(filepath.Join("testdata", "linux-sdl"), dash.ConfigureParams{Consumer: makeConsumer(t)})
494504
require.NoError(t, err)

‎elf.go‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ func probeWindowing(ef *elf.File, info *LinuxInfo) {
323323
if s := ef.Section(".rodata"); s != nil && s.Size <= maxRodataScan {
324324
data, _ = s.Data()
325325
}
326-
has := func(needle string) bool { return bytes.Contains(data, []byte(needle)) }
326+
has := func(needle string) bool { return hasCString(data, needle, false) }
327327
for _, d := range displayLibraries {
328-
if has(d.prefix) {
328+
if hasCString(data, d.prefix, true) {
329329
found[d.tag] = true
330330
}
331331
}
@@ -335,10 +335,11 @@ func probeWindowing(ef *elf.File, info *LinuxInfo) {
335335
info.SDL, info.SDLBundled, info.SDLDynamicAPI = "2", true, true
336336
case has("SDL3_DYNAMIC_API"):
337337
info.SDL, info.SDLBundled, info.SDLDynamicAPI = "3", true, true
338-
case has("SDL_VIDEODRIVER"):
339-
info.SDL, info.SDLBundled = "2", true
338+
// SDL3 still reads the SDL2 name, so its own name goes first
340339
case has("SDL_VIDEO_DRIVER"):
341340
info.SDL, info.SDLBundled = "3", true
341+
case has("SDL_VIDEODRIVER"):
342+
info.SDL, info.SDLBundled = "2", true
342343
}
343344
}
344345

@@ -348,6 +349,28 @@ func probeWindowing(ef *elf.File, info *LinuxInfo) {
348349
sort.Strings(info.Display)
349350
}
350351

352+
// hasCString reports whether needle is a whole NUL-terminated string in
353+
// data. With prefix, needle is a library file name that may continue past
354+
// the match and may end a path, as in "/libGL.so.1". Go and other languages
355+
// that pack literals without terminators never match, so a tool that embeds
356+
// these needles (butler embeds dash) does not detect itself.
357+
func hasCString(data []byte, needle string, prefix bool) bool {
358+
n := []byte(needle)
359+
for off := 0; ; {
360+
i := bytes.Index(data[off:], n)
361+
if i < 0 {
362+
return false
363+
}
364+
i += off
365+
end := i + len(n)
366+
start := i == 0 || data[i-1] == 0 || (prefix && data[i-1] == '/')
367+
if start && (prefix || (end < len(data) && data[end] == 0)) {
368+
return true
369+
}
370+
off = i + 1
371+
}
372+
}
373+
351374
// ProbeELF returns the full Linux record for one executable: what the
352375
// magic pass reads from the header plus what DeepProbe adds. It is for
353376
// tools that look at a single file rather than an install folder.

‎testdata/linux-sdl/build.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ void _start(void) { for (;;) {} }
3131
C
3232
$CC sdl3-bundled -static "$tmp/sdl3.c"
3333

34+
# SDL3 without the dynamic API keeps the SDL2 hint name too (Godot)
35+
cat >"$tmp/sdl3-fixed.c" <<'C'
36+
const char *const strings[] = {"SDL_VIDEODRIVER", "SDL_VIDEO_DRIVER", "/libGL.so.1"};
37+
void _start(void) { for (;;) {} }
38+
C
39+
$CC sdl3-bundled-fixed -static "$tmp/sdl3-fixed.c"
40+
41+
# the needles packed without terminators, as Go lays out string literals
42+
cat >"$tmp/packed.c" <<'C'
43+
const char packed[] = "error: %#vSDL_DYNAMIC_APISDL_VIDEODRIVER|dmc_unrarlibX11.solibgbm.solibEGL.sogodot-pck";
44+
void _start(void) { for (;;) {} }
45+
C
46+
$CC go-packed -static "$tmp/packed.c"
47+
3448
# linked against the system's SDL2, and against GLFW with X11
3549
echo 'int SDL_Init(unsigned f) { return (int)f; }' >"$tmp/lib.c"
3650
for so in libSDL2-2.0.so.0 libglfw.so.3 libX11.so.6 libGL.so.1; do

‎testdata/linux-sdl/go-packed‎

1.71 KB
Binary file not shown.
5.43 KB
Binary file not shown.

0 commit comments

Comments
 (0)