1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
From afbff931d56c467791b0347c784ebc4a7c73014d Mon Sep 17 00:00:00 2001
From: cryptobro <andrey.roussev@gmail.com>
Date: Tue, 12 Oct 2021 12:29:10 +0300
Subject: [PATCH] Fix UB/crashes
Sometimes you may encounter strange errors like:
libvgstr3412_x64_asm.s:4473: error: Unrecognized register name `%ym'
libvgstr3412_x64_asm.s:4473: error: expected `,'
make[2]: *** [Makefile:576: libvgstr3412_la-libvgstr3412_x64_asm.lo] Error 1g
Valgrind shows the places:
valgrind /tmp/yasm-1.3.0/yasm -e -p gnu -f elf64 -DPROC_TYPE_X64 -DELF libvgstr3412_x64_asm.s -DPIC -o xxxx
==8224== Memcheck, a memory error detector
==8224== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8224== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==8224== Command: /tmp/yasm-1.3.0/yasm -e -p gnu -f elf64 -DPROC_TYPE_X64 -DELF libvgstr3412_x64_asm.s -DPIC -o xxxx
==8224==
==8224== Invalid read of size 1
==8224== at 0x4846C70: strncmp (vg_replace_strmem.c:663)
==8224== by 0x43125C: expand_macro (gas-preproc.c:915)
==8224== by 0x43125C: process_line (gas-preproc.c:1174)
==8224== by 0x4317A2: gas_preproc_get_line (gas-preproc.c:1336)
==8224== by 0x402A13: do_preproc_only (yasm.c:323)
==8224== by 0x402A13: main (yasm.c:724)
==8224== Address 0x4fa7330 is 0 bytes inside a block of size 4 free'd
==8224== at 0x48430E4: free (vg_replace_malloc.c:755)
==8224== by 0x43151D: expand_macro (gas-preproc.c:936)
==8224== by 0x43151D: process_line (gas-preproc.c:1174)
==8224== by 0x4317A2: gas_preproc_get_line (gas-preproc.c:1336)
==8224== by 0x402A13: do_preproc_only (yasm.c:323)
==8224== by 0x402A13: main (yasm.c:724)
==8224== Block was alloc'd at
==8224== at 0x48456AF: realloc (vg_replace_malloc.c:1192)
==8224== by 0x414887: def_xrealloc (xmalloc.c:101)
==8224== by 0x42F1A7: gas_scan (gas-preproc.c:440)
==8224== by 0x4311FF: expand_macro (gas-preproc.c:906)
==8224== by 0x4311FF: process_line (gas-preproc.c:1174)
==8224== by 0x4317A2: gas_preproc_get_line (gas-preproc.c:1336)
==8224== by 0x402A13: do_preproc_only (yasm.c:323)
==8224== by 0x402A13: main (yasm.c:724)
==8224==
==8224== Invalid read of size 1
==8224== at 0x431268: expand_macro (gas-preproc.c:916)
==8224== by 0x431268: process_line (gas-preproc.c:1174)
==8224== by 0x4317A2: gas_preproc_get_line (gas-preproc.c:1336)
==8224== by 0x402A13: do_preproc_only (yasm.c:323)
==8224== by 0x402A13: main (yasm.c:724)
==8224== Address 0x56c5581 is 1 bytes inside a block of size 13 free'd
==8224== at 0x48430E4: free (vg_replace_malloc.c:755)
==8224== by 0x43151D: expand_macro (gas-preproc.c:936)
==8224== by 0x43151D: process_line (gas-preproc.c:1174)
==8224== by 0x4317A2: gas_preproc_get_line (gas-preproc.c:1336)
==8224== by 0x402A13: do_preproc_only (yasm.c:323)
==8224== by 0x402A13: main (yasm.c:724)
==8224== Block was alloc'd at
==8224== at 0x48456AF: realloc (vg_replace_malloc.c:1192)
==8224== by 0x414887: def_xrealloc (xmalloc.c:101)
==8224== by 0x42F1A7: gas_scan (gas-preproc.c:440)
==8224== by 0x4311FF: expand_macro (gas-preproc.c:906)
==8224== by 0x4311FF: process_line (gas-preproc.c:1174)
==8224== by 0x4317A2: gas_preproc_get_line (gas-preproc.c:1336)
==8224== by 0x402A13: do_preproc_only (yasm.c:323)
==8224== by 0x402A13: main (yasm.c:724)
---
modules/preprocs/gas/gas-preproc.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/modules/preprocs/gas/gas-preproc.c b/modules/preprocs/gas/gas-preproc.c
index 94d8f5da3..8f4c79323 100644
--- a/modules/preprocs/gas/gas-preproc.c
+++ b/modules/preprocs/gas/gas-preproc.c
@@ -912,6 +912,9 @@ static void expand_macro(yasm_preproc_gas *pp, macro_entry *macro, const char *a
char *end = strstr(macro->params[j], "=");
int len = (end ? (size_t)(end - macro->params[j])
: strlen(macro->params[j]));
+ if (!tokval.t_charptr) {
+ continue;
+ }
if (!strncmp(tokval.t_charptr, macro->params[j], len)
&& tokval.t_charptr[len] == '\0') {
/* now, find matching argument. */
@@ -932,6 +935,9 @@ static void expand_macro(yasm_preproc_gas *pp, macro_entry *macro, const char *a
memcpy(line + cursor - len, value, value_length);
pp->expr.string = work = line;
pp->expr.string_cursor += delta;
+ if (pp->expr.symbol == tokval.t_charptr) {
+ tokval.t_charptr = NULL;
+ }
if (pp->expr.symbol) {
yasm_xfree(pp->expr.symbol);
pp->expr.symbol = NULL;
|