Ensure that you cannot return a suspension

This commit is contained in:
Nigel Tao committed 2020-06-03 11:08:23 +10:00
1 parent ed9025c8dd
commit 6c83516afa
5 files changed
+65 -13

No files matched your search

+16 -4
View File
@@ -14,10 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
static inline wuffs_base__empty_struct //
wuffs_base__ignore_status(wuffs_base__status z) {
return wuffs_base__make_empty_struct();
}
// ---------------- Fundamentals
// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.
// It's not foolproof, given C doesn't automatically zero memory before use,
@@ -73,6 +70,21 @@ wuffs_base__ignore_status(wuffs_base__status z) {
#define WUFFS_BASE__UNLIKELY(expr) (expr)
#endif
// --------
static inline wuffs_base__empty_struct //
wuffs_base__ignore_status(wuffs_base__status z) {
return wuffs_base__make_empty_struct();
}
static inline wuffs_base__status //
wuffs_base__status__ensure_not_a_suspension(wuffs_base__status z) {
if (z.repr && (*z.repr == '$')) {
z.repr = wuffs_base__error__cannot_return_a_suspension;
}
return z;
}
// ---------------- Numeric Types
extern const uint8_t wuffs_base__low_bits_mask__u8[9];
+5 -3
View File
@@ -184,9 +184,11 @@ const basePixConvSubmoduleC = "" +
""
const baseFundamentalPrivateH = "" +
"static inline wuffs_base__empty_struct //\nwuffs_base__ignore_status(wuffs_base__status z) {\n return wuffs_base__make_empty_struct();\n}\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// WUFFS_BASE__DISABLED is a magic number to indicate that a non-recoverable\n// error was previously encountered.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"disabled\").\n#define WUFFS_BASE__DISABLED ((uint32_t)0x075AE3D2)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && defined(__cplusplus) && (__cplusplus >= 201103L)\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BAS" +
"E__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (!status.repr) { \\\n goto ok; \\\n } else if (*status.repr != '$') { \\\n goto exit; \\\n } \\\n coro_susp_point" +
" = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n" +
"// ---------------- Fundamentals\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// WUFFS_BASE__DISABLED is a magic number to indicate that a non-recoverable\n// error was previously encountered.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"disabled\").\n#define WUFFS_BASE__DISABLED ((uint32_t)0x075AE3D2)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && defined(__cplusplus) && (__cplusplus >= 201103L)\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch " +
"cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (!status.repr) { \\\n goto ok; \\\n } else if (*status.repr != '$') { \\\n goto exit; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; " +
" \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n" +
"" +
"// --------\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__ignore_status(wuffs_base__status z) {\n return wuffs_base__make_empty_struct();\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__status__ensure_not_a_suspension(wuffs_base__status z) {\n if (z.repr && (*z.repr == '$')) {\n z.repr = wuffs_base__error__cannot_return_a_suspension;\n }\n return z;\n}\n\n" +
"" +
"// ---------------- Numeric Types\n\nextern const uint8_t wuffs_base__low_bits_mask__u8[9];\nextern const uint16_t wuffs_base__low_bits_mask__u16[17];\nextern const uint32_t wuffs_base__low_bits_mask__u32[33];\nextern const uint64_t wuffs_base__low_bits_mask__u64[65];\n\n#define WUFFS_BASE__LOW_BITS_MASK__U8(n) (wuffs_base__low_bits_mask__u8[n])\n#define WUFFS_BASE__LOW_BITS_MASK__U16(n) (wuffs_base__low_bits_mask__u16[n])\n#define WUFFS_BASE__LOW_BITS_MASK__U32(n) (wuffs_base__low_bits_mask__u32[n])\n#define WUFFS_BASE__LOW_BITS_MASK__U64(n) (wuffs_base__low_bits_mask__u64[n])\n\n" +
"" +
+23 -2
View File
@@ -483,9 +483,30 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
b.writes("return ")
if g.currFunk.astFunc.Out() == nil {
b.writes("wuffs_base__make_empty_struct()")
} else if err := g.writeExpr(b, retExpr, depth); err != nil {
return err
} else {
couldBeSuspension := false
if retExpr.MType().IsStatus() && !n.RetsError() {
couldBeSuspension = true
if retExpr.Operator() == 0 {
if id := retExpr.Ident(); id == t.IDOk {
couldBeSuspension = false
} else if s := g.tm.ByID(id); (len(s) > 1) && (s[0] == '"') {
couldBeSuspension = s[1] == '$'
}
}
}
if couldBeSuspension {
b.writes("wuffs_base__status__ensure_not_a_suspension(")
}
if err := g.writeExpr(b, retExpr, depth); err != nil {
return err
}
if couldBeSuspension {
b.writeb(')')
}
}
b.writeb(';')
return nil
}
+5
View File
@@ -776,6 +776,11 @@ func (p *parser) parseStatement1() (*a.Node, error) {
return nil, fmt.Errorf(`parse: %s an impure expression at %s:%d`,
x.Str(p.tm), p.filename, p.line())
}
if (x == t.IDReturn) && (value.Operator() == 0) {
if s := p.tm.ByID(value.Ident()); (len(s) > 1) && (s[0] == '"') && (s[1] == '$') {
return nil, fmt.Errorf(`parse: cannot return a suspension at %s:%d`, p.filename, p.line())
}
}
return a.NewRet(x, value).AsNode(), nil
case t.IDWhile:
+16 -4
View File
@@ -7400,10 +7400,7 @@ struct wuffs_zlib__decoder__struct {
extern "C" {
#endif
static inline wuffs_base__empty_struct //
wuffs_base__ignore_status(wuffs_base__status z) {
return wuffs_base__make_empty_struct();
}
// ---------------- Fundamentals
// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.
// It's not foolproof, given C doesn't automatically zero memory before use,
@@ -7459,6 +7456,21 @@ wuffs_base__ignore_status(wuffs_base__status z) {
#define WUFFS_BASE__UNLIKELY(expr) (expr)
#endif
// --------
static inline wuffs_base__empty_struct //
wuffs_base__ignore_status(wuffs_base__status z) {
return wuffs_base__make_empty_struct();
}
static inline wuffs_base__status //
wuffs_base__status__ensure_not_a_suspension(wuffs_base__status z) {
if (z.repr && (*z.repr == '$')) {
z.repr = wuffs_base__error__cannot_return_a_suspension;
}
return z;
}
// ---------------- Numeric Types
extern const uint8_t wuffs_base__low_bits_mask__u8[9];