feat: panicking on direct calls to external funcs#18126
Conversation
b4943a1 to
b4a5454
Compare
728ea91 to
9c49f01
Compare
b0bd5bd to
7ff9178
Compare
201dd56 to
4b8cc14
Compare
|
|
||
| for function in contract.functions { | ||
| if function.custom_attributes.contains(&"public".to_string()) { | ||
| if function.custom_attributes.contains(&"abi_public".to_string()) { |
There was a problem hiding this comment.
Renamed the attribute from public to abi_public because now the dev never sees it and abi_public is used only to inform the artifact generation regarding what type of function it is.
abi_public is a much better name as it's way more searchable and it makes it clearer that now it's just a low-level thing.
|
|
||
| pub(crate) comptime fn transform_private(f: FunctionDefinition) -> Quoted { | ||
| // TODO(benesjan): This function should only generate the new function's quote. Move the following to a different | ||
| // place. |
There was a problem hiding this comment.
In a followup PR I would rename these functions and make them solely new function quote-generation focused.
| /// | ||
| /// # Important | ||
| /// This function needs to be run before transformations modifying the signature of the target function `f` are applied | ||
| /// (e.g. transform_private). |
There was a problem hiding this comment.
No longer relevant as the original is now not getting transformed.
noir-projects/aztec-nr/aztec/src/macros/internals_functions_generation/abi_export.nr
Show resolved
Hide resolved
| @@ -249,36 +264,45 @@ comptime fn generate_contract_library_method_compute_note_hash_and_nullifier() - | |||
| } | |||
|
|
|||
| comptime fn generate_sync_private_state() -> Quoted { | |||
There was a problem hiding this comment.
Here is quite a big change as now I could not apply the external utility macro to the functions because the transformation functionality would not get triggered as that is handled in the aztec macro which is currently being processed here.
So I needed to do the transformation manually. The ugliest here is the manual creation of the funciton abi exports but I would say it's ok.
yarn-project/simulator/src/public/public_tx_simulator/apps_tests/bench.test.ts
Outdated
Show resolved
Hide resolved
80e26bf to
2102b17
Compare
2102b17 to
69a40c3
Compare
346fe6e to
78376ff
Compare
nventuro
left a comment
There was a problem hiding this comment.
Thanks for swimming through the macro mud!
docs/examples/bootstrap.sh
Outdated
| export BB=${BB:-"$REPO_ROOT/barretenberg/cpp/build/bin/bb"} | ||
| export NARGO=${NARGO:-"$REPO_ROOT/noir/noir-repo/target/release/nargo"} | ||
| export TRANSPILER=${TRANSPILER:-"$REPO_ROOT/avm-transpiler/target/release/avm-transpiler"} | ||
| export STRIP_INTERNAL_PREFIX=${STRIP_INTERNAL_PREFIX:-"$REPO_ROOT/noir-projects/noir-contracts/scripts/strip_internal_prefix.sh"} |
There was a problem hiding this comment.
'internal' is maybe a complicated term given that #[internal] is a thing no?
There was a problem hiding this comment.
You have a point. Renamed it to strip_aztec_nr_prefix.sh.
| // #[initializer], and #[noinitcheck]. Since we cannot enforce a specific ordering between these modifiers and | ||
| // #[external(...)], and we cannot access the #[external(...)] argument from within these modifiers' implementations |
There was a problem hiding this comment.
We could check this in the #[aztec] macro no? Go through all utility fns and check they're correct etc. Essentially first collect all atrtributes, put them in the registry, and then do all the work in one pass.
There was a problem hiding this comment.
You are right that now we could. Now it's possible because we have the external macro add the function definitions to the corresponding to the corresponding external function registry by type. So we can move these checks there.
That will be cleaner.
Will take a note to do it.
| // TODO(benesjan): This function should only generate the new function's quote. Move the following to a different | ||
| // place. |
There was a problem hiding this comment.
Meant the following line where is the call to register_public_fn_stubs. I've already did the change in a PR up the stack so will not clarify this here to avoid unnecessary conflicts.
noir-projects/aztec-nr/aztec/src/macros/internals_functions_generation/abi_marker_attributes.nr
Outdated
Show resolved
Hide resolved
| //! The functionality in this module is triggered by the `#[aztec]` macro. It generates new functions, prefixed with | ||
| //! `__aztec_nr_internals___`, from the ones marked with `#[external(...)]` attributes. The original functions are then | ||
| //! modified to be uncallable. This prevents developers from inadvertently calling a function directly, instead of | ||
| //! performing a proper contract call. |
There was a problem hiding this comment.
When did you start doing these module comments?
There was a problem hiding this comment.
When I learnt from you that they exist. Do you like it?
| // We call a function whose name is prefixed with `__aztec_nr_internals__`. This is necessary because the | ||
| // original function is intentionally made uncallable, preventing direct invocation within the contract. | ||
| // Instead, a new function with the same name, but prefixed by `__aztec_nr_internals__`, has been generated to | ||
| // be called here. For more details see the `process_functions` function. | ||
| let name = f"__aztec_nr_internals__{fn_name}".quoted_contents(); |
There was a problem hiding this comment.
Is this right? Don't we want to keep the original name?
There was a problem hiding this comment.
At this point we've made the original external functions uncallable by injecting the static assert and hence here in the public dispatch we need to use the modified naming that invokes the modified generated functions.
.../noir-contracts-comp-failures/contracts/panic_on_direct_private_external_fn_call/src/main.nr
Show resolved
Hide resolved
69a40c3 to
c263145
Compare
78376ff to
69667c0
Compare
c263145 to
1e6a6a6
Compare
5ef6d41 to
d5d1f0d
Compare
Flakey Tests🤖 says: This CI run detected 1 tests that failed, but were tolerated due to a .test_patterns.yml entry. |

Fixes F-27
Given
#[private] fn foo()we want forfoo()to issue a compilation error. To achieve that we need to replace the function's body with astatic_assert(false).Given that the #[external(...)] macro is being processed before the #[aztec] macro we can do the following:
#[external(...)]macro invocation I register the function into 3 global variables based on the external type (private,publicorutility),#[aztec]macros for each of these functions I generate a new function with__aztecnr_internals__prefix by taking the original function's args and body, doing the same transformation on it as we do now for external funcs and then I inject these new functions into the contract,static_assert(false, "yo, go check the docs how to actually call a function");#[aztec]macro (e.g.sync_private_state) I would not apply the#[external(...)]macro attribute as is currently done but instead I would just directly call the "transformation function" on it that is also used in step 3 and then inject the function.Note that this PR is currently not mergeable as I needed to disable caching in BB to get a fresh AVM transpiler + there is a broken test. Both of these are (most likely) unrelated to this PR so I think this can be already reviewed.