Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] All functions must be safe #147

Closed
japaric opened this issue Feb 13, 2019 · 6 comments · Fixed by #176
Closed

[RFC] All functions must be safe #147

japaric opened this issue Feb 13, 2019 · 6 comments · Fixed by #176
Labels
RFC This issue needs you input! S-accepted This RFC has been accepted but not yet implemented
Milestone

Comments

@japaric
Copy link
Collaborator

japaric commented Feb 13, 2019

Summary

Functions marked with any of these attributes: exception, idle, init,
interrupt and task must be safe fns and not unsafe fns.

Motivation

Put us in line with rust-lang/rfcs#2585. All the functions inside the app
block can only be called by the hardware and are safe to call by the hardware
(the DSL ensures that's the case) -- this should be reflected in the signature.

If a user wants to use unsafe code inside any of these functions they should
explicitly use an unsafe block instead of using unsafe fn as a short-hand
for unsafe fn() { unsafe { .. } }.

Design

The DSL will reject unsafe fns. For example:

#[rtfm::app(..)]
const APP: () = {
    #[init]
    fn init() { .. } // OK

    #[idle]
    unsafe fn idle() -> ! { .. } //~ ERROR: `idle` must have signature `fn() -> !`
};

Drawbacks

Unsafe code will suffer from (even more) rightward drift.

#[rtfm::app(..)]
const APP: () = {
    #[init]
    fn init() {
        unsafe {
            // unsafe stuff
        }

        // safe stuff
    }
};
@japaric japaric added the RFC This issue needs you input! label Feb 13, 2019
@japaric japaric added this to the v0.5.0 milestone Feb 13, 2019
@TeXitoi
Copy link
Collaborator

TeXitoi commented Feb 13, 2019

That's almost a safety bug, as the framework have no way of knowing the contract to allow calling these function.

@japaric
Copy link
Collaborator Author

japaric commented Feb 13, 2019

That's almost a safety bug

Can you elaborate on what you mean by "that"? Do you mean the current state or something else? (Today, the framework won't let call any of these functions from software; that's not going to change)

@TeXitoi
Copy link
Collaborator

TeXitoi commented Feb 13, 2019

I mean if your interrupt handler is unsafe, RTFM has no way of knowing in which context this function can be called.

@korken89
Copy link
Collaborator

I would say that this is a good addition, and to be in line with rust-lang/rfcs#2585, makes sense.

@japaric
Copy link
Collaborator Author

japaric commented Mar 26, 2019

I think this seems uncontroversial so let's vote for approving this RFC:

@japaric
Copy link
Collaborator Author

japaric commented Apr 16, 2019

This has enough approvals and no concerns have been raised over the course of several weeks so this RFC is now officially accepted. 🎉

@japaric japaric added S-accepted This RFC has been accepted but not yet implemented and removed disposition-merge labels Apr 16, 2019
bors bot added a commit that referenced this issue May 1, 2019
176:  implement RFCs 147 and 155, fix #141, etc. r=japaric a=japaric

This PR:

- Implements RFC 147: "all functions must be safe"

- Implements RFC 155: "explicit Context parameter"

- Implements the pending breaking change #141: reject assign syntax in `init`
  (which was used to initialize late resources)

- Refactors code generation to make it more readable -- there are no more random
  identifiers in the output -- and align it with the book description of RTFM
  internals (see PR #175).

- Makes the framework hard depend on `core::mem::MaybeUninit` and thus will
  require nightly until that API is stabilized.

- Fixes a ceiling analysis bug where the priority of the system timer was not
  considered in the analysis (TODO backport this into the v0.4.x branch).

- Shrinks the size of all the internal queues by turning `AtomicUsize` indices
  into `AtomicU8`s.

- Removes the integration with `owned_singleton`.

closes #141
closes #147
closes #155

Additionally:

- This changes CI to push v0.5.x docs to
  https://japaric.github.io/rtfm5/book/en/ -- we need to do this because our
  official docs are hosted on https://japaric.github.io/cortex-m-rtfm and we
  need to keep them on v0.4.x until we release v0.5.0

- I propose that we use the master branch to develop the upcoming v0.5.0.

- I have created a branch v0.4.x for backports; new v0.4.x releases will come
  from that branch.

r? @korken89 @TeXitoi, sorry for doing all the impl work in a single commit --
I know that makes things harder to review for you.

Suggestions for compile-pass and compile-fail tests are welcome


Co-authored-by: Jorge Aparicio <jorge@japaric.io>
@bors bors bot closed this as completed in #176 May 1, 2019
bors bot added a commit to quartiq/stabilizer that referenced this issue Nov 15, 2019
53: build(deps): bump cortex-m-rtfm from v0.5.0-beta.1 to v0.5.0 r=jordens a=dependabot-preview[bot]

Bumps [cortex-m-rtfm](https://github.com/rtfm-rs/cortex-m-rtfm) from v0.5.0-beta.1 to v0.5.0.
<details>
<summary>Changelog</summary>

*Sourced from [cortex-m-rtfm's changelog](https://github.com/rtfm-rs/cortex-m-rtfm/blob/master/CHANGELOG.md).*

> ## v0.5.0 - 2019-11-14
> 
> ### Added
> 
> - Experimental support for homogeneous and heterogeneous multi-core
>   microcontrollers has been added. Support is gated behind the `homogeneous` and
>   `heterogeneous` Cargo features.
> 
> ### Changed
> 
> - [breaking-change][] [RFC 155] "explicit `Context` parameter" has been
>   implemented.
> 
> [RFC 155]: [rtic-rs/rtic#155](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/155)
> 
> - [breaking-change][] [RFC 147] "all functions must be safe" has been
>   implemented.
> 
> [RFC 147]: [rtic-rs/rtic#147](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/147)
> 
> - All the queues internally used by the framework now use `AtomicU8` indices
>   instead of `AtomicUsize`; this reduces the static memory used by the
>   framework.
> 
> - [breaking-change][] when the `capacity` argument is omitted, the capacity of
>   the task is assumed to be `1`. Before, a reasonable (but hard to predict)
>   capacity was computed based on the number of `spawn` references the task had.
> 
> - [breaking-change][] resources that are appear as exclusive references
>   (`&mut-`) no longer appear behind the `Exclusive` newtype.
> 
> - [breaking-change][] the `timer-queue` Cargo feature has been removed. The
>   `schedule` API can be used without enabling any Cargo feature.
> 
> - [breaking-change][] when the `schedule` API is used the type of
>   `init::Context.core` changes from `cortex_m::Peripherals` to
>   `rtfm::Peripherals`. The fields of `rtfm::Peripherals` do not change when
>   Cargo features are enabled.
> 
> - [breaking-change][] the monotonic timer used to implement the `schedule` API
>   is now user configurable via the `#[app(monotonic = ..)]` argument. IMPORTANT:
>   it is now the responsibility of the application author to configure and
>   initialize the chosen `monotonic` timer during the `#[init]` phase.
> 
> - [breaking-change][] the `peripherals` field is not include in `init::Context`
>   by default. One must opt-in using the `#[app(peripherals = ..)]` argument.
> 
> - [breaking-change][] the `#[exception]` and `#[interrupt]` attributes have been
>   removed. Hardware tasks are now declared using the `#[task(binds = ..)]`
>   attribute.
></tr></table> ... (truncated)
</details>
<details>
<summary>Commits</summary>

- [`6b0a2df`](rtic-rs/rtic@6b0a2df) Merge [#272](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/272)
- [`4fcb6ab`](rtic-rs/rtic@4fcb6ab) v0.5.0 final release
- [`e28294b`](rtic-rs/rtic@e28294b) Merge [#271](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/271)
- [`2441b7e`](rtic-rs/rtic@2441b7e) Minor docs update to monotonic
- [`85463ed`](rtic-rs/rtic@85463ed) Merge [#268](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/268) [#270](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/270)
- [`76e2345`](rtic-rs/rtic@76e2345) Added struct de-structure-ing example in tips & tricks
- [`e9a8394`](rtic-rs/rtic@e9a8394) fix bash comparison
- [`da9c6a7`](rtic-rs/rtic@da9c6a7) run cfail tests only when rustc --version == $MSRV
- [`31b392f`](rtic-rs/rtic@31b392f) CI: replace compiletest-rs with trybuild
- [`72e84cb`](rtic-rs/rtic@72e84cb) Merge [#266](https://github-redirect.dependabot.com/rtfm-rs/cortex-m-rtfm/issues/266)
- Additional commits viewable in [compare view](rtic-rs/rtic@1fe9767...6b0a2df)
</details>
<br />

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
- `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language
- `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language
- `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language
- `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language
- `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme

Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com):
- Update frequency (including time of day and day of week)
- Pull request limits (per update run and/or open at any time)
- Automerge options (never/patch/minor, and dev/runtime dependencies)
- Out-of-range updates (receive only lockfile updates, if desired)
- Security updates (receive only security updates, if desired)



</details>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
andrewgazelka pushed a commit to andrewgazelka/cortex-m-rtic that referenced this issue Nov 3, 2021
147: add thumbv8m baseline r=thejpster a=evq



Co-authored-by: eV <ev@7pr.xyz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC This issue needs you input! S-accepted This RFC has been accepted but not yet implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants