Hi,

two tccasm crashes on mob aade7baf (x86_64 Linux, ASan tcc_s), found by fuzzing.

1) A number or char after .section/.pushsection/.type/.reloc/.option:
$ cat a1.c
asm(".section 1a");
$ ./tcc_s -B. -c a1.c -o /dev/null
tccpp.c:557:16: runtime error: member access within null pointer of type 'union 
CValue'

These call get_tok_str(tok, NULL); patch 1 passes &tokc, as 8a8388c6
did for .ident.

2) .size on an enum constant:
$ cat a2.c
int f(void) { enum { a = 5 }; asm(".size a, 1"); return 0; }
$ ./tcc_s -B. -c a2.c -o /dev/null
ERROR: AddressSanitizer: heap-buffer-overflow
    #0 in asm_parse_directive tccasm.c:851

elfsym() takes the enum value as a symtab index (code from 30afb50e,
arm64-win32 review); patch 2 sets st_size only for VT_SYM symbols.
asm(".long a") with that enum still reads out of bounds (not covered).

Test: make test passes; the inputs above now compile or give errors.

Regards,
Sechang Jang
The text and patch were prepared with help from an LLM (Claude).
From 3c97af6c56ec24f6e176ba9e6113915c71ddcc81 Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccasm: pass tokc to get_tok_str() in more directives

asm(".section 1a") crashed in get_tok_str() with cv == NULL
for a number or char token.  Same as for .ident in 8a8388c6;
also .type, .pushsection, .reloc and riscv64 .option.
---
 tccasm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tccasm.c b/tccasm.c
index eb24e3a0..25b44ee2 100644
--- a/tccasm.c
+++ b/tccasm.c
@@ -869,7 +869,7 @@ static void asm_parse_directive(TCCState *s1, int global)
             } else {
                 if (tok == '@' || tok == '%')
                     next();
-                newtype = get_tok_str(tok, NULL);
+                newtype = get_tok_str(tok, &tokc);
             }
 
             if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
@@ -906,7 +906,7 @@ static void asm_parse_directive(TCCState *s1, int global)
                 if (tok == TOK_STR)
                     pstrcat(sname, sizeof(sname), tokc.str.data);
                 else
-                    pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
+                    pstrcat(sname, sizeof(sname), get_tok_str(tok, &tokc));
                 next();
             }
             if (tok == ',') {
@@ -999,10 +999,10 @@ static void asm_parse_directive(TCCState *s1, int global)
                 break;
             case TOK_ASM_arch:
                 /* TODO: unimplemented, requires extra parsing */
-                tcc_error("unimp .option '.%s'", get_tok_str(tok, NULL));
+                tcc_error("unimp .option '.%s'", get_tok_str(tok, &tokc));
                 break;
             default:
-                tcc_error("unknown .option '.%s'", get_tok_str(tok, NULL));
+                tcc_error("unknown .option '.%s'", get_tok_str(tok, &tokc));
                 break;
         }
         break;
@@ -1025,7 +1025,7 @@ static void asm_parse_directive(TCCState *s1, int global)
 	    next();
 	    asm_expr(s1, &e);
 	    skip(',');
-	    reloc_name = get_tok_str(tok, NULL);
+	    reloc_name = get_tok_str(tok, &tokc);
 #if defined(TCC_TARGET_ARM64)
 	    if (!strcmp(reloc_name, "R_AARCH64_CALL26"))
 	        reloc_type = R_AARCH64_CALL26;
-- 
2.34.1

From e7df157055f98b72907d2fdacad9ac3b4880e307 Mon Sep 17 00:00:00 2001
From: Sechang Jang <[email protected]>
Date: Fri, 13 Nov 2026 20:06:10 +0900
Subject: [PATCH] tccasm: .size on an enum constant

    int f(void) { enum { a = 5 }; asm(".size a, 1"); return 0; }

elfsym() used the enum value as symtab index and .size wrote
st_size there (heap overflow, from 30afb50e).  Set st_size only
for symbols with VT_SYM, ignore .size otherwise as before.
---
 tccasm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tccasm.c b/tccasm.c
index eb24e3a0..b81e7581 100644
--- a/tccasm.c
+++ b/tccasm.c
@@ -846,7 +846,7 @@ static void asm_parse_directive(TCCState *s1, int global)
             next();
             skip(',');
             n = asm_int_expr(s1);
-            esym = elfsym(sym);
+            esym = sym->r & VT_SYM ? elfsym(sym) : NULL; /* e.g. an enum constant */
             if (esym) {
                 esym->st_size = n;
             }
-- 
2.34.1

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to