summaryrefslogtreecommitdiff
path: root/lib9p/9p.gen
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/9p.gen')
-rwxr-xr-xlib9p/9p.gen37
1 files changed, 33 insertions, 4 deletions
diff --git a/lib9p/9p.gen b/lib9p/9p.gen
index 6e01b17..6f2ad2c 100755
--- a/lib9p/9p.gen
+++ b/lib9p/9p.gen
@@ -101,9 +101,7 @@ class Member:
re_membername = "(?:[a-zA-Z_][a-zA-Z_0-9]*)"
-re_memberspec = (
- f"(?:(?P<cnt>{re_membername})\\*\\()?(?P<name>{re_membername})\\[(?P<typ>[^,]*)(?:,max=(?P<max>[0-9]+))?\\]\\)?"
-)
+re_memberspec = f"(?:(?P<cnt>{re_membername})\\*\\()?(?P<name>{re_membername})\\[(?P<typ>[^,]*)(?:,max=(?P<max>[0-9]+))?\\]\\)?"
def parse_members(
@@ -138,7 +136,9 @@ def parse_members(
if maxstr := m.group("max"):
if (not isinstance(member.typ, Atom)) or member.cnt:
- raise ValueError(f"',max=' may only be specified on a non-repeated atom")
+ raise ValueError(
+ f"',max=' may only be specified on a non-repeated atom"
+ )
member.max = int(maxstr)
ret += [member]
@@ -431,6 +431,8 @@ enum {idprefix}version {{
ret += "\n"
ret += c_typename(idprefix, struct) + " {\n"
for member in struct.members:
+ if struct.name == "stat" and member.name == "stat_size": # SPECIAL
+ continue
ret += f"\t{c_typename(idprefix, member.typ).ljust(typewidth)} {'*' if member.cnt else ' '}{member.name};"
if (not all_the_same) and (comment := c_vercomment(member.ver)):
ret += (" " * (namewidth - len(member.name))) + " " + comment
@@ -655,6 +657,9 @@ static ALWAYS_INLINE bool _validate_list(struct _validate_ctx *ctx,
ret += "}\n"
continue
+ if typ.name == "stat": # SPECIAL
+ ret += f"\n\tuint32_t size_offset = ctx->net_offset;"
+
prefix0 = "\treturn "
prefix1 = "\t || "
prefix2 = "\t "
@@ -673,6 +678,7 @@ static ALWAYS_INLINE bool _validate_list(struct _validate_ctx *ctx,
else:
ret += f"validate_{member.typ.name}(ctx)"
if member.max:
+ assert member.static_size
ret += f"\n{prefix1}(decode_u{member.static_size*8}le(&ctx->net_bytes[ctx->net_offset-{member.static_size}]) > ({c_typename(idprefix, member.typ)})({member.max})"
ret += f'\n{prefix2}\t? lib9p_errorf(ctx->ctx, LINUX_EBADMSG, "list size is too large (%"PRIu{member.static_size*8}" > %"PRIu{member.static_size*8}")",'
ret += f"\n{prefix2}\t\tdecode_u{member.static_size*8}le(&ctx->net_bytes[ctx->net_offset-{member.static_size}]), ({c_typename(idprefix, member.typ)})({member.max}))"
@@ -681,6 +687,11 @@ static ALWAYS_INLINE bool _validate_list(struct _validate_ctx *ctx,
ret += " )"
prefix = prefix1
prev_size = member.static_size
+ if typ.name == "stat": # SPECIAL
+ assert typ.members[0].static_size
+ ret += f"\n{prefix1}((uint32_t)decode_u{typ.members[0].static_size*8}le(&ctx->net_bytes[size_offset]) != ctx->net_offset - size_offset)"
+ ret += f'\n{prefix2}\t? lib9p_error(ctx->ctx, LINUX_EBADMSG, "stat size does not match stat contents")"'
+ ret += f"\n{prefix2}\t: false)"
ret += ";\n"
ret += "}\n"
@@ -726,6 +737,9 @@ static ALWAYS_INLINE void unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *out)
if typ.members:
struct_versions = typ.members[0].ver
for member in typ.members:
+ if typ.name == "stat" and member.name == "stat_size": # SPECIAL
+ ret += f"\tsize->net_offset += {member.static_size};\n"
+ continue
ret += "\t"
prefix = "\t"
if member.ver != struct_versions:
@@ -809,6 +823,9 @@ static ALWAYS_INLINE bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) {
ret += "}\n"
continue
+ if typ.name == "stat": # SPECIAL
+ ret += "\n\tuint32_t size_offset = ctx->net_offset;"
+
prefix0 = "\treturn "
prefix1 = "\t || "
prefix2 = "\t "
@@ -816,6 +833,12 @@ static ALWAYS_INLINE bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) {
struct_versions = typ.members[0].ver
prefix = prefix0
for member in typ.members:
+ if typ.name == "stat" and member.name == "stat_size": # SPECIAL:
+ assert member.static_size
+ ret += f"\n{prefix }((ctx->net_offset + {member.static_size} > ctx->ctx->max_msg_size)"
+ ret += f"\n{prefix2}\t? _marshal_too_large(ctx)"
+ ret += f"\n{prefix2}\t: ({{ ctx->net_offset += {member.static_size}; false; }}))"
+ continue
ret += f"\n{prefix}"
if member.ver != struct_versions:
ret += "( " + c_vercond(idprefix, member.ver) + " && "
@@ -831,6 +854,12 @@ static ALWAYS_INLINE bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) {
if member.ver != struct_versions:
ret += " )"
prefix = prefix1
+ if typ.name == "stat": # SPECIAL
+ assert typ.members[0].static_size
+ ret += f"\n{prefix1}((ctx->net_offset - size_offset > UINT16_MAX)"
+ ret += f'\n{prefix2}\t? lib9p_error(ctx->ctx, LINUX_ERANGE, "stat object too large")'
+ ret += f"\n{prefix2}\t: ({{ encode_u{typ.members[0].static_size*8}le((uint{typ.members[0].static_size*8}_t)(ctx->net_offset - size_offset), &ctx->net_bytes[size_offset]);"
+ ret += f"\n{prefix2} false; }}))"
ret += ";\n"
ret += "}\n"