Re: [PATCH v2 0/2] Lock and Pointer guards
From: Kees Cook
Date: Tue Jun 06 2023 - 12:06:49 EST
On Tue, Jun 06, 2023 at 07:50:47AM -0700, Linus Torvalds wrote:
> So you could have something like
>
> #define RAII(type, var, init, exit) \
> __RAII(type, var, init, exit, __UNIQUE_ID(fn)
>
> #define __RAII(type, var, init, exit, exitname) \
> void exitname(type *p) { exit } \
> type var __attribute__((__cleanup__(exitname))) = (init)
>
> and do all of the above with
>
> RAII(struct fd, fd, fdget(f), fdput(fd));
"fdput(fd)" needs to be "fdput(*p)", since otherwise "fdput(fd)" is
referencing "fd" before it has been declared.
But regardless, yes, Clang is angry about the nested function. Also,
while my toy[1] example doesn't show it, GCC may also generate code
that requires an executable stack for some instances (or at least it
did historically) that need trampolines.
[1] https://godbolt.org/z/WTjx6Gs7x
Also, more nits on naming: isn't this more accurately called Scope-based
Resource Management (SBRM) not RAII? (RAII is technically object lifetime,
and SBRM is scope entry/exit.)
--
Kees Cook