Symbolic names in iso-files
Hi all,
In some code, there are macros used for different purposes. One typical case is having a "foreach" macro, but this is covered using "iterator" so that you can deal with code like this:
static Jsonb *BuildJsonObject(List *items) {
ListCell *cell;
JsonbParseState *state = NULL;
JsonbValue *value; pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
foreach (cell, items) {
KVItem *item = (KVItem *)lfirst(cell);
AddJsonField(&state, item->key, item->value);
}
value = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
return JsonbValueToJsonb(value);}
However, if you want to make sure to deal with resource allocation and make sure that you release a short-lived lock, for example (grossly simplified, just to demonstrate the problem):
@@
expression lock, mode;
@@
LWLockAcquire(lock, mode);
... when exists
(
LWLockRelease(lock);
|
+ LWLockRelease(lock);
return;
)If fails miserably with this code:
Datum start_worker(PG_FUNCTION_ARGS) {
pid_t pid; if (!autoprewarm)
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("autoprewarm is disabled")));
apw_init_shmem();
LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE);
pid = apw_state->bgworker_pid;
if (pid != InvalidPid)
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("autoprewarm worker is already running
under PID %d",
(int)pid)));
apw_start_leader_worker();
PG_RETURN_VOID();}
Giving the patch, where the call to LWLockRelease should be before the PG_RETURN_VOID:
--- code/lwlock.c
+++ /tmp/cocci-output-12611-ff946d-lwlock.c
@@ -18,6 +18,7 @@ Datum start_worker(PG_FUNCTION_ARGS) {
apw_start_leader_worker();PG_RETURN_VOID();
+ LWLockRelease(&apw_state->lock);
}
Since this is such a common construct, and you do not want to deal with this for every potential case, this seems like an obvious case for using an iso-file, so I created one that looks like this, with the idea that we support replacing "PG_RETURN_VOID()" with just a "return".
Statement
@ pg_return @
@@
PG_RETURN_VOID() => return
However, this fails with the following error message:
mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci
code/lwlock.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
iso main: parse error:
File "./postgres.iso", line 4, column 19, charpos = 46
around = '=>',
whole content = PG_RETURN_VOID() => returnWhich I suppose mean that there is no support for handling anything but standard tokens in an .iso file.
Have I misunderstood the situation and this is already supported in some way?
Are there any plans that add such support to the ISO grammar?
Best wishes,
Mats Kindahl, Timescale
Can you just add semicolons in your isomorphism?
Sent from my iPhone
Quoted 94 lines · Mats Kindahl
Hi all,In some code, there are macros used for different purposes. One typical case is having a "foreach" macro, but this is covered using "iterator" so that you can deal with code like this:
static Jsonb *BuildJsonObject(List *items) {
ListCell *cell;
JsonbParseState *state = NULL;
JsonbValue *value;pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL); foreach (cell, items) { KVItem *item = (KVItem *)lfirst(cell); AddJsonField(&state, item->key, item->value); } value = pushJsonbValue(&state, WJB_END_OBJECT, NULL);return JsonbValueToJsonb(value);
}However, if you want to make sure to deal with resource allocation and make sure that you release a short-lived lock, for example (grossly simplified, just to demonstrate the problem):
@@
expression lock, mode;
@@
LWLockAcquire(lock, mode);
... when exists
(
LWLockRelease(lock);
|
+ LWLockRelease(lock);
return;
)If fails miserably with this code:
Datum
start_worker(PG_FUNCTION_ARGS){
pid_tpid;if (!autoprewarm) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm is disabled"))); apw_init_shmem(); LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE); pid = apw_state->bgworker_pid;if (pid != InvalidPid)
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("autoprewarm worker is already running under PID %d",
(int)pid)));apw_start_leader_worker();
PG_RETURN_VOID();
}Giving the patch, where the call to LWLockRelease should be before the
PG_RETURN_VOID:tmp/cocci-output-12611-ff946d-lwlock.c renamed from lwlock.c +1
-0@@ -18,6 +18,7 @@ Datum start_worker(PG_FUNCTION_ARGS) {apw_start_leader_worker ();PG_RETURN_VOID (); +LWLockRelease (&apw_state->lock); }Since this is such a common construct, and you do not want to deal with this for every potential case, this seems like an obvious case for using an iso-file, so I created one that looks like this, with the idea that we support replacing "
PG_RETURN_VOID()" with just a "return".Statement
@pg_return@
@@
PG_RETURN_VOID()=> returnHowever, this fails with the following error message:
mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci code/lwlock.c init_defs_builtins: /usr/lib/coccinelle/standard.h iso main: parse error: File "./postgres.iso", line 4, column 19, charpos = 46 around = '=>', whole content = PG_RETURN_VOID() => returnWhich I suppose mean that there is no support for handling anything but standard tokens in an .iso file.
Have I misunderstood the situation and this is already supported in some way?
Are there any plans that add such support to the ISO grammar?
Best wishes,
Mats Kindahl, Timescale
Quoted 2 lines · Julia Lawall
Can you just add semicolons in your isomorphism?
Sent from my iPhone
Hi Julia,
I assume you mean to write something like this:
Statement
@ pg_return @
@@
PG_RETURN_VOID(); => return;But that fails with the same error message:
mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci
code/lwlock.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
iso main: parse error:
File "./postgres.iso", line 4, column 2, charpos = 29
around = 'PG_RETURN_VOID',
whole content = PG_RETURN_VOID(); => return;Best wishes,
Mats Kindahl
Quoted 101 lines
Quoted 101 lines · Mats Kindahl
Hi all,
In some code, there are macros used for different purposes. One typical case is having a "foreach" macro, but this is covered using "iterator" so that you can deal with code like this:
static Jsonb *BuildJsonObject(List *items) { ListCell *cell; JsonbParseState *state = NULL; JsonbValue *value; pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL); foreach (cell, items) { KVItem *item = (KVItem *)lfirst(cell); AddJsonField(&state, item->key, item->value); } value = pushJsonbValue(&state, WJB_END_OBJECT, NULL); return JsonbValueToJsonb(value); }However, if you want to make sure to deal with resource allocation and make sure that you release a short-lived lock, for example (grossly simplified, just to demonstrate the problem):
@@ expression lock, mode; @@ LWLockAcquire(lock, mode); ... when exists ( LWLockRelease(lock); | + LWLockRelease(lock); return; )If fails miserably with this code:
Datum start_worker(PG_FUNCTION_ARGS) { pid_t pid; if (!autoprewarm) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm is disabled"))); apw_init_shmem(); LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE); pid = apw_state->bgworker_pid; if (pid != InvalidPid) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm worker is already running under PID %d", (int)pid))); apw_start_leader_worker(); PG_RETURN_VOID(); }Giving the patch, where the call to LWLockRelease should be before the
PG_RETURN_VOID:--- code/lwlock.c +++ /tmp/cocci-output-12611-ff946d-lwlock.c @@ -18,6 +18,7 @@ Datum start_worker(PG_FUNCTION_ARGS) { apw_start_leader_worker(); PG_RETURN_VOID(); + LWLockRelease(&apw_state->lock); }Since this is such a common construct, and you do not want to deal with this for every potential case, this seems like an obvious case for using an iso-file, so I created one that looks like this, with the idea that we support replacing "
PG_RETURN_VOID()" with just a "return".Statement
@pg_return@
@@
PG_RETURN_VOID()=> returnHowever, this fails with the following error message:
mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci code/lwlock.c init_defs_builtins: /usr/lib/coccinelle/standard.h iso main: parse error: File "./postgres.iso", line 4, column 19, charpos = 46 around = '=>', whole content = PG_RETURN_VOID() => returnWhich I suppose mean that there is no support for handling anything but standard tokens in an .iso file.
Have I misunderstood the situation and this is already supported in some way?
Are there any plans that add such support to the ISO grammar?
Best wishes,
Mats Kindahl, Timescale
Quoted 16 lines · Mats Kindahl
On 2024-08-24 14:56, Julia Lawall wrote:
Can you just add semicolons in your isomorphism? Sent from my iPhoneHi Julia,
I assume you mean to write something like this:
Statement @ pg_return @ @@ PG_RETURN_VOID(); => return;But that fails with the same error message:
Quoted 7 lines
mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci
code/lwlock.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
iso main: parse error:
File "./postgres.iso", line 4, column 2, charpos = 29
around = 'PG_RETURN_VOID',
whole content = PG_RETURN_VOID(); => return;Thanks for the report. I will check on it.
Even without the isomorphism, you should be able to write a disjunction in your semantic patch?
julia
Quoted 130 lines
Best wishes,
Mats KindahlOn 24 Aug 2024, at 14:23, Mats Kindahl <[email protected]> wrote: Hi all, In some code, there are macros used for different purposes. One typical case is having a "foreach" macro, but this is covered using "iterator" so that you can deal with code like this: static Jsonb *BuildJsonObject(List *items) { ListCell *cell; JsonbParseState *state = NULL; JsonbValue *value; pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL); foreach (cell, items) { KVItem *item = (KVItem *)lfirst(cell); AddJsonField(&state, item->key, item->value); } value = pushJsonbValue(&state, WJB_END_OBJECT, NULL); return JsonbValueToJsonb(value); } However, if you want to make sure to deal with resource allocation and make sure that you release a short-lived lock, for example (grossly simplified, just to demonstrate the problem): @@ expression lock, mode; @@ LWLockAcquire(lock, mode); ... when exists ( LWLockRelease(lock); | + LWLockRelease(lock); return; ) If fails miserably with this code: Datum start_worker(PG_FUNCTION_ARGS) { pid_t pid; if (!autoprewarm) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm is disabled"))); apw_init_shmem(); LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE); pid = apw_state->bgworker_pid; if (pid != InvalidPid) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm worker is already running under PID %d", (int)pid))); apw_start_leader_worker(); PG_RETURN_VOID(); } Giving the patch, where the call to LWLockRelease should be before the PG_RETURN_VOID: --- code/lwlock.c +++ /tmp/cocci-output-12611-ff946d-lwlock.c @@ -18,6 +18,7 @@ Datum start_worker(PG_FUNCTION_ARGS) { apw_start_leader_worker(); PG_RETURN_VOID(); + LWLockRelease(&apw_state->lock); } Since this is such a common construct, and you do not want to deal with this for every potential case, this seems like an obvious case for using an iso-file, so I created one that looks like this, with the idea that we support replacing "PG_RETURN_VOID()" with just a "return". Statement @ pg_return @ @@ PG_RETURN_VOID() => return However, this fails with the following error message: mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci code/lwlock.c init_defs_builtins: /usr/lib/coccinelle/standard.h iso main: parse error: File "./postgres.iso", line 4, column 19, charpos = 46 around = '=>', whole content = PG_RETURN_VOID() => return Which I suppose mean that there is no support for handling anything but standard tokens in an .iso file. Have I misunderstood the situation and this is already supported in some way? Are there any plans that add such support to the ISO grammar? Best wishes, Mats Kindahl, Timescale
Quoted 26 lines · Julia Lawall
Quoted 16 lines · Mats Kindahl
On 2024-08-24 14:56, Julia Lawall wrote:
Can you just add semicolons in your isomorphism? Sent from my iPhoneHi Julia,
I assume you mean to write something like this:
Statement @ pg_return @ @@ PG_RETURN_VOID(); => return;But that fails with the same error message:
Quoted 7 lines
mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci code/lwlock.c init_defs_builtins: /usr/lib/coccinelle/standard.h iso main: parse error: File "./postgres.iso", line 4, column 2, charpos = 29 around = 'PG_RETURN_VOID', whole content = PG_RETURN_VOID(); => return;
Quoted 4 lines
Thanks for the report. I will check on it.
Even without the isomorphism, you should be able to write a disjunction in your semantic patch?
Yes, of course, but it becomes quite cumbersome if you have to deal with this all the time, so this was more about an opportunity to improve the tool by recognizing yet another common situation in which macros are used as replacement for other constructs.
It is of course not possible to cover all weird macro uses, but the "iterator" pattern is quite common as is this "return" pattern.
/Mats
Quoted 132 lines
julia
Quoted 129 lines
Best wishes,
Mats KindahlOn 24 Aug 2024, at 14:23, Mats Kindahl <[email protected]> wrote: Hi all, In some code, there are macros used for different purposes. One typical case is having a "foreach" macro, but this is covered using "iterator" so that you can deal with code like this: static Jsonb *BuildJsonObject(List *items) { ListCell *cell; JsonbParseState *state = NULL; JsonbValue *value; pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL); foreach (cell, items) { KVItem *item = (KVItem *)lfirst(cell); AddJsonField(&state, item->key, item->value); } value = pushJsonbValue(&state, WJB_END_OBJECT, NULL); return JsonbValueToJsonb(value); } However, if you want to make sure to deal with resource allocation and make sure that you release a short-lived lock, for example (grossly simplified, just to demonstrate the problem): @@ expression lock, mode; @@ LWLockAcquire(lock, mode); ... when exists ( LWLockRelease(lock); | + LWLockRelease(lock); return; ) If fails miserably with this code: Datum start_worker(PG_FUNCTION_ARGS) { pid_t pid; if (!autoprewarm) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm is disabled"))); apw_init_shmem(); LWLockAcquire(&apw_state->lock, LW_EXCLUSIVE); pid = apw_state->bgworker_pid; if (pid != InvalidPid) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("autoprewarm worker is already running under PID %d", (int)pid))); apw_start_leader_worker(); PG_RETURN_VOID(); } Giving the patch, where the call to LWLockRelease should be before the PG_RETURN_VOID: --- code/lwlock.c +++ /tmp/cocci-output-12611-ff946d-lwlock.c @@ -18,6 +18,7 @@ Datum start_worker(PG_FUNCTION_ARGS) { apw_start_leader_worker(); PG_RETURN_VOID(); + LWLockRelease(&apw_state->lock); } Since this is such a common construct, and you do not want to deal with this for every potential case, this seems like an obvious case for using an iso-file, so I created one that looks like this, with the idea that we support replacing "PG_RETURN_VOID()" with just a "return". Statement @ pg_return @ @@ PG_RETURN_VOID() => return However, this fails with the following error message: mats@abzu:~/lang/cocci/hacks$ spatch --sp-file lwlock.cocci code/lwlock.c init_defs_builtins: /usr/lib/coccinelle/standard.h iso main: parse error: File "./postgres.iso", line 4, column 19, charpos = 46 around = '=>', whole content = PG_RETURN_VOID() => return Which I suppose mean that there is no support for handling anything but standard tokens in an .iso file. Have I misunderstood the situation and this is already supported in some way? Are there any plans that add such support to the ISO grammar? Best wishes, Mats Kindahl, Timescale